예제 #1
0
 public static boolean hasJavaLangImportConflict(String fqName, PsiJavaFile file) {
   final String shortName = ClassUtil.extractClassName(fqName);
   final String packageName = ClassUtil.extractPackageName(fqName);
   if (HardcodedMethodConstants.JAVA_LANG.equals(packageName)) {
     return false;
   }
   final Project project = file.getProject();
   final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
   final PsiPackage javaLangPackage = psiFacade.findPackage(HardcodedMethodConstants.JAVA_LANG);
   if (javaLangPackage == null) {
     return false;
   }
   return javaLangPackage.containsClassNamed(shortName);
 }
예제 #2
0
 public static boolean hasDefaultImportConflict(String fqName, PsiJavaFile file) {
   final String shortName = ClassUtil.extractClassName(fqName);
   final String packageName = ClassUtil.extractPackageName(fqName);
   final String filePackageName = file.getPackageName();
   if (filePackageName.equals(packageName)) {
     return false;
   }
   final Project project = file.getProject();
   final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
   final PsiPackage filePackage = psiFacade.findPackage(filePackageName);
   if (filePackage == null) {
     return false;
   }
   return filePackage.containsClassNamed(shortName);
 }
 /**
  * @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);
 }
예제 #4
0
 /**
  * ImportUtils currently checks all inner classes, even those that are contained in inner classes
  * themselves, because it doesn't know the location of the original fully qualified reference. It
  * should really only check if the containing class of the fully qualified reference has any
  * conflicting inner classes.
  */
 private static boolean containsConflictingInnerClass(String fqName, PsiClass aClass) {
   final String shortName = ClassUtil.extractClassName(fqName);
   if (shortName.equals(aClass.getName()) && !fqName.equals(aClass.getQualifiedName())) {
     return true;
   }
   final PsiClass[] classes = aClass.getInnerClasses();
   for (PsiClass innerClass : classes) {
     if (containsConflictingInnerClass(fqName, innerClass)) {
       return true;
     }
   }
   return false;
 }
 public static boolean hasDefaultImportConflict(String fqName, PsiJavaFile file) {
   final String shortName = ClassUtil.extractClassName(fqName);
   final String packageName = ClassUtil.extractPackageName(fqName);
   final String filePackageName = file.getPackageName();
   if (filePackageName.equals(packageName)) {
     return false;
   }
   final Project project = file.getProject();
   final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
   final PsiPackage filePackage = psiFacade.findPackage(filePackageName);
   if (filePackage == null) {
     return false;
   }
   final PsiClass[] classes = filePackage.getClasses();
   for (PsiClass aClass : classes) {
     final String className = aClass.getName();
     if (shortName.equals(className)) {
       return true;
     }
   }
   return false;
 }
예제 #6
0
 public static boolean nameCanBeImported(@NotNull String fqName, @NotNull PsiElement context) {
   final PsiClass containingClass = PsiTreeUtil.getParentOfType(context, PsiClass.class);
   if (containingClass != null) {
     if (fqName.equals(containingClass.getQualifiedName())) {
       return true;
     }
     final String shortName = ClassUtil.extractClassName(fqName);
     final PsiClass[] innerClasses = containingClass.getAllInnerClasses();
     for (PsiClass innerClass : innerClasses) {
       if (innerClass.hasModifierProperty(PsiModifier.PRIVATE)) {
         continue;
       }
       if (innerClass.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) {
         if (!ClassUtils.inSamePackage(innerClass, containingClass)) {
           continue;
         }
       }
       final String className = innerClass.getName();
       if (shortName.equals(className)) {
         return false;
       }
     }
     PsiField field = containingClass.findFieldByName(shortName, false);
     if (field != null) {
       return false;
     }
     field = containingClass.findFieldByName(shortName, true);
     if (field != null
         && PsiUtil.isAccessible(containingClass.getProject(), field, containingClass, null)) {
       return false;
     }
   }
   final PsiJavaFile file = PsiTreeUtil.getParentOfType(context, PsiJavaFile.class);
   if (file == null) {
     return false;
   }
   if (hasExactImportConflict(fqName, file)) {
     return false;
   }
   if (hasOnDemandImportConflict(fqName, file, true)) {
     return false;
   }
   if (containsConflictingReference(file, fqName)) {
     return false;
   }
   if (containsConflictingClass(fqName, file)) {
     return false;
   }
   return !containsConflictingClassName(fqName, file);
 }
 @Nullable
 public static PsiMethod findPsiMethod(PsiManager manager, String externalName) {
   final int spaceIdx = externalName.indexOf(' ');
   final String className = externalName.substring(0, spaceIdx);
   final PsiClass psiClass = ClassUtil.findPsiClass(manager, className);
   if (psiClass == null) return null;
   try {
     PsiElementFactory factory =
         JavaPsiFacade.getInstance(psiClass.getProject()).getElementFactory();
     String methodSignature = externalName.substring(spaceIdx + 1);
     PsiMethod patternMethod = factory.createMethodFromText(methodSignature, psiClass);
     return psiClass.findMethodBySignature(patternMethod, false);
   } catch (IncorrectOperationException e) {
     // Do nothing. Returning null is acceptable in this case.
     return null;
   }
 }
예제 #8
0
 public static void addImportIfNeeded(@NotNull PsiClass aClass, @NotNull PsiElement context) {
   final PsiFile file = context.getContainingFile();
   if (!(file instanceof PsiJavaFile)) {
     return;
   }
   final PsiJavaFile javaFile = (PsiJavaFile) file;
   final PsiClass outerClass = aClass.getContainingClass();
   if (outerClass == null) {
     if (PsiTreeUtil.isAncestor(javaFile, aClass, true)) {
       return;
     }
   } else if (PsiTreeUtil.isAncestor(outerClass, context, true)) {
     final PsiElement brace = outerClass.getLBrace();
     if (brace != null && brace.getTextOffset() < context.getTextOffset()) {
       return;
     }
   }
   final String qualifiedName = aClass.getQualifiedName();
   if (qualifiedName == null) {
     return;
   }
   final PsiImportList importList = javaFile.getImportList();
   if (importList == null) {
     return;
   }
   final String containingPackageName = javaFile.getPackageName();
   @NonNls final String packageName = ClassUtil.extractPackageName(qualifiedName);
   if (containingPackageName.equals(packageName)
       || importList.findSingleClassImportStatement(qualifiedName) != null) {
     return;
   }
   if (importList.findOnDemandImportStatement(packageName) != null
       && !hasDefaultImportConflict(qualifiedName, javaFile)
       && !hasOnDemandImportConflict(qualifiedName, javaFile)) {
     return;
   }
   final Project project = importList.getProject();
   final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
   final PsiElementFactory elementFactory = psiFacade.getElementFactory();
   final PsiImportStatement importStatement = elementFactory.createImportStatement(aClass);
   importList.add(importStatement);
 }
예제 #9
0
 private ConflictingClassReferenceVisitor(String fullyQualifiedName) {
   name = ClassUtil.extractClassName(fullyQualifiedName);
   this.fullyQualifiedName = fullyQualifiedName;
 }