|
15 | 15 | */ |
16 | 16 | package org.openrewrite.java.migrate.lang; |
17 | 17 |
|
18 | | -import lombok.Getter; |
| 18 | +import lombok.EqualsAndHashCode; |
| 19 | +import lombok.Value; |
| 20 | +import org.jspecify.annotations.Nullable; |
19 | 21 | import org.openrewrite.ExecutionContext; |
20 | 22 | import org.openrewrite.Preconditions; |
21 | | -import org.openrewrite.Recipe; |
| 23 | +import org.openrewrite.ScanningRecipe; |
22 | 24 | import org.openrewrite.TreeVisitor; |
23 | 25 | import org.openrewrite.java.JavaIsoVisitor; |
24 | 26 | import org.openrewrite.java.MethodMatcher; |
|
29 | 31 | import org.openrewrite.java.tree.TypeUtils; |
30 | 32 | import org.openrewrite.staticanalysis.VariableReferences; |
31 | 33 |
|
| 34 | +import java.util.HashSet; |
32 | 35 | import java.util.List; |
33 | | -import java.util.concurrent.atomic.AtomicBoolean; |
| 36 | +import java.util.Set; |
34 | 37 |
|
35 | 38 | import static java.util.Collections.emptyList; |
36 | 39 | import static java.util.stream.Collectors.toList; |
37 | 40 |
|
38 | | -public class MigrateMainMethodToInstanceMain extends Recipe { |
| 41 | +@Value |
| 42 | +@EqualsAndHashCode(callSuper = false) |
| 43 | +public class MigrateMainMethodToInstanceMain extends ScanningRecipe<Set<JavaType.FullyQualified>> { |
39 | 44 |
|
40 | 45 | private static final MethodMatcher MAIN_METHOD_MATCHER = new MethodMatcher("*..* main(String[])", false); |
41 | 46 |
|
42 | | - @Getter |
43 | | - final String displayName = "Migrate `public static void main(String[] args)` to instance `void main()`"; |
| 47 | + String displayName = "Migrate `public static void main(String[] args)` to instance `void main()`"; |
44 | 48 |
|
45 | | - @Getter |
46 | | - final String description = "Migrate `public static void main(String[] args)` method to instance `void main()` method when the `args` parameter is unused, as supported by JEP 512 in Java 25+."; |
| 49 | + String description = "Migrate `public static void main(String[] args)` method to instance `void main()` method when the `args` parameter is unused, as supported by JEP 512 in Java 25+."; |
47 | 50 |
|
48 | 51 | @Override |
49 | | - public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 52 | + public Set<JavaType.FullyQualified> getInitialValue(ExecutionContext ctx) { |
| 53 | + return new HashSet<>(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public TreeVisitor<?, ExecutionContext> getScanner(Set<JavaType.FullyQualified> referencedMains) { |
| 58 | + return new JavaIsoVisitor<ExecutionContext>() { |
| 59 | + @Override |
| 60 | + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { |
| 61 | + if (MAIN_METHOD_MATCHER.matches(method.getMethodType())) { |
| 62 | + recordDeclaringType(method.getMethodType()); |
| 63 | + } |
| 64 | + return super.visitMethodInvocation(method, ctx); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public J.MemberReference visitMemberReference(J.MemberReference memberRef, ExecutionContext ctx) { |
| 69 | + if (MAIN_METHOD_MATCHER.matches(memberRef.getMethodType())) { |
| 70 | + recordDeclaringType(memberRef.getMethodType()); |
| 71 | + } |
| 72 | + return super.visitMemberReference(memberRef, ctx); |
| 73 | + } |
| 74 | + |
| 75 | + private void recordDeclaringType(JavaType.@Nullable Method methodType) { |
| 76 | + if (methodType != null && methodType.getDeclaringType() != null) { |
| 77 | + referencedMains.add(methodType.getDeclaringType()); |
| 78 | + } |
| 79 | + } |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public TreeVisitor<?, ExecutionContext> getVisitor(Set<JavaType.FullyQualified> referencedMains) { |
50 | 85 | TreeVisitor<?, ExecutionContext> preconditions = Preconditions.and( |
51 | 86 | new UsesJavaVersion<>(25), |
52 | 87 | new DeclaresMethod<>(MAIN_METHOD_MATCHER) |
@@ -78,7 +113,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex |
78 | 113 | // Do not migrate in any of these cases |
79 | 114 | if (hasSpringBootApplicationAnnotation(enclosingClass) || |
80 | 115 | !hasNoArgConstructor(enclosingClass) || |
81 | | - isMainMethodReferenced(md)) { |
| 116 | + isMainMethodReferenced(enclosingClass)) { |
82 | 117 | return md; |
83 | 118 | } |
84 | 119 |
|
@@ -115,25 +150,17 @@ private boolean hasNoArgConstructor(J.ClassDeclaration classDecl) { |
115 | 150 | ctor.hasModifier(J.Modifier.Type.Public)); |
116 | 151 | } |
117 | 152 |
|
118 | | - private boolean isMainMethodReferenced(J.MethodDeclaration mainMethod) { |
119 | | - J.CompilationUnit cu = getCursor().firstEnclosing(J.CompilationUnit.class); |
120 | | - if (cu == null) { |
| 153 | + private boolean isMainMethodReferenced(J.ClassDeclaration classDecl) { |
| 154 | + JavaType.FullyQualified type = classDecl.getType(); |
| 155 | + if (type == null) { |
121 | 156 | return false; |
122 | 157 | } |
123 | | - |
124 | | - // XXX Only picks up references in the same compilation unit; convert to scanning recipe if needed |
125 | | - return new JavaIsoVisitor<AtomicBoolean>() { |
126 | | - @Override |
127 | | - public J.MemberReference visitMemberReference(J.MemberReference memberRef, AtomicBoolean referenced) { |
128 | | - // Check if this is a reference to the main method |
129 | | - if ("main".equals(memberRef.getReference().getSimpleName()) && |
130 | | - memberRef.getMethodType() != null && |
131 | | - TypeUtils.isOfType(memberRef.getMethodType(), mainMethod.getMethodType())) { |
132 | | - referenced.set(true); |
133 | | - } |
134 | | - return super.visitMemberReference(memberRef, referenced); |
| 158 | + for (JavaType.FullyQualified referenced : referencedMains) { |
| 159 | + if (TypeUtils.isOfType(referenced, type)) { |
| 160 | + return true; |
135 | 161 | } |
136 | | - }.reduce(cu, new AtomicBoolean()).get(); |
| 162 | + } |
| 163 | + return false; |
137 | 164 | } |
138 | 165 | }); |
139 | 166 | } |
|
0 commit comments