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);
        }
      }
    }
  protected boolean queryExternalUsagesRequests(
      final RefManager manager,
      final GlobalJavaInspectionContext globalContext,
      final ProblemDescriptionsProcessor processor) {
    manager.iterate(
        new RefJavaVisitor() {
          @Override
          public void visitElement(RefEntity refEntity) {
            if (refEntity instanceof RefElement && processor.getDescriptions(refEntity) != null) {
              refEntity.accept(
                  new RefJavaVisitor() {
                    @Override
                    public void visitMethod(final RefMethod refMethod) {
                      globalContext.enqueueMethodUsagesProcessor(
                          refMethod,
                          new GlobalJavaInspectionContext.UsagesProcessor() {
                            public boolean process(PsiReference psiReference) {
                              processor.ignoreElement(refMethod);
                              return false;
                            }
                          });
                    }
                  });
            }
          }
        });

    return false;
  }
Ejemplo n.º 3
0
    @Override
    public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
      final PsiElement element = descriptor.getPsiElement();
      final PsiModifierListOwner psiElement =
          PsiTreeUtil.getParentOfType(element, PsiModifierListOwner.class);
      if (psiElement != null) {
        RefJavaElement refElement =
            (RefJavaElement) (myManager != null ? myManager.getReference(psiElement) : null);
        try {
          if (psiElement instanceof PsiVariable) {
            ((PsiVariable) psiElement).normalizeDeclaration();
          }
          final PsiModifierList modifierList = psiElement.getModifierList();
          LOG.assertTrue(modifierList != null);
          modifierList.setModifierProperty(PsiModifier.FINAL, true);
          modifierList.setModifierProperty(PsiModifier.VOLATILE, false);
        } catch (IncorrectOperationException e) {
          LOG.error(e);
        }

        if (refElement != null) {
          RefJavaUtil.getInstance().setIsFinal(refElement, true);
        }
      }
    }
 @Override
 protected boolean queryExternalUsagesRequests(
     @NotNull final RefManager manager,
     @NotNull final GlobalJavaInspectionContext context,
     @NotNull final ProblemDescriptionsProcessor descriptionsProcessor) {
   manager.iterate(
       new RefJavaVisitor() {
         @Override
         public void visitMethod(@NotNull final RefMethod refMethod) {
           if (descriptionsProcessor.getDescriptions(refMethod)
               != null) { // suspicious method -> need to check external usages
             final GlobalJavaInspectionContext.UsagesProcessor usagesProcessor =
                 new GlobalJavaInspectionContext.UsagesProcessor() {
                   @Override
                   public boolean process(PsiReference psiReference) {
                     final PsiElement psiReferenceExpression = psiReference.getElement();
                     if (psiReferenceExpression instanceof PsiReferenceExpression
                         && !isInvertedMethodCall(
                             (PsiReferenceExpression) psiReferenceExpression)) {
                       descriptionsProcessor.ignoreElement(refMethod);
                     }
                     return false;
                   }
                 };
             traverseSuperMethods(refMethod, context, usagesProcessor);
           }
         }
       });
   return false;
 }
    @Override
    public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
      if (!FileModificationService.getInstance()
          .preparePsiElementForWrite(descriptor.getPsiElement())) return;
      final PsiModifierListOwner element =
          PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiModifierListOwner.class);
      if (element != null) {
        RefElement refElement = null;
        if (myManager != null) {
          refElement = myManager.getReference(element);
        }
        try {
          if (element instanceof PsiVariable) {
            ((PsiVariable) element).normalizeDeclaration();
          }

          PsiModifierList list = element.getModifierList();

          LOG.assertTrue(list != null);

          if (element instanceof PsiMethod) {
            PsiMethod psiMethod = (PsiMethod) element;
            PsiClass containingClass = psiMethod.getContainingClass();
            if (containingClass != null
                && containingClass.getParent() instanceof PsiFile
                && myHint == PsiModifier.PRIVATE
                && list.hasModifierProperty(PsiModifier.FINAL)) {
              list.setModifierProperty(PsiModifier.FINAL, false);
            }
          }

          list.setModifierProperty(myHint, true);
          if (refElement instanceof RefJavaElement) {
            RefJavaUtil.getInstance().setAccessModifier((RefJavaElement) refElement, myHint);
          }
        } catch (IncorrectOperationException e) {
          LOG.error(e);
        }
      }
    }
  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;
  }
Ejemplo n.º 7
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;
  }