Search in sources :

Example 21 with PsiShortNamesCache

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

the class TreeJavaClassChooserDialog method getClassesByName.

@NotNull
protected List<PsiClass> getClassesByName(final String name, final boolean checkBoxState, final String pattern, final GlobalSearchScope searchScope) {
    final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(getProject());
    PsiClass[] classes = cache.getClassesByName(name, checkBoxState ? searchScope : GlobalSearchScope.projectScope(getProject()).intersectWith(searchScope));
    return ContainerUtil.newArrayList(classes);
}
Also used : PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) PsiClass(com.intellij.psi.PsiClass) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with PsiShortNamesCache

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

the class StaticImportMethodFix method getMembersToImport.

@NotNull
@Override
protected List<PsiMethod> getMembersToImport(boolean applicableOnly) {
    final Project project = myMethodCall.getProject();
    PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project);
    final PsiMethodCallExpression element = myMethodCall.getElement();
    PsiReferenceExpression reference = element == null ? null : element.getMethodExpression();
    String name = reference == null ? null : reference.getReferenceName();
    if (name == null)
        return Collections.emptyList();
    final StaticMembersProcessor<PsiMethod> processor = new MyStaticMethodProcessor(element);
    cache.processMethodsWithName(name, element.getResolveScope(), processor);
    return processor.getMembersToImport(applicableOnly);
}
Also used : Project(com.intellij.openapi.project.Project) PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with PsiShortNamesCache

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

the class CreateFromUsageUtils method addClassesWithMember.

public static void addClassesWithMember(final String memberName, final PsiFile file, final Set<String> possibleClassNames, final boolean method, final boolean staticAccess, final boolean addObjectInheritors) {
    final Project project = file.getProject();
    final Module moduleForFile = ModuleUtilCore.findModuleForPsiElement(file);
    if (moduleForFile == null)
        return;
    final GlobalSearchScope searchScope = ApplicationManager.getApplication().runReadAction(new Computable<GlobalSearchScope>() {

        @Override
        public GlobalSearchScope compute() {
            return file.getResolveScope();
        }
    });
    GlobalSearchScope descendantsSearchScope = GlobalSearchScope.moduleWithDependenciesScope(moduleForFile);
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project);
    if (handleObjectMethod(possibleClassNames, facade, searchScope, method, memberName, staticAccess, addObjectInheritors)) {
        return;
    }
    final PsiMember[] members = ApplicationManager.getApplication().runReadAction(new Computable<PsiMember[]>() {

        @Override
        public PsiMember[] compute() {
            return method ? cache.getMethodsByName(memberName, searchScope) : cache.getFieldsByName(memberName, searchScope);
        }
    });
    for (int i = 0; i < members.length; ++i) {
        final PsiMember member = members[i];
        if (hasCorrectModifiers(member, staticAccess)) {
            final PsiClass containingClass = member.getContainingClass();
            if (containingClass != null) {
                final String qName = getQualifiedName(containingClass);
                if (qName == null)
                    continue;
                ClassInheritorsSearch.search(containingClass, descendantsSearchScope, true, true, false).forEach(psiClass -> {
                    ContainerUtil.addIfNotNull(possibleClassNames, getQualifiedName(psiClass));
                    return true;
                });
                possibleClassNames.add(qName);
            }
        }
        members[i] = null;
    }
}
Also used : Project(com.intellij.openapi.project.Project) PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module)

Example 24 with PsiShortNamesCache

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

the class HighlightStressTest method testRandomEditingForUnused.

public void testRandomEditingForUnused() throws Exception {
    configureFromFileText("Stress.java", "class X {<caret>}");
    PsiShortNamesCache cache = PsiShortNamesCache.getInstance(getProject());
    String[] names = cache.getAllClassNames();
    final StringBuilder imports = new StringBuilder();
    final StringBuilder usages = new StringBuilder();
    int v = 0;
    outer: for (String name : names) {
        PsiClass[] classes = cache.getClassesByName(name, GlobalSearchScope.allScope(getProject()));
        if (classes.length == 0)
            continue;
        PsiClass aClass = classes[0];
        if (!aClass.hasModifierProperty(PsiModifier.PUBLIC))
            continue;
        if (aClass.getSuperClass() == null)
            continue;
        PsiClassType[] superTypes = aClass.getSuperTypes();
        if (superTypes.length == 0)
            continue;
        for (PsiClassType superType : superTypes) {
            PsiClass superClass = superType.resolve();
            if (superClass == null || !superClass.hasModifierProperty(PsiModifier.PUBLIC))
                continue outer;
        }
        String qualifiedName = aClass.getQualifiedName();
        // java.lang.invoke.MethodHandle has weird access attributes in recent rt.jar which causes spurious highlighting errors
        if (qualifiedName.startsWith("java.lang.invoke"))
            continue;
        if (!accessible(aClass, new THashSet<>()))
            continue;
        imports.append("import " + qualifiedName + ";\n");
        usages.append("/**/ " + aClass.getName() + " var" + v + " = null; var" + v + ".toString();\n");
        v++;
        if (v > 100)
            break;
    }
    final String text = imports + "\n class X {{\n" + usages + "}}";
    WriteCommandAction.runWriteCommandAction(null, () -> getEditor().getDocument().setText(text));
    List<HighlightInfo> errors = DaemonAnalyzerTestCase.filter(doHighlighting(), HighlightSeverity.WARNING);
    assertEmpty(text, errors);
    Random random = new Random();
    int unused = 0;
    for (int i = 0; i < 100; i++) {
        String s = myFile.getText();
        int offset;
        while (true) {
            offset = random.nextInt(s.length());
            if (CharArrayUtil.regionMatches(s, offset, "/**/") || CharArrayUtil.regionMatches(s, offset, "//"))
                break;
        }
        char next = offset < s.length() - 1 ? s.charAt(offset + 1) : 0;
        if (next == '/') {
            myEditor.getCaretModel().moveToOffset(offset + 1);
            type("**");
            unused--;
        } else if (next == '*') {
            myEditor.getCaretModel().moveToOffset(offset + 1);
            delete();
            delete();
            unused++;
        } else {
            continue;
        }
        PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
        getFile().accept(new PsiRecursiveElementVisitor() {

            @Override
            public void visitElement(PsiElement element) {
                assertTrue(element.toString(), element.isValid());
                super.visitElement(element);
            }
        });
        //System.out.println("i = " + i + " " + next + " at "+offset);
        List<HighlightInfo> infos = doHighlighting();
        errors = DaemonAnalyzerTestCase.filter(infos, HighlightSeverity.ERROR);
        assertEmpty(errors);
        List<HighlightInfo> warns = DaemonAnalyzerTestCase.filter(infos, HighlightSeverity.WARNING);
        if (unused != warns.size()) {
            assertEquals(warns.toString(), unused, warns.size());
        }
    }
    FileEditorManagerEx.getInstanceEx(getProject()).closeAllFiles();
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache)

Example 25 with PsiShortNamesCache

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

the class JavaTestFinder method findClassesForTest.

@NotNull
public Collection<PsiElement> findClassesForTest(@NotNull PsiElement element) {
    PsiClass klass = findSourceElement(element);
    if (klass == null)
        return Collections.emptySet();
    GlobalSearchScope scope = getSearchScope(element, true);
    PsiShortNamesCache cache = PsiShortNamesCache.getInstance(element.getProject());
    List<Pair<? extends PsiNamedElement, Integer>> classesWithWeights = new ArrayList<>();
    for (Pair<String, Integer> eachNameWithWeight : TestFinderHelper.collectPossibleClassNamesWithWeights(klass.getName())) {
        for (PsiClass eachClass : cache.getClassesByName(eachNameWithWeight.first, scope)) {
            if (isTestSubjectClass(eachClass)) {
                classesWithWeights.add(Pair.create(eachClass, eachNameWithWeight.second));
            }
        }
    }
    return TestFinderHelper.getSortedElements(classesWithWeights, false);
}
Also used : PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiNamedElement(com.intellij.psi.PsiNamedElement) PsiClass(com.intellij.psi.PsiClass) ArrayList(java.util.ArrayList) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiShortNamesCache (com.intellij.psi.search.PsiShortNamesCache)30 NotNull (org.jetbrains.annotations.NotNull)20 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)11 Project (com.intellij.openapi.project.Project)6 PsiClass (com.intellij.psi.PsiClass)5 ArrayList (java.util.ArrayList)4 Nullable (org.jetbrains.annotations.Nullable)4 com.intellij.psi (com.intellij.psi)3 Processor (com.intellij.util.Processor)3 HashSet (com.intellij.util.containers.HashSet)3 Module (com.intellij.openapi.module.Module)2 PsiField (com.intellij.psi.PsiField)2 PsiMethod (com.intellij.psi.PsiMethod)2 THashSet (gnu.trove.THashSet)2 List (java.util.List)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 NodeIterator (com.intellij.dupLocator.iterators.NodeIterator)1 DefaultPsiElementCellRenderer (com.intellij.ide.util.DefaultPsiElementCellRenderer)1