|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | | - |
17 | 16 | package org.openrewrite.java.testing.mockito; |
18 | 17 |
|
| 18 | +import org.jspecify.annotations.Nullable; |
19 | 19 | import org.openrewrite.ExecutionContext; |
20 | 20 | import org.openrewrite.Recipe; |
| 21 | +import org.openrewrite.Tree; |
21 | 22 | import org.openrewrite.TreeVisitor; |
22 | 23 | import org.openrewrite.java.JavaIsoVisitor; |
23 | 24 | import org.openrewrite.java.JavaParser; |
|
26 | 27 | import org.openrewrite.java.search.FindTypes; |
27 | 28 | import org.openrewrite.java.search.IsLikelyTest; |
28 | 29 | import org.openrewrite.java.tree.J; |
| 30 | +import org.openrewrite.kotlin.KotlinIsoVisitor; |
| 31 | +import org.openrewrite.kotlin.KotlinParser; |
| 32 | +import org.openrewrite.kotlin.KotlinTemplate; |
| 33 | +import org.openrewrite.kotlin.tree.K; |
29 | 34 |
|
30 | 35 | import static java.util.Comparator.comparing; |
31 | 36 | import static org.openrewrite.Preconditions.*; |
@@ -60,20 +65,52 @@ public TreeVisitor<?, ExecutionContext> getVisitor() { |
60 | 65 | // prevent addition if present |
61 | 66 | not(hasExtendedWithAnnotation), |
62 | 67 | or(hasAnyMockitoAnnotation)), |
63 | | - new JavaIsoVisitor<ExecutionContext>() { |
| 68 | + new TreeVisitor<Tree, ExecutionContext>() { |
64 | 69 | @Override |
65 | | - public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { |
66 | | - |
67 | | - maybeAddImport("org.mockito.junit.jupiter.MockitoExtension"); |
68 | | - maybeAddImport("org.junit.jupiter.api.extension.ExtendWith"); |
69 | | - |
70 | | - return JavaTemplate.builder("@ExtendWith(MockitoExtension.class)") |
71 | | - .imports("org.mockito.junit.jupiter.MockitoExtension") |
72 | | - .imports("org.junit.jupiter.api.extension.ExtendWith") |
73 | | - .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api", "mockito-junit-jupiter")) |
74 | | - .build() |
75 | | - .apply(getCursor(), classDecl.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))); |
| 70 | + public @Nullable Tree preVisit(Tree tree, ExecutionContext ctx) { |
| 71 | + stopAfterPreVisit(); |
| 72 | + if (tree instanceof J.CompilationUnit) { |
| 73 | + return getJavaVisitor().visit(tree, ctx); |
| 74 | + } |
| 75 | + if (tree instanceof K.CompilationUnit) { |
| 76 | + return getKotlinVisitor().visit(tree, ctx); |
| 77 | + } |
| 78 | + return tree; |
76 | 79 | } |
77 | 80 | }); |
78 | 81 | } |
| 82 | + |
| 83 | + private JavaIsoVisitor<ExecutionContext> getJavaVisitor() { |
| 84 | + return new JavaIsoVisitor<ExecutionContext>() { |
| 85 | + @Override |
| 86 | + public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { |
| 87 | + maybeAddImport("org.mockito.junit.jupiter.MockitoExtension"); |
| 88 | + maybeAddImport("org.junit.jupiter.api.extension.ExtendWith"); |
| 89 | + |
| 90 | + return JavaTemplate.builder("@ExtendWith(MockitoExtension.class)") |
| 91 | + .imports("org.mockito.junit.jupiter.MockitoExtension") |
| 92 | + .imports("org.junit.jupiter.api.extension.ExtendWith") |
| 93 | + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api", "mockito-junit-jupiter")) |
| 94 | + .build() |
| 95 | + .apply(getCursor(), classDecl.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))); |
| 96 | + } |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + private KotlinIsoVisitor<ExecutionContext> getKotlinVisitor() { |
| 101 | + return new KotlinIsoVisitor<ExecutionContext>() { |
| 102 | + @Override |
| 103 | + public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { |
| 104 | + maybeAddImport("org.mockito.junit.jupiter.MockitoExtension"); |
| 105 | + maybeAddImport("org.junit.jupiter.api.extension.ExtendWith"); |
| 106 | + |
| 107 | + return KotlinTemplate.builder("@ExtendWith(MockitoExtension::class)") |
| 108 | + .imports("org.mockito.junit.jupiter.MockitoExtension") |
| 109 | + .imports("org.junit.jupiter.api.extension.ExtendWith") |
| 110 | + .parser(KotlinParser.builder().classpathFromResources(ctx, "junit-jupiter-api", "mockito-junit-jupiter")) |
| 111 | + .build() |
| 112 | + .apply(getCursor(), classDecl.getCoordinates().addAnnotation(comparing(J.Annotation::getSimpleName))); |
| 113 | + } |
| 114 | + }; |
| 115 | + } |
79 | 116 | } |
0 commit comments