Search in sources :

Example 1 with PsiReferenceProcessor

use of com.intellij.psi.search.PsiReferenceProcessor in project intellij-community by JetBrains.

the class UnusedParametersInspection method queryExternalUsagesRequests.

protected boolean queryExternalUsagesRequests(@NotNull final RefManager manager, @NotNull final GlobalJavaInspectionContext globalContext, @NotNull 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(@NotNull RefEntity refEntity) {
            if (refEntity instanceof RefMethod) {
                RefMethod refMethod = (RefMethod) refEntity;
                final PsiModifierListOwner element = refMethod.getElement();
                if (element instanceof PsiMethod) {
                    //implicit constructors 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).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 == null || !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() {

                                            @Override
                                            public boolean execute(PsiReference element) {
                                                refParameter.parameterReferenced(false);
                                                processor.ignoreElement(refParameter);
                                                found[0] = true;
                                                return false;
                                            }
                                        }));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    });
    return false;
}
Also used : ArrayList(java.util.ArrayList) PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) AnalysisScope(com.intellij.analysis.AnalysisScope) Project(com.intellij.openapi.project.Project) PsiReferenceProcessor(com.intellij.psi.search.PsiReferenceProcessor) PsiReferenceProcessorAdapter(com.intellij.psi.search.PsiReferenceProcessorAdapter)

Aggregations

AnalysisScope (com.intellij.analysis.AnalysisScope)1 Project (com.intellij.openapi.project.Project)1 PsiReferenceProcessor (com.intellij.psi.search.PsiReferenceProcessor)1 PsiReferenceProcessorAdapter (com.intellij.psi.search.PsiReferenceProcessorAdapter)1 PsiSearchHelper (com.intellij.psi.search.PsiSearchHelper)1 ArrayList (java.util.ArrayList)1