Skip to content

Commit dd77a80

Browse files
authored
Inline JavaTemplate fields at their usage sites (#1036)
Remove stored JavaTemplate fields in favor of inline builder chains, matching the recommended pattern used throughout the rest of the codebase.
1 parent 926ea39 commit dd77a80

4 files changed

Lines changed: 35 additions & 42 deletions

File tree

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,15 @@ public class NoGuavaAtomicsNewReference extends Recipe {
4545
@Override
4646
public TreeVisitor<?, ExecutionContext> getVisitor() {
4747
return Preconditions.check(new UsesMethod<>(NEW_ATOMIC_REFERENCE), new JavaVisitor<ExecutionContext>() {
48-
private final JavaTemplate newAtomicReference = JavaTemplate.builder("new AtomicReference<>()")
49-
.imports("java.util.concurrent.atomic.AtomicReference")
50-
.build();
51-
5248
@Override
5349
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
5450
if (NEW_ATOMIC_REFERENCE.matches(method)) {
5551
maybeRemoveImport("com.google.common.util.concurrent.Atomics");
5652
maybeAddImport("java.util.concurrent.atomic.AtomicReference");
57-
return ((J.NewClass) newAtomicReference.apply(getCursor(), method.getCoordinates().replace()))
53+
return ((J.NewClass) JavaTemplate.builder("new AtomicReference<>()")
54+
.imports("java.util.concurrent.atomic.AtomicReference")
55+
.build()
56+
.apply(getCursor(), method.getCoordinates().replace()))
5857
.withArguments(method.getArguments());
5958
}
6059
return super.visitMethodInvocation(method, ctx);

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

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,40 +51,37 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5151
new UsesMethod<>(NEW_ARRAY_LIST),
5252
new UsesMethod<>(NEW_ARRAY_LIST_ITERABLE),
5353
new UsesMethod<>(NEW_ARRAY_LIST_CAPACITY)), new JavaVisitor<ExecutionContext>() {
54-
private final JavaTemplate newArrayList = JavaTemplate.builder("new ArrayList<>()")
55-
.contextSensitive()
56-
.imports("java.util.ArrayList")
57-
.build();
58-
59-
private final JavaTemplate newArrayListCollection = JavaTemplate.builder("new ArrayList<>(#{any(java.util.Collection)})")
60-
.contextSensitive()
61-
.imports("java.util.ArrayList")
62-
.build();
63-
64-
private final JavaTemplate newArrayListCapacity = JavaTemplate.builder("new ArrayList<>(#{any(int)})")
65-
.contextSensitive()
66-
.imports("java.util.ArrayList")
67-
.build();
68-
6954
@Override
7055
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
7156
if (NEW_ARRAY_LIST.matches(method)) {
7257
maybeRemoveImport("com.google.common.collect.Lists");
7358
maybeAddImport("java.util.ArrayList");
74-
return newArrayList.apply(getCursor(), method.getCoordinates().replace());
59+
return JavaTemplate.builder("new ArrayList<>()")
60+
.contextSensitive()
61+
.imports("java.util.ArrayList")
62+
.build()
63+
.apply(getCursor(), method.getCoordinates().replace());
7564
}
7665
if (NEW_ARRAY_LIST_ITERABLE.matches(method) && method.getArguments().size() == 1 &&
7766
TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
7867
maybeRemoveImport("com.google.common.collect.Lists");
7968
maybeAddImport("java.util.ArrayList");
80-
return newArrayListCollection.apply(getCursor(), method.getCoordinates().replace(),
81-
method.getArguments().get(0));
69+
return JavaTemplate.builder("new ArrayList<>(#{any(java.util.Collection)})")
70+
.contextSensitive()
71+
.imports("java.util.ArrayList")
72+
.build()
73+
.apply(getCursor(), method.getCoordinates().replace(),
74+
method.getArguments().get(0));
8275
}
8376
if (NEW_ARRAY_LIST_CAPACITY.matches(method)) {
8477
maybeRemoveImport("com.google.common.collect.Lists");
8578
maybeAddImport("java.util.ArrayList");
86-
return newArrayListCapacity.apply(getCursor(), method.getCoordinates().replace(),
87-
method.getArguments().get(0));
79+
return JavaTemplate.builder("new ArrayList<>(#{any(int)})")
80+
.contextSensitive()
81+
.imports("java.util.ArrayList")
82+
.build()
83+
.apply(getCursor(), method.getCoordinates().replace(),
84+
method.getArguments().get(0));
8885
}
8986
return super.visitMethodInvocation(method, ctx);
9087
}

src/main/java/org/openrewrite/java/migrate/javax/AnnotateTypesVisitor.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,10 @@
2727
public class AnnotateTypesVisitor extends JavaIsoVisitor<Set<String>> {
2828
private final String annotationToBeAdded;
2929
private final AnnotationMatcher annotationMatcher;
30-
private final JavaTemplate template;
3130

3231
public AnnotateTypesVisitor(String annotationToBeAdded) {
3332
this.annotationToBeAdded = annotationToBeAdded;
34-
String[] split = this.annotationToBeAdded.split("\\.");
35-
String className = split[split.length - 1];
36-
String packageName = this.annotationToBeAdded.substring(0, this.annotationToBeAdded.lastIndexOf("."));
3733
this.annotationMatcher = new AnnotationMatcher("@" + this.annotationToBeAdded);
38-
String interfaceAsString = String.format("package %s\npublic @interface %s {}", packageName, className);
39-
//noinspection LanguageMismatch
40-
this.template = JavaTemplate.builder("@" + className)
41-
.imports(this.annotationToBeAdded)
42-
.javaParser(JavaParser.fromJavaVersion().dependsOn(interfaceAsString))
43-
.build();
4434
}
4535

4636
@Override
@@ -49,7 +39,16 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Se
4939
if (cd.getType() != null && injectedTypes.contains(cd.getType().getFullyQualifiedName()) &&
5040
cd.getLeadingAnnotations().stream().noneMatch(annotationMatcher::matches)) {
5141
maybeAddImport(annotationToBeAdded);
52-
cd = template.apply(getCursor(), cd.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)));
42+
String[] split = annotationToBeAdded.split("\\.");
43+
String className = split[split.length - 1];
44+
String packageName = annotationToBeAdded.substring(0, annotationToBeAdded.lastIndexOf("."));
45+
String interfaceAsString = String.format("package %s\npublic @interface %s {}", packageName, className);
46+
//noinspection LanguageMismatch
47+
cd = JavaTemplate.builder("@" + className)
48+
.imports(annotationToBeAdded)
49+
.javaParser(JavaParser.fromJavaVersion().dependsOn(interfaceAsString))
50+
.build()
51+
.apply(getCursor(), cd.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)));
5352
cd = maybeAutoFormat(classDecl, cd, cd.getName(), injectedTypes, getCursor());
5453
}
5554
return cd;

src/main/java/org/openrewrite/java/migrate/lombok/LombokValueToRecord.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,6 @@ private boolean hasMemberVariableAssignments(List<J.VariableDeclarations> member
214214

215215
@RequiredArgsConstructor
216216
private static class LombokValueToRecordVisitor extends JavaIsoVisitor<ExecutionContext> {
217-
private static final JavaTemplate TO_STRING_TEMPLATE = JavaTemplate
218-
.builder("@Override public String toString() { return \"#{}(\" +\n#{}\n\")\"; }")
219-
.contextSensitive()
220-
.build();
221-
222217
private static final String TO_STRING_MEMBER_LINE_PATTERN = "\"%s=\" + %s +";
223218
private static final String TO_STRING_MEMBER_DELIMITER = "\", \" +\n";
224219
private static final String STANDARD_GETTER_PREFIX = "get";
@@ -319,7 +314,10 @@ private static List<Statement> mapToConstructorArguments(List<J.VariableDeclarat
319314

320315
private J.ClassDeclaration addExactToStringMethod(J.ClassDeclaration classDeclaration,
321316
List<J.VariableDeclarations> memberVariables) {
322-
return classDeclaration.withBody(TO_STRING_TEMPLATE
317+
return classDeclaration.withBody(JavaTemplate
318+
.builder("@Override public String toString() { return \"#{}(\" +\n#{}\n\")\"; }")
319+
.contextSensitive()
320+
.build()
323321
.apply(new Cursor(getCursor(), classDeclaration.getBody()),
324322
classDeclaration.getBody().getCoordinates().lastStatement(),
325323
classDeclaration.getSimpleName(),

0 commit comments

Comments
 (0)