/*
 *  Copyright 2000-2007 JetBrains s.r.o.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package org.jetbrains.plugins.groovy.lang.psi;

import com.intellij.extapi.psi.PsiFileBase;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiSubstitutor;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiPackage;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.psi.scope.NameHint;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.GroovyFileType;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition;
import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement;

/**
 * Implements all abstractionos related to Groovy file
 *
 * @author Ilya Sergey
 */
public class GroovyFile extends PsiFileBase {

  public GroovyFile(FileViewProvider viewProvider) {
    super(viewProvider, GroovyFileType.GROOVY_FILE_TYPE.getLanguage());
  }

  @NotNull
  public FileType getFileType() {
    return GroovyFileType.GROOVY_FILE_TYPE;
  }

  public String toString() {
    return "Groovy script";
  }

  public GrTypeDefinition[] getTypeDefinitions() {
    return findChildrenByClass(GrTypeDefinition.class);
  }

  @NotNull
  public String getPackageName() {
    GrPackageDefinition packageDef = findChildByClass(GrPackageDefinition.class);
    if (packageDef != null) {
      return packageDef.getPackageName();
    }

    return "";
  }

  public GrStatement[] getStatements() {
    return findChildrenByClass(GrStatement.class);
  }

  public GrImportStatement[] getImportStatements() {
    return findChildrenByClass(GrImportStatement.class);
  }

  public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull PsiSubstitutor substitutor, PsiElement lastParent, @NotNull PsiElement place) {
    NameHint nameHint = processor.getHint(NameHint.class);
    for (final GrTypeDefinition typeDefinition : getTypeDefinitions()) {
      if (nameHint == null || nameHint.getName().equals(typeDefinition.getName())) {
        if (!processor.execute(typeDefinition, PsiSubstitutor.EMPTY)) return false;
      }
    }
    
    for (final GrImportStatement importStatement : getImportStatements()) {
      if (!importStatement.processDeclarations(processor, substitutor, lastParent, place)) return false;
    }

    for (final String implicitlyImported : IMPLICITLY_IMPORTED_PACKAGES) {
      PsiPackage aPackage = getManager().findPackage(implicitlyImported);
      if (aPackage != null && !aPackage.processDeclarations(processor, substitutor, lastParent, place)) return false;
    }

    return true;
  }

  private static final String[] IMPLICITLY_IMPORTED_PACKAGES = new String[] {
      "java.lang",
      "java.util",
      /* todo check others*/
  };
}


@@ -3057,24 +3057,295 @@
 lse;%0A    %7D%0A%0A
+    String currentPackageName = getPackageName();%0A    PsiPackage currentPackage = getManager().findPackage(currentPackageName);%0A    if (currentPackage != null && !currentPackage.processDeclarations(processor, substitutor, lastParent, place)) %7B%0A      return false;%0A    %7D%0A%0A
     for (fin
@@ -3754,30 +3754,78 @@
     
-/* todo check others*/
+%22java.io%22,%0A      %22java.net%22,%0A      %22groovy.lang%22,%0A      %22groovy.util%22,
 %0A  %7D

resolve fix