|
| 1 | +/* |
| 2 | + * Copyright Java Operator SDK Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.javaoperatorsdk.operator.migration; |
| 17 | + |
| 18 | +import org.openrewrite.ExecutionContext; |
| 19 | +import org.openrewrite.Option; |
| 20 | +import org.openrewrite.Recipe; |
| 21 | +import org.openrewrite.TreeVisitor; |
| 22 | +import org.openrewrite.java.JavaIsoVisitor; |
| 23 | +import org.openrewrite.java.MethodMatcher; |
| 24 | +import org.openrewrite.java.tree.J; |
| 25 | + |
| 26 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 27 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 28 | + |
| 29 | +public class RemoveMethodDeclaration extends Recipe { |
| 30 | + |
| 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 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public String getDisplayName() { |
| 44 | + return "Remove method declaration"; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public String getDescription() { |
| 49 | + return "Removes method declarations matching the given method pattern."; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 54 | + var matcher = new MethodMatcher(methodPattern, true); |
| 55 | + return new JavaIsoVisitor<>() { |
| 56 | + @Override |
| 57 | + public J.MethodDeclaration visitMethodDeclaration( |
| 58 | + J.MethodDeclaration method, ExecutionContext ctx) { |
| 59 | + var classDecl = getCursor().firstEnclosing(J.ClassDeclaration.class); |
| 60 | + if (classDecl != null && matcher.matches(method, classDecl)) { |
| 61 | + //noinspection DataFlowIssue |
| 62 | + return null; |
| 63 | + } |
| 64 | + return super.visitMethodDeclaration(method, ctx); |
| 65 | + } |
| 66 | + }; |
| 67 | + } |
| 68 | +} |
0 commit comments