Skip to content

Commit 130d158

Browse files
OpenRewrite recipe best practices (#949)
* OpenRewrite recipe best practices Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.recipes.rewrite.OpenRewriteRecipeBestPractices?organizationId=QUxML01vZGVybmUvTW9kZXJuZSArIE9wZW5SZXdyaXRl Co-authored-by: Moderne <team@moderne.io> * Fix compilation errors: cast visitor return type to MethodDeclaration RemoveAnnotationVisitor.visit() and RemoveAnnotation.getVisitor().visit() return generic J, but the calling methods expect J.MethodDeclaration. Added explicit casts in three files to match the existing pattern. --------- Co-authored-by: Moderne <team@moderne.io>
1 parent cf368d7 commit 130d158

15 files changed

Lines changed: 84 additions & 48 deletions

src/main/java/org/openrewrite/java/testing/cleanup/RemoveTestPrefix.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Atomi
148148
}
149149
return super.visitMethodInvocation(method, atomicBoolean);
150150
}
151-
}.visitMethodDeclaration(m, skip);
151+
}.visit(m, skip, getCursor().getParentTreeCursor());
152152
if (skip.get()) {
153153
return m;
154154
}

src/main/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration md, Execut
8080

8181
// Remove @Test annotation if present
8282
maybeAddImport("org.junit.jupiter.params.ParameterizedTest");
83-
return new RemoveAnnotationVisitor(TEST_ANNOTATION_MATCHER).visitMethodDeclaration(m, ctx);
83+
return (J.MethodDeclaration) new RemoveAnnotationVisitor(TEST_ANNOTATION_MATCHER).visit(m, ctx, getCursor().getParentTreeCursor());
8484
}
8585
}
8686
}

src/main/java/org/openrewrite/java/testing/junit5/RemoveDuplicateTestTemplates.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration md, Execut
5151
if (m.getLeadingAnnotations().stream().anyMatch(TEST_ANNOTATION_MATCHER::matches) &&
5252
m.getLeadingAnnotations().stream().anyMatch(REPEATED_TEST_ANNOTATION_MATCHER::matches)) {
5353
maybeRemoveImport("org.junit.jupiter.api.Test");
54-
return new RemoveAnnotationVisitor(TEST_ANNOTATION_MATCHER).visitMethodDeclaration(m, ctx);
54+
return (J.MethodDeclaration) new RemoveAnnotationVisitor(TEST_ANNOTATION_MATCHER).visit(m, ctx, getCursor().getParentTreeCursor());
5555
}
5656
return m;
5757
}

src/main/java/org/openrewrite/java/testing/junit6/MinimumJreConditions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon
188188
private J.MethodDeclaration removeAnnotation(J.MethodDeclaration m, String annotationType, ExecutionContext ctx) {
189189
RemoveAnnotation removeAnnotation = new RemoveAnnotation("@" + annotationType);
190190
maybeRemoveImport(JRE_IMPORT);
191-
return removeAnnotation.getVisitor().visitMethodDeclaration(m, ctx);
191+
return (J.MethodDeclaration) removeAnnotation.getVisitor().visit(m, ctx, getCursor().getParentTreeCursor());
192192
}
193193

194194
private J.MethodDeclaration updateAnnotationVersions(J.MethodDeclaration m, List<String> versions, String annotationType, ExecutionContext ctx) {

src/main/java/org/openrewrite/java/testing/mockito/ArgumentMatcherToLambda.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
import org.openrewrite.marker.Markers;
2727

2828
import java.util.ArrayList;
29-
import java.util.Collections;
3029
import java.util.List;
3130

3231
import static java.util.Collections.emptyList;
32+
import static java.util.Collections.singletonList;
3333
import static org.openrewrite.Tree.randomId;
3434

3535
public class ArgumentMatcherToLambda extends Recipe {
@@ -118,7 +118,7 @@ public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
118118
J.Lambda.Parameters lambdaParams = new J.Lambda.Parameters(
119119
randomId(), Space.EMPTY, Markers.EMPTY,
120120
false,
121-
Collections.singletonList(JRightPadded.build(lambdaParam))
121+
singletonList(JRightPadded.build(lambdaParam))
122122
);
123123

124124
J.Lambda lambda = new J.Lambda(

src/main/java/org/openrewrite/java/testing/mockito/PowerMockRunnerDelegateToRunWith.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929
import org.openrewrite.java.tree.Expression;
3030
import org.openrewrite.java.tree.J;
3131

32-
import java.util.Collections;
3332
import java.util.List;
3433

34+
import static java.util.Collections.singletonList;
35+
3536
public class PowerMockRunnerDelegateToRunWith extends Recipe {
3637

3738
private static final String POWER_MOCK_RUNNER_DELEGATE = "org.powermock.modules.junit4.PowerMockRunnerDelegate";
@@ -74,7 +75,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
7475
maybeRemoveImport(POWER_MOCK_RUNNER_DELEGATE);
7576
return cd.withLeadingAnnotations(ListUtils.map(cd.getLeadingAnnotations(), annotation -> {
7677
if (RUN_WITH_POWER_MOCK_RUNNER_MATCHER.matches(annotation)) {
77-
return annotation.withArguments(Collections.singletonList(delegateRunnerArg));
78+
return annotation.withArguments(singletonList(delegateRunnerArg));
7879
}
7980
if (DELEGATE_MATCHER.matches(annotation)) {
8081
return null;

src/main/java/org/openrewrite/java/testing/mockito/PowerMockWhiteboxToJavaReflection.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import org.jspecify.annotations.Nullable;
2020
import org.openrewrite.*;
2121
import org.openrewrite.internal.ListUtils;
22-
import org.openrewrite.java.*;
22+
import org.openrewrite.java.JavaIsoVisitor;
23+
import org.openrewrite.java.JavaParser;
24+
import org.openrewrite.java.JavaTemplate;
25+
import org.openrewrite.java.MethodMatcher;
2326
import org.openrewrite.java.search.UsesType;
2427
import org.openrewrite.java.tree.*;
2528
import org.openrewrite.marker.Markers;

src/main/java/org/openrewrite/java/testing/mockito/RemovePowerMockClassExtensions.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5050
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
5151
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);
5252
cd = maybeRemoveExtension(cd, POWER_MOCK_CONFIG);
53-
cd = maybeRemoveExtension(cd, POWER_MOCK_TEST_CASE);
54-
return cd;
53+
return maybeRemoveExtension(cd, POWER_MOCK_TEST_CASE);
5554
}
5655

5756
private J.ClassDeclaration maybeRemoveExtension(J.ClassDeclaration classDecl, String extensionFQN) {

src/main/resources/META-INF/rewrite/examples.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4690,6 +4690,38 @@ examples:
46904690
language: java
46914691
---
46924692
type: specs.openrewrite.org/v1beta/example
4693+
recipeName: org.openrewrite.java.testing.mockito.PowerMockWhiteboxToJavaReflection
4694+
examples:
4695+
- description: '`PowerMockWhiteboxToJavaReflectionTest#setInternalStateReplacedWithReflection`'
4696+
sources:
4697+
- before: |
4698+
class MyService {
4699+
private String name;
4700+
}
4701+
language: java
4702+
- before: |
4703+
import org.powermock.reflect.Whitebox;
4704+
4705+
class MyServiceTest {
4706+
void testSetField() {
4707+
MyService service = new MyService();
4708+
Whitebox.setInternalState(service, "name", "expectedValue");
4709+
}
4710+
}
4711+
after: |
4712+
import java.lang.reflect.Field;
4713+
4714+
class MyServiceTest {
4715+
void testSetField() throws Exception {
4716+
MyService service = new MyService();
4717+
Field nameField = service.getClass().getDeclaredField("name");
4718+
nameField.setAccessible(true);
4719+
nameField.set(service, "expectedValue");
4720+
}
4721+
}
4722+
language: java
4723+
---
4724+
type: specs.openrewrite.org/v1beta/example
46934725
recipeName: org.openrewrite.java.testing.mockito.PowerMockitoMockStaticToMockito
46944726
examples:
46954727
- description: '`PowerMockitoMockStaticToMockitoTest#prepareForTestAnnotationIsReplacedBySingleField`'

src/test/java/org/openrewrite/java/testing/assertj/AdoptAssertJDurationAssertionsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ void testMethod(Temporal timestampA, Temporal timestampB) {
251251
@ParameterizedTest
252252
void simplifyDurationAssertions(String before, String after) {
253253
//language=java
254-
String template = """
254+
var template = """
255255
import java.time.Duration;
256256
257257
import static org.assertj.core.api.Assertions.assertThat;

0 commit comments

Comments
 (0)