Skip to content

Commit

Permalink
Fix convert to lambda for one statement that has non-NLS marker (#2000)
Browse files Browse the repository at this point in the history
- modify LambdaExpressionsFixCore to not convert a single
  expression statement into a lambda specifying expression if the
  statement has a line comment such as a non-NLS marker
- add new test to CleanUpTest1d8
- fixes #1999
  • Loading branch information
jjohnstn authored Feb 6, 2025
1 parent fbdc89d commit 0aacc6d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -717,12 +717,19 @@ public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore
if (statements.size() == 1) {
// use short form with just an expression body if possible
Statement statement= statements.get(0);
if (statement instanceof ExpressionStatement) {
lambdaBody= ((ExpressionStatement) statement).getExpression();
} else if (statement instanceof ReturnStatement) {
Expression returnExpression= ((ReturnStatement) statement).getExpression();
if (returnExpression != null) {
lambdaBody= returnExpression;
CompilationUnit root= cuRewrite.getRoot();
int extendedStart= root.getExtendedStartPosition(statement);
int extendedLength= root.getExtendedLength(statement);
// verify no line comment exists at the end of the node in which case
// we can use short form - otherwise use original block with comments
if (extendedStart + extendedLength <= statement.getStartPosition() + statement.getLength()) {
if (statement instanceof ExpressionStatement) {
lambdaBody= ((ExpressionStatement) statement).getExpression();
} else if (statement instanceof ReturnStatement) {
Expression returnExpression= ((ReturnStatement) statement).getExpression();
if (returnExpression != null) {
lambdaBody= returnExpression;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7053,4 +7053,52 @@ private void assertIteratorProducesExpectedList(
assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected }, null);

}

@Test
public void testConvertToLambdaIssue1999() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
String original= """
package test1;
import java.util.Iterator;
import java.util.List;
public class E {
void setRunnable(Runnable r) {
}
void test1() {
setRunnable(new Runnable() {
@Override
public void run() {
System.out.println("abc"); //$NON-NLS-1$
}
});
}
}
""";
ICompilationUnit cu1= pack1.createCompilationUnit("E.java", original, false, null);

enable(CleanUpConstants.CONVERT_FUNCTIONAL_INTERFACES);
enable(CleanUpConstants.USE_LAMBDA);

String expected= """
package test1;
import java.util.Iterator;
import java.util.List;
public class E {
void setRunnable(Runnable r) {
}
void test1() {
setRunnable(() -> {
System.out.println("abc"); //$NON-NLS-1$
});
}
}
""";

assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected }, null);

}
}

0 comments on commit 0aacc6d

Please sign in to comment.