[for java caller hierarchy] when occurrence in anonymous class search for usages of containing method (IDEA-73312), also search occurrences past constructors of anonymous classes (IDEA-56615)

This commit is contained in:
Maxim.Mossienko
2014-11-20 14:13:02 +01:00
parent bf187f88c0
commit 2b8dd92eb5
6 changed files with 68 additions and 1 deletions

View File

@@ -46,8 +46,15 @@ public final class CallerMethodsTreeStructure extends HierarchyTreeStructure {
@NotNull
@Override
protected final Object[] buildChildren(@NotNull final HierarchyNodeDescriptor descriptor) {
final PsiMember enclosingElement = ((CallHierarchyNodeDescriptor)descriptor).getEnclosingElement();
PsiMember enclosingElement = ((CallHierarchyNodeDescriptor)descriptor).getEnclosingElement();
HierarchyNodeDescriptor nodeDescriptor = getBaseDescriptor();
PsiClass clazz;
if (isAnonymousClass(enclosingElement)) {
enclosingElement = CallHierarchyNodeDescriptor.getEnclosingElement(enclosingElement.getParent());
} else if (enclosingElement instanceof PsiMethod && isAnonymousClass(clazz = enclosingElement.getContainingClass())) {
enclosingElement = CallHierarchyNodeDescriptor.getEnclosingElement(clazz.getParent());
}
if (!(enclosingElement instanceof PsiMethod) || nodeDescriptor == null) {
return ArrayUtil.EMPTY_OBJECT_ARRAY;
}
@@ -80,6 +87,10 @@ public final class CallerMethodsTreeStructure extends HierarchyTreeStructure {
return methodToDescriptorMap.values().toArray(new Object[methodToDescriptorMap.size()]);
}
private static boolean isAnonymousClass(PsiMember enclosingElement) {
return enclosingElement instanceof PsiClass && ((PsiClass)enclosingElement).getQualifiedName() == null;
}
@Override
public boolean isAlwaysShowPlus() {
return true;

View File

@@ -0,0 +1,25 @@
interface Runnable {
void run() {}
}
class A {
void foo() {
action(new Runnable() {
public void run() {
doIt();
}
});
}
void doIt() {}
static void action(Runnable action) {
action.run();
}
}
class Bar extends Runnable {
Bar() {
run();
}
}

View File

@@ -0,0 +1,3 @@
<node text="A.doIt() ()" base="true">
<node text="Anonymous in foo() in A.run() ()"/>
</node>

View File

@@ -0,0 +1,15 @@
abstract class A {
A(int a) {}
abstract void g();
void h() {
f();
}
void f() {
A x = new A(42) {
void g() {}
};
}
}

View File

@@ -0,0 +1,5 @@
<node text="A.A(int) ()" base="true">
<node text="Anonymous in f() in A ()">
<node text="A.h() ()"/>
</node>
</node>

View File

@@ -57,6 +57,14 @@ public class JavaCallHierarchyTest extends HierarchyViewTestBase {
doJavaCallTypeHierarchyTest("A", "main", "B.java", "A.java");
}
public void testAnonymous() throws Exception {
doJavaCallTypeHierarchyTest("A", "A", "A.java");
}
public void testAnonymous2() throws Exception {
doJavaCallTypeHierarchyTest("A", "doIt", "A.java");
}
public void testActionAvailableInXml() throws Exception {
configureByText(XmlFileType.INSTANCE, "<foo>java.lang.Str<caret>ing</foo>");
BrowseTypeHierarchyAction action = new BrowseTypeHierarchyAction();