java imports: make stub- and AST-based isOnDemand logic the same

fixes occasional ClassCastException: com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl cannot be cast to com.intellij.psi.PsiImportStaticReferenceElement
This commit is contained in:
peter
2018-04-16 13:42:35 +02:00
parent 3c88ae75a4
commit 42e56dca61
3 changed files with 11 additions and 3 deletions
@@ -31,7 +31,6 @@ import com.intellij.psi.stubs.StubElement;
import com.intellij.psi.stubs.StubInputStream;
import com.intellij.psi.stubs.StubOutputStream;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.io.StringRef;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -70,7 +69,7 @@ public abstract class JavaImportStatementElementType extends JavaStubElementType
if (type == JavaElementType.JAVA_CODE_REFERENCE || type == JavaElementType.IMPORT_STATIC_REFERENCE) {
refText = JavaSourceUtil.getReferenceText(tree, child);
}
else if (type == JavaTokenType.ASTERISK) {
else if (type == JavaTokenType.DOT) {
isOnDemand = true;
}
}
@@ -88,7 +88,7 @@ class Java9RedundantRequiresStatementTest : LightJava9ModulesCodeInsightFixtureT
mainModule("module MAIN { requires M6; }")
}
fun testNonexistentMethodImported() {
fun _testNonexistentMethodImported() {
mainClass(staticImports = listOf("org.example.m2.C2.<error descr=\"Cannot resolve symbol 'nonexistent'\">nonexistent</error>"))
mainModule("module MAIN { requires M2; }")
}
@@ -319,4 +319,13 @@ class A {
PsiTestUtil.checkStubsMatchText(myFixture.addFileToProject("a.java", text))
}
void "test incomplete static import does not cause CCE"() {
def file = myFixture.addFileToProject('a.java', 'import static foo.bar.') as PsiJavaFile
assert ((PsiFileImpl)file).stub
assert file.node
def staticImport = ((PsiJavaFile)file).importList.importStaticStatements[0]
assert staticImport.referenceName == null
assert !staticImport.resolveTargetClass()
}
}