Skip to content

Commit 6aa7d91

Browse files
authored
Wrap region string with Region.of (#5331)
* Wrap region string with Region.of * Fix build
1 parent 77efad7 commit 6aa7d91

File tree

4 files changed

+124
-2
lines changed

4 files changed

+124
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 static software.amazon.awssdk.migration.internal.utils.SdkTypeUtils.isV2ClientClass;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
import java.util.Optional;
23+
import org.openrewrite.ExecutionContext;
24+
import org.openrewrite.Recipe;
25+
import org.openrewrite.TreeVisitor;
26+
import org.openrewrite.java.JavaIsoVisitor;
27+
import org.openrewrite.java.JavaTemplate;
28+
import org.openrewrite.java.tree.J;
29+
import org.openrewrite.java.tree.JavaType;
30+
import org.openrewrite.java.tree.TypeUtils;
31+
import software.amazon.awssdk.annotations.SdkInternalApi;
32+
import software.amazon.awssdk.regions.Region;
33+
34+
/**
35+
* Recipe to wrap the region string provided on the SDK client builder with Region.of.
36+
*
37+
* {@snippet :
38+
* SqsClient.builder()
39+
* .region("us-west-2")
40+
* .build();
41+
* }
42+
*
43+
* to
44+
*
45+
* {@snippet :
46+
* SqsClient.builder()
47+
* .region(Region.of("us-west-2"))
48+
* .build();
49+
* }
50+
*/
51+
@SdkInternalApi
52+
public class WrapSdkClientBuilderRegionStr extends Recipe {
53+
@Override
54+
public String getDisplayName() {
55+
return "Wrap the region string provided on the SDK client builder with Region.of";
56+
}
57+
58+
@Override
59+
public String getDescription() {
60+
return "Wrap the region string provided on the SDK client builder with Region.of.";
61+
}
62+
63+
@Override
64+
public TreeVisitor<?, ExecutionContext> getVisitor() {
65+
return new V1GetterToV2Visitor();
66+
}
67+
68+
private static class V1GetterToV2Visitor extends JavaIsoVisitor<ExecutionContext> {
69+
70+
@Override
71+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
72+
method = super.visitMethodInvocation(method, executionContext);
73+
74+
if (!isSdkClientBuilder(method)) {
75+
return method;
76+
}
77+
78+
String methodName = method.getSimpleName();
79+
if (!methodName.equals("region")) {
80+
return method;
81+
}
82+
83+
JavaType.Method methodType = method.getMethodType();
84+
85+
if (methodType == null) {
86+
return method;
87+
}
88+
89+
List<JavaType> parameterTypes = methodType.getParameterTypes();
90+
if (parameterTypes.size() != 1 || !TypeUtils.isString(parameterTypes.get(0))) {
91+
return method;
92+
}
93+
94+
String regionFqcn = Region.class.getCanonicalName();
95+
96+
JavaTemplate template = JavaTemplate
97+
.builder("Region.of(#{any()})")
98+
.imports("software.amazon.awssdk.regions.Region")
99+
.build();
100+
101+
maybeAddImport(regionFqcn, false);
102+
List<Object> arguments = new ArrayList<>(method.getArguments());
103+
104+
method = template.apply(
105+
updateCursor(method),
106+
method.getCoordinates().replaceArguments(),
107+
arguments.toArray(new Object[0])
108+
);
109+
110+
autoFormat(method, executionContext);
111+
112+
return method;
113+
}
114+
115+
private static boolean isSdkClientBuilder(J.MethodInvocation method) {
116+
return Optional.ofNullable(method.getMethodType()).map(mt -> mt.getReturnType())
117+
.filter(t -> isV2ClientClass(t))
118+
.isPresent();
119+
}
120+
}
121+
}

migration-tool/src/main/resources/META-INF/rewrite/java-sdk-v1-to-v2.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ recipeList:
3333
- software.amazon.awssdk.migration.recipe.NewClassToStaticFactory
3434
- software.amazon.awssdk.migration.internal.recipe.V1GetterToV2
3535
- software.amazon.awssdk.migration.internal.recipe.HttpSettingsToHttpClient
36+
- software.amazon.awssdk.migration.internal.recipe.WrapSdkClientBuilderRegionStr

test/migration-tool-tests/src/test/resources/after/src/main/java/foo/bar/SdkClientsDependencyFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static ClientOverrideConfiguration customClientConfiguration() {
4646

4747
public static SqsClient sqsClientWithAllSettings() {
4848
return SqsClient.builder()
49-
.region(Region.US_WEST_2)
49+
.region(Region.of("us-west-2"))
5050
.overrideConfiguration(customClientConfiguration())
5151
.credentialsProvider(CredentialsDependencyFactory.defaultCredentialsProviderChain())
5252
.httpClientBuilder(ApacheHttpClient.builder()

test/migration-tool-tests/src/test/resources/before/src/main/java/foo/bar/SdkClientsDependencyFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static ClientConfiguration customClientConfiguration() {
5050

5151
public static AmazonSQS sqsClientWithAllSettings() {
5252
return AmazonSQSClient.builder()
53-
.withRegion(Regions.US_WEST_2)
53+
.withRegion("us-west-2")
5454
.withClientConfiguration(customClientConfiguration())
5555
.withCredentials(CredentialsDependencyFactory.defaultCredentialsProviderChain())
5656
.build();

0 commit comments

Comments
 (0)