Skip to content

Commit 3fb6b46

Browse files
committed
Prefix comment to method call
Insert the comment to before the method call to make it easier and more apparent what the comment is referring to. Using Space to insert comments here is more idiomatic, and gives us more control of where the comments appear and what they look like.
1 parent 55da9ba commit 3fb6b46

File tree

3 files changed

+50
-26
lines changed

3 files changed

+50
-26
lines changed

migration-tool/src/main/java/software/amazon/awssdk/migration/internal/recipe/AddCommentToMethod.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,21 @@
1717

1818
import com.fasterxml.jackson.annotation.JsonCreator;
1919
import com.fasterxml.jackson.annotation.JsonProperty;
20+
import java.util.ArrayList;
21+
import java.util.List;
2022
import org.openrewrite.ExecutionContext;
2123
import org.openrewrite.Option;
2224
import org.openrewrite.Recipe;
2325
import org.openrewrite.TreeVisitor;
2426
import org.openrewrite.java.JavaIsoVisitor;
2527
import org.openrewrite.java.MethodMatcher;
28+
import org.openrewrite.java.tree.Comment;
29+
import org.openrewrite.java.tree.Expression;
2630
import org.openrewrite.java.tree.J;
27-
import org.openrewrite.marker.SearchResult;
31+
import org.openrewrite.java.tree.JRightPadded;
32+
import org.openrewrite.java.tree.Space;
33+
import org.openrewrite.java.tree.TextComment;
34+
import org.openrewrite.marker.Markers;
2835
import software.amazon.awssdk.annotations.SdkInternalApi;
2936

3037
/**
@@ -69,10 +76,12 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
6976
private static final class Visitor extends JavaIsoVisitor<ExecutionContext> {
7077
private final MethodMatcher methodMatcher;
7178
private final String comment;
79+
private final Comment commentToAdd;
7280

7381
Visitor(String methodPattern, String comment) {
7482
this.methodMatcher = new MethodMatcher(methodPattern, false);
75-
this.comment = COMMENT_PREFIX + comment + "\n";
83+
this.comment = COMMENT_PREFIX + comment;
84+
this.commentToAdd = new TextComment(true, this.comment, "", Markers.EMPTY);
7685
}
7786

7887
@Override
@@ -83,7 +92,33 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocat
8392
return method;
8493
}
8594

86-
return SearchResult.found(method, comment);
95+
96+
J.MethodInvocation.Padding padding = method.getPadding();
97+
JRightPadded<Expression> select = padding.getSelect();
98+
99+
if (select == null) {
100+
return method;
101+
}
102+
103+
Space after = select.getAfter();
104+
List<Comment> comments = new ArrayList<>(after.getComments());
105+
106+
if (comments.contains(commentToAdd)) {
107+
return method;
108+
}
109+
comments.add(commentToAdd);
110+
after = after.withComments(comments);
111+
112+
return new J.MethodInvocation(
113+
method.getId(),
114+
method.getPrefix(),
115+
method.getMarkers(),
116+
select.withAfter(after),
117+
padding.getTypeParameters(),
118+
method.getName(),
119+
padding.getArguments(),
120+
method.getMethodType()
121+
);
87122
}
88123
}
89124
}

migration-tool/src/test/java/software/amazon/awssdk/migration/recipe/AddCommentToMethodTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ void shouldAddCommentToMethod() {
4949
+ " \n"
5050
+ " void test() {\n"
5151
+ " ClientConfiguration clientConfiguration = new ClientConfiguration();\n"
52-
+ " /*~~(AWS SDK for Java v2 migration: a comment\n"
53-
+ ")~~>*/clientConfiguration.setCacheResponseMetadata(false);\n"
52+
+ " clientConfiguration/*AWS SDK for Java v2 migration: a comment*/.setCacheResponseMetadata(false);\n"
5453
+ " }\n"
5554
+ "}"
5655
)
@@ -82,8 +81,7 @@ void hasExistingComments_shouldNotClobber() {
8281
+ " void test() {\n"
8382
+ " ClientConfiguration clientConfiguration = new ClientConfiguration();\n"
8483
+ " // Existing comment \n"
85-
+ " /*existing comment*/ /*~~(AWS SDK for Java v2 migration: a comment\n"
86-
+ ")~~>*/clientConfiguration.setCacheResponseMetadata(false);\n"
84+
+ " /*existing comment*/ clientConfiguration/*AWS SDK for Java v2 migration: a comment*/.setCacheResponseMetadata(false);\n"
8785
+ " }\n"
8886
+ "}"
8987
)

migration-tool/src/test/java/software/amazon/awssdk/migration/recipe/ChangeConfigTypesTest.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -140,29 +140,20 @@ void defaultConfigurationWithUnsupportedSettings_shouldAddComment() {
140140
+ "public class Example {\n"
141141
+ "\n"
142142
+ " void test() {\n"
143-
+ " ClientOverrideConfiguration clientConfiguration = /*~~(AWS SDK for Java v2 migration: useExpectContinue is removed in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues\n"
144-
+ ")~~>*//*~~(AWS SDK for Java v2 migration: userAgentSuffix override is a request-level config in v2. See https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/core/RequestOverrideConfiguration.Builder.html#addApiName(software.amazon.awssdk.core.ApiName).\n"
145-
+ ")~~>*//*~~(AWS SDK for Java v2 migration: userAgentPrefix override is a request-level config in v2. See https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/core/RequestOverrideConfiguration.Builder.html#addApiName(software.amazon.awssdk.core.ApiName).\n"
146-
+ ")~~>*//*~~(AWS SDK for Java v2 migration: userAgent override is a request-level config in v2. See https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/core/RequestOverrideConfiguration.Builder.html#addApiName(software.amazon.awssdk.core.ApiName).\n"
147-
+ ")~~>*//*~~(AWS SDK for Java v2 migration: localAddress is not supported in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues\n"
148-
+ ")~~>*//*~~(AWS SDK for Java v2 migration: gzip is not supported in v2 tracking in https://github.com/aws/aws-sdk-java-v2/issues/866. Consider removing it.\n"
149-
+ ")~~>*//*~~(AWS SDK for Java v2 migration: dnsResolver is not supported in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues\n"
150-
+ ")~~>*//*~~(AWS SDK for Java v2 migration: disableHostPrefixInjection is deprecated and not supported removed in v2. Consider removing it.\n"
151-
+ ")~~>*//*~~(AWS SDK for Java v2 migration: cacheResponseMetadata is deprecated and not supported in v2. Consider removing it.\n"
152-
+ ")~~>*/ClientOverrideConfiguration.builder()\n"
153-
+ " .cacheResponseMetadata(false)\n"
154-
+ " .disableHostPrefixInjection(true)\n"
143+
+ " ClientOverrideConfiguration clientConfiguration = ClientOverrideConfiguration.builder()\n"
144+
+ " /*AWS SDK for Java v2 migration: cacheResponseMetadata is deprecated and not supported in v2. Consider removing it.*/.cacheResponseMetadata(false)\n"
145+
+ " /*AWS SDK for Java v2 migration: disableHostPrefixInjection is deprecated and not supported removed in v2. Consider removing it.*/.disableHostPrefixInjection(true)\n"
155146
+ " .disableSocketProxy(true)\n"
156-
+ " .dnsResolver(null)\n"
157-
+ " .gzip(true)\n"
158-
+ " .localAddress(null)\n"
147+
+ " /*AWS SDK for Java v2 migration: dnsResolver is not supported in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues*/.dnsResolver(null)\n"
148+
+ " /*AWS SDK for Java v2 migration: gzip is not supported in v2 tracking in https://github.com/aws/aws-sdk-java-v2/issues/866. Consider removing it.*/.gzip(true)\n"
149+
+ " /*AWS SDK for Java v2 migration: localAddress is not supported in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues*/.localAddress(null)\n"
159150
+ " .secureRandom(null)\n"
160151
+ " .throttledRetries(true)\n"
161152
+ " .protocol(null)\n"
162-
+ " .userAgent(\"test\")\n"
163-
+ " .userAgentPrefix(\"test\")\n"
164-
+ " .userAgentSuffix(\"test\")\n"
165-
+ " .useExpectContinue(false).build();\n"
153+
+ " /*AWS SDK for Java v2 migration: userAgent override is a request-level config in v2. See https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/core/RequestOverrideConfiguration.Builder.html#addApiName(software.amazon.awssdk.core.ApiName).*/.userAgent(\"test\")\n"
154+
+ " /*AWS SDK for Java v2 migration: userAgentPrefix override is a request-level config in v2. See https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/core/RequestOverrideConfiguration.Builder.html#addApiName(software.amazon.awssdk.core.ApiName).*/.userAgentPrefix(\"test\")\n"
155+
+ " /*AWS SDK for Java v2 migration: userAgentSuffix override is a request-level config in v2. See https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/core/RequestOverrideConfiguration.Builder.html#addApiName(software.amazon.awssdk.core.ApiName).*/.userAgentSuffix(\"test\")\n"
156+
+ " /*AWS SDK for Java v2 migration: useExpectContinue is removed in v2. Please submit a feature request https://github.com/aws/aws-sdk-java-v2/issues*/.useExpectContinue(false).build();\n"
166157
+ " }\n"
167158
+ "}"
168159
)

0 commit comments

Comments
 (0)