|
| 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.v2migration; |
| 17 | + |
| 18 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V1_S3_PKG; |
| 19 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_S3_PKG; |
| 20 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.createComments; |
| 21 | +import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.isStringType; |
| 22 | +import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.isUriType; |
| 23 | + |
| 24 | +import java.util.List; |
| 25 | +import java.util.regex.Pattern; |
| 26 | +import org.openrewrite.ExecutionContext; |
| 27 | +import org.openrewrite.NlsRewrite; |
| 28 | +import org.openrewrite.Recipe; |
| 29 | +import org.openrewrite.TreeVisitor; |
| 30 | +import org.openrewrite.java.AddImport; |
| 31 | +import org.openrewrite.java.JavaTemplate; |
| 32 | +import org.openrewrite.java.JavaVisitor; |
| 33 | +import org.openrewrite.java.RemoveImport; |
| 34 | +import org.openrewrite.java.tree.Comment; |
| 35 | +import org.openrewrite.java.tree.Expression; |
| 36 | +import org.openrewrite.java.tree.J; |
| 37 | +import org.openrewrite.java.tree.JavaType; |
| 38 | +import org.openrewrite.java.tree.TypeTree; |
| 39 | +import software.amazon.awssdk.annotations.SdkInternalApi; |
| 40 | + |
| 41 | +@SdkInternalApi |
| 42 | +public class S3UriToV2 extends Recipe { |
| 43 | + |
| 44 | + private static final Pattern V1_AMAZON_S3_URI = Pattern.compile(V1_S3_PKG + "AmazonS3URI"); |
| 45 | + private static final String V2_S3_URI = V2_S3_PKG + "S3Uri"; |
| 46 | + |
| 47 | + @Override |
| 48 | + public @NlsRewrite.DisplayName String getDisplayName() { |
| 49 | + return "Convert v1 AmazonS3URI to v2 S3Uri"; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public @NlsRewrite.Description String getDescription() { |
| 54 | + return "Convert v1 AmazonS3URI to v2 S3Uri"; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 59 | + return new Visitor(); |
| 60 | + } |
| 61 | + |
| 62 | + private static final class Visitor extends JavaVisitor<ExecutionContext> { |
| 63 | + |
| 64 | + @Override |
| 65 | + public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations variableDecls, ExecutionContext ctx) { |
| 66 | + J.VariableDeclarations varDec = (J.VariableDeclarations) super.visitVariableDeclarations(variableDecls, ctx); |
| 67 | + |
| 68 | + JavaType type = varDec.getType(); |
| 69 | + if (type == null) { |
| 70 | + return varDec; |
| 71 | + } |
| 72 | + |
| 73 | + if (!type.isAssignableFrom(V1_AMAZON_S3_URI)) { |
| 74 | + return varDec; |
| 75 | + } |
| 76 | + |
| 77 | + addV2S3UriImport(); |
| 78 | + removeV1S3UriImport(); |
| 79 | + JavaType v2Type = JavaType.buildType(V2_S3_URI); |
| 80 | + TypeTree v2TypeTree = TypeTree.build("S3Uri"); |
| 81 | + return varDec.withType(v2Type).withTypeExpression(v2TypeTree); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { |
| 86 | + method = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); |
| 87 | + JavaType.Method methodType = method.getMethodType(); |
| 88 | + |
| 89 | + if (methodType == null) { |
| 90 | + return method; |
| 91 | + } |
| 92 | + |
| 93 | + Expression select = method.getSelect(); |
| 94 | + if (select == null) { |
| 95 | + return method; |
| 96 | + } |
| 97 | + JavaType selectType = select.getType(); |
| 98 | + if (selectType == null || !selectType.isAssignableFrom(V1_AMAZON_S3_URI)) { |
| 99 | + return method; |
| 100 | + } |
| 101 | + |
| 102 | + String methodName = method.getSimpleName(); |
| 103 | + String v2Method; |
| 104 | + |
| 105 | + switch (methodName) { |
| 106 | + case "getVersionId": |
| 107 | + v2Method = "#{any()}.firstMatchingRawQueryParameter(\"versionId\").orElse(null)"; |
| 108 | + break; |
| 109 | + case "getBucket": |
| 110 | + v2Method = "#{any()}.bucket().orElse(null)"; |
| 111 | + break; |
| 112 | + case "getKey": |
| 113 | + v2Method = "#{any()}.key().orElse(null)"; |
| 114 | + break; |
| 115 | + case "getRegion": |
| 116 | + v2Method = "#{any()}.region().map(Region::id).orElse(null)"; |
| 117 | + addV2RegionImport(); |
| 118 | + break; |
| 119 | + default: |
| 120 | + return method; |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + return JavaTemplate.builder(v2Method).build().apply(getCursor(), method.getCoordinates().replace(), |
| 125 | + method.getSelect()); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public J visitNewClass(J.NewClass previousNewClass, ExecutionContext ctx) { |
| 130 | + J.NewClass newClass = super.visitNewClass(previousNewClass, ctx).cast(); |
| 131 | + |
| 132 | + JavaType type = newClass.getType(); |
| 133 | + if (!(type instanceof JavaType.FullyQualified) || !type.isAssignableFrom(V1_AMAZON_S3_URI)) { |
| 134 | + return newClass; |
| 135 | + } |
| 136 | + |
| 137 | + List<Expression> args = newClass.getArguments(); |
| 138 | + Expression uriArg = args.get(0); |
| 139 | + JavaType uriType = uriArg.getType(); |
| 140 | + if (uriType == null) { |
| 141 | + return newClass; |
| 142 | + } |
| 143 | + |
| 144 | + StringBuilder sb = new StringBuilder("S3Utilities.builder().build().parseUri("); |
| 145 | + |
| 146 | + if (isUriType(uriType)) { |
| 147 | + sb.append("#{any()})"); |
| 148 | + } else if (isStringType(uriType)) { |
| 149 | + sb.append("URI.create(#{any()})"); |
| 150 | + addJavaUriImport(); |
| 151 | + } |
| 152 | + |
| 153 | + J.MethodInvocation newMethod = JavaTemplate.builder(sb.toString()).build() |
| 154 | + .apply(getCursor(), newClass.getCoordinates().replace(), uriArg); |
| 155 | + |
| 156 | + if (shouldAddWarning(uriType, args)) { |
| 157 | + newMethod = newMethod.withComments(v2DoesNotEncodeWarning()); |
| 158 | + } |
| 159 | + |
| 160 | + removeV1S3UriImport(); |
| 161 | + addS3UtilitiesImport(); |
| 162 | + return newMethod; |
| 163 | + } |
| 164 | + |
| 165 | + private boolean shouldAddWarning(JavaType uriType, List<Expression> args) { |
| 166 | + if (isUriType(uriType)) { |
| 167 | + return false; |
| 168 | + } |
| 169 | + if (args.size() == 1) { |
| 170 | + return true; |
| 171 | + } |
| 172 | + return !urlEncodeArgIsLiteralFalse(args.get(1)); |
| 173 | + } |
| 174 | + |
| 175 | + private boolean urlEncodeArgIsLiteralFalse(Expression arg) { |
| 176 | + if (arg instanceof J.Literal) { |
| 177 | + J.Literal literal = (J.Literal) arg; |
| 178 | + return literal.getValue().equals(Boolean.FALSE); |
| 179 | + } |
| 180 | + return false; |
| 181 | + } |
| 182 | + |
| 183 | + |
| 184 | + private List<Comment> v2DoesNotEncodeWarning() { |
| 185 | + String warning = "v2 S3Uri does not URL-encode a String URI. If you relied on this functionality in v1 you must " |
| 186 | + + "update your code to manually encode the String."; |
| 187 | + return createComments(warning); |
| 188 | + } |
| 189 | + |
| 190 | + private void removeV1S3UriImport() { |
| 191 | + doAfterVisit(new RemoveImport<>(V1_AMAZON_S3_URI.toString(), true)); |
| 192 | + } |
| 193 | + |
| 194 | + private void addV2S3UriImport() { |
| 195 | + doAfterVisit(new AddImport<>(V2_S3_URI, null, false)); |
| 196 | + } |
| 197 | + |
| 198 | + private void addS3UtilitiesImport() { |
| 199 | + String fqcn = V2_S3_PKG + "S3Utilities"; |
| 200 | + doAfterVisit(new AddImport<>(fqcn, null, false)); |
| 201 | + } |
| 202 | + |
| 203 | + private void addV2RegionImport() { |
| 204 | + String fqcn = "software.amazon.awssdk.regions.Region"; |
| 205 | + doAfterVisit(new AddImport<>(fqcn, null, false)); |
| 206 | + } |
| 207 | + |
| 208 | + private void addJavaUriImport() { |
| 209 | + String fqcn = "java.net.URI"; |
| 210 | + doAfterVisit(new AddImport<>(fqcn, null, false)); |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments