2323import org .openrewrite .java .JavaTemplate ;
2424import org .openrewrite .java .MethodMatcher ;
2525import org .openrewrite .java .search .UsesMethod ;
26- import org .openrewrite .java .tree .J .Literal ;
27- import org .openrewrite .java .tree .J .MethodInvocation ;
26+ import org .openrewrite .java .tree .J ;
27+ import org .openrewrite .java .tree .JavaType ;
28+
29+ import static java .util .Collections .emptyList ;
2830
2931public class SimplifyWebTestClientCalls extends Recipe {
3032
@@ -44,10 +46,10 @@ public String getDescription() {
4446 public TreeVisitor <?, ExecutionContext > getVisitor () {
4547 return Preconditions .check (new UsesMethod <>(IS_EQUAL_TO_INT_MATCHER ), new JavaIsoVisitor <ExecutionContext >() {
4648 @ Override
47- public MethodInvocation visitMethodInvocation (MethodInvocation method , ExecutionContext ctx ) {
48- MethodInvocation m = super .visitMethodInvocation (method , ctx );
49+ public J . MethodInvocation visitMethodInvocation (J . MethodInvocation method , ExecutionContext ctx ) {
50+ J . MethodInvocation m = super .visitMethodInvocation (method , ctx );
4951 if (IS_EQUAL_TO_INT_MATCHER .matches (m .getMethodType ())) {
50- int statusCode = (int ) ((Literal ) m .getArguments ().get (0 )).getValue ();
52+ int statusCode = (int ) ((J . Literal ) m .getArguments ().get (0 )).getValue ();
5153 switch (statusCode ) {
5254 case 200 :
5355 return replaceMethod (m , "isOk()" );
@@ -80,9 +82,12 @@ public MethodInvocation visitMethodInvocation(MethodInvocation method, Execution
8082 return m ;
8183 }
8284
83- private MethodInvocation replaceMethod (MethodInvocation method , String methodName ) {
85+ private J . MethodInvocation replaceMethod (J . MethodInvocation method , String methodName ) {
8486 JavaTemplate template = JavaTemplate .builder (methodName ).build ();
85- return template .apply (getCursor (), method .getCoordinates ().replaceMethod ());
87+ J .MethodInvocation methodInvocation = template .apply (getCursor (), method .getCoordinates ().replaceMethod ());
88+ JavaType .Method type = methodInvocation .getMethodType ().withParameterNames (emptyList ()).withParameterTypes (emptyList ());
89+ return methodInvocation .withArguments (emptyList ()).withMethodType (type ).withName (methodInvocation .getName ().withType (type ));
90+
8691 }
8792 });
8893 }
0 commit comments