|
| 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_TM_PKG; |
| 19 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_S3_CLIENT; |
| 20 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_S3_MODEL_PKG; |
| 21 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_TM_CLIENT; |
| 22 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.createComments; |
| 23 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.v2TmMethodMatcher; |
| 24 | + |
| 25 | +import java.util.regex.Pattern; |
| 26 | +import org.openrewrite.ExecutionContext; |
| 27 | +import org.openrewrite.Recipe; |
| 28 | +import org.openrewrite.TreeVisitor; |
| 29 | +import org.openrewrite.java.JavaIsoVisitor; |
| 30 | +import org.openrewrite.java.MethodMatcher; |
| 31 | +import org.openrewrite.java.tree.J; |
| 32 | +import org.openrewrite.java.tree.JavaType; |
| 33 | +import software.amazon.awssdk.annotations.SdkInternalApi; |
| 34 | + |
| 35 | +@SdkInternalApi |
| 36 | +public class S3TmAddComments extends Recipe { |
| 37 | + |
| 38 | + private static final Pattern S3_TM = Pattern.compile(V2_TM_CLIENT); |
| 39 | + private static final Pattern S3_CLIENT = Pattern.compile(V2_S3_CLIENT); |
| 40 | + |
| 41 | + private static final MethodMatcher COPY = v2TmMethodMatcher("copy(..)"); |
| 42 | + private static final MethodMatcher DOWNLOAD = v2TmMethodMatcher(String.format("download(%sGetObjectRequest, java.io.File, " |
| 43 | + + "%sinternal.S3ProgressListener, ..)", V2_S3_MODEL_PKG, |
| 44 | + V1_TM_PKG)); |
| 45 | + private static final MethodMatcher DOWNLOAD_DIRECTORY = v2TmMethodMatcher("downloadDirectory(..)"); |
| 46 | + private static final MethodMatcher UPLOAD = v2TmMethodMatcher("upload(..)"); |
| 47 | + private static final MethodMatcher UPLOAD_DIRECTORY = v2TmMethodMatcher("uploadDirectory(..)"); |
| 48 | + |
| 49 | + @Override |
| 50 | + public String getDisplayName() { |
| 51 | + return "Add imports and comments to unsupported S3 transfer manager transforms."; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String getDescription() { |
| 56 | + return "Add imports and comments to unsupported S3 transfer manager transforms."; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 61 | + return new S3TmAddComments.Visitor(); |
| 62 | + } |
| 63 | + |
| 64 | + private static class Visitor extends JavaIsoVisitor<ExecutionContext> { |
| 65 | + |
| 66 | + @Override |
| 67 | + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { |
| 68 | + if (COPY.matches(method) && (method.getArguments().size() == 2 || method.getArguments().size() == 3)) { |
| 69 | + String comment = "Migration for TransferStateChangeListener is not supported by the migration tool. Please " |
| 70 | + + "manually migrate the code using TransferListener in v2"; |
| 71 | + return method.withComments(createComments(comment)); |
| 72 | + } |
| 73 | + if (DOWNLOAD.matches(method)) { |
| 74 | + String comment = "Migration for S3ProgressListener is not supported by the migration tool. Please manually " |
| 75 | + + "migrate the code using TransferListener in v2"; |
| 76 | + return method.withComments(createComments(comment)); |
| 77 | + } |
| 78 | + if (DOWNLOAD_DIRECTORY.matches(method) && method.getArguments().size() > 3) { |
| 79 | + String comment = "Migration for KeyFilter is not supported by the migration tool. Please " |
| 80 | + + "manually migrate the code using DownloadFilter in v2"; |
| 81 | + return method.withComments(createComments(comment)); |
| 82 | + } |
| 83 | + if (UPLOAD.matches(method) && method.getArguments().size() == 4) { |
| 84 | + String comment = "Migration for InputStream and ObjectMetadata as argument for upload is not supported by the " |
| 85 | + + "migration tool."; |
| 86 | + return method.withComments(createComments(comment)); |
| 87 | + } |
| 88 | + if (UPLOAD_DIRECTORY.matches(method) && method.getArguments().size() > 4) { |
| 89 | + String comment = "Migration for ObjectMetadataProvider as argument for uploadDirectory is not supported by the " |
| 90 | + + "migration tool."; |
| 91 | + return method.withComments(createComments(comment)); |
| 92 | + } |
| 93 | + |
| 94 | + return method; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) { |
| 99 | + JavaType type = newClass.getType(); |
| 100 | + if (!(type instanceof JavaType.FullyQualified)) { |
| 101 | + return newClass; |
| 102 | + } |
| 103 | + |
| 104 | + if (type.isAssignableFrom(S3_TM) && |
| 105 | + !newClass.getArguments().isEmpty() && |
| 106 | + newClass.getArguments().get(0).getType() != null) { |
| 107 | + if (newClass.getArguments().get(0).getType().isAssignableFrom(S3_CLIENT)) { |
| 108 | + String comment = "S3TransferManager requires S3AsyncClient in v2. Please create a new S3AsyncClient " |
| 109 | + + "instance for v2 S3TransferManager."; |
| 110 | + return newClass.withComments(createComments(comment)); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return newClass; |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments