|
15 | 15 | */ |
16 | 16 | package io.javaoperatorsdk.operator.migration; |
17 | 17 |
|
| 18 | +import java.util.Objects; |
| 19 | + |
18 | 20 | import org.openrewrite.ExecutionContext; |
| 21 | +import org.openrewrite.NlsRewrite; |
19 | 22 | import org.openrewrite.Option; |
20 | 23 | import org.openrewrite.Recipe; |
21 | 24 | import org.openrewrite.TreeVisitor; |
22 | 25 | import org.openrewrite.java.JavaIsoVisitor; |
23 | | -import org.openrewrite.java.MethodMatcher; |
24 | 26 | import org.openrewrite.java.tree.J; |
25 | | - |
26 | | -import com.fasterxml.jackson.annotation.JsonCreator; |
27 | | -import com.fasterxml.jackson.annotation.JsonProperty; |
| 27 | +import org.openrewrite.java.tree.JavaType; |
28 | 28 |
|
29 | 29 | public class RemoveMethodDeclaration extends Recipe { |
30 | 30 |
|
31 | 31 | @Option( |
32 | | - displayName = "Method pattern", |
33 | | - description = "A method pattern used to find matching method declarations.", |
34 | | - example = "com.example.Foo bar(..)") |
35 | | - private final String methodPattern; |
36 | | - |
37 | | - @JsonCreator |
38 | | - public RemoveMethodDeclaration(@JsonProperty("methodPattern") String methodPattern) { |
39 | | - this.methodPattern = methodPattern; |
40 | | - } |
| 32 | + displayName = "Interface name", |
| 33 | + description = "Fully qualified or simple name of the interface.", |
| 34 | + example = "com.example.YourInterface") |
| 35 | + String interfaceName; |
| 36 | + |
| 37 | + @Option( |
| 38 | + displayName = "Method name", |
| 39 | + description = "Name of the method to remove.", |
| 40 | + example = "removedMethod") |
| 41 | + String methodName; |
41 | 42 |
|
42 | 43 | @Override |
43 | 44 | public String getDisplayName() { |
44 | | - return "Remove method declaration"; |
| 45 | + return "Remove obsolete method from implementing classes"; |
45 | 46 | } |
46 | 47 |
|
47 | 48 | @Override |
48 | | - public String getDescription() { |
49 | | - return "Removes method declarations matching the given method pattern."; |
| 49 | + public @NlsRewrite.Description String getDescription() { |
| 50 | + return "Remove obsolete method from implementing classes"; |
50 | 51 | } |
51 | 52 |
|
52 | 53 | @Override |
53 | 54 | public TreeVisitor<?, ExecutionContext> getVisitor() { |
54 | | - var matcher = new MethodMatcher(methodPattern, true); |
55 | | - return new JavaIsoVisitor<>() { |
| 55 | + return new JavaIsoVisitor<ExecutionContext>() { |
| 56 | + |
| 57 | + @Override |
| 58 | + public J.ClassDeclaration visitClassDeclaration( |
| 59 | + J.ClassDeclaration classDecl, ExecutionContext ctx) { |
| 60 | + J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx); |
| 61 | + |
| 62 | + if (cd.getType() == null || !typeMatchesOrImplements(cd.getType())) { |
| 63 | + return cd; |
| 64 | + } |
| 65 | + |
| 66 | + // Mutate the type info in place to remove the method from the declared methods list, |
| 67 | + // so all AST nodes sharing this type reference stay consistent. |
| 68 | + var type = cd.getType(); |
| 69 | + if (type instanceof JavaType.Class classType) { |
| 70 | + var updatedMethods = |
| 71 | + classType.getMethods().stream().filter(m -> !m.getName().equals(methodName)).toList(); |
| 72 | + classType.unsafeSet( |
| 73 | + classType.getTypeParameters(), |
| 74 | + classType.getSupertype(), |
| 75 | + classType.getOwningClass(), |
| 76 | + classType.getAnnotations(), |
| 77 | + classType.getInterfaces(), |
| 78 | + classType.getMembers(), |
| 79 | + updatedMethods); |
| 80 | + } |
| 81 | + |
| 82 | + return cd; |
| 83 | + } |
| 84 | + |
56 | 85 | @Override |
57 | 86 | public J.MethodDeclaration visitMethodDeclaration( |
58 | 87 | J.MethodDeclaration method, ExecutionContext ctx) { |
59 | | - if (method.getMethodType() != null && matcher.matches(method.getMethodType())) { |
60 | | - //noinspection DataFlowIssue |
61 | | - return null; |
| 88 | + if (!method.getSimpleName().equals(methodName)) { |
| 89 | + return super.visitMethodDeclaration(method, ctx); |
| 90 | + } |
| 91 | + |
| 92 | + J.ClassDeclaration classDecl = getCursor().firstEnclosing(J.ClassDeclaration.class); |
| 93 | + if (classDecl == null || classDecl.getType() == null) { |
| 94 | + return super.visitMethodDeclaration(method, ctx); |
62 | 95 | } |
63 | | - var classDecl = getCursor().firstEnclosing(J.ClassDeclaration.class); |
64 | | - if (classDecl != null && matcher.matches(method, classDecl)) { |
| 96 | + |
| 97 | + if (typeMatchesOrImplements(classDecl.getType())) { |
65 | 98 | //noinspection DataFlowIssue |
66 | 99 | return null; |
67 | 100 | } |
| 101 | + |
68 | 102 | return super.visitMethodDeclaration(method, ctx); |
69 | 103 | } |
| 104 | + |
| 105 | + private boolean typeMatchesOrImplements(JavaType.FullyQualified type) { |
| 106 | + for (var iface : type.getInterfaces()) { |
| 107 | + if (iface.getFullyQualifiedName().equals(interfaceName) |
| 108 | + || typeMatchesOrImplements(iface)) { |
| 109 | + return true; |
| 110 | + } |
| 111 | + } |
| 112 | + var supertype = type.getSupertype(); |
| 113 | + if (supertype != null && !supertype.getFullyQualifiedName().equals("java.lang.Object")) { |
| 114 | + return typeMatchesOrImplements(supertype); |
| 115 | + } |
| 116 | + return false; |
| 117 | + } |
70 | 118 | }; |
71 | 119 | } |
| 120 | + |
| 121 | + public String getInterfaceName() { |
| 122 | + return interfaceName; |
| 123 | + } |
| 124 | + |
| 125 | + public void setInterfaceName(String interfaceName) { |
| 126 | + this.interfaceName = interfaceName; |
| 127 | + } |
| 128 | + |
| 129 | + public String getMethodName() { |
| 130 | + return methodName; |
| 131 | + } |
| 132 | + |
| 133 | + public void setMethodName(String methodName) { |
| 134 | + this.methodName = methodName; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public boolean equals(Object o) { |
| 139 | + if (o == null || getClass() != o.getClass()) return false; |
| 140 | + if (!super.equals(o)) return false; |
| 141 | + RemoveMethodDeclaration that = (RemoveMethodDeclaration) o; |
| 142 | + return Objects.equals(interfaceName, that.interfaceName) |
| 143 | + && Objects.equals(methodName, that.methodName); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public int hashCode() { |
| 148 | + return Objects.hash(super.hashCode(), interfaceName, methodName); |
| 149 | + } |
72 | 150 | } |
0 commit comments