-
Notifications
You must be signed in to change notification settings - Fork 236
feat: add OpenRewrite module for migrations and recipe for 5.3.0 #3236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6d55635
OpenRewrite migration
csviri 49dbbd6
wip
csviri 9eedfaf
wip
csviri f3981b9
wip
csviri c6fca77
wip
csviri 13222ab
wip
csviri 1fb20c6
wip
csviri 77e94fb
wip
csviri edb20f3
wip
csviri 91f3428
wip
csviri 8306e08
wip
csviri 96f123c
wip
csviri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
|
|
||
| Copyright Java Operator SDK Authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| --> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>io.javaoperatorsdk</groupId> | ||
| <artifactId>java-operator-sdk</artifactId> | ||
| <version>5.3.1-SNAPSHOT</version> | ||
| </parent> | ||
|
|
||
| <artifactId>migration</artifactId> | ||
| <name>Operator SDK - Migration Recipes</name> | ||
| <description>OpenRewrite migration recipes for Java Operator SDK</description> | ||
|
|
||
| <properties> | ||
| <openrewrite.version>8.46.1</openrewrite.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.openrewrite</groupId> | ||
| <artifactId>rewrite-java</artifactId> | ||
| <version>${openrewrite.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.openrewrite</groupId> | ||
| <artifactId>rewrite-maven</artifactId> | ||
| <version>${openrewrite.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.openrewrite</groupId> | ||
| <artifactId>rewrite-test</artifactId> | ||
| <version>${openrewrite.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.openrewrite</groupId> | ||
| <artifactId>rewrite-java-17</artifactId> | ||
| <version>${openrewrite.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-api</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-engine</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.assertj</groupId> | ||
| <artifactId>assertj-core</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| </project> |
150 changes: 150 additions & 0 deletions
150
migration/src/main/java/io/javaoperatorsdk/operator/migration/RemoveMethodDeclaration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| /* | ||
| * Copyright Java Operator SDK Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.javaoperatorsdk.operator.migration; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.NlsRewrite; | ||
| import org.openrewrite.Option; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.JavaType; | ||
|
|
||
| public class RemoveMethodDeclaration extends Recipe { | ||
|
|
||
| @Option( | ||
| displayName = "Interface name", | ||
| description = "Fully qualified or simple name of the interface.", | ||
| example = "com.example.YourInterface") | ||
| String interfaceName; | ||
|
|
||
| @Option( | ||
| displayName = "Method name", | ||
| description = "Name of the method to remove.", | ||
| example = "removedMethod") | ||
| String methodName; | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Remove obsolete method from implementing classes"; | ||
| } | ||
|
|
||
| @Override | ||
| public @NlsRewrite.Description String getDescription() { | ||
| return "Remove obsolete method from implementing classes"; | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return new JavaIsoVisitor<ExecutionContext>() { | ||
|
|
||
| @Override | ||
| public J.ClassDeclaration visitClassDeclaration( | ||
| J.ClassDeclaration classDecl, ExecutionContext ctx) { | ||
| J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx); | ||
|
|
||
| if (cd.getType() == null || !typeMatchesOrImplements(cd.getType())) { | ||
| return cd; | ||
| } | ||
|
|
||
| // Mutate the type info in place to remove the method from the declared methods list, | ||
| // so all AST nodes sharing this type reference stay consistent. | ||
| var type = cd.getType(); | ||
| if (type instanceof JavaType.Class classType) { | ||
| var updatedMethods = | ||
| classType.getMethods().stream().filter(m -> !m.getName().equals(methodName)).toList(); | ||
| classType.unsafeSet( | ||
| classType.getTypeParameters(), | ||
| classType.getSupertype(), | ||
| classType.getOwningClass(), | ||
| classType.getAnnotations(), | ||
| classType.getInterfaces(), | ||
| classType.getMembers(), | ||
| updatedMethods); | ||
| } | ||
|
|
||
| return cd; | ||
| } | ||
|
|
||
| @Override | ||
| public J.MethodDeclaration visitMethodDeclaration( | ||
| J.MethodDeclaration method, ExecutionContext ctx) { | ||
| if (!method.getSimpleName().equals(methodName)) { | ||
| return super.visitMethodDeclaration(method, ctx); | ||
| } | ||
|
|
||
| J.ClassDeclaration classDecl = getCursor().firstEnclosing(J.ClassDeclaration.class); | ||
| if (classDecl == null || classDecl.getType() == null) { | ||
| return super.visitMethodDeclaration(method, ctx); | ||
| } | ||
|
|
||
| if (typeMatchesOrImplements(classDecl.getType())) { | ||
| //noinspection DataFlowIssue | ||
| return null; | ||
| } | ||
|
|
||
| return super.visitMethodDeclaration(method, ctx); | ||
| } | ||
|
|
||
| private boolean typeMatchesOrImplements(JavaType.FullyQualified type) { | ||
| for (var iface : type.getInterfaces()) { | ||
| if (iface.getFullyQualifiedName().equals(interfaceName) | ||
| || typeMatchesOrImplements(iface)) { | ||
| return true; | ||
| } | ||
| } | ||
| var supertype = type.getSupertype(); | ||
| if (supertype != null && !supertype.getFullyQualifiedName().equals("java.lang.Object")) { | ||
| return typeMatchesOrImplements(supertype); | ||
| } | ||
| return false; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| public String getInterfaceName() { | ||
| return interfaceName; | ||
| } | ||
|
|
||
| public void setInterfaceName(String interfaceName) { | ||
| this.interfaceName = interfaceName; | ||
| } | ||
|
|
||
| public String getMethodName() { | ||
| return methodName; | ||
| } | ||
|
|
||
| public void setMethodName(String methodName) { | ||
| this.methodName = methodName; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| if (!super.equals(o)) return false; | ||
| RemoveMethodDeclaration that = (RemoveMethodDeclaration) o; | ||
| return Objects.equals(interfaceName, that.interfaceName) | ||
| && Objects.equals(methodName, that.methodName); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(super.hashCode(), interfaceName, methodName); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had to change this is open rewrite is jvm specific there is an issue with AbstractSyntaxtTree (AST) when having this on java 25. I'm pretty sure we could improve on these things regardin open rewite (like test with multiple java version but I don't have the bandwidth.
Note that all java versions are tested in integration tests