Search in sources :

Example 1 with PsiFileStub

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

the class StubBuildingVisitor method visitMethod.

@Override
@Nullable
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    // See IDEA-78649
    if (isSet(access, Opcodes.ACC_SYNTHETIC))
        return null;
    if (name == null)
        return null;
    if (SYNTHETIC_CLASS_INIT_METHOD.equals(name))
        return null;
    // skip semi-synthetic enum methods
    boolean isEnum = myResult.isEnum();
    if (isEnum) {
        if ("values".equals(name) && desc.startsWith("()"))
            return null;
        //noinspection SpellCheckingInspection
        if ("valueOf".equals(name) && desc.startsWith("(Ljava/lang/String;)"))
            return null;
    }
    boolean isConstructor = SYNTHETIC_INIT_METHOD.equals(name);
    boolean isDeprecated = isSet(access, Opcodes.ACC_DEPRECATED);
    boolean isVarargs = isSet(access, Opcodes.ACC_VARARGS);
    boolean isStatic = isSet(access, Opcodes.ACC_STATIC);
    boolean isAnnotationMethod = myResult.isAnnotationType();
    byte flags = PsiMethodStubImpl.packFlags(isConstructor, isAnnotationMethod, isVarargs, isDeprecated, false, false);
    String canonicalMethodName = isConstructor ? myResult.getName() : name;
    MethodInfo info = null;
    boolean generic = false;
    if (signature != null) {
        try {
            info = parseMethodSignature(signature, exceptions);
            generic = true;
        } catch (ClsFormatException e) {
            if (LOG.isDebugEnabled())
                LOG.debug("source=" + mySource + " signature=" + signature, e);
        }
    }
    if (info == null) {
        info = parseMethodDescription(desc, exceptions);
    }
    PsiMethodStubImpl stub = new PsiMethodStubImpl(myResult, canonicalMethodName, TypeInfo.fromString(info.returnType, false), flags, null);
    PsiModifierListStub modList = new PsiModifierListStubImpl(stub, packMethodFlags(access, myResult.isInterface()));
    PsiTypeParameterListStub list = new PsiTypeParameterListStubImpl(stub);
    for (Pair<String, String[]> parameter : info.typeParameters) {
        PsiTypeParameterStub parameterStub = new PsiTypeParameterStubImpl(list, StringRef.fromString(parameter.first));
        newReferenceList(JavaStubElementTypes.EXTENDS_BOUND_LIST, parameterStub, parameter.second);
    }
    boolean isEnumConstructor = isEnum && isConstructor;
    boolean isInnerClassConstructor = isConstructor && !(myParent instanceof PsiFileStub) && !isSet(myModList.getModifiersMask(), Opcodes.ACC_STATIC);
    List<String> args = info.argTypes;
    if (!generic && isEnumConstructor && args.size() >= 2 && CommonClassNames.JAVA_LANG_STRING.equals(args.get(0)) && "int".equals(args.get(1))) {
        // omit synthetic enum constructor parameters
        args = args.subList(2, args.size());
    }
    PsiParameterListStubImpl parameterList = new PsiParameterListStubImpl(stub);
    int paramCount = args.size();
    PsiParameterStubImpl[] paramStubs = new PsiParameterStubImpl[paramCount];
    for (int i = 0; i < paramCount; i++) {
        // omit synthetic inner class constructor parameter
        if (i == 0 && !generic && isInnerClassConstructor)
            continue;
        String arg = args.get(i);
        boolean isEllipsisParam = isVarargs && i == paramCount - 1;
        TypeInfo typeInfo = TypeInfo.fromString(arg, isEllipsisParam);
        String paramName = i < parameterNames.length ? parameterNames[i] : "p" + (i + 1);
        PsiParameterStubImpl parameterStub = new PsiParameterStubImpl(parameterList, paramName, typeInfo, isEllipsisParam, true);
        paramStubs[i] = parameterStub;
        new PsiModifierListStubImpl(parameterStub, 0);
    }
    newReferenceList(JavaStubElementTypes.THROWS_LIST, stub, ArrayUtil.toStringArray(info.throwTypes));
    int localVarIgnoreCount = isStatic ? 0 : isEnumConstructor ? 3 : 1;
    int paramIgnoreCount = isEnumConstructor ? 2 : isInnerClassConstructor ? 1 : 0;
    return new MethodAnnotationCollectingVisitor(stub, modList, localVarIgnoreCount, paramIgnoreCount, paramCount, paramStubs, myMapping);
}
Also used : TypeInfo(com.intellij.psi.impl.cache.TypeInfo) PsiFileStub(com.intellij.psi.stubs.PsiFileStub) ClsFormatException(com.intellij.util.cls.ClsFormatException) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PsiFileStub

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

the class PsiClassImpl method isAnonymousOrLocal.

private static boolean isAnonymousOrLocal(PsiClass aClass) {
    if (aClass instanceof PsiAnonymousClass)
        return true;
    final PsiClassStub stub = ((PsiClassImpl) aClass).getGreenStub();
    if (stub != null) {
        final StubElement parentStub = stub.getParentStub();
        return !(parentStub instanceof PsiClassStub || parentStub instanceof PsiFileStub);
    }
    PsiElement parent = aClass.getParent();
    while (parent != null) {
        if (parent instanceof PsiMethod || parent instanceof PsiField || parent instanceof PsiClassInitializer)
            return true;
        if (parent instanceof PsiClass || parent instanceof PsiFile)
            return false;
        parent = parent.getParent();
    }
    return false;
}
Also used : PsiFileStub(com.intellij.psi.stubs.PsiFileStub) StubElement(com.intellij.psi.stubs.StubElement) PsiClassStub(com.intellij.psi.impl.java.stubs.PsiClassStub)

Example 3 with PsiFileStub

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

the class ClsStubBuilderTest method doTest.

private static void doTest(VirtualFile clsFile, String resultFileName) {
    try {
        PsiFileStub stub = ClsFileImpl.buildFileStub(clsFile, clsFile.contentsToByteArray());
        assertNotNull(stub);
        String actual = ((StubBase) stub).printTree().trim();
        File resultFile = new File(JavaTestUtil.getJavaTestDataPath() + "/psi/cls/stubBuilder/" + resultFileName);
        if (!resultFile.exists()) {
            System.out.println("No expected data found at: " + resultFile + ", creating one.");
            FileUtil.writeToFile(resultFile, actual);
            fail("No test data found. Created one");
        }
        String expected = StringUtil.convertLineSeparators(FileUtil.loadFile(resultFile)).trim();
        assertEquals(expected, actual);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : PsiFileStub(com.intellij.psi.stubs.PsiFileStub) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 4 with PsiFileStub

use of com.intellij.psi.stubs.PsiFileStub 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)

Example 5 with PsiFileStub

use of com.intellij.psi.stubs.PsiFileStub in project intellij-plugins by JetBrains.

the class FlexStyleIndex method getIndexer.

@NotNull
@Override
public DataIndexer<String, Set<FlexStyleIndexInfo>, FileContent> getIndexer() {
    return new DataIndexer<String, Set<FlexStyleIndexInfo>, FileContent>() {

        @Override
        @NotNull
        public Map<String, Set<FlexStyleIndexInfo>> map(@NotNull FileContent inputData) {
            final THashMap<String, Set<FlexStyleIndexInfo>> map = new THashMap<>();
            if (JavaScriptSupportLoader.isFlexMxmFile(inputData.getFileName())) {
                PsiFile file = inputData.getPsiFile();
                VirtualFile virtualFile = inputData.getFile();
                if (file instanceof XmlFile) {
                    indexMxmlFile((XmlFile) file, virtualFile, map);
                }
            } else {
                StubTree tree = JSPackageIndex.getStubTree(inputData);
                if (tree != null) {
                    for (StubElement e : tree.getPlainList()) {
                        if (e instanceof JSClassStub) {
                            final PsiElement psiElement = e.getPsi();
                            if (psiElement instanceof JSClass) {
                                final String qName = ((JSClass) psiElement).getQualifiedName();
                                indexAttributes(psiElement, qName, true, map);
                            }
                        } else if (e instanceof PsiFileStub) {
                            PsiElement psiElement = e.getPsi();
                            if (psiElement instanceof JSFile) {
                                String name = ((JSFile) psiElement).getName();
                                indexAttributes(psiElement, name, false, map);
                            }
                        }
                    }
                }
            }
            return map;
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Set(java.util.Set) XmlFile(com.intellij.psi.xml.XmlFile) StubTree(com.intellij.psi.stubs.StubTree) StubElement(com.intellij.psi.stubs.StubElement) NotNull(org.jetbrains.annotations.NotNull) THashMap(gnu.trove.THashMap) JSClassStub(com.intellij.lang.javascript.psi.stubs.JSClassStub) PsiFileStub(com.intellij.psi.stubs.PsiFileStub) PsiFile(com.intellij.psi.PsiFile) JSFile(com.intellij.lang.javascript.psi.JSFile) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiFileStub (com.intellij.psi.stubs.PsiFileStub)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 StubElement (com.intellij.psi.stubs.StubElement)3 NotNull (org.jetbrains.annotations.NotNull)2 Language (com.intellij.lang.Language)1 JSFile (com.intellij.lang.javascript.psi.JSFile)1 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)1 JSClassStub (com.intellij.lang.javascript.psi.stubs.JSClassStub)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 TypeInfo (com.intellij.psi.impl.cache.TypeInfo)1 PsiClassStub (com.intellij.psi.impl.java.stubs.PsiClassStub)1 StubTree (com.intellij.psi.stubs.StubTree)1 XmlFile (com.intellij.psi.xml.XmlFile)1 ClsFormatException (com.intellij.util.cls.ClsFormatException)1 THashMap (gnu.trove.THashMap)1 File (java.io.File)1 Set (java.util.Set)1 NonNls (org.jetbrains.annotations.NonNls)1 Nullable (org.jetbrains.annotations.Nullable)1