Skip to content

Commit ddc560f

Browse files
refactor: OpenRewrite best practices (#4243)
Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.recipes.OpenRewriteBestPractices?organizationId=T3BlblJld3JpdGU%3D Co-authored-by: Moderne <[email protected]>
1 parent 9bcc84c commit ddc560f

File tree

9 files changed

+81
-71
lines changed

9 files changed

+81
-71
lines changed

rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeTransitiveDependencyVersion.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
483483
private static class RemoveVersionVisitor extends GroovyIsoVisitor<ExecutionContext> {
484484

485485
@Override
486-
public J.Return visitReturn(J.Return _return, ExecutionContext executionContext) {
487-
J.Return r = super.visitReturn(_return, executionContext);
486+
public J.Return visitReturn(J.Return _return, ExecutionContext ctx) {
487+
J.Return r = super.visitReturn(_return, ctx);
488488
if(r.getExpression() == null) {
489489
//noinspection DataFlowIssue
490490
return null;
@@ -493,8 +493,8 @@ public J.Return visitReturn(J.Return _return, ExecutionContext executionContext)
493493
}
494494

495495
@Override
496-
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
497-
J.MethodInvocation m = super.visitMethodInvocation(method, executionContext);
496+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
497+
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
498498
if("version".equals(m.getSimpleName()) && m.getArguments().size() == 1 && m.getArguments().get(0) instanceof J.Lambda) {
499499
//noinspection DataFlowIssue
500500
return null;

rewrite-gradle/src/test/java/org/openrewrite/gradle/RemoveEnableFeaturePreviewTest.java

+17-15
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.junit.jupiter.api.Nested;
1919
import org.junit.jupiter.api.Test;
20+
import org.openrewrite.DocumentExample;
2021
import org.openrewrite.test.RecipeSpec;
2122
import org.openrewrite.test.RewriteTest;
2223

@@ -29,29 +30,30 @@ public void defaults(RecipeSpec spec) {
2930
spec.recipe(new RemoveEnableFeaturePreview("ONE_LOCKFILE_PER_PROJECT"));
3031
}
3132

33+
@DocumentExample
3234
@Test
3335
void singleQuotes() {
3436
//language=gradle
3537
rewriteRun(
3638
settingsGradle(
3739
"""
38-
pluginManagement {
39-
repositories {
40-
gradlePluginPortal()
41-
}
42-
}
43-
44-
rootProject.name = 'merge-service'
45-
enableFeaturePreview('ONE_LOCKFILE_PER_PROJECT')
40+
pluginManagement {
41+
repositories {
42+
gradlePluginPortal()
43+
}
44+
}
45+
46+
rootProject.name = 'merge-service'
47+
enableFeaturePreview('ONE_LOCKFILE_PER_PROJECT')
4648
""",
4749
"""
48-
pluginManagement {
49-
repositories {
50-
gradlePluginPortal()
51-
}
52-
}
53-
54-
rootProject.name = 'merge-service'
50+
pluginManagement {
51+
repositories {
52+
gradlePluginPortal()
53+
}
54+
}
55+
56+
rootProject.name = 'merge-service'
5557
"""
5658
)
5759
);

rewrite-java-test/src/test/java/org/openrewrite/java/ChangePackageTest.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,10 @@ class A {
444444
}
445445
""",
446446
"""
447-
import org.openrewrite.test.other.Test;
448-
class A {
449-
Test test = null;
450-
}
447+
import org.openrewrite.test.other.Test;
448+
class A {
449+
Test test = null;
450+
}
451451
""",
452452
spec -> spec.afterRecipe(cu -> {
453453
assertThat(cu.findType("org.openrewrite.other.Test")).isEmpty();
@@ -502,10 +502,10 @@ class A {
502502
}
503503
""",
504504
"""
505-
import org.openrewrite.test.other.Test;
506-
class A {
507-
Test test = null;
508-
}
505+
import org.openrewrite.test.other.Test;
506+
class A {
507+
Test test = null;
508+
}
509509
""",
510510
spec -> spec.afterRecipe(cu -> {
511511
assertThat(cu.findType("org.openrewrite.other.Test")).isEmpty();
@@ -560,10 +560,10 @@ class A {
560560
}
561561
""",
562562
"""
563-
import org.openrewrite.test.other.Test;
564-
class A {
565-
Test test = null;
566-
}
563+
import org.openrewrite.test.other.Test;
564+
class A {
565+
Test test = null;
566+
}
567567
""",
568568
spec -> spec.afterRecipe(cu -> {
569569
assertThat(cu.findType("org.openrewrite.other.Test")).isEmpty();

rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateTest.java

+28-27
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void assignmentWithinIfPredicate() {
3838
rewriteRun(
3939
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
4040
@Override
41-
public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext p) {
41+
public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext ctx) {
4242
if ((assignment.getAssignment() instanceof J.Literal) &&
4343
((J.Literal) assignment.getAssignment()).getValue().equals(1)) {
4444
return JavaTemplate.builder("value = 0")
@@ -77,7 +77,7 @@ void lambdaIsNewClass() {
7777
rewriteRun(
7878
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
7979
@Override
80-
public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext p) {
80+
public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext ctx) {
8181
var a = assignment;
8282
if (a.getAssignment() instanceof J.MethodInvocation) {
8383
J.MethodInvocation mi = (J.MethodInvocation) a.getAssignment();
@@ -120,8 +120,8 @@ void replaceForEachControlVariableType() {
120120
rewriteRun(
121121
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
122122
@Override
123-
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext p) {
124-
var mv = super.visitVariableDeclarations(multiVariable, p);
123+
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
124+
var mv = super.visitVariableDeclarations(multiVariable, ctx);
125125
if (mv.getVariables().get(0).getInitializer() == null && TypeUtils.isOfType(mv.getTypeExpression()
126126
.getType(), JavaType.Primitive.String)) {
127127
mv = JavaTemplate.apply("Object #{}", getCursor(),
@@ -158,8 +158,8 @@ void replaceForEachControlIterable() {
158158
rewriteRun(
159159
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
160160
@Override
161-
public J.ForEachLoop.Control visitForEachControl(J.ForEachLoop.Control control, ExecutionContext executionContext) {
162-
control = super.visitForEachControl(control, executionContext);
161+
public J.ForEachLoop.Control visitForEachControl(J.ForEachLoop.Control control, ExecutionContext ctx) {
162+
control = super.visitForEachControl(control, ctx);
163163
Expression iterable = control.getIterable();
164164
if (!TypeUtils.isOfClassType(iterable.getType(), "java.lang.String")) {
165165
return control;
@@ -192,8 +192,8 @@ void replaceBinaryExpression() {
192192
rewriteRun(
193193
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
194194
@Override
195-
public J.Binary visitBinary(J.Binary binary, ExecutionContext executionContext) {
196-
binary = super.visitBinary(binary, executionContext);
195+
public J.Binary visitBinary(J.Binary binary, ExecutionContext ctx) {
196+
binary = super.visitBinary(binary, ctx);
197197
if (binary.getLeft() instanceof J.Literal lit && lit.getValue().equals(42)) {
198198
return JavaTemplate.apply("43", getCursor(), lit.getCoordinates().replace());
199199
}
@@ -225,7 +225,7 @@ void replaceForEachControlIterator() {
225225
rewriteRun(
226226
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
227227
@Override
228-
public J visitNewClass(J.NewClass newClass, ExecutionContext p) {
228+
public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
229229
if (TypeUtils.isOfClassType(newClass.getType(), "java.util.ArrayList")) {
230230
return JavaTemplate.builder("Collections.emptyList()")
231231
.imports("java.util.Collections").build()
@@ -313,7 +313,7 @@ void replaceAnonymousClassObject() {
313313
rewriteRun(
314314
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
315315
@Override
316-
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
316+
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
317317
if (method.getSimpleName().equals("asList")) {
318318
maybeAddImport("java.util.Collections");
319319
maybeRemoveImport("java.util.Arrays");
@@ -361,7 +361,7 @@ void replaceGenericTypedObject() {
361361
rewriteRun(
362362
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
363363
@Override
364-
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
364+
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
365365
if (method.getSimpleName().equals("asList")) {
366366
maybeAddImport("java.util.Collections");
367367
maybeRemoveImport("java.util.Arrays");
@@ -411,7 +411,7 @@ void replaceParameterizedTypeObject() {
411411
rewriteRun(
412412
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
413413
@Override
414-
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
414+
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
415415
if (method.getSimpleName().equals("asList")) {
416416
maybeAddImport("java.util.Collections");
417417
maybeRemoveImport("java.util.Arrays");
@@ -465,7 +465,7 @@ void templatingWhileLoopCondition() {
465465
rewriteRun(
466466
spec -> spec.expectedCyclesThatMakeChanges(2).recipe(toRecipe(() -> new JavaVisitor<>() {
467467
@Override
468-
public J visitBinary(J.Binary binary, ExecutionContext p) {
468+
public J visitBinary(J.Binary binary, ExecutionContext ctx) {
469469
if (binary.getLeft() instanceof J.MethodInvocation) {
470470
J.MethodInvocation mi = (J.MethodInvocation) binary.getLeft();
471471
return JavaTemplate.builder("!#{any(java.util.List)}.isEmpty()")
@@ -508,8 +508,8 @@ void javaTemplateControlsSemiColons() {
508508
.imports("java.math.RoundingMode").build();
509509

510510
@Override
511-
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext p) {
512-
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, p);
511+
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
512+
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
513513
if (bigDecimalSetScale.matches(mi)) {
514514
mi = twoArgScale.apply(updateCursor(mi), mi.getCoordinates().replaceArguments(),
515515
mi.getArguments().get(0), "RoundingMode.HALF_UP");
@@ -550,7 +550,7 @@ void replaceExpressionWithAnotherExpression() {
550550
rewriteRun(
551551
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
552552
@Override
553-
public J visitUnary(J.Unary unary, ExecutionContext p) {
553+
public J visitUnary(J.Unary unary, ExecutionContext ctx) {
554554
return JavaTemplate.builder("#{any()}++")
555555
.contextSensitive()
556556
.build().apply(
@@ -585,7 +585,7 @@ void replaceMemberReference() {
585585
rewriteRun(
586586
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
587587
@Override
588-
public J visitMemberReference(J.MemberReference memberRef, ExecutionContext executionContext) {
588+
public J visitMemberReference(J.MemberReference memberRef, ExecutionContext ctx) {
589589
return JavaTemplate.builder("() -> new ArrayList<>(1)")
590590
.contextSensitive()
591591
.imports("java.util.ArrayList")
@@ -624,12 +624,12 @@ void replaceFieldAccessWithMethodInvocation() {
624624
rewriteRun(
625625
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
626626
@Override
627-
public J visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext p) {
628-
return method.withBody((J.Block) visit(method.getBody(), p));
627+
public J visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
628+
return method.withBody((J.Block) visit(method.getBody(), ctx));
629629
}
630630

631631
@Override
632-
public J visitFieldAccess(J.FieldAccess fa, ExecutionContext p) {
632+
public J visitFieldAccess(J.FieldAccess fa, ExecutionContext ctx) {
633633
if (fa.getSimpleName().equals("f")) {
634634
return JavaTemplate.apply("#{any(java.io.File)}.getCanonicalFile().toPath()",
635635
getCursor(), fa.getCoordinates().replace(), fa);
@@ -705,7 +705,7 @@ void innerEnumWithStaticMethod() {
705705
rewriteRun(
706706
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
707707
@Override
708-
public J visitNewClass(J.NewClass newClass, ExecutionContext p) {
708+
public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
709709
if (newClass.getArguments().get(0) instanceof J.Empty) {
710710
return newClass;
711711
}
@@ -774,7 +774,7 @@ void arrayInitializer() {
774774
final MethodMatcher mm = new MethodMatcher("abc.ArrayHelper of(..)");
775775

776776
@Override
777-
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext p) {
777+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
778778
if (mm.matches(method)) {
779779
return JavaTemplate.builder("Arrays.asList(#{any(java.lang.Integer)}, #{any(java.lang.Integer)}, #{any(java.lang.Integer)})")
780780
.imports("java.util.Arrays")
@@ -828,8 +828,8 @@ void multiDimensionalArrayInitializer() {
828828
final MethodMatcher mm = new MethodMatcher("java.util.stream.IntStream sum()");
829829

830830
@Override
831-
public J visitNewClass(J.NewClass newClass, ExecutionContext p) {
832-
J.NewClass nc = (J.NewClass) super.visitNewClass(newClass, p);
831+
public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
832+
J.NewClass nc = (J.NewClass) super.visitNewClass(newClass, ctx);
833833
return JavaTemplate.apply("Integer.valueOf(#{any(java.lang.Integer)}",
834834
getCursor(), nc.getCoordinates().replace(), nc.getArguments().get(0));
835835
}
@@ -855,7 +855,7 @@ void dontDropTheAssert() {
855855
rewriteRun(
856856
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
857857
@Override
858-
public J visitBinary(J.Binary binary, ExecutionContext p) {
858+
public J visitBinary(J.Binary binary, ExecutionContext ctx) {
859859
J isEmpty = JavaTemplate.apply("!#{any(java.util.Collection)}.isEmpty()", getCursor(),
860860
binary.getCoordinates().replace(), ((J.MethodInvocation) binary.getLeft()).getSelect());
861861
return isEmpty.withPrefix(binary.getPrefix());
@@ -889,7 +889,7 @@ void nestedEnums() {
889889
rewriteRun(
890890
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
891891
@Override
892-
public J visitBinary(J.Binary binary, ExecutionContext p) {
892+
public J visitBinary(J.Binary binary, ExecutionContext ctx) {
893893
return JavaTemplate.builder("\"ab\"").contextSensitive().build()
894894
.apply(getCursor(), binary.getCoordinates().replace());
895895
}
@@ -1199,7 +1199,8 @@ interface Test {
11991199
void finalMethodParameter() {
12001200
rewriteRun(
12011201
spec -> spec.recipe(new ReplaceAnnotation("@org.jetbrains.annotations.NotNull", "@lombok.NonNull", null)),
1202-
java("""
1202+
java(
1203+
"""
12031204
import org.jetbrains.annotations.NotNull;
12041205
12051206
class A {

rewrite-java-test/src/test/java/org/openrewrite/java/ReplaceStringLiteralWithConstantTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,10 @@ void missingFieldNoError() {
249249
rewriteRun(
250250
spec -> spec.recipe(RewriteTest.toRecipe(() -> new JavaVisitor<>(){
251251
@Override
252-
public @Nullable J visit(@Nullable Tree tree, ExecutionContext executionContext) {
252+
public @Nullable J visit(@Nullable Tree tree, ExecutionContext ctx) {
253253
// Circumvent validation to match use in rewrite-spring's ReplaceStringLiteralsWithMediaTypeConstants
254254
doAfterVisit(new ReplaceStringLiteralWithConstant(EXAMPLE_STRING_FQN + "_xyz").getVisitor());
255-
return super.visit(tree, executionContext);
255+
return super.visit(tree, ctx);
256256
}
257257
})),
258258
java(

rewrite-java/src/main/java/org/openrewrite/java/ReplaceAnnotation.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ public class ReplaceAnnotation extends Recipe {
4040

4141
@Option(displayName = "Classpath resource",
4242
description = "If the annotation's type is defined by a jar within the META-INF/rewrite/classpath directory provide its name here " +
43-
"so that it can be loaded. " +
44-
"When this parameter is not passed the runtime classpath of the recipe is provided to the parser producing the new annotation. " +
45-
"This is necessary when the annotation is not on the runtime classpath of the recipe and isn't in the Java standard library.",
43+
"so that it can be loaded. " +
44+
"When this parameter is not passed the runtime classpath of the recipe is provided to the parser producing the new annotation. " +
45+
"This is necessary when the annotation is not on the runtime classpath of the recipe and isn't in the Java standard library.",
46+
example = "annotations",
4647
required = false)
4748
@Nullable
4849
String classpathResourceName;

rewrite-maven/src/test/java/org/openrewrite/maven/AddPropertyTest.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ void ifRemoteParentIsDefined() {
178178
<artifactId>my-parent</artifactId>
179179
<version>1</version>
180180
</project>
181-
""","""
181+
""",
182+
"""
182183
<project>
183184
<parent>
184185
<groupId>org.springframework.boot</groupId>
@@ -214,7 +215,8 @@ void ifRemoteParentIsDefined_2() {
214215
<artifactId>my-parent</artifactId>
215216
<version>1</version>
216217
</project>
217-
""","""
218+
""",
219+
"""
218220
<project>
219221
<parent>
220222
<groupId>org.springframework.boot</groupId>
@@ -251,7 +253,8 @@ void ifRemoteParentIsDefined_3() {
251253
<artifactId>my-parent</artifactId>
252254
<version>1</version>
253255
</project>
254-
""","""
256+
""",
257+
"""
255258
<project>
256259
<parent>
257260
<groupId>org.springframework.boot</groupId>

0 commit comments

Comments
 (0)