Skip to content

Commit 50bc3ca

Browse files
authored
Add method reference support to Call hierarchy callee tree (eclipse-jdt#2035)
- modify CallAnalyzerVisitor to handle MethodReference - add new test to CallHierarchyContentProviderTest and add support for this in CallHierarchyTestHelper - fixes eclipse-jdt#2029
1 parent 7920faf commit 50bc3ca

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/callhierarchy/CalleeAnalyzerVisitor.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2019 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -42,6 +42,7 @@
4242
import org.eclipse.jdt.core.dom.ITypeBinding;
4343
import org.eclipse.jdt.core.dom.MethodDeclaration;
4444
import org.eclipse.jdt.core.dom.MethodInvocation;
45+
import org.eclipse.jdt.core.dom.MethodReference;
4546
import org.eclipse.jdt.core.dom.Modifier;
4647
import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
4748
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
@@ -180,6 +181,27 @@ public boolean visit(MethodInvocation node) {
180181
return true;
181182
}
182183

184+
/**
185+
* Find all method invocations from the called method. Since we only traverse into
186+
* the AST on the wanted method declaration, this method should not hit on more
187+
* method invocations than those in the wanted method.
188+
*
189+
* @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.MethodInvocation)
190+
*/
191+
@Override
192+
public boolean visit(MethodReference node) {
193+
progressMonitorWorked(1);
194+
if (!isFurtherTraversalNecessary(node)) {
195+
return false;
196+
}
197+
198+
if (isNodeWithinMethod(node)) {
199+
addMethodCall(node.resolveMethodBinding(), node);
200+
}
201+
202+
return true;
203+
}
204+
183205
/**
184206
* Find invocations of the supertype's constructor from the called method
185207
* (=constructor). Since we only traverse into the AST on the wanted method

org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/callhierarchy/CallHierarchyContentProviderTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2020 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -274,6 +274,24 @@ public void testLambdaCallers() throws Exception {
274274
6, thirdLevelChildren.length, "Wrong number of third level children");
275275
}
276276

277+
/*
278+
* Tests getChildren and hasChildren on a callee tree with lambda method reference.
279+
*/
280+
@Test
281+
public void testLambdaCalleesWithMethodReference() throws Exception {
282+
helper.createClassWithLambdaCalls();
283+
284+
TreeRoot root= wrapCalleeRoot(helper.getMethod3());
285+
Object[] children= fProvider.getChildren(root);
286+
assertEquals(1, children.length, "Wrong number of children");
287+
helper.assertCalls(new IMember[] { helper.getMethod3()}, children);
288+
assertEquals(helper.getMethod3(), ((MethodWrapper) children[0]).getMember(), "Wrong method");
289+
assertTrue(fProvider.hasChildren(root), "root's hasChildren");
290+
291+
Object[] secondLevelChildren= fProvider.getChildren(children[0]);
292+
assertNotNull(helper.findMethodWrapper(helper.getMethod2(), secondLevelChildren), "Did not find transform call");
293+
}
294+
277295
private void assertCalleeMethodWrapperChildren(Object[] children) {
278296
for (Object child : children) {
279297
assertTrue(child.getClass().getName().endsWith(".CalleeMethodWrapper"), "Wrong class returned");

org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/callhierarchy/CallHierarchyTestHelper.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2021 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -300,23 +300,23 @@ public void createClassWithLambdaCalls() throws Exception {
300300
public class Snippet {
301301
static Function<? super String, ? extends String> mapper1 = y -> transform(y);
302302
Function<? super String, ? extends String> mapper2 = y -> transform(y);
303-
303+
304304
static {
305305
mapper1 = y -> transform(y);
306306
}
307-
307+
308308
public Snippet() {
309309
mapper2 = y -> transform(y);
310310
}
311-
311+
312312
public static void main(String[] args) {
313-
mapper1 = y -> transform(y);
313+
mapper1 = Snippet::transform;
314314
}
315-
315+
316316
Object[] funcCall() {
317-
return List.of("aaa").stream().map(y -> transform(y)).toArray();
317+
return List.of("aaa").stream().map(Snippet::transform).toArray();
318318
}
319-
319+
320320
static String transform(String s) {
321321
x();
322322
return s.toUpperCase();
@@ -332,8 +332,10 @@ static String x() {\
332332
cu.createImport("java.util.function.Function", fType1, null);
333333
fMethod1= fType1.getMethod("x", EMPTY);
334334
fMethod2= fType1.getMethod("transform", new String[] { "QString;" });
335+
fMethod3= fType1.getMethod("funcCall", EMPTY);
335336
Assertions.assertNotNull(fMethod1);
336337
Assertions.assertNotNull(fMethod2);
338+
Assertions.assertNotNull(fMethod3);
337339
assertBuildWithoutErrors(fPack1);
338340
}
339341

@@ -460,14 +462,14 @@ public void createCalleeClasses() throws Exception {
460462
public class P {
461463
private A handler;
462464
private Abs absHandler;
463-
465+
464466
public void callFoo() {
465467
handler.foo();
466468
}
467469
public void callAbsFoo() {
468470
absHandler.absFoo();
469471
}
470-
472+
471473
}""",
472474
null,
473475
true,

0 commit comments

Comments
 (0)