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;
}
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");
}
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));
}
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;
}
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";
}
Aggregations