use of com.intellij.psi.search.SearchScope in project intellij-community by JetBrains.
the class JavaClassInheritorsSearcher method getOrComputeSubClasses.
@NotNull
private static Iterable<PsiClass> getOrComputeSubClasses(@NotNull Project project, @NotNull PsiClass baseClass, @NotNull SearchScope searchScopeForNonPhysical) {
ConcurrentMap<PsiClass, Iterable<PsiClass>> map = HighlightingCaches.getInstance(project).ALL_SUB_CLASSES;
Iterable<PsiClass> cached = map.get(baseClass);
if (cached == null) {
// returns lazy collection of subclasses. Each call to next() leads to calculation of next batch of subclasses.
Function<PsiAnchor, PsiClass> converter = anchor -> ReadAction.compute(() -> (PsiClass) anchor.retrieve());
Predicate<PsiClass> applicableFilter = candidate -> !(candidate instanceof PsiAnonymousClass) && candidate != null && !candidate.hasModifierProperty(PsiModifier.FINAL);
// for non-physical elements ignore the cache completely because non-physical elements created so often/unpredictably so I can't figure out when to clear caches in this case
boolean isPhysical = ReadAction.compute(baseClass::isPhysical);
SearchScope scopeToUse = isPhysical ? GlobalSearchScope.allScope(project) : searchScopeForNonPhysical;
LazyConcurrentCollection.MoreElementsGenerator<PsiAnchor, PsiClass> generator = (candidate, processor) -> DirectClassInheritorsSearch.search(candidate, scopeToUse).forEach(subClass -> {
ProgressManager.checkCanceled();
PsiAnchor pointer = ReadAction.compute(() -> PsiAnchor.create(subClass));
processor.consume(pointer);
return true;
});
PsiAnchor seed = ReadAction.compute(() -> PsiAnchor.create(baseClass));
// lazy collection: store underlying queue as PsiAnchors, generate new elements by running direct inheritors
Iterable<PsiClass> computed = new LazyConcurrentCollection<>(seed, converter, applicableFilter, generator);
// make sure concurrent calls of this method always return the same collection to avoid expensive duplicate work
cached = isPhysical ? ConcurrencyUtil.cacheOrGet(map, baseClass, computed) : computed;
}
return cached;
}
use of com.intellij.psi.search.SearchScope in project intellij-community by JetBrains.
the class JavaDirectInheritorsSearcher method performSearchUsingCompilerIndices.
private static CompilerDirectHierarchyInfo performSearchUsingCompilerIndices(@NotNull DirectClassInheritorsSearch.SearchParameters parameters, @NotNull SearchScope useScope, @NotNull Project project) {
if (!(useScope instanceof GlobalSearchScope))
return null;
SearchScope scope = parameters.getScope();
if (!(scope instanceof GlobalSearchScope))
return null;
PsiClass searchClass = ReadAction.compute(() -> (PsiClass) PsiUtil.preferCompiledElement(parameters.getClassToProcess()));
final CompilerReferenceService compilerReferenceService = CompilerReferenceService.getInstance(project);
return compilerReferenceService.getDirectInheritors(searchClass, (GlobalSearchScope) useScope, (GlobalSearchScope) scope, JavaFileType.INSTANCE);
}
use of com.intellij.psi.search.SearchScope in project intellij-community by JetBrains.
the class JavaOverridingMethodsSearcher method execute.
@Override
public boolean execute(@NotNull final OverridingMethodsSearch.SearchParameters parameters, @NotNull final Processor<PsiMethod> consumer) {
final PsiMethod method = parameters.getMethod();
Project project = ReadAction.compute(method::getProject);
final SearchScope searchScope = parameters.getScope();
if (searchScope instanceof LocalSearchScope) {
return processLocalScope((LocalSearchScope) searchScope, method, project, consumer);
}
Iterable<PsiMethod> cached = HighlightingCaches.getInstance(project).OVERRIDING_METHODS.get(method);
if (cached == null) {
cached = compute(method, project);
// for non-physical elements ignore the cache completely because non-physical elements created so often/unpredictably so I can't figure out when to clear caches in this case
if (ReadAction.compute(method::isPhysical)) {
HighlightingCaches.getInstance(project).OVERRIDING_METHODS.put(method, cached);
}
}
for (final PsiMethod subMethod : cached) {
ProgressManager.checkCanceled();
if (!ReadAction.compute(() -> PsiSearchScopeUtil.isInScope(searchScope, subMethod))) {
continue;
}
if (!consumer.process(subMethod) || !parameters.isCheckDeep()) {
return false;
}
}
return true;
}
use of com.intellij.psi.search.SearchScope in project intellij-community by JetBrains.
the class AnnotatedElementsSearcher method execute.
@Override
public boolean execute(@NotNull final AnnotatedElementsSearch.Parameters p, @NotNull final Processor<PsiModifierListOwner> consumer) {
final PsiClass annClass = p.getAnnotationClass();
if (!annClass.isAnnotationType())
throw new IllegalArgumentException("Annotation type should be passed to annotated members search but got: " + annClass);
String annotationFQN = ApplicationManager.getApplication().runReadAction(new Computable<String>() {
@Override
public String compute() {
return annClass.getQualifiedName();
}
});
if (annotationFQN == null)
throw new IllegalArgumentException("FQN is null for " + annClass);
final PsiManager psiManager = ApplicationManager.getApplication().runReadAction(new Computable<PsiManager>() {
@Override
public PsiManager compute() {
return annClass.getManager();
}
});
final SearchScope useScope = p.getScope();
final Class<? extends PsiModifierListOwner>[] types = p.getTypes();
for (final PsiAnnotation ann : getAnnotationCandidates(annClass, useScope, psiManager.getProject())) {
final PsiModifierListOwner candidate = ApplicationManager.getApplication().runReadAction(new Computable<PsiModifierListOwner>() {
@Override
public PsiModifierListOwner compute() {
PsiElement parent = ann.getContext();
if (!(parent instanceof PsiModifierList)) {
// Can be a PsiNameValuePair, if annotation is used to annotate annotation parameters
return null;
}
final PsiElement owner = parent.getParent();
if (!isInstanceof(owner, types)) {
return null;
}
if (p.isApproximate()) {
return (PsiModifierListOwner) owner;
}
final PsiJavaCodeReferenceElement ref = ann.getNameReferenceElement();
if (ref == null || !psiManager.areElementsEquivalent(ref.resolve(), annClass)) {
return null;
}
return (PsiModifierListOwner) owner;
}
});
if (candidate != null && !consumer.process(candidate)) {
return false;
}
}
return true;
}
use of com.intellij.psi.search.SearchScope in project intellij-community by JetBrains.
the class JavaCompilingVisitor method buildDescendants.
private static List<PsiClass> buildDescendants(String className, boolean includeSelf, OptimizingSearchHelper searchHelper, CompileContext context) {
if (!searchHelper.doOptimizing())
return Collections.emptyList();
final SearchScope scope = context.getOptions().getScope();
if (!(scope instanceof GlobalSearchScope))
return Collections.emptyList();
final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(context.getProject());
final PsiClass[] classes = cache.getClassesByName(className, (GlobalSearchScope) scope);
final List<PsiClass> results = new ArrayList<>();
final Processor<PsiClass> processor = aClass -> {
results.add(aClass);
return true;
};
for (PsiClass aClass : classes) {
ClassInheritorsSearch.search(aClass, scope, true).forEach(processor);
}
if (includeSelf) {
Collections.addAll(results, classes);
}
return results;
}
Aggregations