resolve() fails with "Invalid stub element type in index" error message
In my PsiTreeChangeListener, in the event handler childAdded(), when the added child is a method I want to use resolve() on its parameters' types to get corresponding classes.
For example, if the newly added method is foo(X x, Y, y), then in the event handler I call resolve() on the parameter types corresponding to X and Y. However, it sometimes happen that I get an error:
Invalid stub element type in index with a file name and the found stub. What is the reason for that? I believe it's that I'm trying to call resolve() before IntelliJ has updated its stub indexes. Is there any way to overcome it?
请先登录再写评论。
To give it more context. In my PsiTreeChangeListener I have the following code:
@Override public void childAdded(@NotNull PsiTreeChangeEvent event) { PsiElement element = event.getChild(); PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class, false); for (PsiParameter param: method.getParameterList().getParameters()) { PsiType paramType = param.getType(); if (paramType instanceof PsiClassType) { PsiClassType paramClassType = (PsiClassType) paramType; paramClassType.resolve(); // this fails } } }This is the error message I see: 3:16:18 PM Throwable: Invalid stub element type in index: file:///Users/dawid/myProject/SomeJavaFile.java; found: PsiImportStatement; file stamp: 338141; file modCount: 1377641715000; unsaved: false; doc stamp: 53398; committed: true; psi stamp: 2; viewProvider stamp: 53398
And this is the stack trace:
[ 123296] ERROR - tellij.psi.stubs.StubIndexImpl - IntelliJ IDEA 12.1.4 Build #IC-129.713
[ 123296] ERROR - tellij.psi.stubs.StubIndexImpl - JDK: 1.6.0_51
[ 123296] ERROR - tellij.psi.stubs.StubIndexImpl - VM: Java HotSpot(TM) 64-Bit Server VM
[ 123296] ERROR - tellij.psi.stubs.StubIndexImpl - Vendor: Apple Inc.
[ 123296] ERROR - tellij.psi.stubs.StubIndexImpl - OS: Mac OS X
[ 123296] ERROR - tellij.psi.stubs.StubIndexImpl - Last Action:
[ 123296] ERROR - tellij.psi.stubs.StubIndexImpl - Current Command: Reload From Disk
[ 124889] ERROR - tellij.psi.stubs.StubIndexImpl - Invalid stub element type in index: file:///Users/dawid/myProject/SomeJavaFile.java; found: PsiImportStatement; file stamp: 340798; file modCount: 1377641715000; unsaved: false; doc stamp: 55099; committed: true; psi stamp: 2; viewProvider stamp: 55099
java.lang.Throwable
at com.intellij.openapi.diagnostic.Logger.error(Logger.java:54)
at com.intellij.psi.stubs.StubIndexImpl.reportStubPsiMismatch(StubIndexImpl.java:438)
at com.intellij.psi.stubs.StubIndex.safeGet(StubIndex.java:76)
at com.intellij.psi.impl.java.stubs.index.JavaFullClassNameIndex.get(JavaFullClassNameIndex.java:48)
at com.intellij.psi.impl.file.impl.JavaFileManagerBase.findClassInIndex(JavaFileManagerBase.java:276)
at com.intellij.psi.impl.file.impl.JavaFileManagerBase.findClass(JavaFileManagerBase.java:186)
at com.intellij.psi.impl.JavaPsiFacadeImpl$PsiElementFinderImpl.findClass(JavaPsiFacadeImpl.java:281)
at com.intellij.psi.impl.JavaPsiFacadeImpl.findClass(JavaPsiFacadeImpl.java:109)
at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl.resolve(PsiJavaCodeReferenceElementImpl.java:390)
at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl.access$000(PsiJavaCodeReferenceElementImpl.java:54)
at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl$OurGenericsResolver.resolve(PsiJavaCodeReferenceElementImpl.java:325)
at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl$OurGenericsResolver.resolve(PsiJavaCodeReferenceElementImpl.java:317)
at com.intellij.psi.impl.source.resolve.ResolveCache$2.compute(ResolveCache.java:107)
at com.intellij.openapi.util.RecursionManager$2.doPreventingRecursion(RecursionManager.java:110)
at com.intellij.psi.impl.source.resolve.ResolveCache.resolve(ResolveCache.java:104)
at com.intellij.psi.impl.source.resolve.ResolveCache.resolveWithCaching(ResolveCache.java:132)
at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl.multiResolve(PsiJavaCodeReferenceElementImpl.java:371)
at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl.advancedResolve(PsiJavaCodeReferenceElementImpl.java:345)
at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl.resolve(PsiJavaCodeReferenceElementImpl.java:314)
at com.intellij.psi.impl.source.PsiImportStatementBaseImpl.resolve(PsiImportStatementBaseImpl.java:63)
at com.intellij.psi.impl.source.PsiJavaFileBaseImpl.processDeclarationsNoGuess(PsiJavaFileBaseImpl.java:314)
at com.intellij.psi.impl.source.PsiJavaFileBaseImpl.processDeclarations(PsiJavaFileBaseImpl.java:288)
at com.intellij.psi.scope.util.PsiScopesUtil.treeWalkUp(PsiScopesUtil.java:71)
at com.intellij.psi.scope.util.PsiScopesUtil.treeWalkUp(PsiScopesUtil.java:53)
at com.intellij.psi.scope.util.PsiScopesUtil.resolveAndWalk(PsiScopesUtil.java:219)
at com.intellij.psi.scope.util.PsiScopesUtil.resolveAndWalk(PsiScopesUtil.java:150)
at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl.resolve(PsiJavaCodeReferenceElementImpl.java:444)
at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl.access$000(PsiJavaCodeReferenceElementImpl.java:54)
at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl$OurGenericsResolver.resolve(PsiJavaCodeReferenceElementImpl.java:325)
at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl$OurGenericsResolver.resolve(PsiJavaCodeReferenceElementImpl.java:317)
at com.intellij.psi.impl.source.resolve.ResolveCache$2.compute(ResolveCache.java:107)
at com.intellij.openapi.util.RecursionManager$2.doPreventingRecursion(RecursionManager.java:110)
at com.intellij.psi.impl.source.resolve.ResolveCache.resolve(ResolveCache.java:104)
at com.intellij.psi.impl.source.resolve.ResolveCache.resolveWithCaching(ResolveCache.java:132)
at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl.multiResolve(PsiJavaCodeReferenceElementImpl.java:371)
at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl.advancedResolve(PsiJavaCodeReferenceElementImpl.java:345)
at com.intellij.psi.impl.source.PsiClassReferenceType.resolveGenerics(PsiClassReferenceType.java:141)
at com.intellij.psi.impl.source.PsiClassReferenceType.resolve(PsiClassReferenceType.java:90)
Are you sure that this exception comes from the resolve() method?
I tried your example and for me it works.
hello,
any luck with pinpointing the cause and finding a workaround ?
I'm facing a similar issue (resolve() used from within VirtualFileListener - after a file change has been detected) and unfortunatelly the only advice I got from JB people is to use MergingUpdateQueue or Alarm to postpone an update of my data object structure.
But I have no idea how long should I wait for the stub index to be updated and accessible and if I'm correct - the time constraints may vary between different projects (bigger ones may require more time for an update..)