Skip to content

Commit a6be409

Browse files
authored
Migration tool recipe for adding comments for unsupported methods (#6195)
* comments added * all comments added
1 parent 078cd75 commit a6be409

File tree

8 files changed

+198
-5
lines changed

8 files changed

+198
-5
lines changed

test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-tm/after/src/main/java/foo/bar/TransferManagerS3.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,22 @@
2828
import software.amazon.awssdk.transfer.s3.model.Copy;
2929
import software.amazon.awssdk.transfer.s3.model.CopyRequest;
3030
import software.amazon.awssdk.transfer.s3.model.DirectoryDownload;
31+
import software.amazon.awssdk.transfer.s3.model.DirectoryUpload;
3132
import software.amazon.awssdk.transfer.s3.model.DownloadDirectoryRequest;
3233
import software.amazon.awssdk.transfer.s3.model.DownloadFileRequest;
3334
import software.amazon.awssdk.transfer.s3.model.FileDownload;
3435
import software.amazon.awssdk.transfer.s3.model.FileUpload;
3536
import software.amazon.awssdk.transfer.s3.model.ResumableFileDownload;
3637
import software.amazon.awssdk.transfer.s3.model.ResumableFileUpload;
38+
import software.amazon.awssdk.transfer.s3.model.ResumableTransfer;
39+
import software.amazon.awssdk.transfer.s3.model.UploadDirectoryRequest;
3740
import software.amazon.awssdk.transfer.s3.model.UploadFileRequest;
3841
import software.amazon.awssdk.transfer.s3.model.UploadRequest;
42+
import software.amazon.awssdk.transfer.s3.progress.TransferProgress;
3943

4044
import java.io.File;
45+
import java.io.IOException;
46+
import java.io.OutputStream;
4147
import java.time.Duration;
4248

4349
public class TransferManagerS3 {
@@ -93,8 +99,19 @@ void downloadDirectory(S3TransferManager tm, File destination) {
9399
tm.close();
94100
}
95101

102+
void uploadDirectory(S3TransferManager tm) {
103+
DirectoryUpload fileUpload1 = tm.uploadDirectory(UploadDirectoryRequest.builder().bucket("bucket").s3Prefix("prefix").source(file.toPath()).maxDepth(true ? Integer.MAX_VALUE : 1).build());
104+
}
105+
96106
void resume(S3TransferManager tm, ResumableFileDownload persistableDownload, ResumableFileUpload persistableUpload) {
97107
FileDownload download = tm.resumeDownloadFile(persistableDownload);
98108
FileUpload upload = tm.resumeUploadFile(persistableUpload);
99109
}
100-
}
110+
111+
void POJO_methods(ResumableTransfer transfer, OutputStream outputStream, TransferProgress progress) throws IOException {
112+
String s = transfer.serializeToString();
113+
transfer.serializeToOutputStream(outputStream);
114+
115+
long bytesTransferred = progress.snapshot().transferredBytes();
116+
}
117+
}

test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-tm/before/src/main/java/foo/bar/TransferManagerS3.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@
2323
import com.amazonaws.services.s3.transfer.Copy;
2424
import com.amazonaws.services.s3.transfer.Download;
2525
import com.amazonaws.services.s3.transfer.MultipleFileDownload;
26+
import com.amazonaws.services.s3.transfer.MultipleFileUpload;
2627
import com.amazonaws.services.s3.transfer.PersistableDownload;
28+
import com.amazonaws.services.s3.transfer.PersistableTransfer;
2729
import com.amazonaws.services.s3.transfer.PersistableUpload;
2830
import com.amazonaws.services.s3.transfer.TransferManager;
2931
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
32+
import com.amazonaws.services.s3.transfer.TransferProgress;
3033
import com.amazonaws.services.s3.transfer.Upload;
3134
import java.io.File;
35+
import java.io.IOException;
36+
import java.io.OutputStream;
3237

3338
public class TransferManagerS3 {
3439

@@ -78,8 +83,19 @@ void downloadDirectory(TransferManager tm, File destination) {
7883
tm.shutdownNow();
7984
}
8085

86+
void uploadDirectory(TransferManager tm) {
87+
MultipleFileUpload fileUpload1 = tm.uploadDirectory("bucket", "prefix", file, true);
88+
}
89+
8190
void resume(TransferManager tm, PersistableDownload persistableDownload, PersistableUpload persistableUpload) {
8291
Download download = tm.resumeDownload(persistableDownload);
8392
Upload upload = tm.resumeUpload(persistableUpload);
8493
}
85-
}
94+
95+
void POJO_methods(PersistableTransfer transfer, OutputStream outputStream, TransferProgress progress) throws IOException {
96+
String s = transfer.serialize();
97+
transfer.serialize(outputStream);
98+
99+
long bytesTransferred = progress.getBytesTransferred();
100+
}
101+
}

v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3AddImportsAndComments.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public class S3AddImportsAndComments extends Recipe {
5858
private static final MethodMatcher GET_EXPIRY_TIME = v1EnMethodMatcher("S3EventNotification.RestoreEventDataEntity "
5959
+ "getLifecycleRestorationExpiryTime(..)");
6060

61-
6261
private static final Pattern CANNED_ACL = Pattern.compile(V1_S3_MODEL_PKG + "CannedAccessControlList");
6362
private static final Pattern GET_OBJECT_REQUEST = Pattern.compile(V1_S3_MODEL_PKG + "GetObjectRequest");
6463
private static final Pattern CREATE_BUCKET_REQUEST = Pattern.compile(V1_S3_MODEL_PKG + "CreateBucketRequest");
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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, ..)",
44+
V2_S3_MODEL_PKG, 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.matches(method) && method.getArguments().size() == 2) {
89+
String comment = "Migration for S3ProgressListener is not supported by the migration tool. Please manually "
90+
+ "migrate the code using TransferListener in v2";
91+
return method.withComments(createComments(comment));
92+
}
93+
if (UPLOAD_DIRECTORY.matches(method) && method.getArguments().size() > 4) {
94+
String comment = "Migration for ObjectMetadataProvider as argument for uploadDirectory is not supported by the "
95+
+ "migration tool.";
96+
return method.withComments(createComments(comment));
97+
}
98+
99+
return method;
100+
}
101+
102+
@Override
103+
public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
104+
JavaType type = newClass.getType();
105+
if (!(type instanceof JavaType.FullyQualified)) {
106+
return newClass;
107+
}
108+
109+
if (type.isAssignableFrom(S3_TM) &&
110+
!newClass.getArguments().isEmpty() &&
111+
newClass.getArguments().get(0).getType() != null) {
112+
if (newClass.getArguments().get(0).getType().isAssignableFrom(S3_CLIENT)) {
113+
String comment = "S3TransferManager requires S3AsyncClient in v2. Please create a new S3AsyncClient "
114+
+ "instance for v2 S3TransferManager.";
115+
return newClass.withComments(createComments(comment));
116+
}
117+
}
118+
119+
return newClass;
120+
}
121+
}
122+
}

v2-migration/src/main/java/software/amazon/awssdk/v2migration/TransferManagerMethodsToV2.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public class TransferManagerMethodsToV2 extends Recipe {
5353

5454
private static final MethodMatcher DOWNLOAD_DIR = v2TmMethodMatcher("downloadDirectory(String, String, java.io.File)");
5555

56+
private static final MethodMatcher UPLOAD_DIR = v2TmMethodMatcher("uploadDirectory(String, String, java.io.File, boolean)");
57+
5658
private static final Pattern S3_TM_CREDENTIAL = Pattern.compile(V2_TM_CLIENT);
5759
private static final Pattern V2_AWSCREDENTAIL = Pattern.compile("software.amazon.awssdk.auth.credentials.AwsCredentials");
5860
private static final Pattern V2_CREDENTIAL_PROVIDER = Pattern.compile("software.amazon.awssdk.auth.credentials"
@@ -110,6 +112,10 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext execu
110112
method = transformDownloadDirectory(method);
111113
return super.visitMethodInvocation(method, executionContext);
112114
}
115+
if (UPLOAD_DIR.matches(method, false)) {
116+
method = transformUploadDirectory(method);
117+
return super.visitMethodInvocation(method, executionContext);
118+
}
113119

114120
return super.visitMethodInvocation(method, executionContext);
115121
}
@@ -165,11 +171,24 @@ private J.MethodInvocation transformDownloadDirectory(J.MethodInvocation method)
165171
method.getArguments().get(0), method.getArguments().get(1),
166172
method.getArguments().get(2));
167173

168-
addTmImport("DirectoryDownload");
169174
addTmImport("DownloadDirectoryRequest");
170175
return method;
171176
}
172177

178+
private J.MethodInvocation transformUploadDirectory(J.MethodInvocation method) {
179+
String v2Method = "#{any()}.uploadDirectory(UploadDirectoryRequest.builder()"
180+
+ ".bucket(#{any()}).s3Prefix(#{any()}).source(#{any()}.toPath())"
181+
+ ".maxDepth(#{any()} ? Integer.MAX_VALUE : 1).build())";
182+
183+
method = JavaTemplate.builder(v2Method).build()
184+
.apply(getCursor(), method.getCoordinates().replace(), method.getSelect(),
185+
method.getArguments().get(0), method.getArguments().get(1),
186+
method.getArguments().get(2), method.getArguments().get(3));
187+
188+
addTmImport("UploadDirectoryRequest");
189+
return method;
190+
}
191+
173192
private J.MethodInvocation transformUploadWithBucketKeyFile(J.MethodInvocation method) {
174193
String v2Method = "#{any()}.uploadFile(UploadFileRequest.builder()"
175194
+ ".putObjectRequest(PutObjectRequest.builder().bucket(#{any()}).key(#{any()}).build())"

v2-migration/src/main/java/software/amazon/awssdk/v2migration/internal/utils/S3TransformUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public final class S3TransformUtils {
3838
public static final String V1_S3_MODEL_PKG = "com.amazonaws.services.s3.model.";
3939
public static final String V1_S3_PKG = "com.amazonaws.services.s3.";
4040
public static final String V1_EN_PKG = "com.amazonaws.services.s3.event.";
41+
public static final String V1_TM_PKG = "com.amazonaws.services.s3.transfer.";
4142

4243
public static final String V2_S3_CLIENT = "software.amazon.awssdk.services.s3.S3Client";
4344
public static final String V2_S3_MODEL_PKG = "software.amazon.awssdk.services.s3.model.";

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ recipeList:
5353
- software.amazon.awssdk.v2migration.S3NonStreamingRequestToV2Complex
5454
- software.amazon.awssdk.v2migration.S3PutObjectRequestToV2
5555
- software.amazon.awssdk.v2migration.SettersToBuilderV2
56+
- software.amazon.awssdk.v2migration.S3TmAddComments
5657
- software.amazon.awssdk.v2migration.ChangeTransferManagerSimpleMethods
5758
- software.amazon.awssdk.v2migration.TransferManagerMethodsToV2

v2-migration/src/main/resources/META-INF/rewrite/change-transfer-manager-simple-methods.yml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,22 @@ recipeList:
2525
newMethodName: resumeUploadFile
2626
- org.openrewrite.java.ChangeMethodName:
2727
methodPattern: software.amazon.awssdk.transfer.s3.S3TransferManager shutdownNow()
28-
newMethodName: close
28+
newMethodName: close
29+
- org.openrewrite.java.ChangeMethodName:
30+
methodPattern: software.amazon.awssdk.transfer.s3.model.Transfer getProgress()
31+
newMethodName: progress
32+
- org.openrewrite.java.ChangeMethodName:
33+
methodPattern: software.amazon.awssdk.transfer.s3.model.ResumableTransfer serialize()
34+
newMethodName: serializeToString
35+
- org.openrewrite.java.ChangeMethodName:
36+
methodPattern: software.amazon.awssdk.transfer.s3.model.ResumableTransfer serialize(java.io.OutputStream)
37+
newMethodName: serializeToOutputStream
38+
- org.openrewrite.java.ChangeMethodName:
39+
methodPattern: software.amazon.awssdk.transfer.s3.progress.TransferProgress getBytesTransferred()
40+
newMethodName: snapshot().transferredBytes
41+
- org.openrewrite.java.ChangeMethodName:
42+
methodPattern: software.amazon.awssdk.transfer.s3.progress.TransferProgress getTotalBytesToTransfer()
43+
newMethodName: snapshot().totalBytes
44+
- org.openrewrite.java.ChangeMethodName:
45+
methodPattern: software.amazon.awssdk.transfer.s3.progress.TransferProgress getPercentTransferred()
46+
newMethodName: snapshot().ratioTransferred

0 commit comments

Comments
 (0)