Skip to content

Commit

Permalink
Minor fixes to imports & method pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Jul 31, 2023
1 parent a0b25a3 commit 0bbe51c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ dependencies {
testImplementation("org.openrewrite:rewrite-gradle")
testImplementation("org.openrewrite:rewrite-maven")

implementation("com.squareup.okhttp3:okhttp:4.11.0")

testImplementation("org.junit.jupiter:junit-jupiter-engine:latest.release")

testImplementation("com.squareup.okhttp3:okhttp:4.11.0")
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.Expression;
Expand All @@ -41,21 +42,24 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
}

private static class ReorderArgumentsVisitor extends JavaIsoVisitor<ExecutionContext> {
private static MethodMatcher requestBodyCreateMatcher = new MethodMatcher("com.squareup.okhttp.RequestBody create(com.square.okhttp.RequestBody, java.lang.String)");
private static MethodMatcher requestBodyCreateMatcher = new MethodMatcher(
"okhttp3.RequestBody create(okhttp3.MediaType, java.lang.String)");
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation mi, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(mi, ctx);
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocation, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(methodInvocation, ctx);

if (!requestBodyCreateMatcher.matches(m)) {
return m;
if (!requestBodyCreateMatcher.matches(mi)) {
return mi;
}

Expression firstArg = m.getArguments().get(0);
Expression secondArg = m.getArguments().get(1);
// TODO Reuse ReorderMethodArguments instead
Expression firstArg = mi.getArguments().get(0);
Expression secondArg = mi.getArguments().get(1);

return JavaTemplate.builder("RequestBody.create(#{any()}, #{any()})")
.javaParser(JavaParser.fromJavaVersion().classpath("okhttp"))
.build()
.apply(getCursor(), m.getCoordinates().replace(), secondArg, firstArg);
.apply(getCursor(), mi.getCoordinates().replace(), secondArg, firstArg);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,46 @@
package org.openrewrite.okhttp;

import org.junit.jupiter.api.Test;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.testing.okhttp.ReorderRequestBodyCreateArguments;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

public class ReorderRequestBodyCreateArgumentsTest implements RewriteTest {
class ReorderRequestBodyCreateArgumentsTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(),
"squareup-okhttp"))
.parser(JavaParser.fromJavaVersion().classpath("okhttp"))
.recipe(new ReorderRequestBodyCreateArguments());
}

@Test
void reorderArguments() {
//language=java
rewriteRun(
java(
"""
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody;
class MyTest {
void testMethod() {
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType,"some request");
}
}
""",
"""
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody;
class MyTest {
void testMethod() {
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create("some request", mediaType);
}
}
"""
)
java("""
import okhttp3.MediaType;
import okhttp3.RequestBody;
class MyTest {
void testMethod() {
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "some request");
}
}
""", """
import okhttp3.MediaType;
import okhttp3.RequestBody;
class MyTest {
void testMethod() {
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create("some request", mediaType);
}
}
""")
);
}
}

0 comments on commit 0bbe51c

Please sign in to comment.