unresolved ondemand imports
authorAlexey Kudravtsev <cdr@intellij.com>
Tue, 25 May 2010 10:14:30 +0000 (14:14 +0400)
committerAlexey Kudravtsev <cdr@intellij.com>
Wed, 26 May 2010 07:13:07 +0000 (11:13 +0400)
java/java-impl/src/com/intellij/psi/impl/source/codeStyle/ImportHelper.java

index dcd70b4fd3be28daf8c217976be8a1542986fe1a..04d4e1bc33d44c2cfc4e9aa66b56bb5132a87cc5 100644 (file)
@@ -703,6 +703,7 @@ public class ImportHelper{
   private static void addUnresolvedImportNames(@NotNull final Set<Pair<String, Boolean>> namesToImport, @NotNull PsiJavaFile file) {
     PsiImportStatementBase[] imports = file.getImportList().getAllImportStatements();
     final Map<String, Pair<String, Boolean>> unresolvedNames = new THashMap<String, Pair<String, Boolean>>();
+    @NotNull Set<Pair<String, Boolean>> unresolvedOnDemand = new THashSet<Pair<String, Boolean>>();
     for (PsiImportStatementBase anImport : imports) {
       PsiJavaCodeReferenceElement ref = anImport.getImportReference();
       if (ref == null) continue;
@@ -714,15 +715,16 @@ public class ImportHelper{
         }
 
         Pair<String, Boolean> pair = Pair.create(text, anImport instanceof PsiImportStaticStatement);
-        if (!anImport.isOnDemand()) {
-          unresolvedNames.put(ref.getReferenceName(), pair);
+        if (anImport.isOnDemand()) {
+          unresolvedOnDemand.add(pair);
         }
         else {
-          namesToImport.add(pair);
+          unresolvedNames.put(ref.getReferenceName(), pair);
         }
       }
     }
 
+    final boolean[] hasResolveProblem = {false};
     // do not visit imports
     for (PsiClass aClass : file.getClasses()) {
       aClass.accept(new JavaRecursiveElementWalkingVisitor() {
@@ -730,14 +732,21 @@ public class ImportHelper{
         public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
           String name = reference.getReferenceName();
           Pair<String, Boolean> pair = unresolvedNames.get(name);
-          if (pair != null && reference.multiResolve(false).length == 0) {
-            namesToImport.add(pair);
-            unresolvedNames.remove(name);
+          if (reference.multiResolve(false).length == 0) {
+            hasResolveProblem[0] = true;
+            if (pair != null) {
+              namesToImport.add(pair);
+              unresolvedNames.remove(name);
+            }
           }
           super.visitReferenceElement(reference);
         }
       });
     }
+    if (hasResolveProblem[0]) {
+      namesToImport.addAll(unresolvedOnDemand);
+    }
+    // otherwise, optimize out all red on demand imports for green file
   }
 
   public static boolean isImplicitlyImported(@NotNull String className, @NotNull PsiJavaFile file) {