Search in sources :

Example 26 with StubElement

use of com.intellij.psi.stubs.StubElement in project intellij-community by JetBrains.

the class PsiClassImplUtil method isInReferenceList.

static boolean isInReferenceList(@Nullable PsiReferenceList list, @NotNull PsiClass baseClass, @Nullable String baseName, @NotNull PsiManager manager) {
    if (list == null)
        return false;
    if (list instanceof StubBasedPsiElement) {
        StubElement stub = ((StubBasedPsiElement) list).getStub();
        if (stub instanceof PsiClassReferenceListStub && baseName != null) {
            // classStub.getReferencedNames() is cheaper than getReferencedTypes()
            PsiClassReferenceListStub classStub = (PsiClassReferenceListStub) stub;
            String[] names = classStub.getReferencedNames();
            for (int i = 0; i < names.length; i++) {
                String name = names[i];
                int typeParam = name.indexOf('<');
                if (typeParam != -1) {
                    name = name.substring(0, typeParam);
                }
                // baseName=="ArrayList" classStub.getReferenceNames()[i]=="java.util.ArrayList"
                if (name.endsWith(baseName)) {
                    PsiClassType[] referencedTypes = classStub.getReferencedTypes();
                    PsiClass resolved = referencedTypes[i].resolve();
                    if (manager.areElementsEquivalent(baseClass, resolved))
                        return true;
                }
            }
            return false;
        }
        if (stub != null) {
            // groovy etc
            for (PsiClassType type : list.getReferencedTypes()) {
                if (Comparing.equal(type.getClassName(), baseName) && manager.areElementsEquivalent(baseClass, type.resolve())) {
                    return true;
                }
            }
            return false;
        }
    }
    if (list.getLanguage() == JavaLanguage.INSTANCE) {
        // groovy doesn't have list.getReferenceElements()
        for (PsiJavaCodeReferenceElement referenceElement : list.getReferenceElements()) {
            if (Comparing.strEqual(baseName, referenceElement.getReferenceName()) && manager.areElementsEquivalent(baseClass, referenceElement.resolve())) {
                return true;
            }
        }
        return false;
    }
    for (PsiClassType type : list.getReferencedTypes()) {
        if (Comparing.equal(type.getClassName(), baseName) && manager.areElementsEquivalent(baseClass, type.resolve())) {
            return true;
        }
    }
    return false;
}
Also used : StubElement(com.intellij.psi.stubs.StubElement) PsiClassReferenceListStub(com.intellij.psi.impl.java.stubs.PsiClassReferenceListStub) ElementClassHint(com.intellij.psi.scope.ElementClassHint) NameHint(com.intellij.psi.scope.NameHint)

Example 27 with StubElement

use of com.intellij.psi.stubs.StubElement in project intellij-community by JetBrains.

the class JavaStubBuilderTest method testSOEProof.

public void testSOEProof() {
    StringBuilder sb = new StringBuilder();
    SecureRandom random = new SecureRandom();
    sb.append("class SOE_test {\n BigInteger BIG = new BigInteger(\n");
    int i;
    for (i = 0; i < 100000; i++) {
        sb.append("  \"").append(Math.abs(random.nextInt())).append("\" +\n");
    }
    sb.append("  \"\");\n}");
    PsiJavaFile file = (PsiJavaFile) createLightFile("SOE_test.java", sb.toString());
    long t = System.currentTimeMillis();
    StubElement tree = myBuilder.buildStubTree(file);
    t = System.currentTimeMillis() - t;
    assertEquals("PsiJavaFileStub []\n" + "  IMPORT_LIST:PsiImportListStub\n" + "  CLASS:PsiClassStub[name=SOE_test fqn=SOE_test]\n" + "    MODIFIER_LIST:PsiModifierListStub[mask=0]\n" + "    TYPE_PARAMETER_LIST:PsiTypeParameterListStub\n" + "    EXTENDS_LIST:PsiRefListStub[EXTENDS_LIST:]\n" + "    IMPLEMENTS_LIST:PsiRefListStub[IMPLEMENTS_LIST:]\n" + "    FIELD:PsiFieldStub[BIG:BigInteger=;INITIALIZER_NOT_STORED;]\n" + "      MODIFIER_LIST:PsiModifierListStub[mask=0]\n", DebugUtil.stubTreeToString(tree));
    System.out.println("SOE depth=" + i + ", time=" + t + "ms");
}
Also used : SecureRandom(java.security.SecureRandom) StubElement(com.intellij.psi.stubs.StubElement)

Example 28 with StubElement

use of com.intellij.psi.stubs.StubElement in project intellij-community by JetBrains.

the class JavaStubBuilderTest method doTest.

private void doTest(String source, String expected) {
    PsiJavaFile file = (PsiJavaFile) createLightFile("test.java", source);
    FileASTNode fileNode = file.getNode();
    assertNotNull(fileNode);
    assertFalse(fileNode.isParsed());
    StubElement lightTree = myBuilder.buildStubTree(file);
    assertFalse(fileNode.isParsed());
    // force switch to AST
    file.getNode().getChildren(null);
    StubElement astBasedTree = myBuilder.buildStubTree(file);
    assertTrue(fileNode.isParsed());
    assertEquals("light tree differs", expected, DebugUtil.stubTreeToString(lightTree));
    assertEquals("AST-based tree differs", expected, DebugUtil.stubTreeToString(astBasedTree));
}
Also used : FileASTNode(com.intellij.lang.FileASTNode) StubElement(com.intellij.psi.stubs.StubElement)

Example 29 with StubElement

use of com.intellij.psi.stubs.StubElement in project intellij-community by JetBrains.

the class PsiTreeUtil method getStubChildrenOfTypeAsList.

@NotNull
public static <T extends PsiElement> List<T> getStubChildrenOfTypeAsList(@Nullable PsiElement element, @NotNull Class<T> aClass) {
    if (element == null)
        return Collections.emptyList();
    StubElement<?> stub = element instanceof StubBasedPsiElement ? ((StubBasedPsiElement) element).getStub() : null;
    if (stub == null) {
        return getChildrenOfTypeAsList(element, aClass);
    }
    List<T> result = new SmartList<>();
    for (StubElement childStub : stub.getChildrenStubs()) {
        PsiElement child = childStub.getPsi();
        if (aClass.isInstance(child)) {
            //noinspection unchecked
            result.add((T) child);
        }
    }
    return result;
}
Also used : SmartList(com.intellij.util.SmartList) StubElement(com.intellij.psi.stubs.StubElement) NotNull(org.jetbrains.annotations.NotNull)

Example 30 with StubElement

use of com.intellij.psi.stubs.StubElement in project intellij-community by JetBrains.

the class PsiInvalidElementAccessException method reason.

@NonNls
@NotNull
private static String reason(@NotNull PsiElement root) {
    if (root == PsiUtilCore.NULL_PSI_ELEMENT)
        return "NULL_PSI_ELEMENT";
    PsiElement element = root instanceof PsiFile ? root : root.getParent();
    if (element == null) {
        String m = "parent is null";
        if (root instanceof StubBasedPsiElement) {
            StubElement stub = ((StubBasedPsiElement) root).getStub();
            while (stub != null) {
                m += "\n  each stub=" + stub;
                if (stub instanceof PsiFileStub) {
                    m += "; fileStub.psi=" + stub.getPsi() + "; reason=" + ((PsiFileStub) stub).getInvalidationReason();
                }
                stub = stub.getParentStub();
            }
        }
        return m;
    }
    while (element != null && !(element instanceof PsiFile)) element = element.getParent();
    PsiFile file = (PsiFile) element;
    if (file == null)
        return "containing file is null";
    FileViewProvider provider = file.getViewProvider();
    VirtualFile vFile = provider.getVirtualFile();
    if (!vFile.isValid())
        return vFile + " is invalid";
    if (!provider.isPhysical()) {
        PsiElement context = file.getContext();
        if (context != null && !context.isValid()) {
            return "invalid context: " + reason(context);
        }
    }
    PsiManager manager = file.getManager();
    if (manager.getProject().isDisposed())
        return "project is disposed";
    Language language = file.getLanguage();
    if (language != provider.getBaseLanguage())
        return "File language:" + language + " != Provider base language:" + provider.getBaseLanguage();
    FileViewProvider p = manager.findViewProvider(vFile);
    if (provider != p)
        return "different providers: " + provider + "(" + id(provider) + "); " + p + "(" + id(p) + ")";
    // "dummy" file?
    if (!provider.isPhysical())
        return "non-physical provider: " + provider;
    return "psi is outdated";
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Language(com.intellij.lang.Language) PsiFileStub(com.intellij.psi.stubs.PsiFileStub) StubElement(com.intellij.psi.stubs.StubElement) NonNls(org.jetbrains.annotations.NonNls) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

StubElement (com.intellij.psi.stubs.StubElement)38 Nullable (org.jetbrains.annotations.Nullable)9 PsiElement (com.intellij.psi.PsiElement)7 NotNull (org.jetbrains.annotations.NotNull)7 StubBasedPsiElement (com.intellij.psi.StubBasedPsiElement)4 PsiClassStub (com.intellij.psi.impl.java.stubs.PsiClassStub)4 StubTree (com.intellij.psi.stubs.StubTree)4 StubBasedPsiElementBase (com.intellij.extapi.psi.StubBasedPsiElementBase)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)3 PsiFileStub (com.intellij.psi.stubs.PsiFileStub)3 SmartList (com.intellij.util.SmartList)3 PyClassStub (com.jetbrains.python.psi.stubs.PyClassStub)3 GoFileStub (com.goide.stubs.GoFileStub)2 ASTNode (com.intellij.lang.ASTNode)2 FileASTNode (com.intellij.lang.FileASTNode)2 PsiFile (com.intellij.psi.PsiFile)2 StubBuilder (com.intellij.psi.StubBuilder)2 PsiMethodStub (com.intellij.psi.impl.java.stubs.PsiMethodStub)2 IElementType (com.intellij.psi.tree.IElementType)2