Skip to content

Commit f1e53d2

Browse files
feature(LambdaBlockToExpression): convert lambda with method invocation as well (#241)
* add 2 test cases for method invocation * add transform for lambdas with method invocation * remove test with external dependency the other test covers the expected behavior sufficiently Co-authored-by: Tim te Beek <[email protected]> * fix other test * Retain original prefix to minimize diff * Extract statement to local variable --------- Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: Tim te Beek <[email protected]>
1 parent 4de0242 commit f1e53d2

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

Diff for: src/main/java/org/openrewrite/staticanalysis/LambdaBlockToExpression.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,22 @@ public J.Lambda visitLambda(J.Lambda lambda, ExecutionContext ctx) {
4646
J.Lambda l = super.visitLambda(lambda, ctx);
4747
if (lambda.getBody() instanceof J.Block) {
4848
List<Statement> statements = ((J.Block) lambda.getBody()).getStatements();
49-
if (statements.size() == 1 && statements.get(0) instanceof J.Return) {
50-
Space prefix = statements.get(0).getPrefix();
51-
if (prefix.getComments().isEmpty()) {
52-
return l.withBody(((J.Return) statements.get(0)).getExpression());
53-
} else {
54-
return l.withBody(((J.Return) statements.get(0)).getExpression().withPrefix(prefix));
49+
if (statements.size() == 1) {
50+
Statement statement = statements.get(0);
51+
Space prefix = statement.getPrefix();
52+
if (statement instanceof J.Return) {
53+
Expression expression = ((J.Return) statement).getExpression();
54+
if (prefix.getComments().isEmpty()) {
55+
return l.withBody(expression);
56+
} else {
57+
return l.withBody(expression.withPrefix(prefix));
58+
}
59+
} else if (statement instanceof J.MethodInvocation) {
60+
if (prefix.getComments().isEmpty()) {
61+
return l.withBody(statement);
62+
} else {
63+
return l.withBody(statement.withPrefix(prefix));
64+
}
5565
}
5666
}
5767
}

Diff for: src/test/java/org/openrewrite/staticanalysis/LambdaBlockToExpressionTest.java

+31
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,35 @@ void doTest() {
114114
);
115115
}
116116

117+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/236")
118+
@Test
119+
void simplifyLambdaBlockReturningVoidAsWell2() {
120+
//language=java
121+
rewriteRun(
122+
java(
123+
"""
124+
public class Main {
125+
126+
public void run() {
127+
Runnable runHelloWorld = () -> {
128+
System.out.println("Hello world!");
129+
};
130+
runHelloWorld.run();
131+
}
132+
}
133+
""",
134+
"""
135+
public class Main {
136+
137+
public void run() {
138+
Runnable runHelloWorld = () ->
139+
System.out.println("Hello world!");
140+
runHelloWorld.run();
141+
}
142+
}
143+
"""
144+
)
145+
);
146+
}
147+
117148
}

Diff for: src/test/java/org/openrewrite/staticanalysis/UseLambdaForFunctionalInterfaceTest.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,9 @@ class Test {
299299
void bar(Consumer<Integer> c) {
300300
}
301301
void foo() {
302-
bar(i -> {
303-
bar(i2 -> {
304-
});
305-
});
302+
bar(i ->
303+
bar(i2 -> {
304+
}));
306305
}
307306
}
308307
"""

0 commit comments

Comments
 (0)