@@ -58,12 +58,13 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
58
58
59
59
class TemporaryFolderToTempDirVisitor extends JavaVisitor <ExecutionContext > {
60
60
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
+
67
68
final JavaTemplate createTempDirTemplate = JavaTemplate .builder ("Files.createTempDirectory(\" junit\" ).toFile()" )
68
69
.imports ("java.nio.file.Files" )
69
70
.build ();
@@ -91,34 +92,29 @@ public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
91
92
@ Override
92
93
public J visitVariableDeclarations (J .VariableDeclarations multiVariable , ExecutionContext ctx ) {
93
94
J .VariableDeclarations mv = (J .VariableDeclarations ) super .visitVariableDeclarations (multiVariable , ctx );
94
- if (!TypeUtils .isOfClassType (multiVariable .getTypeAsFullyQualified (), temporaryFolder )) {
95
+ if (!TypeUtils .isOfClassType (multiVariable .getTypeAsFullyQualified (), TEMPORARY_FOLDER )) {
95
96
return mv ;
96
97
}
97
98
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 ());
110
106
}
111
107
112
108
@ Override
113
109
public @ Nullable J visitNewClass (J .NewClass newClass , ExecutionContext ctx ) {
114
110
boolean hasRuleAnnotation = hasRuleAnnotation ();
115
- if (newTemporaryFolder .matches (newClass )) {
111
+ if (NEW_TEMPORARY_FOLDER .matches (newClass )) {
116
112
if (hasRuleAnnotation ) {
117
113
return null ;
118
114
}
119
115
return createTempDirTemplate .apply (getCursor (), newClass .getCoordinates ().replace ());
120
116
}
121
- if (newTemporaryFolderWithArg .matches (newClass )) {
117
+ if (NEW_TEMPORARY_FOLDER_WITH_ARG .matches (newClass )) {
122
118
if (hasRuleAnnotation ) {
123
119
return null ;
124
120
}
@@ -161,7 +157,7 @@ private boolean hasRuleAnnotation() {
161
157
if (vd == null ) {
162
158
return false ;
163
159
}
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 ));
165
161
}
166
162
167
163
private J convertToNewFile (J .MethodInvocation mi , ExecutionContext ctx ) {
0 commit comments