@NotNull
 public PsiElementVisitor buildVisitor(
     @NotNull ProblemsHolder holder,
     boolean isOnTheFly,
     @NotNull final LocalInspectionToolSession session) {
   session.putUserData(LARGE_FUNCTIONS_KEY, new HashSet<ScopeOwner>());
   return new Visitor(holder, session);
 }
コード例 #2
0
  @NotNull
  @Override
  public PsiElementVisitor buildVisitor(
      @NotNull final ProblemsHolder holder,
      final boolean isOnTheFly,
      final LocalInspectionToolSession session) {
    final PsiFile file = session.getFile();
    Module module = ModuleUtil.findModuleForPsiElement(file);
    if (module == null) return super.buildVisitor(holder, isOnTheFly, session);
    final GlobalSearchScope searchScope = GlobalSearchScope.moduleWithDependentsScope(module);
    final PsiSearchHelper searchHelper = file.getManager().getSearchHelper();
    return new PsiElementVisitor() {
      @Override
      public void visitElement(PsiElement element) {
        if (!(element instanceof Property)) return;
        Property property = (Property) element;

        final ProgressIndicator original = ProgressManager.getInstance().getProgressIndicator();
        if (original != null) {
          if (original.isCanceled()) return;
          original.setText(
              PropertiesBundle.message(
                  "searching.for.property.key.progress.text", property.getUnescapedKey()));
        }

        String name = property.getName();
        if (name == null) return;
        PsiSearchHelper.SearchCostResult cheapEnough =
            searchHelper.isCheapEnoughToSearch(name, searchScope, file, original);
        if (cheapEnough == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES) return;

        final PsiReference usage =
            cheapEnough == PsiSearchHelper.SearchCostResult.ZERO_OCCURRENCES
                ? null
                : ReferencesSearch.search(property, searchScope, false).findFirst();
        if (usage != null) return;
        final ASTNode propertyNode = property.getNode();
        assert propertyNode != null;

        ASTNode[] nodes = propertyNode.getChildren(null);
        PsiElement key = nodes.length == 0 ? property : nodes[0].getPsi();
        String description = PropertiesBundle.message("unused.property.problem.descriptor.name");

        holder.registerProblem(
            key,
            description,
            ProblemHighlightType.LIKE_UNUSED_SYMBOL,
            RemovePropertyLocalFix.INSTANCE);
      }
    };
  }
コード例 #3
0
  private static void checkAnnotationsJarAttached(@NotNull LocalInspectionToolSession session) {
    PsiFile file = session.getFile();
    final Project project = file.getProject();
    PsiClass event =
        JavaPsiFacade.getInstance(project)
            .findClass("java.awt.event.InputEvent", GlobalSearchScope.allScope(project));
    if (event == null) return; // no jdk to attach
    PsiMethod[] methods = event.findMethodsByName("getModifiers", false);
    if (methods.length != 1) return; // no jdk to attach
    PsiMethod getModifiers = methods[0];
    PsiAnnotation annotation =
        ExternalAnnotationsManager.getInstance(project)
            .findExternalAnnotation(getModifiers, MagicConstant.class.getName());
    if (annotation != null) return;
    final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(getModifiers);
    if (virtualFile == null) return; // no jdk to attach
    final List<OrderEntry> entries =
        ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(virtualFile);
    Sdk jdk = null;
    for (OrderEntry orderEntry : entries) {
      if (orderEntry instanceof JdkOrderEntry) {
        jdk = ((JdkOrderEntry) orderEntry).getJdk();
        if (jdk != null) break;
      }
    }
    if (jdk == null) return; // no jdk to attach

    if (!ApplicationManager.getApplication().isUnitTestMode()) {
      final Sdk finalJdk = jdk;
      ApplicationManager.getApplication()
          .invokeLater(
              new Runnable() {
                @Override
                public void run() {
                  ApplicationManager.getApplication()
                      .runWriteAction(
                          new Runnable() {
                            public void run() {
                              attachJdkAnnotations(finalJdk);
                            }
                          });
                }
              },
              ModalityState.NON_MODAL,
              project.getDisposed());
    }
  }