Skip to content

Commit b3d705a

Browse files
committed
Update usage of annotated trait
1 parent 5156e2f commit b3d705a

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

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

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5858

5959
class TemporaryFolderToTempDirVisitor extends JavaVisitor<ExecutionContext> {
6060

61-
final String temporaryFolder = "org.junit.rules.TemporaryFolder";
62-
final String tempDir = "org.junit.jupiter.api.io.TempDir";
63-
final AnnotationMatcher classRule = new AnnotationMatcher("@org.junit.ClassRule");
64-
final AnnotationMatcher rule = new AnnotationMatcher("@org.junit.Rule");
65-
final MethodMatcher newTemporaryFolder = new MethodMatcher(temporaryFolder + "<constructor>()");
66-
final MethodMatcher newTemporaryFolderWithArg = new MethodMatcher(temporaryFolder + "<constructor>(java.io.File)");
61+
private static final String TEMPORARY_FOLDER = "org.junit.rules.TemporaryFolder";
62+
private static final String TEMP_DIR = "org.junit.jupiter.api.io.TempDir";
63+
private static final AnnotationMatcher CLASS_RULE = new AnnotationMatcher("@org.junit.ClassRule");
64+
private static final AnnotationMatcher RULE = new AnnotationMatcher("@org.junit.Rule");
65+
private static final MethodMatcher NEW_TEMPORARY_FOLDER = new MethodMatcher(TEMPORARY_FOLDER + "<constructor>()");
66+
private static final MethodMatcher NEW_TEMPORARY_FOLDER_WITH_ARG = new MethodMatcher(TEMPORARY_FOLDER + "<constructor>(java.io.File)");
67+
6768
final JavaTemplate createTempDirTemplate = JavaTemplate.builder("Files.createTempDirectory(\"junit\").toFile()")
6869
.imports("java.nio.file.Files")
6970
.build();
@@ -91,34 +92,29 @@ public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
9192
@Override
9293
public J visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
9394
J.VariableDeclarations mv = (J.VariableDeclarations) super.visitVariableDeclarations(multiVariable, ctx);
94-
if (!TypeUtils.isOfClassType(multiVariable.getTypeAsFullyQualified(), temporaryFolder)) {
95+
if (!TypeUtils.isOfClassType(multiVariable.getTypeAsFullyQualified(), TEMPORARY_FOLDER)) {
9596
return mv;
9697
}
9798
mv = mv.withTypeExpression(toFileIdentifier(mv.getTypeExpression()));
98-
JavaTemplate template = JavaTemplate.builder("@TempDir")
99-
.imports(tempDir)
100-
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5"))
101-
.build();
102-
return (J.VariableDeclarations) annotated("@org.junit.*Rule").asVisitor(a ->
103-
(new JavaIsoVisitor<ExecutionContext>() {
104-
@Override
105-
public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ctx) {
106-
return template.apply(updateCursor(annotation), annotation.getCoordinates().replace());
107-
}
108-
}).visit(a.getTree(), ctx, a.getCursor().getParentOrThrow()))
109-
.visit(mv, ctx, getCursor().getParentOrThrow());
99+
return (J.VariableDeclarations) annotated("@org.junit.*Rule")
100+
.asVisitor(a -> JavaTemplate.builder("@TempDir")
101+
.imports(TEMP_DIR)
102+
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5"))
103+
.build()
104+
.apply(a.getCursor(), a.getTree().getCoordinates().replace()))
105+
.visitNonNull(mv, ctx, getCursor().getParentOrThrow());
110106
}
111107

112108
@Override
113109
public @Nullable J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
114110
boolean hasRuleAnnotation = hasRuleAnnotation();
115-
if (newTemporaryFolder.matches(newClass)) {
111+
if (NEW_TEMPORARY_FOLDER.matches(newClass)) {
116112
if (hasRuleAnnotation) {
117113
return null;
118114
}
119115
return createTempDirTemplate.apply(getCursor(), newClass.getCoordinates().replace());
120116
}
121-
if (newTemporaryFolderWithArg.matches(newClass)) {
117+
if (NEW_TEMPORARY_FOLDER_WITH_ARG.matches(newClass)) {
122118
if (hasRuleAnnotation) {
123119
return null;
124120
}
@@ -161,7 +157,7 @@ private boolean hasRuleAnnotation() {
161157
if (vd == null) {
162158
return false;
163159
}
164-
return vd.getLeadingAnnotations().stream().anyMatch(anno -> classRule.matches(anno) || rule.matches(anno));
160+
return vd.getLeadingAnnotations().stream().anyMatch(anno -> CLASS_RULE.matches(anno) || RULE.matches(anno));
165161
}
166162

167163
private J convertToNewFile(J.MethodInvocation mi, ExecutionContext ctx) {

0 commit comments

Comments
 (0)