Skip to content

Commit c54f423

Browse files
committed
Fix RemoveSystemOutPrintln for lambdas
1 parent 61f8b01 commit c54f423

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

src/main/java/org/openrewrite/staticanalysis/RemoveSystemOutPrintln.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
1616
package org.openrewrite.staticanalysis;
1717

1818
import org.jspecify.annotations.Nullable;
19-
import org.openrewrite.ExecutionContext;
20-
import org.openrewrite.Preconditions;
21-
import org.openrewrite.Recipe;
22-
import org.openrewrite.TreeVisitor;
19+
import org.openrewrite.*;
2320
import org.openrewrite.java.JavaIsoVisitor;
2421
import org.openrewrite.java.MethodMatcher;
2522
import org.openrewrite.java.search.UsesMethod;
2623
import org.openrewrite.java.tree.J;
24+
import org.openrewrite.java.tree.JRightPadded;
25+
import org.openrewrite.java.tree.Space;
26+
import org.openrewrite.marker.Markers;
27+
28+
import java.util.Collections;
29+
30+
import static org.openrewrite.Tree.randomId;
2731

2832
public class RemoveSystemOutPrintln extends Recipe {
2933
private static final MethodMatcher SYSTEM_OUT_PRINTLN = new MethodMatcher("java.io.PrintStream println(..)");
@@ -43,9 +47,19 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
4347
return Preconditions.check(new UsesMethod<>(SYSTEM_OUT_PRINTLN), new JavaIsoVisitor<ExecutionContext>() {
4448

4549
@Override
50+
public J.Lambda visitLambda(J.Lambda lambda, ExecutionContext ctx) {
51+
J.Lambda l = super.visitLambda(lambda, ctx);
52+
//noinspection ConstantValue
53+
if (l.getBody() == null) {
54+
l = l.withBody(new J.Block(randomId(), lambda.getPrefix(), Markers.EMPTY, JRightPadded.build(false), Collections.emptyList(), Space.EMPTY));
55+
}
56+
return l;
57+
}
58+
59+
@Override
60+
@SuppressWarnings("NullableProblems")
4661
public J.@Nullable MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
4762
if (SYSTEM_OUT_PRINTLN.matches(method)) {
48-
//noinspection DataFlowIssue
4963
return null;
5064
}
5165
return super.visitMethodInvocation(method, ctx);

src/test/java/org/openrewrite/staticanalysis/RemoveSystemOutPrintlnTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void removePrintln() {
3838
"""
3939
class Test {
4040
void test() {
41-
System.out.println("Hello, world!");
41+
System.out.println("Hello, world!");
4242
}
4343
}
4444
""",
@@ -51,4 +51,31 @@ void test() {
5151
)
5252
);
5353
}
54+
55+
@Test
56+
void lambda() {
57+
rewriteRun(
58+
//language=java
59+
java(
60+
"""
61+
import java.util.function.Consumer;
62+
63+
class Test {
64+
void test() {
65+
Consumer<String> c = s -> System.out.println(s);
66+
}
67+
}
68+
""",
69+
"""
70+
import java.util.function.Consumer;
71+
72+
class Test {
73+
void test() {
74+
Consumer<String> c = s -> {};
75+
}
76+
}
77+
"""
78+
)
79+
);
80+
}
5481
}

0 commit comments

Comments
 (0)