2626import org .openrewrite .java .tree .J ;
2727import org .openrewrite .java .tree .JRightPadded ;
2828import org .openrewrite .java .tree .Space ;
29+ import org .openrewrite .java .tree .TypeUtils ;
2930import org .openrewrite .marker .Markers ;
3031
3132import static java .util .Collections .emptyList ;
@@ -41,7 +42,8 @@ public String getDisplayName() {
4142
4243 @ Override
4344 public String getDescription () {
44- return "Print statements are often left accidentally after debugging an issue." ;
45+ return "Print statements are often left accidentally after debugging an issue. " +
46+ "This recipe removes all `System.out#println` and `System.err#println` statements from the code." ;
4547 }
4648
4749 @ Override
@@ -60,8 +62,11 @@ public J.Lambda visitLambda(J.Lambda lambda, ExecutionContext ctx) {
6062
6163 @ Override
6264 @ SuppressWarnings ("NullableProblems" )
63- public J .@ Nullable MethodInvocation visitMethodInvocation (J .MethodInvocation method , ExecutionContext ctx ) {
64- if (SYSTEM_OUT_PRINTLN .matches (method )) {
65+ public J .@ Nullable MethodInvocation visitMethodInvocation (J .MethodInvocation method , ExecutionContext ctx ) {
66+ if (SYSTEM_OUT_PRINTLN .matches (method ) &&
67+ method .getSelect () instanceof J .FieldAccess &&
68+ (((J .FieldAccess ) method .getSelect ()).getTarget () instanceof J .Identifier &&
69+ TypeUtils .isAssignableTo ("java.lang.System" , ((J .FieldAccess ) method .getSelect ()).getTarget ().getType ()))) {
6570 return null ;
6671 }
6772 return super .visitMethodInvocation (method , ctx );
0 commit comments