private static void traverseSuperMethods(
     RefMethod refMethod,
     GlobalJavaInspectionContext globalContext,
     GlobalJavaInspectionContext.UsagesProcessor processor) {
   final Collection<RefMethod> superMethods = refMethod.getSuperMethods();
   for (RefMethod superMethod : superMethods) {
     traverseSuperMethods(superMethod, globalContext, processor);
   }
   globalContext.enqueueMethodUsagesProcessor(refMethod, processor);
 }
  protected boolean queryExternalUsagesRequests(
      final RefManager manager,
      final GlobalJavaInspectionContext globalContext,
      final ProblemDescriptionsProcessor processor) {
    final Project project = manager.getProject();
    for (RefElement entryPoint : globalContext.getEntryPointsManager(manager).getEntryPoints()) {
      processor.ignoreElement(entryPoint);
    }

    final PsiSearchHelper helper = PsiSearchHelper.SERVICE.getInstance(project);
    final AnalysisScope scope = manager.getScope();
    manager.iterate(
        new RefJavaVisitor() {
          @Override
          public void visitElement(RefEntity refEntity) {
            if (refEntity instanceof RefMethod) {
              RefMethod refMethod = (RefMethod) refEntity;
              final PsiModifierListOwner element = refMethod.getElement();
              if (element instanceof PsiMethod) { // implicit construcors are invisible
                PsiMethod psiMethod = (PsiMethod) element;
                if (!refMethod.isStatic()
                    && !refMethod.isConstructor()
                    && !PsiModifier.PRIVATE.equals(refMethod.getAccessModifier())) {
                  final ArrayList<RefParameter> unusedParameters = getUnusedParameters(refMethod);
                  if (unusedParameters.isEmpty()) return;
                  PsiMethod[] derived =
                      OverridingMethodsSearch.search(psiMethod, true)
                          .toArray(PsiMethod.EMPTY_ARRAY);
                  for (final RefParameter refParameter : unusedParameters) {
                    if (refMethod.isAbstract() && derived.length == 0) {
                      refParameter.parameterReferenced(false);
                      processor.ignoreElement(refParameter);
                    } else {
                      int idx = refParameter.getIndex();
                      final boolean[] found = {false};
                      for (int i = 0; i < derived.length && !found[0]; i++) {
                        if (!scope.contains(derived[i])) {
                          final PsiParameter[] parameters =
                              derived[i].getParameterList().getParameters();
                          if (parameters.length >= idx) continue;
                          PsiParameter psiParameter = parameters[idx];
                          ReferencesSearch.search(
                                  psiParameter, helper.getUseScope(psiParameter), false)
                              .forEach(
                                  new PsiReferenceProcessorAdapter(
                                      new PsiReferenceProcessor() {
                                        public boolean execute(PsiReference element) {
                                          refParameter.parameterReferenced(false);
                                          processor.ignoreElement(refParameter);
                                          found[0] = true;
                                          return false;
                                        }
                                      }));
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        });
    return false;
  }
Esempio n. 3
0
  @Override
  protected boolean queryExternalUsagesRequests(
      @NotNull final RefManager manager,
      @NotNull final GlobalJavaInspectionContext globalContext,
      @NotNull final ProblemDescriptionsProcessor problemsProcessor) {
    for (RefElement entryPoint : globalContext.getEntryPointsManager(manager).getEntryPoints()) {
      problemsProcessor.ignoreElement(entryPoint);
    }

    manager.iterate(
        new RefJavaVisitor() {
          @Override
          public void visitElement(@NotNull RefEntity refEntity) {
            if (problemsProcessor.getDescriptions(refEntity) == null) return;
            refEntity.accept(
                new RefJavaVisitor() {
                  @Override
                  public void visitMethod(@NotNull final RefMethod refMethod) {
                    if (!refMethod.isStatic()
                        && !PsiModifier.PRIVATE.equals(refMethod.getAccessModifier())
                        && !(refMethod instanceof RefImplicitConstructor)) {
                      globalContext.enqueueDerivedMethodsProcessor(
                          refMethod,
                          new GlobalJavaInspectionContext.DerivedMethodsProcessor() {
                            @Override
                            public boolean process(PsiMethod derivedMethod) {
                              ((RefElementImpl) refMethod)
                                  .setFlag(false, CanBeFinalAnnotator.CAN_BE_FINAL_MASK);
                              problemsProcessor.ignoreElement(refMethod);
                              return false;
                            }
                          });
                    }
                  }

                  @Override
                  public void visitClass(@NotNull final RefClass refClass) {
                    if (!refClass.isAnonymous()) {
                      globalContext.enqueueDerivedClassesProcessor(
                          refClass,
                          new GlobalJavaInspectionContext.DerivedClassesProcessor() {
                            @Override
                            public boolean process(PsiClass inheritor) {
                              ((RefClassImpl) refClass)
                                  .setFlag(false, CanBeFinalAnnotator.CAN_BE_FINAL_MASK);
                              problemsProcessor.ignoreElement(refClass);
                              return false;
                            }
                          });
                    }
                  }

                  @Override
                  public void visitField(@NotNull final RefField refField) {
                    globalContext.enqueueFieldUsagesProcessor(
                        refField,
                        new GlobalJavaInspectionContext.UsagesProcessor() {
                          @Override
                          public boolean process(PsiReference psiReference) {
                            PsiElement expression = psiReference.getElement();
                            if (expression instanceof PsiReferenceExpression
                                && PsiUtil.isAccessedForWriting((PsiExpression) expression)) {
                              ((RefFieldImpl) refField)
                                  .setFlag(false, CanBeFinalAnnotator.CAN_BE_FINAL_MASK);
                              problemsProcessor.ignoreElement(refField);
                              return false;
                            }
                            return true;
                          }
                        });
                  }
                });
          }
        });

    return false;
  }
  @Override
  protected boolean queryExternalUsagesRequests(
      @NotNull final RefManager manager,
      @NotNull final GlobalJavaInspectionContext globalContext,
      @NotNull final ProblemDescriptionsProcessor processor) {
    final EntryPointsManager entryPointsManager = globalContext.getEntryPointsManager(manager);
    for (RefElement entryPoint : entryPointsManager.getEntryPoints()) {
      ignoreElement(processor, entryPoint);
    }

    ExtensionPoint<VisibilityExtension> point =
        Extensions.getRootArea().getExtensionPoint(ExtensionPoints.VISIBLITY_TOOL);
    for (VisibilityExtension addin : point.getExtensions()) {
      addin.fillIgnoreList(manager, processor);
    }
    manager.iterate(
        new RefJavaVisitor() {
          @Override
          public void visitElement(@NotNull final RefEntity refEntity) {
            if (!(refEntity instanceof RefElement)) return;
            if (processor.getDescriptions(refEntity) == null) return;
            refEntity.accept(
                new RefJavaVisitor() {
                  @Override
                  public void visitField(@NotNull final RefField refField) {
                    if (refField.getAccessModifier() != PsiModifier.PRIVATE) {
                      globalContext.enqueueFieldUsagesProcessor(
                          refField,
                          new GlobalJavaInspectionContext.UsagesProcessor() {
                            @Override
                            public boolean process(PsiReference psiReference) {
                              ignoreElement(processor, refField);
                              return false;
                            }
                          });
                    }
                  }

                  @Override
                  public void visitMethod(@NotNull final RefMethod refMethod) {
                    if (!refMethod.isExternalOverride()
                        && refMethod.getAccessModifier() != PsiModifier.PRIVATE
                        && !(refMethod instanceof RefImplicitConstructor)) {
                      globalContext.enqueueDerivedMethodsProcessor(
                          refMethod,
                          new GlobalJavaInspectionContext.DerivedMethodsProcessor() {
                            @Override
                            public boolean process(PsiMethod derivedMethod) {
                              ignoreElement(processor, refMethod);
                              return false;
                            }
                          });

                      globalContext.enqueueMethodUsagesProcessor(
                          refMethod,
                          new GlobalJavaInspectionContext.UsagesProcessor() {
                            @Override
                            public boolean process(PsiReference psiReference) {
                              ignoreElement(processor, refMethod);
                              return false;
                            }
                          });

                      if (entryPointsManager.isAddNonJavaEntries()) {
                        final RefClass ownerClass = refMethod.getOwnerClass();
                        if (refMethod.isConstructor()
                            && ownerClass.getDefaultConstructor() != null) {
                          String qualifiedName = ownerClass.getElement().getQualifiedName();
                          if (qualifiedName != null) {
                            final Project project = manager.getProject();
                            PsiSearchHelper.SERVICE
                                .getInstance(project)
                                .processUsagesInNonJavaFiles(
                                    qualifiedName,
                                    new PsiNonJavaFileReferenceProcessor() {
                                      @Override
                                      public boolean process(
                                          PsiFile file, int startOffset, int endOffset) {
                                        entryPointsManager.addEntryPoint(refMethod, false);
                                        ignoreElement(processor, refMethod);
                                        return false;
                                      }
                                    },
                                    GlobalSearchScope.projectScope(project));
                          }
                        }
                      }
                    }
                  }

                  @Override
                  public void visitClass(@NotNull final RefClass refClass) {
                    if (!refClass.isAnonymous()) {
                      globalContext.enqueueDerivedClassesProcessor(
                          refClass,
                          new GlobalJavaInspectionContext.DerivedClassesProcessor() {
                            @Override
                            public boolean process(PsiClass inheritor) {
                              ignoreElement(processor, refClass);
                              return false;
                            }
                          });

                      globalContext.enqueueClassUsagesProcessor(
                          refClass,
                          new GlobalJavaInspectionContext.UsagesProcessor() {
                            @Override
                            public boolean process(PsiReference psiReference) {
                              ignoreElement(processor, refClass);
                              return false;
                            }
                          });
                    }
                  }
                });
          }
        });
    return false;
  }