Skip to content

Commit b80c30c

Browse files
refactor: Use JavaParser.Builder when constructing JavaTemplate
Co-authored-by: Moderne <[email protected]>
1 parent b5a9926 commit b80c30c

File tree

7 files changed

+20
-31
lines changed

7 files changed

+20
-31
lines changed

src/main/java/org/openrewrite/java/testing/assertj/JUnitFailToAssertJFail.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
146146

147147
method = method.withTemplate(JavaTemplate.builder(this::getCursor, templateBuilder.toString())
148148
.staticImports("org.assertj.core.api.Assertions" + ".fail")
149-
.javaParser(() -> JavaParser.fromJavaVersion()
150-
.classpathFromResources(ctx, "assertj-core-3.24.2")
151-
.build())
149+
.javaParser(JavaParser.fromJavaVersion()
150+
.classpathFromResources(ctx, "assertj-core-3.24.2"))
152151
.build(),
153152
method.getCoordinates().replace(),
154153
arguments.toArray()

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,8 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
106106
@NonNull
107107
private JavaTemplate getNestedJavaTemplate(ExecutionContext ctx) {
108108
return JavaTemplate.builder(this::getCursor, "@Nested")
109-
.javaParser(() -> JavaParser.fromJavaVersion()
110-
.classpathFromResources(ctx, "junit-jupiter-api-5.9.2")
111-
.build())
109+
.javaParser(JavaParser.fromJavaVersion()
110+
.classpathFromResources(ctx, "junit-jupiter-api-5.9.2"))
112111
.imports(NESTED)
113112
.build();
114113
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,8 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
9898
@NonNull
9999
private JavaTemplate getNestedJavaTemplate(ExecutionContext ctx) {
100100
return JavaTemplate.builder(this::getCursor, "@Nested")
101-
.javaParser(() -> JavaParser.fromJavaVersion()
102-
.classpathFromResources(ctx, "junit-jupiter-api-5.9.2")
103-
.build())
101+
.javaParser(JavaParser.fromJavaVersion()
102+
.classpathFromResources(ctx, "junit-jupiter-api-5.9.2"))
104103
.imports(NESTED)
105104
.build();
106105
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,8 @@ private J.MethodDeclaration updateMethodDeclarationAnnotationAndModifier(J.Metho
154154
J.MethodDeclaration md = methodDeclaration;
155155
if (FindAnnotations.find(methodDeclaration.withBody(null), "@" + fullyQualifiedAnnotation).isEmpty()) {
156156
md = methodDeclaration.withTemplate(JavaTemplate.builder(this::getCursor, annotation)
157-
.javaParser(() -> JavaParser.fromJavaVersion()
158-
.classpathFromResources(ctx, "junit-jupiter-api-5.9.2")
159-
.build())
157+
.javaParser(JavaParser.fromJavaVersion()
158+
.classpathFromResources(ctx, "junit-jupiter-api-5.9.2"))
160159
.imports(fullyQualifiedAnnotation).build(),
161160
methodDeclaration.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)));
162161
md = maybeAddPublicModifier(md);

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,8 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
113113

114114
cd = cd.withTemplate(
115115
JavaTemplate.builder(this::getCursor, "@ExtendWith(MockitoExtension.class)")
116-
.javaParser(() ->
117-
JavaParser.fromJavaVersion()
118-
.classpathFromResources(ctx, "junit-jupiter-api-5.9.2", "mockito-junit-jupiter-3.12.4")
119-
.build())
116+
.javaParser(JavaParser.fromJavaVersion()
117+
.classpathFromResources(ctx, "junit-jupiter-api-5.9.2", "mockito-junit-jupiter-3.12.4"))
120118
.imports("org.junit.jupiter.api.extension.ExtendWith", "org.mockito.junit.jupiter.MockitoExtension")
121119
.build(),
122120
cd.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName))

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,11 @@ protected TreeVisitor<?, ExecutionContext> getVisitor() {
9191
private JavaTemplate getExtendsWithTemplate(ExecutionContext ctx) {
9292
if (extendsWithTemplate == null) {
9393
extendsWithTemplate = JavaTemplate.builder(this::getCursor, "@ExtendWith(#{}.class)")
94-
.javaParser(() -> JavaParser.fromJavaVersion()
94+
.javaParser(JavaParser.fromJavaVersion()
9595
.classpathFromResources(ctx, "junit-jupiter-api-5.9.2")
9696
.dependsOn( "package " + extensionType.getPackageName() + ";\n" +
9797
"import org.junit.jupiter.api.extension.Extension;\n" +
98-
"public class " + extensionType.getClassName() + " implements Extension {}")
99-
.build())
98+
"public class " + extensionType.getClassName() + " implements Extension {}"))
10099
.imports("org.junit.jupiter.api.extension.ExtendWith",
101100
"org.junit.jupiter.api.extension.Extension",
102101
extension)

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,8 @@ private J.ClassDeclaration addFieldDeclarationForMockedTypes(J.ClassDeclaration
189189
.withTemplate(
190190
JavaTemplate.builder(() -> getCursor().getParentTreeCursor(),
191191
"private MockedStatic<#{}> mocked#{};")
192-
.javaParser(() -> JavaParser.fromJavaVersion()
193-
.classpathFromResources(ctx, "mockito-core-3.12.4")
194-
.build())
192+
.javaParser(JavaParser.fromJavaVersion()
193+
.classpathFromResources(ctx, "mockito-core-3.12.4"))
195194
.staticImports("org.mockito.Mockito.mockStatic")
196195
.imports(MOCKED_STATIC)
197196
.build(),
@@ -253,9 +252,8 @@ private J.ClassDeclaration maybeAddMethodWithAnnotation(J.ClassDeclaration class
253252
.withTemplate(
254253
JavaTemplate.builder(() -> getCursor().getParentTreeCursor(),
255254
methodAnnotationToAdd + " void " + methodName + "() {}")
256-
.javaParser(() -> JavaParser.fromJavaVersion()
257-
.classpathFromResources(ctx, additionalClasspathResource)
258-
.build())
255+
.javaParser(JavaParser.fromJavaVersion()
256+
.classpathFromResources(ctx, additionalClasspathResource))
259257
.imports(importToAdd)
260258
.build(),
261259
tearDownCoordinates));
@@ -348,9 +346,8 @@ public J visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx
348346
m = m.withBody(methodBody.withTemplate(
349347
JavaTemplate.builder(() -> getCursor().getParentTreeCursor(),
350348
"#{any(org.mockito.MockedStatic)}.close();")
351-
.javaParser(() -> JavaParser.fromJavaVersion()
352-
.classpathFromResources(ctx, "mockito-core-3.*")
353-
.build())
349+
.javaParser(JavaParser.fromJavaVersion()
350+
.classpathFromResources(ctx, "mockito-core-3.*"))
354351
.build(),
355352
methodBody.getCoordinates().lastStatement(),
356353
mockedTypesField
@@ -383,9 +380,8 @@ public J visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx
383380
m = m.withBody(methodBody.withTemplate(
384381
JavaTemplate.builder(() -> getCursor().getParentTreeCursor(),
385382
"mocked#{any(org.mockito.MockedStatic)} = mockStatic(#{}.class);")
386-
.javaParser(() -> JavaParser.fromJavaVersion()
387-
.classpathFromResources(ctx, "mockito-core-3.*")
388-
.build())
383+
.javaParser(JavaParser.fromJavaVersion()
384+
.classpathFromResources(ctx, "mockito-core-3.*"))
389385
.build(),
390386
methodBody.getCoordinates().lastStatement(),
391387
mockedTypesField,

0 commit comments

Comments
 (0)