use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.
the class JavaDirectInheritorsSearcher method calculateDirectSubClasses.
@NotNull
private static PsiClass[] calculateDirectSubClasses(@NotNull Project project, @NotNull PsiClass baseClass, @NotNull String baseClassName, @NotNull SearchScope useScope) {
DumbService dumbService = DumbService.getInstance(project);
GlobalSearchScope globalUseScope = dumbService.runReadActionInSmartMode(() -> StubHierarchyInheritorSearcher.restrictScope(GlobalSearchScopeUtil.toGlobalSearchScope(useScope, project)));
Collection<PsiReferenceList> candidates = dumbService.runReadActionInSmartMode(() -> JavaSuperClassNameOccurenceIndex.getInstance().get(baseClassName, project, globalUseScope));
// memory/speed optimisation: it really is a map(string -> PsiClass or List<PsiClass>)
final Map<String, Object> classesWithFqn = new HashMap<>();
processConcurrentlyIfTooMany(candidates, referenceList -> {
ProgressManager.checkCanceled();
ApplicationManager.getApplication().runReadAction(() -> {
final PsiClass candidate = (PsiClass) referenceList.getParent();
boolean isInheritor = candidate.isInheritor(baseClass, false);
if (isInheritor) {
String fqn = candidate.getQualifiedName();
synchronized (classesWithFqn) {
Object value = classesWithFqn.get(fqn);
if (value == null) {
classesWithFqn.put(fqn, candidate);
} else if (value instanceof PsiClass) {
List<PsiClass> list = new ArrayList<>();
list.add((PsiClass) value);
list.add(candidate);
classesWithFqn.put(fqn, list);
} else {
@SuppressWarnings("unchecked") List<PsiClass> list = (List<PsiClass>) value;
list.add(candidate);
}
}
}
});
return true;
});
final List<PsiClass> result = new ArrayList<>();
for (Object value : classesWithFqn.values()) {
if (value instanceof PsiClass) {
result.add((PsiClass) value);
} else {
@SuppressWarnings("unchecked") List<PsiClass> list = (List<PsiClass>) value;
result.addAll(list);
}
}
Collection<PsiAnonymousClass> anonymousCandidates = dumbService.runReadActionInSmartMode(() -> JavaAnonymousClassBaseRefOccurenceIndex.getInstance().get(baseClassName, project, globalUseScope));
processConcurrentlyIfTooMany(anonymousCandidates, candidate -> {
boolean isInheritor = dumbService.runReadActionInSmartMode(() -> candidate.isInheritor(baseClass, false));
if (isInheritor) {
synchronized (result) {
result.add(candidate);
}
}
return true;
});
boolean isEnum = ReadAction.compute(baseClass::isEnum);
if (isEnum) {
// abstract enum can be subclassed in the body
PsiField[] fields = ReadAction.compute(baseClass::getFields);
for (final PsiField field : fields) {
ProgressManager.checkCanceled();
if (field instanceof PsiEnumConstant) {
PsiEnumConstantInitializer initializingClass = ReadAction.compute(((PsiEnumConstant) field)::getInitializingClass);
if (initializingClass != null) {
// it surely is an inheritor
result.add(initializingClass);
}
}
}
}
return result.isEmpty() ? PsiClass.EMPTY_ARRAY : result.toArray(new PsiClass[result.size()]);
}
use of com.intellij.psi.search.GlobalSearchScope 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.GlobalSearchScope in project intellij-community by JetBrains.
the class PsiClassConverter method fromString.
public PsiClass fromString(final String s, final ConvertContext context) {
if (StringUtil.isEmptyOrSpaces(s))
return null;
final DomElement element = context.getInvocationElement();
final GlobalSearchScope scope = element instanceof GenericDomValue ? getScope(context) : null;
return DomJavaUtil.findClass(s.trim(), context.getFile(), context.getModule(), scope);
}
use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.
the class JavaTestGenerator method createTestClass.
@Nullable
private static PsiClass createTestClass(CreateTestDialog d) {
final TestFramework testFrameworkDescriptor = d.getSelectedTestFrameworkDescriptor();
final FileTemplateDescriptor fileTemplateDescriptor = TestIntegrationUtils.MethodKind.TEST_CLASS.getFileTemplateDescriptor(testFrameworkDescriptor);
final PsiDirectory targetDirectory = d.getTargetDirectory();
final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(targetDirectory);
if (aPackage != null) {
final GlobalSearchScope scope = GlobalSearchScopesCore.directoryScope(targetDirectory, false);
final PsiClass[] classes = aPackage.findClassByShortName(d.getClassName(), scope);
if (classes.length > 0) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(classes[0])) {
return null;
}
return classes[0];
}
}
if (fileTemplateDescriptor != null) {
final PsiClass classFromTemplate = createTestClassFromCodeTemplate(d, fileTemplateDescriptor, targetDirectory);
if (classFromTemplate != null) {
return classFromTemplate;
}
}
return JavaDirectoryService.getInstance().createClass(targetDirectory, d.getClassName());
}
use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.
the class JavaTestFinder method collectTests.
private boolean collectTests(PsiClass klass, Processor<Pair<? extends PsiNamedElement, Integer>> processor) {
GlobalSearchScope scope = getSearchScope(klass, false);
PsiShortNamesCache cache = PsiShortNamesCache.getInstance(klass.getProject());
String klassName = klass.getName();
Pattern pattern = Pattern.compile(".*" + StringUtil.escapeToRegexp(klassName) + ".*", Pattern.CASE_INSENSITIVE);
HashSet<String> names = new HashSet<>();
cache.getAllClassNames(names);
for (String eachName : names) {
if (pattern.matcher(eachName).matches()) {
for (PsiClass eachClass : cache.getClassesByName(eachName, scope)) {
if (isTestClass(eachClass, klass)) {
if (!processor.process(Pair.create(eachClass, TestFinderHelper.calcTestNameProximity(klassName, eachName)))) {
return true;
}
}
}
}
}
return false;
}
Aggregations