private static PsiImportStatementBase extractImport( final PsiJavaFile aFile, final boolean isStatic) { final PsiImportList importList = aFile.getImportList(); assert importList != null : aFile; final PsiImportStatementBase[] statements = isStatic ? importList.getImportStaticStatements() : importList.getImportStatements(); assert statements.length == 1 : aFile.getText(); return statements[0]; }
/** * @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; }