Search in sources :

Example 1 with SearchRequestCollector

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

the class GroovyConstructorUsagesSearcher method processConstructorUsages.

static void processConstructorUsages(final PsiMethod constructor, final SearchScope searchScope, final Processor<PsiReference> consumer, final SearchRequestCollector collector, final boolean includeOverloads) {
    if (!constructor.isConstructor())
        return;
    final PsiClass clazz = constructor.getContainingClass();
    if (clazz == null)
        return;
    SearchScope onlyGroovy = GroovyScopeUtil.restrictScopeToGroovyFiles(searchScope, GroovyScopeUtil.getEffectiveScope(constructor));
    Set<PsiClass> processed = collector.getSearchSession().getUserData(LITERALLY_CONSTRUCTED_CLASSES);
    if (processed == null) {
        collector.getSearchSession().putUserData(LITERALLY_CONSTRUCTED_CLASSES, processed = ContainerUtil.newConcurrentSet());
    }
    if (!processed.add(clazz))
        return;
    if (clazz.isEnum() && clazz instanceof GroovyPsiElement) {
        for (PsiField field : clazz.getFields()) {
            if (field instanceof GrEnumConstant) {
                final PsiReference ref = field.getReference();
                if (ref != null && ref.isReferenceTo(constructor)) {
                    if (!consumer.process(ref))
                        return;
                }
            }
        }
    }
    final LiteralConstructorSearcher literalProcessor = new LiteralConstructorSearcher(constructor, consumer, includeOverloads);
    final Processor<GrNewExpression> newExpressionProcessor = grNewExpression -> {
        final PsiMethod resolvedConstructor = grNewExpression.resolveMethod();
        if (includeOverloads || constructor.getManager().areElementsEquivalent(resolvedConstructor, constructor)) {
            return consumer.process(grNewExpression.getReferenceElement());
        }
        return true;
    };
    processGroovyClassUsages(clazz, searchScope, collector, newExpressionProcessor, literalProcessor);
    //this()
    if (clazz instanceof GrTypeDefinition) {
        if (!processConstructors(constructor, consumer, clazz, true)) {
            return;
        }
    }
    //super()
    DirectClassInheritorsSearch.search(clazz, onlyGroovy).forEach(new ReadActionProcessor<PsiClass>() {

        @Override
        public boolean processInReadAction(PsiClass inheritor) {
            if (inheritor instanceof GrTypeDefinition) {
                if (!processConstructors(constructor, consumer, inheritor, false))
                    return false;
            }
            return true;
        }
    });
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) SearchScope(com.intellij.psi.search.SearchScope) ContainerUtil(com.intellij.util.containers.ContainerUtil) ControlFlowUtils(org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils) GrConstructorInvocation(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation) PairProcessor(com.intellij.util.PairProcessor) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor) MethodReferencesSearch(com.intellij.psi.search.searches.MethodReferencesSearch) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) LightMemberReference(com.intellij.psi.impl.light.LightMemberReference) SearchRequestCollector(com.intellij.psi.search.SearchRequestCollector) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) Key(com.intellij.openapi.util.Key) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) Set(java.util.Set) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) TextRange(com.intellij.openapi.util.TextRange) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrSafeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression) QueryExecutorBase(com.intellij.openapi.application.QueryExecutorBase) GrTypeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression) Nullable(org.jetbrains.annotations.Nullable) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) Processor(com.intellij.util.Processor) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) DirectClassInheritorsSearch(com.intellij.psi.search.searches.DirectClassInheritorsSearch) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) SearchScope(com.intellij.psi.search.SearchScope)

Example 2 with SearchRequestCollector

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

the class MethodUsagesSearcher method processQuery.

@Override
public void processQuery(@NotNull final MethodReferencesSearch.SearchParameters p, @NotNull final Processor<PsiReference> consumer) {
    final PsiMethod method = p.getMethod();
    final boolean[] isConstructor = new boolean[1];
    final PsiManager[] psiManager = new PsiManager[1];
    final String[] methodName = new String[1];
    final boolean[] isValueAnnotation = new boolean[1];
    final boolean[] needStrictSignatureSearch = new boolean[1];
    final boolean strictSignatureSearch = p.isStrictSignatureSearch();
    final PsiClass aClass = DumbService.getInstance(p.getProject()).runReadActionInSmartMode(() -> {
        PsiClass aClass1 = method.getContainingClass();
        if (aClass1 == null)
            return null;
        isConstructor[0] = method.isConstructor();
        psiManager[0] = aClass1.getManager();
        methodName[0] = method.getName();
        isValueAnnotation[0] = PsiUtil.isAnnotationMethod(method) && PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME.equals(methodName[0]) && method.getParameterList().getParametersCount() == 0;
        needStrictSignatureSearch[0] = strictSignatureSearch && (aClass1 instanceof PsiAnonymousClass || aClass1.hasModifierProperty(PsiModifier.FINAL) || method.hasModifierProperty(PsiModifier.STATIC) || method.hasModifierProperty(PsiModifier.FINAL) || method.hasModifierProperty(PsiModifier.PRIVATE));
        return aClass1;
    });
    if (aClass == null)
        return;
    final SearchRequestCollector collector = p.getOptimizer();
    final SearchScope searchScope = DumbService.getInstance(p.getProject()).runReadActionInSmartMode(p::getEffectiveSearchScope);
    if (searchScope == GlobalSearchScope.EMPTY_SCOPE) {
        return;
    }
    if (isConstructor[0]) {
        new ConstructorReferencesSearchHelper(psiManager[0]).processConstructorReferences(consumer, method, aClass, searchScope, p.getProject(), false, strictSignatureSearch, collector);
    }
    if (isValueAnnotation[0]) {
        Processor<PsiReference> refProcessor = PsiAnnotationMethodReferencesSearcher.createImplicitDefaultAnnotationMethodConsumer(consumer);
        ReferencesSearch.search(aClass, searchScope).forEach(refProcessor);
    }
    if (needStrictSignatureSearch[0]) {
        ReferencesSearch.searchOptimized(method, searchScope, false, collector, consumer);
        return;
    }
    if (StringUtil.isEmpty(methodName[0])) {
        return;
    }
    DumbService.getInstance(p.getProject()).runReadActionInSmartMode(() -> {
        final PsiMethod[] methods = strictSignatureSearch ? new PsiMethod[] { method } : aClass.findMethodsByName(methodName[0], false);
        short searchContext = UsageSearchContext.IN_CODE | UsageSearchContext.IN_COMMENTS | UsageSearchContext.IN_FOREIGN_LANGUAGES;
        for (PsiMethod m : methods) {
            collector.searchWord(methodName[0], searchScope.intersectWith(m.getUseScope()), searchContext, true, m, getTextOccurrenceProcessor(new PsiMethod[] { m }, aClass, strictSignatureSearch));
        }
        SearchScope accessScope = methods[0].getUseScope();
        for (int i = 1; i < methods.length; i++) {
            PsiMethod method1 = methods[i];
            accessScope = accessScope.union(method1.getUseScope());
        }
        SearchScope restrictedByAccessScope = searchScope.intersectWith(accessScope);
        SimpleAccessorReferenceSearcher.addPropertyAccessUsages(method, restrictedByAccessScope, collector);
        return null;
    });
}
Also used : SearchRequestCollector(com.intellij.psi.search.SearchRequestCollector) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope)

Aggregations

SearchRequestCollector (com.intellij.psi.search.SearchRequestCollector)2 SearchScope (com.intellij.psi.search.SearchScope)2 QueryExecutorBase (com.intellij.openapi.application.QueryExecutorBase)1 ReadActionProcessor (com.intellij.openapi.application.ReadActionProcessor)1 Key (com.intellij.openapi.util.Key)1 TextRange (com.intellij.openapi.util.TextRange)1 com.intellij.psi (com.intellij.psi)1 LightMemberReference (com.intellij.psi.impl.light.LightMemberReference)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 DirectClassInheritorsSearch (com.intellij.psi.search.searches.DirectClassInheritorsSearch)1 MethodReferencesSearch (com.intellij.psi.search.searches.MethodReferencesSearch)1 ReferencesSearch (com.intellij.psi.search.searches.ReferencesSearch)1 PairProcessor (com.intellij.util.PairProcessor)1 Processor (com.intellij.util.Processor)1 ContainerUtil (com.intellij.util.containers.ContainerUtil)1 Set (java.util.Set)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1 ControlFlowUtils (org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1