Skip to content

Commit 41cc6b9

Browse files
Use JavaTemplate.apply() static method (#1037)
Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.java.recipes.UseJavaTemplateStaticApply?organizationId=QUxML01vZGVybmUvTW9kZXJuZSArIE9wZW5SZXdyaXRl Co-authored-by: Moderne <team@moderne.io>
1 parent dd77a80 commit 41cc6b9

10 files changed

Lines changed: 17 additions & 68 deletions

src/main/java/org/openrewrite/java/migrate/AddMissingMethodImplementation.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration cs, Execution
8989
supertype = supertype.getSupertype();
9090
}
9191

92-
return classDecl.withBody(JavaTemplate.builder(methodTemplateString)
93-
.build()
94-
.apply(new Cursor(getCursor(), classDecl.getBody()),
95-
classDecl.getBody().getCoordinates().lastStatement()));
92+
return classDecl.withBody(JavaTemplate.apply( methodTemplateString, new Cursor( getCursor(), classDecl.getBody() ), classDecl.getBody().getCoordinates().lastStatement() ));
9693
}
9794
}
9895
}

src/main/java/org/openrewrite/java/migrate/ReplaceComSunAWTUtilitiesMethods.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -139,48 +139,32 @@ public J visitMethodInvocation(J.MethodInvocation mi, ExecutionContext ctx) {
139139
}
140140
if (isWindowOpaquePatternMethod.matches(mi)) {
141141
maybeRemoveImport(mi.getMethodType().getDeclaringType().getFullyQualifiedName());
142-
return JavaTemplate.builder("#{any()}.isOpaque()")
143-
.build()
144-
.apply(getCursor(), mi.getCoordinates().replace(), mi.getArguments().get(0))
142+
return JavaTemplate.apply( "#{any()}.isOpaque()", getCursor(), mi.getCoordinates().replace(), mi.getArguments().get( 0 ) )
145143
.withPrefix(mi.getPrefix());
146144
}
147145
if (isTranslucencyCapablePatternMethod.matches(mi)) {
148146
maybeRemoveImport(mi.getMethodType().getDeclaringType().getFullyQualifiedName());
149-
return JavaTemplate.builder("#{any()}.isTranslucencyCapable()")
150-
.build()
151-
.apply(getCursor(), mi.getCoordinates().replace(), mi.getArguments().get(0))
147+
return JavaTemplate.apply( "#{any()}.isTranslucencyCapable()", getCursor(), mi.getCoordinates().replace(), mi.getArguments().get( 0 ) )
152148
.withPrefix(mi.getPrefix());
153149
}
154150
if (setWindowOpacityPatternMethod.matches(mi)) {
155151
maybeRemoveImport(mi.getMethodType().getDeclaringType().getFullyQualifiedName());
156-
return JavaTemplate.builder("#{any()}.setOpacity(#{any()})")
157-
.build()
158-
.apply(getCursor(), mi.getCoordinates().replace(),
159-
mi.getArguments().get(0),
160-
mi.getArguments().get(1))
152+
return JavaTemplate.apply( "#{any()}.setOpacity(#{any()})", getCursor(), mi.getCoordinates().replace(), mi.getArguments().get( 0 ), mi.getArguments().get( 1 ) )
161153
.withPrefix(mi.getPrefix());
162154
}
163155
if (getWindowOpacityPatternMethod.matches(mi)) {
164156
maybeRemoveImport(mi.getMethodType().getDeclaringType().getFullyQualifiedName());
165-
return JavaTemplate.builder("#{any()}.getOpacity()")
166-
.build()
167-
.apply(getCursor(), mi.getCoordinates().replace(), mi.getArguments().get(0))
157+
return JavaTemplate.apply( "#{any()}.getOpacity()", getCursor(), mi.getCoordinates().replace(), mi.getArguments().get( 0 ) )
168158
.withPrefix(mi.getPrefix());
169159
}
170160
if (getWindowShapePatternMethod.matches(mi)) {
171161
maybeRemoveImport(mi.getMethodType().getDeclaringType().getFullyQualifiedName());
172-
return JavaTemplate.builder("#{any()}.getShape()")
173-
.build()
174-
.apply(getCursor(), mi.getCoordinates().replace(), mi.getArguments().get(0))
162+
return JavaTemplate.apply( "#{any()}.getShape()", getCursor(), mi.getCoordinates().replace(), mi.getArguments().get( 0 ) )
175163
.withPrefix(mi.getPrefix());
176164
}
177165
if (setComponentMixingCutoutShapePatternMethod.matches(mi)) {
178166
maybeRemoveImport(mi.getMethodType().getDeclaringType().getFullyQualifiedName());
179-
return JavaTemplate.builder("#{any()}.setMixingCutoutShape(#{any()})")
180-
.build()
181-
.apply(getCursor(), mi.getCoordinates().replace(),
182-
mi.getArguments().get(0),
183-
mi.getArguments().get(1))
167+
return JavaTemplate.apply( "#{any()}.setMixingCutoutShape(#{any()})", getCursor(), mi.getCoordinates().replace(), mi.getArguments().get( 0 ), mi.getArguments().get( 1 ) )
184168
.withPrefix(mi.getPrefix());
185169
}
186170
return mi;

src/main/java/org/openrewrite/java/migrate/guava/NoGuavaFunctionsCompose.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
5454
maybeRemoveImport("com.google.common.base.Functions");
5555
maybeAddImport("java.util.function.Function");
5656

57-
return JavaTemplate.builder("#{any(java.util.function.Function)}.compose(#{any(java.util.function.Function)})")
58-
.build()
59-
.apply(getCursor(),
60-
method.getCoordinates().replace(),
61-
method.getArguments().get(0),
62-
method.getArguments().get(1));
57+
return JavaTemplate.apply( "#{any(java.util.function.Function)}.compose(#{any(java.util.function.Function)})", getCursor(), method.getCoordinates().replace(), method.getArguments().get( 0 ), method.getArguments().get( 1 ) );
6358
}
6459
return super.visitMethodInvocation(method, ctx);
6560
}

src/main/java/org/openrewrite/java/migrate/guava/NoGuavaIterablesAll.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
5454
maybeAddImport("java.util.function.Predicate");
5555

5656
if (TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
57-
return JavaTemplate.builder("#{any(java.util.Collection)}.stream().allMatch(#{any(java.util.function.Predicate)})")
58-
.build()
59-
.apply(getCursor(),
60-
method.getCoordinates().replace(),
61-
method.getArguments().get(0),
62-
method.getArguments().get(1));
57+
return JavaTemplate.apply( "#{any(java.util.Collection)}.stream().allMatch(#{any(java.util.function.Predicate)})", getCursor(), method.getCoordinates().replace(), method.getArguments().get( 0 ), method.getArguments().get( 1 ) );
6358
}
6459
return method;
6560
}

src/main/java/org/openrewrite/java/migrate/guava/NoGuavaIterablesAnyFilter.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
5656
maybeAddImport("java.util.function.Predicate");
5757

5858
if (TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
59-
return JavaTemplate.builder("#{any(java.util.Collection)}.stream().anyMatch(#{any(java.util.function.Predicate)})")
60-
.build()
61-
.apply(getCursor(),
62-
method.getCoordinates().replace(),
63-
method.getArguments().get(0),
64-
method.getArguments().get(1));
59+
return JavaTemplate.apply( "#{any(java.util.Collection)}.stream().anyMatch(#{any(java.util.function.Predicate)})", getCursor(), method.getCoordinates().replace(), method.getArguments().get( 0 ), method.getArguments().get( 1 ) );
6560
}
6661
return method;
6762
}
@@ -71,12 +66,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
7166
maybeAddImport("java.util.function.Predicate");
7267

7368
if (TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
74-
return JavaTemplate.builder("#{any(java.util.Collection)}.stream().filter(#{any(java.util.function.Predicate)}).toList()")
75-
.build()
76-
.apply(getCursor(),
77-
method.getCoordinates().replace(),
78-
method.getArguments().get(0),
79-
method.getArguments().get(1));
69+
return JavaTemplate.apply( "#{any(java.util.Collection)}.stream().filter(#{any(java.util.function.Predicate)}).toList()", getCursor(), method.getCoordinates().replace(), method.getArguments().get( 0 ), method.getArguments().get( 1 ) );
8070
}
8171
return method;
8272
}

src/main/java/org/openrewrite/java/migrate/guava/PreferJavaUtilOptionalOrElseNull.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
6262
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
6363
if (OPTIONAL_OR_NULL_MATCHER.matches(mi)) {
6464
mi = mi.withName(mi.getName().withSimpleName("orElse"));
65-
mi = JavaTemplate.builder("null")
66-
.build()
67-
.apply(updateCursor(mi), mi.getCoordinates().replaceArguments());
65+
mi = JavaTemplate.apply( "null", updateCursor( mi ), mi.getCoordinates().replaceArguments() );
6866
}
6967
return mi;
7068
}

src/main/java/org/openrewrite/java/migrate/io/ReplaceSystemOutWithIOPrint.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
5656
}
5757
String methodName = m.getName().getSimpleName();
5858
return m.getArguments().isEmpty() ?
59-
JavaTemplate.builder("IO.#{}()").build()
60-
.apply(getCursor(), m.getCoordinates().replace(), methodName) :
61-
JavaTemplate.builder("IO.#{}(#{any()})").build()
62-
.apply(getCursor(), m.getCoordinates().replace(), methodName, m.getArguments().get(0));
59+
JavaTemplate.apply( "IO.#{}()", getCursor(), m.getCoordinates().replace(), methodName ) :
60+
JavaTemplate.apply( "IO.#{}(#{any()})", getCursor(), m.getCoordinates().replace(), methodName, m.getArguments().get( 0 ) );
6361
}
6462

6563
private boolean isSystemOutMethod(J.MethodInvocation mi) {

src/main/java/org/openrewrite/java/migrate/jakarta/UpdateAddAnnotatedTypes.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
4040
@Override
4141
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
4242
if (methodInputPattern.matches(method)) {
43-
return JavaTemplate.builder("#{any(jakarta.enterprise.inject.spi.AnnotatedType)}, null")
44-
.build()
45-
.apply(updateCursor(method),
46-
method.getCoordinates().replaceArguments(),
47-
method.getArguments().get(0));
43+
return JavaTemplate.apply( "#{any(jakarta.enterprise.inject.spi.AnnotatedType)}, null", updateCursor( method ), method.getCoordinates().replaceArguments(), method.getArguments().get( 0 ) );
4844
}
4945
return super.visitMethodInvocation(method, ctx);
5046
}

src/main/java/org/openrewrite/java/migrate/net/MigrateMulticastSocketSetTTLToSetTimeToLive.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
5050
J.MethodInvocation m = method;
5151
if (MATCHER.matches(m)) {
5252
m = m.withName(m.getName().withSimpleName("setTimeToLive"));
53-
m = JavaTemplate.builder("Byte.valueOf(#{any(byte)}).intValue()")
54-
.build()
55-
.apply(updateCursor(m), m.getCoordinates().replaceArguments(), m.getArguments().get(0));
53+
m = JavaTemplate.apply( "Byte.valueOf(#{any(byte)}).intValue()", updateCursor( m ), m.getCoordinates().replaceArguments(), m.getArguments().get( 0 ) );
5654
}
5755
return super.visitMethodInvocation(m, ctx);
5856
}

src/main/java/org/openrewrite/java/migrate/sql/MigrateDriverManagerSetLogStream.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
5050
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
5151
if (METHOD_MATCHER.matches(m)) {
5252
m = m.withName(m.getName().withSimpleName("setLogWriter"));
53-
m = JavaTemplate.builder("new java.io.PrintWriter(#{any(java.io.PrintStream)})")
54-
.build()
55-
.apply(updateCursor(m), m.getCoordinates().replaceArguments(), m.getArguments().get(0));
53+
m = JavaTemplate.apply( "new java.io.PrintWriter(#{any(java.io.PrintStream)})", updateCursor( m ), m.getCoordinates().replaceArguments(), m.getArguments().get( 0 ) );
5654
}
5755
return m;
5856
}

0 commit comments

Comments
 (0)