Search in sources :

Example 66 with SearchScope

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

the class JavaAllOverridingMethodsSearcher method execute.

@Override
public boolean execute(@NotNull final AllOverridingMethodsSearch.SearchParameters p, @NotNull final Processor<Pair<PsiMethod, PsiMethod>> consumer) {
    final PsiClass psiClass = p.getPsiClass();
    final List<PsiMethod> potentials = ReadAction.compute(() -> ContainerUtil.filter(psiClass.getMethods(), PsiUtil::canBeOverriden));
    final SearchScope scope = p.getScope();
    Processor<PsiClass> inheritorsProcessor = inheritor -> {
        Project project = psiClass.getProject();
        for (PsiMethod superMethod : potentials) {
            ProgressManager.checkCanceled();
            if (superMethod.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) && !JavaPsiFacade.getInstance(project).arePackagesTheSame(psiClass, inheritor))
                continue;
            PsiMethod inInheritor = JavaOverridingMethodsSearcher.findOverridingMethod(project, inheritor, superMethod, psiClass);
            if (inInheritor != null && !consumer.process(Pair.create(superMethod, inInheritor)))
                return false;
        }
        return true;
    };
    return ClassInheritorsSearch.search(psiClass, scope, true).forEach(inheritorsProcessor);
}
Also used : ProgressManager(com.intellij.openapi.progress.ProgressManager) PsiMethod(com.intellij.psi.PsiMethod) QueryExecutor(com.intellij.util.QueryExecutor) JavaPsiFacade(com.intellij.psi.JavaPsiFacade) SearchScope(com.intellij.psi.search.SearchScope) AllOverridingMethodsSearch(com.intellij.psi.search.searches.AllOverridingMethodsSearch) ContainerUtil(com.intellij.util.containers.ContainerUtil) ReadAction(com.intellij.openapi.application.ReadAction) PsiModifier(com.intellij.psi.PsiModifier) PsiClass(com.intellij.psi.PsiClass) List(java.util.List) Processor(com.intellij.util.Processor) Pair(com.intellij.openapi.util.Pair) Project(com.intellij.openapi.project.Project) PsiUtil(com.intellij.psi.util.PsiUtil) ClassInheritorsSearch(com.intellij.psi.search.searches.ClassInheritorsSearch) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) SearchScope(com.intellij.psi.search.SearchScope)

Example 67 with SearchScope

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

the class JavaClassInheritorsSearcher method checkCandidate.

private static boolean checkCandidate(@NotNull PsiClass candidate, @NotNull ClassInheritorsSearch.SearchParameters parameters) {
    SearchScope searchScope = parameters.getScope();
    ProgressManager.checkCanceled();
    if (!PsiSearchScopeUtil.isInScope(searchScope, candidate)) {
        return false;
    }
    if (candidate instanceof PsiAnonymousClass) {
        return true;
    }
    String name = candidate.getName();
    return name != null && parameters.getNameCondition().value(name);
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Example 68 with SearchScope

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

the class JavaDirectInheritorsSearcher method execute.

@Override
public boolean execute(@NotNull final DirectClassInheritorsSearch.SearchParameters parameters, @NotNull final Processor<PsiClass> consumer) {
    final PsiClass baseClass = parameters.getClassToProcess();
    assert parameters.isCheckInheritance();
    final Project project = PsiUtilCore.getProjectInReadAction(baseClass);
    if (JavaClassInheritorsSearcher.isJavaLangObject(baseClass)) {
        SearchScope useScope = ReadAction.compute(baseClass::getUseScope);
        return AllClassesSearch.search(useScope, project).forEach(psiClass -> {
            ProgressManager.checkCanceled();
            if (psiClass.isInterface()) {
                return consumer.process(psiClass);
            }
            final PsiClass superClass = psiClass.getSuperClass();
            return superClass == null || !JavaClassInheritorsSearcher.isJavaLangObject(superClass) || consumer.process(psiClass);
        });
    }
    SearchScope scope;
    SearchScope useScope;
    CompilerDirectHierarchyInfo info = performSearchUsingCompilerIndices(parameters, parameters.getScope(), project);
    if (info == null) {
        scope = parameters.getScope();
        useScope = ReadAction.compute(baseClass::getUseScope);
    } else {
        if (!processInheritorCandidates(info.getHierarchyChildren(), consumer, parameters.includeAnonymous()))
            return false;
        scope = ReadAction.compute(() -> parameters.getScope().intersectWith(info.getDirtyScope()));
        useScope = ReadAction.compute(() -> baseClass.getUseScope().intersectWith(info.getDirtyScope()));
    }
    PsiClass[] cache = getOrCalculateDirectSubClasses(project, baseClass, useScope);
    if (cache.length == 0) {
        return true;
    }
    VirtualFile baseClassJarFile = null;
    // iterate by same-FQN groups. For each group process only same-jar subclasses, or all of them if they are all outside the jarFile.
    int groupStart = 0;
    boolean sameJarClassFound = false;
    String currentFQN = null;
    // here we cache results of isInScope(scope, subClass) to avoid calculating it twice
    boolean[] isOutOfScope = new boolean[cache.length];
    for (int i = 0; i <= cache.length; i++) {
        ProgressManager.checkCanceled();
        PsiClass subClass = i == cache.length ? null : cache[i];
        if (subClass instanceof PsiAnonymousClass) {
            // we reached anonymous classes tail, process them all and exit
            if (!parameters.includeAnonymous()) {
                return true;
            }
        }
        if (i != cache.length && !isInScope(scope, subClass)) {
            isOutOfScope[i] = true;
            continue;
        }
        String fqn = i == cache.length ? null : ReadAction.compute(subClass::getQualifiedName);
        if (currentFQN != null && Comparing.equal(fqn, currentFQN)) {
            VirtualFile currentJarFile = getJarFile(subClass);
            if (baseClassJarFile == null) {
                baseClassJarFile = getJarFile(baseClass);
            }
            boolean fromSameJar = Comparing.equal(currentJarFile, baseClassJarFile);
            if (fromSameJar) {
                if (!consumer.process(subClass))
                    return false;
                sameJarClassFound = true;
            }
        } else {
            currentFQN = fqn;
            // the end of the same-FQN group. Process only same-jar classes in subClasses[groupStart..i-1] group or the whole group if there were none.
            if (!sameJarClassFound) {
                for (int g = groupStart; g < i; g++) {
                    ProgressManager.checkCanceled();
                    if (isOutOfScope[g])
                        continue;
                    PsiClass subClassCandidate = cache[g];
                    if (!consumer.process(subClassCandidate))
                        return false;
                }
            }
            groupStart = i;
            sameJarClassFound = false;
        }
    }
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) SearchScope(com.intellij.psi.search.SearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) CompilerDirectHierarchyInfo(com.intellij.compiler.CompilerDirectHierarchyInfo)

Example 69 with SearchScope

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

the class JavaOverridingMethodsSearcher method compute.

@NotNull
private static Iterable<PsiMethod> compute(@NotNull PsiMethod method, @NotNull Project project) {
    Application application = ApplicationManager.getApplication();
    final PsiClass containingClass = application.runReadAction((Computable<PsiClass>) method::getContainingClass);
    assert containingClass != null;
    Collection<PsiMethod> result = new LinkedHashSet<>();
    Processor<PsiClass> inheritorsProcessor = inheritor -> {
        PsiMethod found = application.runReadAction((Computable<PsiMethod>) () -> findOverridingMethod(project, inheritor, method, containingClass));
        if (found != null) {
            result.add(found);
        }
        return true;
    };
    // use wider scope to handle public method defined in package-private class which is subclassed by public class in the same package which is subclassed by public class from another package with redefined method
    SearchScope allScope = GlobalSearchScope.allScope(project);
    boolean success = ClassInheritorsSearch.search(containingClass, allScope, true).forEach(inheritorsProcessor);
    assert success;
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ProgressManager(com.intellij.openapi.progress.ProgressManager) TypeConversionUtil(com.intellij.psi.util.TypeConversionUtil) Application(com.intellij.openapi.application.Application) MethodSignatureUtil(com.intellij.psi.util.MethodSignatureUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) QueryExecutor(com.intellij.util.QueryExecutor) OverridingMethodsSearch(com.intellij.psi.search.searches.OverridingMethodsSearch) Collection(java.util.Collection) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Computable(com.intellij.openapi.util.Computable) SearchScope(com.intellij.psi.search.SearchScope) ReadAction(com.intellij.openapi.application.ReadAction) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Nullable(org.jetbrains.annotations.Nullable) PsiSearchScopeUtil(com.intellij.psi.search.PsiSearchScopeUtil) MethodSignature(com.intellij.psi.util.MethodSignature) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Project(com.intellij.openapi.project.Project) com.intellij.psi(com.intellij.psi) ClassInheritorsSearch(com.intellij.psi.search.searches.ClassInheritorsSearch) NotNull(org.jetbrains.annotations.NotNull) LinkedHashSet(java.util.LinkedHashSet) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Application(com.intellij.openapi.application.Application) Computable(com.intellij.openapi.util.Computable) NotNull(org.jetbrains.annotations.NotNull)

Example 70 with SearchScope

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

SearchScope (com.intellij.psi.search.SearchScope)90 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)39 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)36 Project (com.intellij.openapi.project.Project)21 NotNull (org.jetbrains.annotations.NotNull)21 VirtualFile (com.intellij.openapi.vfs.VirtualFile)18 PsiElement (com.intellij.psi.PsiElement)16 PsiClass (com.intellij.psi.PsiClass)8 Processor (com.intellij.util.Processor)8 com.intellij.psi (com.intellij.psi)6 PsiMethod (com.intellij.psi.PsiMethod)6 HashMap (com.intellij.util.containers.HashMap)6 ClassInheritorsSearch (com.intellij.psi.search.searches.ClassInheritorsSearch)5 Nullable (org.jetbrains.annotations.Nullable)5 AnalysisScope (com.intellij.analysis.AnalysisScope)4 ProgressManager (com.intellij.openapi.progress.ProgressManager)4 TextRange (com.intellij.openapi.util.TextRange)4 PsiFile (com.intellij.psi.PsiFile)4 PsiSearchHelper (com.intellij.psi.search.PsiSearchHelper)4 QueryExecutor (com.intellij.util.QueryExecutor)4