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);
}
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);
}
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;
}
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;
}
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;
});
}
Aggregations