/** * Adds import if it is needed. * * @return false when the FQ-name have to be used in code (e.g. when conflicting imports already * exist) */ public boolean addImport(@NotNull PsiJavaFile file, @NotNull PsiClass refClass) { final JavaPsiFacade facade = JavaPsiFacade.getInstance(file.getProject()); PsiElementFactory factory = facade.getElementFactory(); PsiResolveHelper helper = facade.getResolveHelper(); String className = refClass.getQualifiedName(); if (className == null) return true; String packageName = getPackageOrClassName(className); String shortName = PsiNameHelper.getShortClassName(className); PsiClass conflictSingleRef = findSingleImportByShortName(file, shortName); if (conflictSingleRef != null) { return className.equals(conflictSingleRef.getQualifiedName()); } PsiClass curRefClass = helper.resolveReferencedClass(shortName, file); if (file.getManager().areElementsEquivalent(refClass, curRefClass)) { return true; } boolean useOnDemand = true; if (packageName.length() == 0) { useOnDemand = false; } PsiElement conflictPackageRef = findImportOnDemand(file, packageName); if (conflictPackageRef != null) { useOnDemand = false; } List<PsiElement> classesToReimport = new ArrayList<PsiElement>(); List<PsiJavaCodeReferenceElement> importRefs = getImportsFromPackage(file, packageName); if (useOnDemand) { if (mySettings.USE_SINGLE_CLASS_IMPORTS && importRefs.size() + 1 < mySettings.CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND && !mySettings.PACKAGES_TO_USE_IMPORT_ON_DEMAND.contains(packageName)) { useOnDemand = false; } // name of class we try to import is the same as of the class defined in this file if (curRefClass != null) { useOnDemand = true; } // check conflicts if (useOnDemand) { PsiElement[] onDemandRefs = file.getOnDemandImports(false, true); if (onDemandRefs.length > 0) { PsiPackage aPackage = facade.findPackage(packageName); if (aPackage != null) { PsiDirectory[] dirs = aPackage.getDirectories(); for (PsiDirectory dir : dirs) { PsiFile[] files = dir.getFiles(); // do not iterate classes - too slow when not loaded for (PsiFile aFile : files) { if (aFile instanceof PsiJavaFile) { String name = aFile.getVirtualFile().getNameWithoutExtension(); for (PsiElement ref : onDemandRefs) { String refName = ref instanceof PsiClass ? ((PsiClass) ref).getQualifiedName() : ((PsiPackage) ref).getQualifiedName(); String conflictClassName = refName + "." + name; GlobalSearchScope resolveScope = file.getResolveScope(); PsiClass conflictClass = facade.findClass(conflictClassName, resolveScope); if (conflictClass != null && helper.isAccessible(conflictClass, file, null)) { String conflictClassName2 = aPackage.getQualifiedName() + "." + name; PsiClass conflictClass2 = facade.findClass(conflictClassName2, resolveScope); if (conflictClass2 != null && helper.isAccessible(conflictClass2, file, null)) { if (ReferencesSearch.search( conflictClass, new LocalSearchScope(file), false) .findFirst() != null) { classesToReimport.add(conflictClass); } } } } } } } } } } } try { PsiImportList importList = file.getImportList(); PsiImportStatement statement; if (useOnDemand) { statement = factory.createImportStatementOnDemand(packageName); } else { statement = factory.createImportStatement(refClass); } importList.add(statement); if (useOnDemand) { for (PsiJavaCodeReferenceElement ref : importRefs) { LOG.assertTrue(ref.getParent() instanceof PsiImportStatement); if (!ref.isValid()) continue; // todo[dsl] Q? classesToReimport.add(ref.resolve()); PsiImportStatement importStatement = (PsiImportStatement) ref.getParent(); importStatement.delete(); } } for (PsiElement aClassesToReimport : classesToReimport) { PsiClass aClass = (PsiClass) aClassesToReimport; if (aClass != null) { addImport(file, aClass); } } } catch (IncorrectOperationException e) { LOG.error(e); } return true; }
@NotNull private static Set<String> findSingleImports( @NotNull final PsiJavaFile file, @NotNull String[] names, @NotNull final Set<String> onDemandImports, @NotNull Set<String> namesToImportStaticly) { final GlobalSearchScope resolveScope = file.getResolveScope(); Set<String> namesToUseSingle = new THashSet<String>(); final String thisPackageName = file.getPackageName(); final Set<String> implicitlyImportedPackages = new THashSet<String>(Arrays.asList(file.getImplicitlyImportedPackages())); final PsiManager manager = file.getManager(); for (String name : names) { String prefix = getPackageOrClassName(name); if (prefix.length() == 0) continue; final boolean isImplicitlyImported = implicitlyImportedPackages.contains(prefix); if (!onDemandImports.contains(prefix) && !isImplicitlyImported) continue; String shortName = PsiNameHelper.getShortClassName(name); String thisPackageClass = thisPackageName.length() > 0 ? thisPackageName + "." + shortName : shortName; if (JavaPsiFacade.getInstance(manager.getProject()).findClass(thisPackageClass, resolveScope) != null) { namesToUseSingle.add(name); continue; } if (!isImplicitlyImported) { String langPackageClass = JAVA_LANG_PACKAGE + "." + shortName; // TODO : JSP! if (JavaPsiFacade.getInstance(manager.getProject()) .findClass(langPackageClass, resolveScope) != null) { namesToUseSingle.add(name); continue; } } for (String onDemandName : onDemandImports) { if (prefix.equals(onDemandName)) continue; if (namesToImportStaticly.contains(name)) { PsiClass aClass = JavaPsiFacade.getInstance(manager.getProject()).findClass(onDemandName, resolveScope); if (aClass != null) { PsiField field = aClass.findFieldByName(shortName, true); if (field != null && field.hasModifierProperty(PsiModifier.STATIC)) { namesToUseSingle.add(name); } else { PsiClass inner = aClass.findInnerClassByName(shortName, true); if (inner != null && inner.hasModifierProperty(PsiModifier.STATIC)) { namesToUseSingle.add(name); } else { PsiMethod[] methods = aClass.findMethodsByName(shortName, true); for (PsiMethod method : methods) { if (method.hasModifierProperty(PsiModifier.STATIC)) { namesToUseSingle.add(name); } } } } } } else { PsiClass aClass = JavaPsiFacade.getInstance(manager.getProject()) .findClass(onDemandName + "." + shortName, resolveScope); if (aClass != null) { namesToUseSingle.add(name); } } } } return namesToUseSingle; }