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) {
     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 CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(project);
   final PsiElementFactory elementFactory = psiFacade.getElementFactory();
   if (imports.size() < codeStyleSettings.NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND) {
     importList.add(elementFactory.createImportStaticStatement(aClass, memberName));
   } else {
     for (PsiImportStaticStatement importStatement : imports) {
       importStatement.delete();
     }
     importList.add(elementFactory.createImportStaticStatement(aClass, "*"));
   }
   return true;
 }
Пример #2
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);
 }
Пример #3
0
  /**
   * 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;
  }
  public static void invoke(
      final Project project, PsiFile file, final Editor editor, PsiElement element) {
    if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;

    final PsiJavaCodeReferenceElement refExpr = (PsiJavaCodeReferenceElement) element.getParent();
    final PsiClass aClass = (PsiClass) refExpr.resolve();
    if (aClass == null) {
      return;
    }
    final PsiClass containingClass = PsiUtil.getTopLevelClass(refExpr);
    if (aClass != containingClass) {
      PsiImportList importList = ((PsiJavaFile) file).getImportList();
      if (importList == null) {
        return;
      }
      boolean alreadyImported = false;
      for (PsiImportStaticStatement statement : importList.getImportStaticStatements()) {
        if (!statement.isOnDemand()) continue;
        PsiClass staticResolve = statement.resolveTargetClass();
        if (aClass == staticResolve) {
          alreadyImported = true;
          break;
        }
      }
      if (!alreadyImported) {
        PsiImportStaticStatement importStaticStatement =
            JavaPsiFacade.getInstance(file.getProject())
                .getElementFactory()
                .createImportStaticStatement(aClass, "*");
        importList.add(importStaticStatement);
      }
    }

    List<PsiFile> roots = file.getViewProvider().getAllFiles();
    for (final PsiFile root : roots) {
      PsiElement copy = root.copy();
      final PsiManager manager = root.getManager();

      final TIntArrayList expressionToDequalifyOffsets = new TIntArrayList();
      copy.accept(
          new JavaRecursiveElementWalkingVisitor() {
            int delta;

            @Override
            public void visitReferenceElement(PsiJavaCodeReferenceElement expression) {
              if (isParameterizedReference(expression)) return;
              PsiElement qualifierExpression = expression.getQualifier();
              if (qualifierExpression instanceof PsiJavaCodeReferenceElement
                  && ((PsiJavaCodeReferenceElement) qualifierExpression).isReferenceTo(aClass)) {
                try {
                  PsiElement resolved = expression.resolve();
                  int end = expression.getTextRange().getEndOffset();
                  qualifierExpression.delete();
                  delta += end - expression.getTextRange().getEndOffset();
                  PsiElement after = expression.resolve();
                  if (manager.areElementsEquivalent(after, resolved)) {
                    expressionToDequalifyOffsets.add(
                        expression.getTextRange().getStartOffset() + delta);
                  }
                } catch (IncorrectOperationException e) {
                  LOG.error(e);
                }
              }
              super.visitElement(expression);
            }
          });

      expressionToDequalifyOffsets.forEachDescending(
          new TIntProcedure() {
            @Override
            public boolean execute(int offset) {
              PsiJavaCodeReferenceElement expression =
                  PsiTreeUtil.findElementOfClassAtOffset(
                      root, offset, PsiJavaCodeReferenceElement.class, false);
              if (expression == null) {
                return false;
              }
              PsiElement qualifierExpression = expression.getQualifier();
              if (qualifierExpression instanceof PsiJavaCodeReferenceElement
                  && ((PsiJavaCodeReferenceElement) qualifierExpression).isReferenceTo(aClass)) {
                qualifierExpression.delete();
                if (editor != null) {
                  HighlightManager.getInstance(project)
                      .addRangeHighlight(
                          editor,
                          expression.getTextRange().getStartOffset(),
                          expression.getTextRange().getEndOffset(),
                          EditorColorsManager.getInstance()
                              .getGlobalScheme()
                              .getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES),
                          false,
                          null);
                }
              }

              return true;
            }
          });
    }
  }