Skip to content

Commit 09b6109

Browse files
committed
Fix method type following openrewrite/rewrite#4688
1 parent 1c195ec commit 09b6109

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/main/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCalls.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import org.openrewrite.java.JavaTemplate;
2424
import org.openrewrite.java.MethodMatcher;
2525
import 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

2931
public 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
}

src/test/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCallsTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ void someMethod() {
159159
}
160160

161161
@Test
162-
@Disabled("Yet to be implemented")
163162
void doesNotUseIsOkForHttpStatus300() {
164163
rewriteRun(
165164
//language=java
@@ -182,5 +181,4 @@ void someMethod() {
182181
)
183182
);
184183
}
185-
186184
}

0 commit comments

Comments
 (0)