|
| 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 | +} |
0 commit comments