javac indices: reorder anonymous indexing to match with intellij idea stub indices

This commit is contained in:
Dmitry Batkovich
2018-04-03 18:57:21 +03:00
parent 14525d5ca1
commit 99bd0d5cb3
13 changed files with 171 additions and 34 deletions
@@ -0,0 +1,19 @@
class Test {
void m() {
new Foo1(new Foo2() {
void m() {
new Foo3() {};
}
}) {
void m1() {
new Foo4() {};
}
void m2() {
new Foo5(new Foo6() {}) {};
}
};
}
}
@@ -0,0 +1,3 @@
public class Foo1 {
Foo1(Foo2 foo2) {}
}
@@ -0,0 +1,2 @@
public class Foo2 {
}
@@ -0,0 +1,2 @@
public class Foo3 {
}
@@ -0,0 +1,2 @@
public class Foo4 {
}
@@ -0,0 +1,3 @@
public class Foo5 {
Foo5(Foo6 foo6) {}
}
@@ -0,0 +1,2 @@
public class Foo6 {
}
@@ -1,6 +1,4 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.compiler;
import com.intellij.JavaTestUtil;
@@ -9,6 +7,7 @@ import com.intellij.compiler.CompilerReferenceService;
import com.intellij.compiler.backwardRefs.CompilerReferenceServiceImpl;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
@@ -17,12 +16,14 @@ import com.intellij.psi.util.PsiUtil;
import com.intellij.testFramework.SkipSlowTestLocally;
import com.intellij.testFramework.builders.JavaModuleFixtureBuilder;
import com.intellij.util.containers.ContainerUtil;
import one.util.streamex.MoreCollectors;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@SkipSlowTestLocally
public class CompilerReferencesTest extends CompilerReferencesTestBase {
@@ -110,6 +111,37 @@ public class CompilerReferencesTest extends CompilerReferencesTestBase {
assertEquals("Foo.ListImpl", inheritor.getQualifiedName());
}
public void testNestedAnonymousInheritors() {
myFixture.configureByFiles(getName() + "/Anonymouses.java",
getName() + "/Foo1.java",
getName() + "/Foo2.java",
getName() + "/Foo3.java",
getName() + "/Foo4.java",
getName() + "/Foo5.java",
getName() + "/Foo6.java");
rebuildProject();
PsiClass[] classes = IntStream
.range(1, 7)
.mapToObj(idx -> "Foo" + idx)
.map(className -> myFixture.findClass(className))
.filter(Objects::nonNull)
.toArray(PsiClass[]::new);
assertSize(6, classes);
for (PsiClass aClass : classes) {
PsiClass inheritor = getDirectInheritorsFor(aClass)
.getHierarchyChildren()
.map(PsiClass.class::cast)
.collect(MoreCollectors.onlyOne())
.orElse(null);
PsiAnonymousClass anonymousInheritor = assertInstanceOf(inheritor, PsiAnonymousClass.class);
PsiClass superFromReference = PsiUtil.resolveClassInType(anonymousInheritor.getBaseClassType());
assertEquals(superFromReference, aClass);
}
}
public void testExtensionRename() {
VirtualFile file = myFixture.configureByFiles(getName() + "/Bar.java", getName() + "/Foo.txt")[1].getVirtualFile();
rebuildProject();
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.jps.javac.ast;
import com.intellij.util.containers.Stack;
@@ -68,8 +54,21 @@ class JavacTreeRefScanner extends TreeScanner<Tree, JavacReferenceCollectorListe
if (element != null) {
collector.sinkReference(collector.asJavacRef(element));
}
return super.visitNewClass(node, collector);
} else {
//anonymous class
myCurrentAnonymousTree.add(node);
try {
scan(node.getEnclosingExpression(), collector);
scan(node.getIdentifier(), collector);
scan(node.getTypeArguments(), collector);
//scan(node.getArguments(), collector);
scan(node.getClassBody(), collector);
return null;
} finally {
myCurrentAnonymousTree.pop();
}
}
return super.visitNewClass(node, collector);
}
@Override
@@ -165,6 +164,7 @@ class JavacTreeRefScanner extends TreeScanner<Tree, JavacReferenceCollectorListe
private final Stack<TypeElement> myCurrentEnclosingElement = new Stack<TypeElement>(1);
private final Stack<Long> myCurrentEnclosingElementOffset = new Stack<Long>(1);
private final Stack<NewClassTree> myCurrentAnonymousTree = new Stack<NewClassTree>(1);
@Override
public Tree visitClass(ClassTree node, JavacReferenceCollectorListener.ReferenceCollector refCollector) {
@@ -199,6 +199,9 @@ class JavacTreeRefScanner extends TreeScanner<Tree, JavacReferenceCollectorListe
if (aClass == null) return null;
refCollector.sinkReference(aClass);
refCollector.sinkDeclaration(new JavacDef.JavacClassDef(aClass, supers));
if (((JavacRef.JavacClass)aClass).isAnonymous()) {
scan(myCurrentAnonymousTree.peek().getArguments(), refCollector);
}
super.visitClass(node, refCollector);
} finally {
myCurrentEnclosingElement.pop();
@@ -0,0 +1,19 @@
class Test {
void m() {
new Foo1(new Foo2() {
void m() {
new Foo3() {};
}
}) {
void m1() {
new Foo4() {};
}
void m2() {
new Foo5(new Foo6() {}) {};
}
};
}
}
@@ -0,0 +1,19 @@
class Foo1 {
Foo1(Foo2 foo2) {}
}
class Foo2 {
}
class Foo3 {
}
class Foo4 {
}
class Foo5 {
Foo5(Foo6 foo6) {}
}
class Foo6 {
}
@@ -0,0 +1,41 @@
Backward Hierarchy:
Foo1 -> anonymous(id=0)
Foo2 -> anonymous(id=1)
Foo3 -> anonymous(id=2)
Foo4 -> anonymous(id=3)
Foo5 -> anonymous(id=4)
Foo6 -> anonymous(id=5)
java.lang.Object -> Foo1 Foo2 Foo3 Foo4 Foo5 Foo6 Test
Backward References:
Foo1 in Anonymouses Classes occurrences = 3
Foo1.<init>(1) in Classes occurrences = 1
Foo1.m1(0) in Anonymouses occurrences = 1
Foo1.m2(0) in Anonymouses occurrences = 1
Foo2 in Anonymouses Classes occurrences = 4
Foo2.m(0) in Anonymouses occurrences = 1
Foo3 in Anonymouses Classes occurrences = 3
Foo4 in Anonymouses Classes occurrences = 3
Foo5 in Anonymouses Classes occurrences = 3
Foo5.<init>(1) in Classes occurrences = 1
Foo6 in Anonymouses Classes occurrences = 4
Test in Anonymouses occurrences = 1
Test.m(0) in Anonymouses occurrences = 1
java.lang.Object.<init>(0) in Classes occurrences = 2
Class Definitions:
Foo1 in Classes
Foo2 in Classes
Foo3 in Classes
Foo4 in Classes
Foo5 in Classes
Foo6 in Classes
Test in Anonymouses
anonymous(id=0) in Anonymouses
anonymous(id=1) in Anonymouses
anonymous(id=2) in Anonymouses
anonymous(id=3) in Anonymouses
anonymous(id=4) in Anonymouses
anonymous(id=5) in Anonymouses
Members Signatures:
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.references
import com.intellij.openapi.application.ex.PathManagerEx
@@ -193,5 +179,9 @@ class ReferenceIndexTest : ReferenceIndexTestBase() {
fun testCastDataGenerics() {
assertIndexOnRebuild("Foo.java")
}
fun testNestedAnonymouses() {
assertIndexOnRebuild("Anonymouses.java", "Classes.java")
}
}