Example #1
0
  @Override
  @Nullable
  public Collection<PsiImportStatementBase> findRedundantImports(final PsiJavaFile file) {
    final PsiImportList importList = file.getImportList();
    if (importList == null) return null;
    final PsiImportStatementBase[] imports = importList.getAllImportStatements();
    if (imports.length == 0) return null;

    Set<PsiImportStatementBase> allImports =
        new THashSet<PsiImportStatementBase>(Arrays.asList(imports));
    final Collection<PsiImportStatementBase> redundant;
    if (FileTypeUtils.isInServerPageFile(file)) {
      // remove only duplicate imports
      redundant = ContainerUtil.newIdentityTroveSet();
      ContainerUtil.addAll(redundant, imports);
      redundant.removeAll(allImports);
      for (PsiImportStatementBase importStatement : imports) {
        if (importStatement instanceof JspxImportStatement
            && importStatement.isForeignFileImport()) {
          redundant.remove(importStatement);
        }
      }
    } else {
      redundant = allImports;
      final List<PsiFile> roots = file.getViewProvider().getAllFiles();
      for (PsiElement root : roots) {
        root.accept(
            new JavaRecursiveElementWalkingVisitor() {
              @Override
              public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
                if (!reference.isQualified()) {
                  final JavaResolveResult resolveResult = reference.advancedResolve(false);
                  if (!inTheSamePackage(file, resolveResult.getElement())) {
                    final PsiElement resolveScope = resolveResult.getCurrentFileResolveScope();
                    if (resolveScope instanceof PsiImportStatementBase) {
                      final PsiImportStatementBase importStatementBase =
                          (PsiImportStatementBase) resolveScope;
                      redundant.remove(importStatementBase);
                    }
                  }
                }
                super.visitReferenceElement(reference);
              }

              private boolean inTheSamePackage(PsiJavaFile file, PsiElement element) {
                if (element instanceof PsiClass
                    && ((PsiClass) element).getContainingClass() == null) {
                  final PsiFile containingFile = element.getContainingFile();
                  if (containingFile instanceof PsiJavaFile) {
                    return Comparing.strEqual(
                        file.getPackageName(), ((PsiJavaFile) containingFile).getPackageName());
                  }
                }
                return false;
              }
            });
      }
    }
    return redundant;
  }
  private static void assertOrder(PsiJavaFile file, @NonNls String... expectedOrder) {
    PsiImportStatementBase[] statements = file.getImportList().getAllImportStatements();

    assertEquals(expectedOrder.length, statements.length);
    for (int i = 0; i < statements.length; i++) {
      PsiImportStatementBase statement = statements[i];
      String text = StringUtil.trimEnd(StringUtil.trimStart(statement.getText(), "import "), ";");
      assertEquals(expectedOrder[i], text);
    }
  }
  public void testAutoImportSkipsClassReferenceInMethodPosition() throws Throwable {
    @NonNls
    String text =
        "package x; import java.util.HashMap; class S { HashMap<String,String> f(){ return  Hash<caret>Map <String, String >();} }  ";
    configureByText(StdFileTypes.JAVA, text);

    boolean old = CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY;
    CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = true;
    DaemonCodeAnalyzerSettings.getInstance().setImportHintEnabled(true);

    try {
      List<HighlightInfo> errs = highlightErrors();
      assertTrue(errs.size() > 1);

      PsiJavaFile javaFile = (PsiJavaFile) getFile();
      assertEquals(1, javaFile.getImportList().getAllImportStatements().length);

      PsiReference ref = javaFile.findReferenceAt(getEditor().getCaretModel().getOffset());
      ImportClassFix fix = new ImportClassFix((PsiJavaCodeReferenceElement) ref);
      assertFalse(fix.isAvailable(getProject(), getEditor(), getFile()));
    } finally {
      CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = old;
    }
  }