Beispiel #1
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 instanceof PsiPackage)) {
       continue;
     }
     final PsiPackage aPackage = (PsiPackage) element;
     if (!strict && aPackage.containsClassNamed(shortName)) {
       return true;
     } else {
       final PsiClass[] classes = aPackage.findClassByShortName(shortName, file.getResolveScope());
       for (final PsiClass aClass : classes) {
         final String qualifiedClassName = aClass.getQualifiedName();
         if (qualifiedClassName == null || fqName.equals(qualifiedClassName)) {
           continue;
         }
         return containsConflictingReference(file, qualifiedClassName);
       }
     }
   }
   return hasJavaLangImportConflict(fqName, file);
 }
Beispiel #2
0
 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;
 }