@Test public void testJavaFileIsEmpty() throws Exception { final PsiJavaFile psiJavaFile = mock(PsiJavaFile.class); final PsiClass toImport = mock(PsiClass.class); final PsiImportStatement psiImportStatement = mock(PsiImportStatement.class); final PsiImportList psiImportList = mock(PsiImportList.class); final RunnableHolder writeActionHolder = getWriteActionHolder(); when(psiJavaFile.getProject()).thenReturn(project); when(psiImportStatement.getParent()).thenReturn(psiImportList); when(psiElementFactory.createImportStatement(toImport)).thenReturn(psiImportStatement); addImportService.addImportStatement(psiJavaFile, toImport); assertNotNull(writeActionHolder.getRunnable()); verify(psiJavaFile, never()).add(any()); writeActionHolder.getRunnable().run(); verify(psiJavaFile).add(psiImportList); }
/** * @param strict if strict is true this method checks if the conflicting class which is imported * is actually used in the file. If it isn't the on demand import can be overridden with an * exact import for the fqName without breaking stuff. */ private static boolean hasOnDemandImportConflict( @NotNull String fqName, @NotNull PsiJavaFile file, boolean strict) { final PsiImportList imports = file.getImportList(); if (imports == null) { return false; } final PsiImportStatement[] importStatements = imports.getImportStatements(); final String shortName = ClassUtil.extractClassName(fqName); final String packageName = ClassUtil.extractPackageName(fqName); for (final PsiImportStatement importStatement : importStatements) { if (!importStatement.isOnDemand()) { continue; } final PsiJavaCodeReferenceElement importReference = importStatement.getImportReference(); if (importReference == null) { continue; } final String packageText = importReference.getText(); if (packageText.equals(packageName)) { continue; } final PsiElement element = importReference.resolve(); if (element == null || !(element instanceof PsiPackage)) { continue; } final PsiPackage aPackage = (PsiPackage) element; final PsiClass[] classes = aPackage.getClasses(); for (final PsiClass aClass : classes) { final String className = aClass.getName(); if (!shortName.equals(className)) { continue; } if (!strict) { return true; } final String qualifiedClassName = aClass.getQualifiedName(); if (qualifiedClassName == null || fqName.equals(qualifiedClassName)) { continue; } return containsReferenceToConflictingClass(file, qualifiedClassName); } } return hasJavaLangImportConflict(fqName, file); }
private static boolean hasExactImportConflict(String fqName, PsiJavaFile file) { final PsiImportList imports = file.getImportList(); if (imports == null) { return false; } final PsiImportStatement[] importStatements = imports.getImportStatements(); final int lastDotIndex = fqName.lastIndexOf((int) '.'); final String shortName = fqName.substring(lastDotIndex + 1); final String dottedShortName = '.' + shortName; for (final PsiImportStatement importStatement : importStatements) { if (importStatement.isOnDemand()) { continue; } final String importName = importStatement.getQualifiedName(); if (importName == null) { return false; } if (!importName.equals(fqName) && importName.endsWith(dottedShortName)) { return true; } } return false; }
/** * 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; }