|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.migration.internal.recipe; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 19 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 20 | +import org.openrewrite.ExecutionContext; |
| 21 | +import org.openrewrite.Option; |
| 22 | +import org.openrewrite.Recipe; |
| 23 | +import org.openrewrite.TreeVisitor; |
| 24 | +import org.openrewrite.java.JavaIsoVisitor; |
| 25 | +import org.openrewrite.java.MethodMatcher; |
| 26 | +import org.openrewrite.java.tree.J; |
| 27 | +import org.openrewrite.marker.SearchResult; |
| 28 | +import software.amazon.awssdk.annotations.SdkInternalApi; |
| 29 | + |
| 30 | +/** |
| 31 | + * Add a comment to a method |
| 32 | + */ |
| 33 | +@SdkInternalApi |
| 34 | +public class AddCommentToMethod extends Recipe { |
| 35 | + |
| 36 | + @Option(displayName = "Method pattern", |
| 37 | + description = "A method pattern that is used to find matching method invocations.", |
| 38 | + example = "org.mockito.Matchers anyVararg()") |
| 39 | + private final String methodPattern; |
| 40 | + |
| 41 | + @Option(displayName = "Comment", |
| 42 | + description = "A comment to add to this method.", |
| 43 | + example = "This method is not supported in AWS SDK for Java v2.") |
| 44 | + private final String comment; |
| 45 | + |
| 46 | + @JsonCreator |
| 47 | + public AddCommentToMethod(@JsonProperty("methodPattern") String methodPattern, |
| 48 | + @JsonProperty("comment") String comment) { |
| 49 | + this.methodPattern = methodPattern; |
| 50 | + this.comment = comment; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public String getDisplayName() { |
| 55 | + return "Add a comment to a method"; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String getDescription() { |
| 60 | + return "Add a comment to a method."; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 65 | + return new Visitor(methodPattern, comment); |
| 66 | + } |
| 67 | + |
| 68 | + private static final class Visitor extends JavaIsoVisitor<ExecutionContext> { |
| 69 | + private final String methodPattern; |
| 70 | + private final MethodMatcher methodMatcher; |
| 71 | + private final String comment; |
| 72 | + |
| 73 | + Visitor(String methodPattern, String comment) { |
| 74 | + this.methodPattern = methodPattern; |
| 75 | + this.methodMatcher = new MethodMatcher(methodPattern, false); |
| 76 | + this.comment = comment; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocation, ExecutionContext executionContext) { |
| 81 | + J.MethodInvocation method = super.visitMethodInvocation(methodInvocation, executionContext); |
| 82 | + |
| 83 | + if (!methodMatcher.matches(method)) { |
| 84 | + return method; |
| 85 | + } |
| 86 | + |
| 87 | + return SearchResult.found(methodInvocation, comment); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments