Skip to content

Commit

Permalink
refactor: OpenRewrite recipe best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
2 people authored and app committed Dec 8, 2023
1 parent f46c001 commit 67815f7
Show file tree
Hide file tree
Showing 20 changed files with 264 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
.build();

@Override
public J visitNewClass(J.NewClass newClass, ExecutionContext executionContext) {
public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
if (noArgsMatcher.matches(newClass.getConstructorType())) {
maybeAddImport("org.apache.http.impl.client.HttpClients");
doAfterVisit(new ChangeType(
Expand All @@ -54,7 +54,7 @@ public J visitNewClass(J.NewClass newClass, ExecutionContext executionContext) {
).getVisitor());
return noArgsTemplate.apply(getCursor(), newClass.getCoordinates().replace());
}
return super.visitNewClass(newClass, executionContext);
return super.visitNewClass(newClass, ctx);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
final MethodMatcher matcher = new MethodMatcher(methodPattern);

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
J.MethodInvocation m = super.visitMethodInvocation(method, executionContext);
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
if (matcher.matches(m)) {
JavaTemplate template = JavaTemplate
.builder(StringUtils.repeat("#{any()}, ", m.getArguments().size()) + "TimeUnit.#{}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ private static class ImplicitWebAnnotationNamesVisitor extends JavaIsoVisitor<Ex


@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext executionContext) {
J.VariableDeclarations varDecls = super.visitVariableDeclarations(multiVariable, executionContext);
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
J.VariableDeclarations varDecls = super.visitVariableDeclarations(multiVariable, ctx);
// Fix when the annotation looses all it's arguments, and there is no prefix between the annotation and the type expression
// i.e: @Annotation(argument)Type is valid but @AnnotationType it's not
if (!varDecls.getLeadingAnnotations().isEmpty()) {
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/openrewrite/java/spring/RenameBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(precondition(), new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method,
ExecutionContext executionContext) {
J.MethodDeclaration m = super.visitMethodDeclaration(method, executionContext);
ExecutionContext ctx) {
J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);

// handle bean declarations
if (m.getMethodType() != null && isRelevantType(m.getMethodType().getReturnType())) {
Expand All @@ -232,8 +232,8 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method,

@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl,
ExecutionContext executionContext) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, executionContext);
ExecutionContext ctx) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);

// handle bean declarations
if (cd.getType() != null && isRelevantType(cd.getType())) {
Expand Down Expand Up @@ -330,8 +330,8 @@ private TreeVisitor<J, ExecutionContext> renameBeanAnnotationValue(J.Annotation
J.Assignment beanNameAssignment) {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext executionContext) {
J.Annotation a = super.visitAnnotation(annotation, executionContext);
public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ctx) {
J.Annotation a = super.visitAnnotation(annotation, ctx);
if (a == beanAnnotation) {
a = a.withArguments(ListUtils.map(a.getArguments(), arg -> {
if (arg == beanNameAssignment) {
Expand All @@ -349,8 +349,8 @@ public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ex
private TreeVisitor<J, ExecutionContext> renameBeanAnnotationValue(J.Annotation beanAnnotation) {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext executionContext) {
J.Annotation a = super.visitAnnotation(annotation, executionContext);
public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ctx) {
J.Annotation a = super.visitAnnotation(annotation, ctx);
if (a == beanAnnotation) {
a = a.withArguments(ListUtils.map(a.getArguments(), arg -> replace(arg, oldName, newName)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Collection<SourceFile> generate(ApiManifest acc, ExecutionContext ctx) {
public TreeVisitor<?, ExecutionContext> getVisitor(ApiManifest acc) {
return Preconditions.check(!acc.isGenerate(), new PlainTextVisitor<ExecutionContext>() {
@Override
public PlainText visitText(PlainText text, ExecutionContext executionContext) {
public PlainText visitText(PlainText text, ExecutionContext ctx) {
if (text.getSourcePath().equals(Paths.get("META-INF/api-manifest.txt"))) {
return text.withText(generateManifest(acc.getApis()).getText());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public UpdateListMethodInvocations(String parameterName) {
private static final MethodMatcher LIST_MATCHER = new MethodMatcher("java.util.List *(..)", true);

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
J.MethodInvocation mi = super.visitMethodInvocation(method, executionContext);
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
if (LIST_MATCHER.matches(mi) && isParameter(mi.getSelect())) {
assert mi.getPadding().getSelect() != null;
// No need to take care of typing here, since it's going to be printed and parsed on the JavaTemplate later on.
Expand Down Expand Up @@ -149,8 +149,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

@Override
public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable, ExecutionContext executionContext) {
J.VariableDeclarations.NamedVariable var = super.visitVariable(variable, executionContext);
public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable, ExecutionContext ctx) {
J.VariableDeclarations.NamedVariable var = super.visitVariable(variable, ctx);

if (notAssignableFromChunk(var) && isParameter(var.getInitializer())) {
var = var.withInitializer(newGetItemsMethodInvocation(
Expand All @@ -161,8 +161,8 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
}

@Override
public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext executionContext) {
J.Assignment a = super.visitAssignment(assignment, executionContext);
public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext ctx) {
J.Assignment a = super.visitAssignment(assignment, ctx);
if (notAssignableFromChunk(a.getVariable().getType()) && isParameter(a.getAssignment())) {
a = a.withAssignment(newGetItemsMethodInvocation(
new JRightPadded<>(a.getAssignment(), Space.EMPTY, Markers.EMPTY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {

static final class RemoveDefaultBatchConfigurerVisitor extends JavaIsoVisitor<ExecutionContext> {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, executionContext);
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);
if (TypeUtils.isAssignableTo(DEFAULT_BATCH_CONFIGURER, cd.getType())) {
// Strip extends DefaultBatchConfigurer
maybeRemoveImport(DEFAULT_BATCH_CONFIGURER);
Expand All @@ -65,8 +65,8 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
}

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext executionContext) {
J.MethodDeclaration md = super.visitMethodDeclaration(method, executionContext);
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
J.MethodDeclaration md = super.visitMethodDeclaration(method, ctx);
if (overridesDefaultBatchConfigurerMethod(md) || callsDefaultBatchConfigurerSuperConstructor(md)) {
// Strip @Override
md = md.withLeadingAnnotations(ListUtils.map(md.getLeadingAnnotations(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesMethod<>(REQUEST_CACHE_MATCHER), new JavaIsoVisitor<ExecutionContext>() {

@Override
public J.Block visitBlock(J.Block block, ExecutionContext executionContext) {
block = super.visitBlock(block, executionContext);
public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
block = super.visitBlock(block, ctx);
List<Statement> statements = block.getStatements();

boolean hasContinueParameterStatement = findContinueParameterStatement(statements);
Expand Down Expand Up @@ -94,7 +94,7 @@ public J.Block visitBlock(J.Block block, ExecutionContext executionContext) {

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method,
ExecutionContext executionContext) {
ExecutionContext ctx) {
if (REQUEST_CACHE_MATCHER.matches(method)) {
Expression arg = method.getArguments().get(0);
if (isNewHttpSessionRequestCacheExpression(arg)) {
Expand All @@ -115,7 +115,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method,
return method;
}

return super.visitMethodInvocation(method, executionContext);
return super.visitMethodInvocation(method, ctx);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,31 @@ void testCookieConstantsMapping() {
//language=java
java(
"""
import org.apache.http.client.params.CookiePolicy;
class A {
void method() {
String c1 = CookiePolicy.BROWSER_COMPATIBILITY;
String c2 = CookiePolicy.NETSCAPE;
String c3 = CookiePolicy.RFC_2109;
String c4 = CookiePolicy.RFC_2965;
String c5 = CookiePolicy.BEST_MATCH;
String c6 = CookiePolicy.IGNORE_COOKIES;
}
import org.apache.http.client.params.CookiePolicy;
class A {
void method() {
String c1 = CookiePolicy.BROWSER_COMPATIBILITY;
String c2 = CookiePolicy.NETSCAPE;
String c3 = CookiePolicy.RFC_2109;
String c4 = CookiePolicy.RFC_2965;
String c5 = CookiePolicy.BEST_MATCH;
String c6 = CookiePolicy.IGNORE_COOKIES;
}
}
""", """
import org.apache.http.client.config.CookieSpecs;
class A {
void method() {
String c1 = CookieSpecs.BROWSER_COMPATIBILITY;
String c2 = CookieSpecs.NETSCAPE;
String c3 = CookieSpecs.STANDARD;
String c4 = CookieSpecs.STANDARD_STRICT;
String c5 = CookieSpecs.BEST_MATCH;
String c6 = CookieSpecs.IGNORE_COOKIES;
}
import org.apache.http.client.config.CookieSpecs;
class A {
void method() {
String c1 = CookieSpecs.BROWSER_COMPATIBILITY;
String c2 = CookieSpecs.NETSCAPE;
String c3 = CookieSpecs.STANDARD;
String c4 = CookieSpecs.STANDARD_STRICT;
String c5 = CookieSpecs.BEST_MATCH;
String c6 = CookieSpecs.IGNORE_COOKIES;
}
}
"""
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,34 @@ void noArgsDefaultHttpClient() {
//language=java
java(
"""
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.IOException;
class A {
void method() throws IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://moderne.io");
HttpResponse httpResponse = httpClient.execute(httpPost);
}
}
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.IOException;
class A {
void method() throws IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://moderne.io");
HttpResponse httpResponse = httpClient.execute(httpPost);
}
}
""", """
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
class A {
void method() throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://moderne.io");
HttpResponse httpResponse = httpClient.execute(httpPost);
}
}
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
class A {
void method() throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://moderne.io");
HttpResponse httpResponse = httpClient.execute(httpPost);
}
}
"""
)
);
Expand Down
Loading

0 comments on commit 67815f7

Please sign in to comment.