Skip to content

Commit 953d0f3

Browse files
authored
Allow an empty method to be inlined in a lambda expression body (eclipse-jdt#2121)
- modify SourceProvider.isSimpleFunction() to allow an empty method - fix CallInliner.replaceCall() to handle an empty method being inlined into a lambda body - add new test to InlineMethodTests - fixes eclipse-jdt#2111
1 parent e0da91c commit 953d0f3

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2024 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
@@ -70,6 +70,7 @@
7070
import org.eclipse.jdt.core.dom.IVariableBinding;
7171
import org.eclipse.jdt.core.dom.IfStatement;
7272
import org.eclipse.jdt.core.dom.LabeledStatement;
73+
import org.eclipse.jdt.core.dom.LambdaExpression;
7374
import org.eclipse.jdt.core.dom.MethodDeclaration;
7475
import org.eclipse.jdt.core.dom.MethodInvocation;
7576
import org.eclipse.jdt.core.dom.Modifier;
@@ -656,7 +657,12 @@ private void replaceCall(RefactoringStatus status, String[] blocks, TextEditGrou
656657
if (fNeedsStatement) {
657658
fRewrite.replace(fTargetNode, fTargetNode.getAST().newEmptyStatement(), textEditGroup);
658659
} else {
659-
fRewrite.remove(fTargetNode, textEditGroup);
660+
if (fTargetNode.getLocationInParent() == LambdaExpression.BODY_PROPERTY) {
661+
ASTNode newNode= fRewrite.createStringPlaceholder("{}", ASTNode.BLOCK); //$NON-NLS-1$
662+
fRewrite.replace(fTargetNode, newNode, textEditGroup);
663+
} else {
664+
fRewrite.remove(fTargetNode, textEditGroup);
665+
}
660666
}
661667
} else {
662668
ASTNode node= null;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,11 @@ public int getNumberOfStatements() {
256256

257257
public boolean isSimpleFunction() {
258258
List<Statement> statements= fDeclaration.getBody().statements();
259-
if (statements.size() != 1)
259+
if (statements.size() > 1)
260260
return false;
261-
return statements.get(0) instanceof ReturnStatement;
261+
if (statements.size() == 1)
262+
return statements.get(0) instanceof ReturnStatement;
263+
return true;
262264
}
263265

264266
public boolean isLastStatementReturn() {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package bugs_in;
2+
3+
public class Test_issue_2111_1 {
4+
protected void a() {
5+
new Thread(() -> /*]*/b()/*[*/);
6+
}
7+
8+
private void b() {}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package bugs_in;
2+
3+
public class Test_issue_2111_1 {
4+
protected void a() {
5+
new Thread(() -> /*]*/{}/*[*/);
6+
}
7+
8+
private void b() {}
9+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,11 @@ public void test_issue_1856_1() throws Exception {
502502
performBugTest();
503503
}
504504

505+
@Test
506+
public void test_issue_2111_1() throws Exception {
507+
performBugTest();
508+
}
509+
505510
/* *********************** Argument Tests ******************************* */
506511

507512
private void performArgumentTest() throws Exception {

0 commit comments

Comments
 (0)