void (PsiJavaCodeReferenceElement reference) { if (reference.getQualifier() != null) { // it's already fully qualified, so the import statement wasn't // responsible return; } // during typing there can be incomplete code final JavaResolveResult resolveResult = reference.advancedResolve(true); PsiElement element = resolveResult.getElement(); if (element == null) { JavaResolveResult[] results = reference.multiResolve(false); if (results.length > 0) { element = results[0].getElement(); } } if (!(element instanceof PsiMember member)) { return; } if (findImport(member, usedImportStatements) != null) { return; } final PsiImportStatementBase foundImport = findImport(member, importStatements); if (foundImport != null) { importStatements.remove(foundImport); usedImportStatements.add(foundImport); } }