1515 */
1616package org .openrewrite .java .logging .log4j ;
1717
18- import static java .util .Objects .requireNonNull ;
19- import static org .openrewrite .Tree .randomId ;
20-
21- import com .fasterxml .jackson .databind .annotation .JsonPOJOBuilder ;
22- import java .util .ArrayList ;
23- import java .util .List ;
2418import lombok .EqualsAndHashCode ;
2519import lombok .Value ;
26- import org .openrewrite .*;
27- import org .openrewrite .internal .lang .Nullable ;
20+ import org .openrewrite .ExecutionContext ;
21+ import org .openrewrite .Preconditions ;
22+ import org .openrewrite .Recipe ;
23+ import org .openrewrite .TreeVisitor ;
2824import org .openrewrite .java .JavaIsoVisitor ;
29- import org .openrewrite .java .JavaVisitor ;
3025import org .openrewrite .java .MethodMatcher ;
3126import org .openrewrite .java .search .UsesMethod ;
32- import org .openrewrite .java .tree .*;
33- import org .openrewrite .java .tree .J .Identifier ;
27+ import org .openrewrite .java .tree .Expression ;
28+ import org .openrewrite .java .tree .J ;
29+ import org .openrewrite .java .tree .JavaType ;
3430import org .openrewrite .java .tree .JavaType .Method ;
3531import org .openrewrite .java .tree .JavaType .Primitive ;
32+ import org .openrewrite .java .tree .Space ;
3633import org .openrewrite .marker .Markers ;
3734
35+ import java .util .ArrayList ;
36+ import java .util .List ;
37+
38+ import static org .openrewrite .Tree .randomId ;
39+
3840/**
3941 * This recipe rewrites JUL's {@link java.util.logging.Logger#entering} method.
4042 */
4143@ Value
4244@ EqualsAndHashCode (callSuper = true )
4345public class ConvertJulEntering extends Recipe {
4446
45- private static final String METHOD_PATTERN = "java.util.logging.Logger entering(..)" ;
46-
47- @ JsonPOJOBuilder (withPrefix = "" )
48- public static class Builder {
49- }
47+ private static final MethodMatcher METHOD_MATCHER = new MethodMatcher ("java.util.logging.Logger entering(String, String, ..)" , false );
5048
5149 @ Override
5250 public String getDisplayName () {
@@ -60,60 +58,39 @@ public String getDescription() {
6058
6159 @ Override
6260 public TreeVisitor <?, ExecutionContext > getVisitor () {
63- return Preconditions .check (new JavaVisitor <ExecutionContext >() {
64- @ Override
65- public J visit (@ Nullable Tree tree , ExecutionContext ctx ) {
66- if (tree instanceof JavaSourceFile ) {
67- JavaSourceFile cu = (JavaSourceFile ) requireNonNull (tree );
68- return new UsesMethod <>(METHOD_PATTERN ).visitNonNull (cu , ctx );
69- }
70- return super .visit (tree , ctx );
71- }
72- }, new EnteringMethodVisitor (new MethodMatcher (METHOD_PATTERN , false )));
73- }
74-
75- private class EnteringMethodVisitor extends JavaIsoVisitor <ExecutionContext > {
76- private final MethodMatcher methodMatcher ;
77-
78- private EnteringMethodVisitor (MethodMatcher methodMatcher ) {
79- this .methodMatcher = methodMatcher ;
80- }
61+ return Preconditions .check (new UsesMethod <>(METHOD_MATCHER ), new JavaIsoVisitor <ExecutionContext >() {
62+ @ Override
63+ public J .MethodInvocation visitMethodInvocation (J .MethodInvocation method , ExecutionContext ctx ) {
64+ J .MethodInvocation m = super .visitMethodInvocation (method , ctx );
65+ if (METHOD_MATCHER .matches (m )) {
66+ List <Expression > originalArgs = m .getArguments ();
67+ int originalArgCount = originalArgs .size ();
68+ if (3 < originalArgCount ) {
69+ return m ;
70+ }
71+ List <Expression > modifiedArgs = new ArrayList <>();
72+ List <JavaType > modifiedTypes = new ArrayList <>();
73+ modifiedArgs .add (buildNullString ());
74+ modifiedTypes .add (Primitive .String );
75+ if (originalArgCount > 2 ) {
76+ JavaType varargType = JavaType .buildType ("java.lang.Object[]" );
77+ modifiedArgs .add (originalArgs .get (2 ));
78+ modifiedTypes .add (varargType );
79+ }
80+ Method mt = m .getMethodType ().withParameterTypes (modifiedTypes );
81+ JavaType .FullyQualified dt = mt .getDeclaringType ().withFullyQualifiedName ("org.apache.logging.log4j.Logger" );
82+ return m .withMethodType (mt )
83+ .withName (m .getName ().withSimpleName ("traceEntry" ))
84+ .withArguments (modifiedArgs )
85+ .withDeclaringType (dt );
86+ }
87+ return m ;
88+ }
8189
82- @ Override
83- public J .MethodInvocation visitMethodInvocation (J .MethodInvocation method , ExecutionContext ctx ) {
84- return (J .MethodInvocation ) visitMethodCall (super .visitMethodInvocation (method , ctx ));
85- }
86-
87- private J .MethodInvocation visitMethodCall (J .MethodInvocation m ) {
88- if (methodMatcher .matches (m ) && m .getMethodType () != null ) {
89- final List <Expression > originalArgs = m .getArguments ();
90- final int originalArgCount = originalArgs .size ();
91- if (originalArgCount < 2 || 3 < originalArgCount ) {
92- throw new IllegalArgumentException ("Unsupported Logger#entering method: " + m .getMethodType ());
93- }
94- final List <Expression > modifiedArgs = new ArrayList <>();
95- final List <JavaType > modifiedTypes = new ArrayList <>();
96- final J .Literal nullString = buildNullString ();
97- modifiedArgs .add (nullString );
98- modifiedTypes .add (Primitive .String );
99- if (originalArgCount > 2 ) {
100- final JavaType varargType = JavaType .buildType ("java.lang.Object[]" );
101- modifiedArgs .add (originalArgs .get (2 ));
102- modifiedTypes .add (varargType );
90+ private J .Literal buildNullString () {
91+ return new J .Literal (randomId (), Space .EMPTY , Markers .EMPTY , null , "null" , null , JavaType .Primitive .String );
92+ }
10393 }
104- final Identifier traceEntry = m .getName ().withSimpleName ("traceEntry" );
105- final Method mt = m .getMethodType ().withParameterTypes (modifiedTypes );
106- return m .withMethodType (mt )
107- .withName (traceEntry )
108- .withArguments (modifiedArgs )
109- .withDeclaringType (mt .getDeclaringType ()
110- .withFullyQualifiedName ("org.apache.logging.log4j.Logger" ));
111- }
112- return m ;
113- }
114- }
115-
116- private static J .Literal buildNullString () {
117- return new J .Literal (randomId (), Space .EMPTY , Markers .EMPTY , null , "null" , null , JavaType .Primitive .String );
94+ );
11895 }
11996}
0 commit comments