Search in sources :

Example 96 with GlobalSearchScope

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()]);
}
Also used : HashMap(com.intellij.util.containers.HashMap) ArrayList(java.util.ArrayList) DumbService(com.intellij.openapi.project.DumbService) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ArrayList(java.util.ArrayList) List(java.util.List) NotNull(org.jetbrains.annotations.NotNull)

Example 97 with GlobalSearchScope

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);
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) CompilerReferenceService(com.intellij.compiler.CompilerReferenceService)

Example 98 with GlobalSearchScope

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);
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Example 99 with GlobalSearchScope

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());
}
Also used : TestFramework(com.intellij.testIntegration.TestFramework) FileTemplateDescriptor(com.intellij.ide.fileTemplates.FileTemplateDescriptor) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Nullable(org.jetbrains.annotations.Nullable)

Example 100 with GlobalSearchScope

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;
}
Also used : Pattern(java.util.regex.Pattern) PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiClass(com.intellij.psi.PsiClass) HashSet(com.intellij.util.containers.HashSet)

Aggregations

GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)485 Project (com.intellij.openapi.project.Project)145 NotNull (org.jetbrains.annotations.NotNull)134 VirtualFile (com.intellij.openapi.vfs.VirtualFile)106 Module (com.intellij.openapi.module.Module)101 Nullable (org.jetbrains.annotations.Nullable)78 PsiClass (com.intellij.psi.PsiClass)53 ArrayList (java.util.ArrayList)37 PsiFile (com.intellij.psi.PsiFile)32 PsiElement (com.intellij.psi.PsiElement)31 THashSet (gnu.trove.THashSet)22 SearchScope (com.intellij.psi.search.SearchScope)19 PsiDirectory (com.intellij.psi.PsiDirectory)18 ContainerUtil (com.intellij.util.containers.ContainerUtil)18 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)17 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)17 com.intellij.psi (com.intellij.psi)17 List (java.util.List)17 StringUtil (com.intellij.openapi.util.text.StringUtil)15 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)15