コード例 #1
0
    public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
      if (!CodeInsightUtilBase.preparePsiElementForWrite(descriptor.getPsiElement())) return;
      final PsiMethod psiMethod =
          PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiMethod.class);
      if (psiMethod != null) {
        final ArrayList<PsiElement> psiParameters = new ArrayList<PsiElement>();
        final RefElement refMethod = myManager != null ? myManager.getReference(psiMethod) : null;
        if (refMethod != null) {
          for (final RefParameter refParameter : getUnusedParameters((RefMethod) refMethod)) {
            psiParameters.add(refParameter.getElement());
          }
        } else {
          final PsiParameter[] parameters = psiMethod.getParameterList().getParameters();
          for (PsiParameter parameter : parameters) {
            if (Comparing.strEqual(parameter.getName(), myHint)) {
              psiParameters.add(parameter);
              break;
            }
          }
        }
        final PsiModificationTracker tracker = psiMethod.getManager().getModificationTracker();
        final long startModificationCount = tracker.getModificationCount();

        removeUnusedParameterViaChangeSignature(psiMethod, psiParameters);
        if (refMethod != null && startModificationCount != tracker.getModificationCount()) {
          myProcessor.ignoreElement(refMethod);
        }
      }
    }
コード例 #2
0
  private static void ignoreElement(
      @NotNull ProblemDescriptionsProcessor processor, @NotNull RefEntity refElement) {
    processor.ignoreElement(refElement);

    if (refElement instanceof RefClass) {
      RefClass refClass = (RefClass) refElement;
      RefMethod defaultConstructor = refClass.getDefaultConstructor();
      if (defaultConstructor != null) {
        processor.ignoreElement(defaultConstructor);
        return;
      }
    }

    RefEntity owner = refElement.getOwner();
    if (owner instanceof RefElement) {
      processor.ignoreElement(owner);
    }
  }
コード例 #3
0
  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;
  }
コード例 #4
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;
  }