Exemple #1
0
 private static boolean hasExactImportStaticConflict(
     String qualifierClass, String memberName, PsiElement context) {
   final PsiFile file = context.getContainingFile();
   if (!(file instanceof PsiJavaFile)) {
     return false;
   }
   final PsiJavaFile javaFile = (PsiJavaFile) file;
   final PsiImportList importList = javaFile.getImportList();
   if (importList == null) {
     return false;
   }
   final PsiImportStaticStatement[] importStaticStatements =
       importList.getImportStaticStatements();
   for (PsiImportStaticStatement importStaticStatement : importStaticStatements) {
     if (importStaticStatement.isOnDemand()) {
       continue;
     }
     final String name = importStaticStatement.getReferenceName();
     if (!memberName.equals(name)) {
       continue;
     }
     final PsiJavaCodeReferenceElement importReference =
         importStaticStatement.getImportReference();
     if (importReference == null) {
       continue;
     }
     final PsiElement qualifier = importReference.getQualifier();
     if (qualifier == null) {
       continue;
     }
     final String qualifierText = qualifier.getText();
     if (!qualifierClass.equals(qualifierText)) {
       return true;
     }
   }
   return false;
 }
Exemple #2
0
 public static boolean addStaticImport(
     @NotNull String qualifierClass,
     @NonNls @NotNull String memberName,
     @NotNull PsiElement context) {
   if (!nameCanBeStaticallyImported(qualifierClass, memberName, context)) {
     return false;
   }
   final PsiClass containingClass = PsiTreeUtil.getParentOfType(context, PsiClass.class);
   if (InheritanceUtil.isInheritor(containingClass, qualifierClass)) {
     return true;
   }
   final PsiFile psiFile = context.getContainingFile();
   if (!(psiFile instanceof PsiJavaFile)) {
     return false;
   }
   final PsiJavaFile javaFile = (PsiJavaFile) psiFile;
   final PsiImportList importList = javaFile.getImportList();
   if (importList == null) {
     return false;
   }
   final PsiImportStatementBase existingImportStatement =
       importList.findSingleImportStatement(memberName);
   if (existingImportStatement != null) {
     if (existingImportStatement instanceof PsiImportStaticStatement) {
       final PsiImportStaticStatement importStaticStatement =
           (PsiImportStaticStatement) existingImportStatement;
       if (!memberName.equals(importStaticStatement.getReferenceName())) {
         return false;
       }
       final PsiClass targetClass = importStaticStatement.resolveTargetClass();
       return targetClass != null && qualifierClass.equals(targetClass.getQualifiedName());
     }
     return false;
   }
   final PsiImportStaticStatement onDemandImportStatement =
       findOnDemandImportStaticStatement(importList, qualifierClass);
   if (onDemandImportStatement != null
       && !hasOnDemandImportStaticConflict(qualifierClass, memberName, context)) {
     return true;
   }
   final Project project = context.getProject();
   final GlobalSearchScope scope = context.getResolveScope();
   final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
   final PsiClass aClass = psiFacade.findClass(qualifierClass, scope);
   if (aClass == null) {
     return false;
   }
   final String qualifiedName = aClass.getQualifiedName();
   if (qualifiedName == null) {
     return false;
   }
   final List<PsiImportStaticStatement> imports = getMatchingImports(importList, qualifiedName);
   final int onDemandCount =
       JavaCodeStyleSettingsFacade.getInstance(project).getNamesCountToUseImportOnDemand();
   final PsiElementFactory elementFactory = psiFacade.getElementFactory();
   if (imports.size() < onDemandCount) {
     importList.add(elementFactory.createImportStaticStatement(aClass, memberName));
   } else {
     for (PsiImportStaticStatement importStatement : imports) {
       importStatement.delete();
     }
     importList.add(elementFactory.createImportStaticStatement(aClass, "*"));
   }
   return true;
 }