Skip to content

Prefix comment to method call #5212

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 3 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.List;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.Comment;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.java.tree.JRightPadded;
import org.openrewrite.java.tree.Space;
import org.openrewrite.java.tree.TextComment;
import org.openrewrite.marker.Markers;
import software.amazon.awssdk.annotations.SdkInternalApi;

/**
Expand Down Expand Up @@ -69,10 +76,12 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
private static final class Visitor extends JavaIsoVisitor<ExecutionContext> {
private final MethodMatcher methodMatcher;
private final String comment;
private final Comment commentToAdd;

Visitor(String methodPattern, String comment) {
this.methodMatcher = new MethodMatcher(methodPattern, false);
this.comment = COMMENT_PREFIX + comment + "\n";
this.comment = COMMENT_PREFIX + comment;
this.commentToAdd = new TextComment(true, this.comment, "", Markers.EMPTY);
}

@Override
Expand All @@ -83,7 +92,33 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocat
return method;
}

return SearchResult.found(method, comment);

J.MethodInvocation.Padding padding = method.getPadding();
JRightPadded<Expression> select = padding.getSelect();

if (select == null) {
return method;
}

Space after = select.getAfter();
List<Comment> comments = new ArrayList<>(after.getComments());

if (comments.contains(commentToAdd)) {
return method;
}
comments.add(commentToAdd);
after = after.withComments(comments);

return new J.MethodInvocation(
method.getId(),
method.getPrefix(),
method.getMarkers(),
select.withAfter(after),
padding.getTypeParameters(),
method.getName(),
padding.getArguments(),
method.getMethodType()
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ void shouldAddCommentToMethod() {
+ " \n"
+ " void test() {\n"
+ " ClientConfiguration clientConfiguration = new ClientConfiguration();\n"
+ " /*~~(AWS SDK for Java v2 migration: a comment\n"
+ ")~~>*/clientConfiguration.setCacheResponseMetadata(false);\n"
+ " clientConfiguration/*AWS SDK for Java v2 migration: a comment*/.setCacheResponseMetadata(false);\n"
+ " }\n"
+ "}"
)
Expand Down Expand Up @@ -82,8 +81,7 @@ void hasExistingComments_shouldNotClobber() {
+ " void test() {\n"
+ " ClientConfiguration clientConfiguration = new ClientConfiguration();\n"
+ " // Existing comment \n"
+ " /*existing comment*/ /*~~(AWS SDK for Java v2 migration: a comment\n"
+ ")~~>*/clientConfiguration.setCacheResponseMetadata(false);\n"
+ " /*existing comment*/ clientConfiguration/*AWS SDK for Java v2 migration: a comment*/.setCacheResponseMetadata(false);\n"
+ " }\n"
+ "}"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,29 +140,20 @@ void defaultConfigurationWithUnsupportedSettings_shouldAddComment() {
+ "public class Example {\n"
+ "\n"
+ " void test() {\n"
+ " 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"
+ ")~~>*//*~~(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"
+ ")~~>*//*~~(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"
+ ")~~>*//*~~(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"
+ ")~~>*//*~~(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"
+ ")~~>*//*~~(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"
+ ")~~>*//*~~(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"
+ ")~~>*//*~~(AWS SDK for Java v2 migration: disableHostPrefixInjection is deprecated and not supported removed in v2. Consider removing it.\n"
+ ")~~>*//*~~(AWS SDK for Java v2 migration: cacheResponseMetadata is deprecated and not supported in v2. Consider removing it.\n"
+ ")~~>*/ClientOverrideConfiguration.builder()\n"
+ " .cacheResponseMetadata(false)\n"
+ " .disableHostPrefixInjection(true)\n"
+ " ClientOverrideConfiguration clientConfiguration = ClientOverrideConfiguration.builder()\n"
+ " /*AWS SDK for Java v2 migration: cacheResponseMetadata is deprecated and not supported in v2. Consider removing it.*/.cacheResponseMetadata(false)\n"
+ " /*AWS SDK for Java v2 migration: disableHostPrefixInjection is deprecated and not supported removed in v2. Consider removing it.*/.disableHostPrefixInjection(true)\n"
+ " .disableSocketProxy(true)\n"
+ " .dnsResolver(null)\n"
+ " .gzip(true)\n"
+ " .localAddress(null)\n"
+ " /*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"
+ " /*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"
+ " /*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"
+ " .secureRandom(null)\n"
+ " .throttledRetries(true)\n"
+ " .protocol(null)\n"
+ " .userAgent(\"test\")\n"
+ " .userAgentPrefix(\"test\")\n"
+ " .userAgentSuffix(\"test\")\n"
+ " .useExpectContinue(false).build();\n"
+ " /*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"
+ " /*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"
+ " /*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"
+ " /*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"
+ " }\n"
+ "}"
)
Expand Down
Loading