Skip to content

Commit 91f3428

Browse files
committed
wip
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent edb20f3 commit 91f3428

File tree

5 files changed

+173
-40
lines changed

5 files changed

+173
-40
lines changed

docs/content/en/blog/releases/v5-3-release.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ checks.
220220

221221
`reconciliationFinished(..)` is extended with `RetryInfo`. `monitorSizeOf(..)` is removed.
222222

223+
### `ResourceAction` relocated
224+
225+
`ResourceAction` in `io.javaoperatorsdk.operator.processing.event.source.controller` has been
226+
removed. Use `io.javaoperatorsdk.operator.processing.event.source.ResourceAction` instead.
227+
223228
See the full [migration guide](/docs/migration/v5-3-migration) for details.
224229

225230
## Getting Started

docs/content/en/docs/migration/v5-3-migration.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,10 @@ The following table shows the relevant method renames:
7272

7373
Other changes:
7474
- `reconciliationFinished(..)` method is extended with `RetryInfo`
75-
- `monitorSizeOf(..)` method is removed.
75+
- `monitorSizeOf(..)` method is removed.
76+
77+
## ResourceAction relocation
78+
79+
The `ResourceAction` enum has been removed from
80+
`io.javaoperatorsdk.operator.processing.event.source.controller` use the one in package
81+
`io.javaoperatorsdk.operator.processing.event.source.ResourceAction`; thus update your imports accordingly.

migration/src/main/java/io/javaoperatorsdk/operator/migration/RemoveMethodDeclaration.java

Lines changed: 101 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,58 +15,136 @@
1515
*/
1616
package io.javaoperatorsdk.operator.migration;
1717

18+
import java.util.Objects;
19+
1820
import org.openrewrite.ExecutionContext;
21+
import org.openrewrite.NlsRewrite;
1922
import org.openrewrite.Option;
2023
import org.openrewrite.Recipe;
2124
import org.openrewrite.TreeVisitor;
2225
import org.openrewrite.java.JavaIsoVisitor;
23-
import org.openrewrite.java.MethodMatcher;
2426
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;
2828

2929
public class RemoveMethodDeclaration extends Recipe {
3030

3131
@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;
4142

4243
@Override
4344
public String getDisplayName() {
44-
return "Remove method declaration";
45+
return "Remove obsolete method from implementing classes";
4546
}
4647

4748
@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";
5051
}
5152

5253
@Override
5354
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+
5685
@Override
5786
public J.MethodDeclaration visitMethodDeclaration(
5887
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);
6295
}
63-
var classDecl = getCursor().firstEnclosing(J.ClassDeclaration.class);
64-
if (classDecl != null && matcher.matches(method, classDecl)) {
96+
97+
if (typeMatchesOrImplements(classDecl.getType())) {
6598
//noinspection DataFlowIssue
6699
return null;
67100
}
101+
68102
return super.visitMethodDeclaration(method, ctx);
69103
}
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+
}
70118
};
71119
}
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+
}
72150
}

migration/src/main/resources/META-INF/rewrite/v5-3-migration.yml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ recipeList:
2626
- io.javaoperatorsdk.operator.migration.RenameJUnitModule
2727
- io.javaoperatorsdk.operator.migration.MetricsMethodRenames
2828
- io.javaoperatorsdk.operator.migration.RemoveMonitorSizeOf
29+
- io.javaoperatorsdk.operator.migration.RelocateResourceAction
2930
---
3031
type: specs.openrewrite.org/v1beta/recipe
3132
name: io.javaoperatorsdk.operator.migration.UpgradeJOSDKVersion
@@ -98,11 +99,18 @@ recipeList:
9899
---
99100
type: specs.openrewrite.org/v1beta/recipe
100101
name: io.javaoperatorsdk.operator.migration.RemoveMonitorSizeOf
101-
displayName: Remove monitorSizeOf method
102-
description: >-
103-
Removes the monitorSizeOf method declarations and invocations which were removed in v5.3.
102+
displayName: Remove MonitorSizeOf
104103
recipeList:
105-
- org.openrewrite.java.RemoveMethodInvocations:
106-
methodPattern: "io.javaoperatorsdk.operator.api.monitoring.Metrics monitorSizeOf(..)"
107104
- io.javaoperatorsdk.operator.migration.RemoveMethodDeclaration:
108-
methodPattern: "io.javaoperatorsdk.operator.api.monitoring.Metrics monitorSizeOf(..)"
105+
interfaceName: io.javaoperatorsdk.operator.api.monitoring.Metrics
106+
methodName: monitorSizeOf
107+
---
108+
type: specs.openrewrite.org/v1beta/recipe
109+
name: io.javaoperatorsdk.operator.migration.RelocateResourceAction
110+
displayName: Relocate ResourceAction class
111+
description: >-
112+
Moves ResourceAction import from the controller sub-package to the event source package.
113+
recipeList:
114+
- org.openrewrite.java.ChangeType:
115+
oldFullyQualifiedTypeName: io.javaoperatorsdk.operator.processing.event.source.controller.ResourceAction
116+
newFullyQualifiedTypeName: io.javaoperatorsdk.operator.processing.event.source.ResourceAction

migration/src/test/java/io/javaoperatorsdk/operator/migration/V53MigrationTest.java

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public void reconciliationSubmitted(Object resource, Object retryInfo, Map<Strin
178178
@Test
179179
void removesMonitorSizeOfFromImplementationWithGenerics() {
180180
rewriteRun(
181-
// Stub for the Metrics interface with generic monitorSizeOf
181+
// Stub for the Metrics interface (unchanged, from JOSDK library)
182182
// language=java
183183
java(
184184
"""
@@ -190,15 +190,6 @@ public interface Metrics {
190190
default void eventReceived(Object event, Map<String, Object> metadata) {}
191191
default <T extends Map<?, ?>> T monitorSizeOf(T map, String name) { return map; }
192192
}
193-
""",
194-
"""
195-
package io.javaoperatorsdk.operator.api.monitoring;
196-
197-
import java.util.Map;
198-
199-
public interface Metrics {
200-
default void eventReceived(Object event, Map<String, Object> metadata) {}
201-
}
202193
"""),
203194
// Implementation that overrides monitorSizeOf with generic signature
204195
// language=java
@@ -287,4 +278,49 @@ default void reconciliationFinished(Object resource, RetryInfo retryInfo, Map<St
287278
}
288279
"""));
289280
}
281+
282+
@Test
283+
void relocatesResourceActionImport() {
284+
rewriteRun(
285+
// Stub for the old ResourceAction location
286+
// language=java
287+
java(
288+
"""
289+
package io.javaoperatorsdk.operator.processing.event.source.controller;
290+
291+
public class ResourceAction {
292+
}
293+
""",
294+
"""
295+
package io.javaoperatorsdk.operator.processing.event.source;
296+
297+
public class ResourceAction {
298+
}
299+
"""),
300+
// Class that imports ResourceAction from the old package
301+
// language=java
302+
java(
303+
"""
304+
package com.example;
305+
306+
import io.javaoperatorsdk.operator.processing.event.source.controller.ResourceAction;
307+
308+
public class MyHandler {
309+
public void handle(ResourceAction action) {
310+
System.out.println(action);
311+
}
312+
}
313+
""",
314+
"""
315+
package com.example;
316+
317+
import io.javaoperatorsdk.operator.processing.event.source.ResourceAction;
318+
319+
public class MyHandler {
320+
public void handle(ResourceAction action) {
321+
System.out.println(action);
322+
}
323+
}
324+
"""));
325+
}
290326
}

0 commit comments

Comments
 (0)