Skip to content

Commit 86514e9

Browse files
authored
Fix inlining to support method references (eclipse-jdt#2122)
- fix Invocations to handle method references for various methods (e.g. method binding) - add method reference support to CallInliner.replaceCall() - add method reference support to TargetProvider.InvocationFinder - add new tests to InlineMethodTests - fixes eclipse-jdt#2118
1 parent 953d0f3 commit 86514e9

File tree

8 files changed

+115
-3
lines changed

8 files changed

+115
-3
lines changed

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/CallInliner.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import org.eclipse.jdt.core.dom.LambdaExpression;
7474
import org.eclipse.jdt.core.dom.MethodDeclaration;
7575
import org.eclipse.jdt.core.dom.MethodInvocation;
76+
import org.eclipse.jdt.core.dom.MethodReference;
7677
import org.eclipse.jdt.core.dom.Modifier;
7778
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
7879
import org.eclipse.jdt.core.dom.Name;
@@ -660,6 +661,20 @@ private void replaceCall(RefactoringStatus status, String[] blocks, TextEditGrou
660661
if (fTargetNode.getLocationInParent() == LambdaExpression.BODY_PROPERTY) {
661662
ASTNode newNode= fRewrite.createStringPlaceholder("{}", ASTNode.BLOCK); //$NON-NLS-1$
662663
fRewrite.replace(fTargetNode, newNode, textEditGroup);
664+
} else if (fTargetNode instanceof MethodReference methodRef) {
665+
IMethodBinding binding= methodRef.resolveMethodBinding();
666+
if (binding != null) {
667+
StringBuilder builder= new StringBuilder("("); //$NON-NLS-1$
668+
String[] parmNames= binding.getParameterNames();
669+
String separator= ""; //$NON-NLS-1$
670+
for (String parmName : parmNames) {
671+
builder.append(separator + parmName);
672+
separator= ", "; //$NON-NLS-1$
673+
}
674+
builder.append(") -> {}"); //$NON-NLS-1$
675+
ASTNode newNode= fRewrite.createStringPlaceholder(builder.toString(), ASTNode.LAMBDA_EXPRESSION);
676+
fRewrite.replace(fTargetNode, newNode, textEditGroup);
677+
}
663678
} else {
664679
fRewrite.remove(fTargetNode, textEditGroup);
665680
}

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/Invocations.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2011 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
@@ -16,6 +16,7 @@
1616
*******************************************************************************/
1717
package org.eclipse.jdt.internal.corext.refactoring.code;
1818

19+
import java.util.Collections;
1920
import java.util.List;
2021

2122
import org.eclipse.jdt.core.dom.ASTNode;
@@ -27,6 +28,7 @@
2728
import org.eclipse.jdt.core.dom.IMethodBinding;
2829
import org.eclipse.jdt.core.dom.ITypeBinding;
2930
import org.eclipse.jdt.core.dom.MethodInvocation;
31+
import org.eclipse.jdt.core.dom.MethodReference;
3032
import org.eclipse.jdt.core.dom.ParameterizedType;
3133
import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
3234
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
@@ -52,7 +54,11 @@ public static List<Expression> getArguments(ASTNode invocation) {
5254
return ((ClassInstanceCreation)invocation).arguments();
5355
case ASTNode.ENUM_CONSTANT_DECLARATION:
5456
return ((EnumConstantDeclaration)invocation).arguments();
55-
57+
case ASTNode.EXPRESSION_METHOD_REFERENCE:
58+
case ASTNode.TYPE_METHOD_REFERENCE:
59+
case ASTNode.SUPER_METHOD_REFERENCE:
60+
case ASTNode.CREATION_REFERENCE:
61+
return Collections.EMPTY_LIST;
5662
default:
5763
throw new IllegalArgumentException(invocation.toString());
5864
}
@@ -97,6 +103,12 @@ public static Expression getExpression(ASTNode invocation) {
97103
case ASTNode.ENUM_CONSTANT_DECLARATION:
98104
return null;
99105

106+
case ASTNode.CREATION_REFERENCE:
107+
case ASTNode.EXPRESSION_METHOD_REFERENCE:
108+
case ASTNode.SUPER_METHOD_REFERENCE:
109+
case ASTNode.TYPE_METHOD_REFERENCE:
110+
return null;
111+
100112
default:
101113
throw new IllegalArgumentException(invocation.toString());
102114
}
@@ -142,6 +154,12 @@ public static IMethodBinding resolveBinding(ASTNode invocation) {
142154
case ASTNode.ENUM_CONSTANT_DECLARATION:
143155
return ((EnumConstantDeclaration)invocation).resolveConstructorBinding();
144156

157+
case ASTNode.CREATION_REFERENCE:
158+
case ASTNode.EXPRESSION_METHOD_REFERENCE:
159+
case ASTNode.SUPER_METHOD_REFERENCE:
160+
case ASTNode.TYPE_METHOD_REFERENCE:
161+
return ((MethodReference)invocation).resolveMethodBinding();
162+
145163
default:
146164
throw new IllegalArgumentException(invocation.toString());
147165
}

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/TargetProvider.java

Lines changed: 28 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
@@ -44,17 +44,22 @@
4444
import org.eclipse.jdt.core.dom.BodyDeclaration;
4545
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
4646
import org.eclipse.jdt.core.dom.ConstructorInvocation;
47+
import org.eclipse.jdt.core.dom.CreationReference;
4748
import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
4849
import org.eclipse.jdt.core.dom.EnumDeclaration;
50+
import org.eclipse.jdt.core.dom.ExpressionMethodReference;
4951
import org.eclipse.jdt.core.dom.FieldDeclaration;
5052
import org.eclipse.jdt.core.dom.IBinding;
5153
import org.eclipse.jdt.core.dom.IMethodBinding;
5254
import org.eclipse.jdt.core.dom.ITypeBinding;
5355
import org.eclipse.jdt.core.dom.Initializer;
5456
import org.eclipse.jdt.core.dom.MethodDeclaration;
5557
import org.eclipse.jdt.core.dom.MethodInvocation;
58+
import org.eclipse.jdt.core.dom.MethodReference;
5659
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
60+
import org.eclipse.jdt.core.dom.SuperMethodReference;
5761
import org.eclipse.jdt.core.dom.TypeDeclaration;
62+
import org.eclipse.jdt.core.dom.TypeMethodReference;
5863
import org.eclipse.jdt.core.manipulation.SharedASTProviderCore;
5964
import org.eclipse.jdt.core.search.IJavaSearchConstants;
6065
import org.eclipse.jdt.core.search.IJavaSearchScope;
@@ -266,6 +271,28 @@ public boolean visit(MethodInvocation node) {
266271
return true;
267272
}
268273
@Override
274+
public boolean visit(ExpressionMethodReference node) {
275+
return handle(node);
276+
}
277+
@Override
278+
public boolean visit(CreationReference node) {
279+
return handle(node);
280+
}
281+
@Override
282+
public boolean visit(SuperMethodReference node) {
283+
return handle(node);
284+
}
285+
@Override
286+
public boolean visit(TypeMethodReference node) {
287+
return handle(node);
288+
}
289+
private boolean handle(MethodReference node) {
290+
if (node.resolveTypeBinding() != null && matches(node.resolveMethodBinding()) && fCurrent != null) {
291+
fCurrent.addInvocation(node);
292+
}
293+
return true;
294+
}
295+
@Override
269296
public boolean visit(SuperMethodInvocation node) {
270297
if (matches(node.getName().resolveBinding()) && fCurrent != null) {
271298
fCurrent.addInvocation(node);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package bugs_in;
2+
3+
public class Test_issue_2111_1 {
4+
public interface A {
5+
void doSomething(int a, int b);
6+
}
7+
8+
protected void foo() {
9+
A t = this::b;
10+
}
11+
12+
public void /*]*/b/*[*/(int x, int y) {
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package bugs_in;
2+
3+
public class Test_issue_2111_2 {
4+
protected void foo() {
5+
Runnable t = this::b;
6+
}
7+
8+
public void /*]*/b/*[*/() {
9+
}
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package bugs_in;
2+
3+
public class Test_issue_2111_1 {
4+
public interface A {
5+
void doSomething(int a, int b);
6+
}
7+
8+
protected void foo() {
9+
A t = (x, y) -> {};
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package bugs_in;
2+
3+
public class Test_issue_2111_2 {
4+
protected void foo() {
5+
Runnable t = () -> {};
6+
}
7+
}

org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineMethodTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,16 @@ public void test_issue_2111_1() throws Exception {
507507
performBugTest();
508508
}
509509

510+
@Test
511+
public void test_issue_2118_1() throws Exception {
512+
performBugTest();
513+
}
514+
515+
@Test
516+
public void test_issue_2118_2() throws Exception {
517+
performBugTest();
518+
}
519+
510520
/* *********************** Argument Tests ******************************* */
511521

512522
private void performArgumentTest() throws Exception {

0 commit comments

Comments
 (0)