Search in sources :

Example 1 with PsiReferenceProcessorAdapter

use of com.intellij.psi.search.PsiReferenceProcessorAdapter 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)

Example 2 with PsiReferenceProcessorAdapter

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

the class JavaFindUsagesHelper method addMethodsUsages.

private static boolean addMethodsUsages(@NotNull final PsiClass aClass, @NotNull final PsiManager manager, @NotNull final JavaClassFindUsagesOptions options, @NotNull final Processor<UsageInfo> processor) {
    if (options.isIncludeInherited) {
        final PsiMethod[] methods = ReadAction.compute(aClass::getAllMethods);
        for (int i = 0; i < methods.length; i++) {
            final PsiMethod method = methods[i];
            // filter overridden methods
            final int finalI = i;
            final PsiClass methodClass = ReadAction.compute(() -> {
                MethodSignature methodSignature = method.getSignature(PsiSubstitutor.EMPTY);
                for (int j = 0; j < finalI; j++) {
                    if (methodSignature.equals(methods[j].getSignature(PsiSubstitutor.EMPTY)))
                        return null;
                }
                return method.getContainingClass();
            });
            if (methodClass == null)
                continue;
            boolean equivalent = ReadAction.compute(() -> manager.areElementsEquivalent(methodClass, aClass));
            if (equivalent) {
                if (!addElementUsages(method, options, processor))
                    return false;
            } else {
                MethodReferencesSearch.SearchParameters parameters = new MethodReferencesSearch.SearchParameters(method, options.searchScope, true, options.fastTrack);
                boolean success = MethodReferencesSearch.search(parameters).forEach(new PsiReferenceProcessorAdapter(reference -> {
                    addResultFromReference(reference, methodClass, manager, aClass, options, processor);
                    return true;
                }));
                if (!success)
                    return false;
            }
        }
    } else {
        PsiMethod[] methods = ReadAction.compute(aClass::getMethods);
        for (PsiMethod method : methods) {
            if (!addElementUsages(method, options, processor))
                return false;
        }
    }
    return true;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) PsiMetaOwner(com.intellij.psi.meta.PsiMetaOwner) java.util(java.util) PsiElementProcessorAdapter(com.intellij.psi.search.PsiElementProcessorAdapter) PsiMetaData(com.intellij.psi.meta.PsiMetaData) UsageInfo(com.intellij.usageView.UsageInfo) NullableComputable(com.intellij.openapi.util.NullableComputable) SearchScope(com.intellij.psi.search.SearchScope) ContainerUtil(com.intellij.util.containers.ContainerUtil) FindBundle(com.intellij.find.FindBundle) ReadAction(com.intellij.openapi.application.ReadAction) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor) Comparing(com.intellij.openapi.util.Comparing) PomService(com.intellij.pom.references.PomService) PsiUtil(com.intellij.psi.util.PsiUtil) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Logger(com.intellij.openapi.diagnostic.Logger) PomTarget(com.intellij.pom.PomTarget) Extensions(com.intellij.openapi.extensions.Extensions) ProgressManager(com.intellij.openapi.progress.ProgressManager) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) TextRange(com.intellij.openapi.util.TextRange) AliasingPsiTargetMapper(com.intellij.psi.targets.AliasingPsiTargetMapper) com.intellij.psi.search.searches(com.intellij.psi.search.searches) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) AliasingPsiTarget(com.intellij.psi.targets.AliasingPsiTarget) MethodSignature(com.intellij.psi.util.MethodSignature) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) ThrowSearchUtil(com.intellij.psi.impl.search.ThrowSearchUtil) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) PsiReferenceProcessorAdapter(com.intellij.psi.search.PsiReferenceProcessorAdapter) MethodSignature(com.intellij.psi.util.MethodSignature) PsiReferenceProcessorAdapter(com.intellij.psi.search.PsiReferenceProcessorAdapter)

Aggregations

PsiReferenceProcessorAdapter (com.intellij.psi.search.PsiReferenceProcessorAdapter)2 AnalysisScope (com.intellij.analysis.AnalysisScope)1 FindBundle (com.intellij.find.FindBundle)1 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 ReadAction (com.intellij.openapi.application.ReadAction)1 ReadActionProcessor (com.intellij.openapi.application.ReadActionProcessor)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Extensions (com.intellij.openapi.extensions.Extensions)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 ProgressManager (com.intellij.openapi.progress.ProgressManager)1 Project (com.intellij.openapi.project.Project)1 Comparing (com.intellij.openapi.util.Comparing)1 NullableComputable (com.intellij.openapi.util.NullableComputable)1 TextRange (com.intellij.openapi.util.TextRange)1 PomTarget (com.intellij.pom.PomTarget)1 PomService (com.intellij.pom.references.PomService)1 com.intellij.psi (com.intellij.psi)1 ThrowSearchUtil (com.intellij.psi.impl.search.ThrowSearchUtil)1 PsiMetaData (com.intellij.psi.meta.PsiMetaData)1