Skip to content

Commit e37cb42

Browse files
committed
Add AddCommentToMethod recipe
1 parent 9831bdf commit e37cb42

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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.recipe;
17+
18+
import static org.openrewrite.java.Assertions.java;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.condition.EnabledOnJre;
22+
import org.junit.jupiter.api.condition.JRE;
23+
import org.openrewrite.java.Java8Parser;
24+
import org.openrewrite.test.RewriteTest;
25+
import software.amazon.awssdk.migration.internal.recipe.AddCommentToMethod;
26+
27+
public class AddCommentToMethodTest implements RewriteTest {
28+
29+
@Test
30+
@EnabledOnJre({JRE.JAVA_8})
31+
void shouldAddCommentToMethod() {
32+
rewriteRun(
33+
spec -> spec.recipe(new AddCommentToMethod("com.amazonaws.ClientConfiguration setCacheResponseMetadata(boolean)",
34+
"a comment"))
35+
.parser(Java8Parser.builder().classpath("sqs", "aws-core", "sdk-core", "aws-java-sdk-sqs")),
36+
java(
37+
"import com.amazonaws.ClientConfiguration;\n"
38+
+ "\n"
39+
+ "public class Example {\n"
40+
+ " \n"
41+
+ " void test() {\n"
42+
+ " ClientConfiguration clientConfiguration = new ClientConfiguration();\n"
43+
+ " clientConfiguration.setCacheResponseMetadata(false);\n"
44+
+ " }\n"
45+
+ "}\n",
46+
"import com.amazonaws.ClientConfiguration;\n"
47+
+ "\n"
48+
+ "public class Example {\n"
49+
+ " \n"
50+
+ " void test() {\n"
51+
+ " ClientConfiguration clientConfiguration = new ClientConfiguration();\n"
52+
+ " /*~~(a comment)~~>*/clientConfiguration.setCacheResponseMetadata(false);\n"
53+
+ " }\n"
54+
+ "}"
55+
)
56+
);
57+
}
58+
59+
@Test
60+
@EnabledOnJre({JRE.JAVA_8})
61+
void hasExistingComments_shouldNotClobber() {
62+
rewriteRun(
63+
spec -> spec.recipe(new AddCommentToMethod("com.amazonaws.ClientConfiguration setCacheResponseMetadata(boolean)",
64+
"a comment"))
65+
.parser(Java8Parser.builder().classpath("sqs", "aws-core", "sdk-core", "aws-java-sdk-sqs")),
66+
java(
67+
"import com.amazonaws.ClientConfiguration;\n"
68+
+ "\n"
69+
+ "public class Example {\n"
70+
+ " \n"
71+
+ " void test() {\n"
72+
+ " ClientConfiguration clientConfiguration = new ClientConfiguration();\n"
73+
+ " // Existing comment \n"
74+
+ " /*existing comment*/ clientConfiguration.setCacheResponseMetadata(false);\n"
75+
+ " }\n"
76+
+ "}\n",
77+
"import com.amazonaws.ClientConfiguration;\n"
78+
+ "\n"
79+
+ "public class Example {\n"
80+
+ " \n"
81+
+ " void test() {\n"
82+
+ " ClientConfiguration clientConfiguration = new ClientConfiguration();\n"
83+
+ " // Existing comment \n"
84+
+ " /*existing comment*/ /*~~(a comment)~~>*/clientConfiguration.setCacheResponseMetadata(false);\n"
85+
+ " }\n"
86+
+ "}"
87+
)
88+
);
89+
}
90+
}

0 commit comments

Comments
 (0)