Skip to content

Wrap region string with Region.of #5331

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 2 commits into from
Jun 25, 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
@@ -0,0 +1,121 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.migration.internal.recipe;

import static software.amazon.awssdk.migration.internal.utils.SdkTypeUtils.isV2ClientClass;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.regions.Region;

/**
* Recipe to wrap the region string provided on the SDK client builder with Region.of.
*
* {@snippet :
* SqsClient.builder()
* .region("us-west-2")
* .build();
* }
*
* to
*
* {@snippet :
* SqsClient.builder()
* .region(Region.of("us-west-2"))
* .build();
* }
*/
@SdkInternalApi
public class WrapSdkClientBuilderRegionStr extends Recipe {
@Override
public String getDisplayName() {
return "Wrap the region string provided on the SDK client builder with Region.of";
}

@Override
public String getDescription() {
return "Wrap the region string provided on the SDK client builder with Region.of.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new V1GetterToV2Visitor();
}

private static class V1GetterToV2Visitor extends JavaIsoVisitor<ExecutionContext> {

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
method = super.visitMethodInvocation(method, executionContext);

if (!isSdkClientBuilder(method)) {
return method;
}

String methodName = method.getSimpleName();
if (!methodName.equals("region")) {
return method;
}

JavaType.Method methodType = method.getMethodType();

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

List<JavaType> parameterTypes = methodType.getParameterTypes();
if (parameterTypes.size() != 1 || !TypeUtils.isString(parameterTypes.get(0))) {
return method;
}

String regionFqcn = Region.class.getCanonicalName();

JavaTemplate template = JavaTemplate
.builder("Region.of(#{any()})")
Copy link
Contributor Author

@zoewangg zoewangg Jun 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No unit tests are created for this class because OpenRewrite testing framework can't recognize types from the same project if JavaTemplate is used.

We only have end-to-end tests for this.

.imports("software.amazon.awssdk.regions.Region")
.build();

maybeAddImport(regionFqcn, false);
List<Object> arguments = new ArrayList<>(method.getArguments());

method = template.apply(
updateCursor(method),
method.getCoordinates().replaceArguments(),
arguments.toArray(new Object[0])
);

autoFormat(method, executionContext);

return method;
}

private static boolean isSdkClientBuilder(J.MethodInvocation method) {
return Optional.ofNullable(method.getMethodType()).map(mt -> mt.getReturnType())
.filter(t -> isV2ClientClass(t))
.isPresent();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ recipeList:
- software.amazon.awssdk.migration.recipe.NewClassToStaticFactory
- software.amazon.awssdk.migration.internal.recipe.V1GetterToV2
- software.amazon.awssdk.migration.internal.recipe.HttpSettingsToHttpClient
- software.amazon.awssdk.migration.internal.recipe.WrapSdkClientBuilderRegionStr
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static ClientOverrideConfiguration customClientConfiguration() {

public static SqsClient sqsClientWithAllSettings() {
return SqsClient.builder()
.region(Region.US_WEST_2)
.region(Region.of("us-west-2"))
.overrideConfiguration(customClientConfiguration())
.credentialsProvider(CredentialsDependencyFactory.defaultCredentialsProviderChain())
.httpClientBuilder(ApacheHttpClient.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static ClientConfiguration customClientConfiguration() {

public static AmazonSQS sqsClientWithAllSettings() {
return AmazonSQSClient.builder()
.withRegion(Regions.US_WEST_2)
.withRegion("us-west-2")
.withClientConfiguration(customClientConfiguration())
.withCredentials(CredentialsDependencyFactory.defaultCredentialsProviderChain())
.build();
Expand Down
Loading