Skip to content

Commit b304a92

Browse files
committed
Delay looking at rule, and inline JavaTemplates where used
1 parent b3d705a commit b304a92

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,6 @@ class TemporaryFolderToTempDirVisitor extends JavaVisitor<ExecutionContext> {
6565
private static final MethodMatcher NEW_TEMPORARY_FOLDER = new MethodMatcher(TEMPORARY_FOLDER + "<constructor>()");
6666
private static final MethodMatcher NEW_TEMPORARY_FOLDER_WITH_ARG = new MethodMatcher(TEMPORARY_FOLDER + "<constructor>(java.io.File)");
6767

68-
final JavaTemplate createTempDirTemplate = JavaTemplate.builder("Files.createTempDirectory(\"junit\").toFile()")
69-
.imports("java.nio.file.Files")
70-
.build();
71-
final JavaTemplate createTempDirTemplateWithArg = JavaTemplate.builder("Files.createTempDirectory(#{any(java.io.File)}.toPath(), \"junit\").toFile()")
72-
.imports("java.nio.file.Files")
73-
.build();
74-
7568
@Override
7669
public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
7770
J.CompilationUnit c = (J.CompilationUnit) super.visitCompilationUnit(cu, ctx);
@@ -107,18 +100,17 @@ public J visitVariableDeclarations(J.VariableDeclarations multiVariable, Executi
107100

108101
@Override
109102
public @Nullable J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
110-
boolean hasRuleAnnotation = hasRuleAnnotation();
111103
if (NEW_TEMPORARY_FOLDER.matches(newClass)) {
112-
if (hasRuleAnnotation) {
113-
return null;
114-
}
115-
return createTempDirTemplate.apply(getCursor(), newClass.getCoordinates().replace());
104+
return hasRuleAnnotation() ? null : JavaTemplate.builder("Files.createTempDirectory(\"junit\").toFile()")
105+
.imports("java.nio.file.Files")
106+
.build()
107+
.apply(getCursor(), newClass.getCoordinates().replace());
116108
}
117109
if (NEW_TEMPORARY_FOLDER_WITH_ARG.matches(newClass)) {
118-
if (hasRuleAnnotation) {
119-
return null;
120-
}
121-
return createTempDirTemplateWithArg.apply(getCursor(), newClass.getCoordinates().replace(), newClass.getArguments().get(0));
110+
return hasRuleAnnotation() ? null : JavaTemplate.builder("Files.createTempDirectory(#{any(java.io.File)}.toPath(), \"junit\").toFile()")
111+
.imports("java.nio.file.Files")
112+
.build()
113+
.apply(getCursor(), newClass.getCoordinates().replace(), newClass.getArguments().get(0));
122114
}
123115
return super.visitNewClass(newClass, ctx);
124116
}

0 commit comments

Comments
 (0)