-
Notifications
You must be signed in to change notification settings - Fork 916
Added recipe for adding comments for unsupported transforms #6033
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
Fred1155
merged 14 commits into
master
from
feature/master/migration-tool-comments-for-unsupported-transforms
Apr 15, 2025
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
19d892d
Add new recipe S3AddImportsAndComments
davidh44 31602ea
Do not remove AccessControlList import
davidh44 48a9157
Fix pattern for InitiateMpu
davidh44 bc7e515
more unsupported method comments added
Fred1155 de2f1d9
Merge branch 'feature/master/migration-tool-comments-for-unsupported-…
Fred1155 59c5dbf
listNextBatchOfObjects comments added
Fred1155 799b7ee
more unsupported method comments added
Fred1155 f96812b
checkstyle fixed
Fred1155 e4d8363
more comments added
Fred1155 113e52b
comments addressed
Fred1155 ba289c1
Merge branch 'master' into feature/master/migration-tool-comments-for…
Fred1155 f0ecebd
integration test fixed
Fred1155 94e009e
minor fix
Fred1155 db1b6e2
Merge branch 'master' into feature/master/migration-tool-comments-for…
Fred1155 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
205 changes: 205 additions & 0 deletions
205
v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3AddImportsAndComments.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
/* | ||
* 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.v2migration; | ||
|
||
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V1_S3_MODEL_PKG; | ||
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_S3_MODEL_PKG; | ||
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.createComments; | ||
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.v1S3MethodMatcher; | ||
|
||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.AddImport; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.MethodMatcher; | ||
import org.openrewrite.java.RemoveImport; | ||
import org.openrewrite.java.tree.Expression; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.JavaType; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
|
||
@SdkInternalApi | ||
public class S3AddImportsAndComments extends Recipe { | ||
|
||
private static final MethodMatcher CREATE_BUCKET = v1S3MethodMatcher("createBucket(String, " | ||
+ V1_S3_MODEL_PKG + "Region"); | ||
private static final MethodMatcher LIST_NEXT_BATCH_OBJECTS = v1S3MethodMatcher("listNextBatchOfObjects(..)"); | ||
private static final MethodMatcher LIST_NEXT_BATCH_VERSIONS = v1S3MethodMatcher("listNextBatchOfVersions(..)"); | ||
private static final MethodMatcher GET_METADATA = v1S3MethodMatcher("getCachedResponseMetadata(..)"); | ||
private static final MethodMatcher SET_BUCKET_ACL = v1S3MethodMatcher("setBucketAcl(..)"); | ||
private static final MethodMatcher SET_ENDPOINT = v1S3MethodMatcher("setEndpoint(..)"); | ||
private static final MethodMatcher SET_OBJECT_ACL = v1S3MethodMatcher("setObjectAcl(..)"); | ||
private static final MethodMatcher SET_REGION = v1S3MethodMatcher("setRegion(..)"); | ||
private static final MethodMatcher SET_S3CLIENT_OPTIONS = v1S3MethodMatcher("setS3ClientOptions(..)"); | ||
private static final MethodMatcher SELECT_OBJECT_CONTENT = v1S3MethodMatcher("selectObjectContent(..)"); | ||
|
||
private static final Pattern CANNED_ACL = Pattern.compile(V1_S3_MODEL_PKG + "CannedAccessControlList"); | ||
private static final Pattern GET_OBJECT_REQUEST = Pattern.compile(V1_S3_MODEL_PKG + "GetObjectRequest"); | ||
private static final Pattern INITIATE_MPU = Pattern.compile(V1_S3_MODEL_PKG + "InitiateMultipartUpload"); | ||
private static final Pattern MULTI_FACTOR_AUTH = Pattern.compile(V1_S3_MODEL_PKG + "MultiFactorAuthentication"); | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Add imports and comments to unsupported S3 transforms."; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Add imports and comments to unsupported S3 transforms."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new Visitor(); | ||
} | ||
|
||
private static class Visitor extends JavaIsoVisitor<ExecutionContext> { | ||
|
||
@Override | ||
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
boolean isSetObjectAcl = SET_OBJECT_ACL.matches(method); | ||
boolean isSetBucketAcl = SET_BUCKET_ACL.matches(method); | ||
|
||
if (isSetObjectAcl || isSetBucketAcl) { | ||
removeV1S3ModelImport("CannedAccessControlList"); | ||
maybeAddV2CannedAclImport(method.getArguments(), isSetObjectAcl, isSetBucketAcl); | ||
|
||
// TODO: add the developer guide link in the comments once the doc is published. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be confusing since the developer guide is not published yet? |
||
String comment = "Transform for AccessControlList and CannedAccessControlList not supported. " | ||
+ "In v2, CannedAccessControlList is replaced by BucketCannedACL for buckets and " | ||
+ "ObjectCannedACL for objects."; | ||
return method.withComments(createComments(comment)); | ||
} | ||
if (LIST_NEXT_BATCH_OBJECTS.matches(method)) { | ||
// TODO: add the developer guide link in the comments once the doc is published. | ||
String comment = "Transform for listNextBatchOfObjects method not supported. " | ||
+ "listNextBatchOfObjects() only exists in SDK v1, for SDK v2 use either " | ||
+ "listObjectsV2Paginator().stream() for automatic pagination" | ||
+ " or manually handle pagination with listObjectsV2() and nextToken in the response for more " | ||
+ "control"; | ||
return method.withComments(createComments(comment)); | ||
} | ||
if (LIST_NEXT_BATCH_VERSIONS.matches(method)) { | ||
// TODO: add the developer guide link in the comments once the doc is published. | ||
String comment = "Transform for listNextBatchOfVersions method not supported." | ||
+ "listNextBatchOfVersions() only exists in SDK v1, for SDK v2 use either " | ||
+ "listObjectVersionsPaginator().stream for automatic pagination" | ||
+ " or manually handle pagination with listObjectVersions() and VersionIdMarker/KeyMarker. "; | ||
return method.withComments(createComments(comment)); | ||
} | ||
if (SET_REGION.matches(method)) { | ||
String comment = "Transform for setRegion method not supported. Please manually " | ||
+ "migrate your code by configuring the region in the s3 client builder"; | ||
return method.withComments(createComments(comment)); | ||
} | ||
if (SET_S3CLIENT_OPTIONS.matches(method)) { | ||
String comment = "Transform for setS3ClientOptions method not supported. Please manually " | ||
+ "migrate setS3ClientOptions by configuring the equivalent settings in " | ||
+ "S3Configuration.builder() when building your S3Client."; | ||
return method.withComments(createComments(comment)); | ||
} | ||
if (SELECT_OBJECT_CONTENT.matches(method)) { | ||
String comment = "Note: selectObjectContent is only supported in AWS SDK v2 with S3AsyncClient. " | ||
+ "Please manually migrate to event-based response handling using " | ||
+ "SelectObjectContentEventStream"; | ||
return method.withComments(createComments(comment)); | ||
} | ||
|
||
if (CREATE_BUCKET.matches(method)) { | ||
String comment = "Transform for createBucket(String bucketName, Region region) method not supported. Please " | ||
+ "manually migrate your code by using the following pattern: " | ||
+ "createBucket(builder -> builder.bucket(bucketName)" | ||
+ ".createBucketConfiguration(cfg -> cfg.locationConstraint(region)))"; | ||
return method.withComments(createComments(comment)); | ||
} | ||
|
||
if (SET_ENDPOINT.matches(method)) { | ||
String comment = "Transform for setEndpoint method not supported. setEndpoint() method is removed in SDK v2. " | ||
+ "Please manually migrate your code by using endpointOverride(URI.create(endpoint)) in " | ||
+ "S3ClientBuilder"; | ||
return method.withComments(createComments(comment)); | ||
} | ||
|
||
if (GET_METADATA.matches(method)) { | ||
String comment = "Transform for getCachedResponseMetadata method not " | ||
+ "supported. getCachedResponseMetadata() is removed in SDK v2. Please manually migrate your " | ||
+ "code by accessing metadata directly from specific response objects instead of cached " | ||
+ "metadata"; | ||
return method.withComments(createComments(comment)); | ||
} | ||
|
||
return method; | ||
} | ||
|
||
@Override | ||
public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) { | ||
JavaType type = newClass.getType(); | ||
if (!(type instanceof JavaType.FullyQualified)) { | ||
return newClass; | ||
} | ||
|
||
if (type.isAssignableFrom(MULTI_FACTOR_AUTH)) { | ||
removeV1S3ModelImport("MultiFactorAuthentication"); | ||
String comment = "v2 does not have a MultiFactorAuthentication POJO. Please manually set the String value on " | ||
+ "the request POJO."; | ||
return newClass.withComments(createComments(comment)); | ||
} | ||
|
||
if (type.isAssignableFrom(GET_OBJECT_REQUEST) && newClass.getArguments().size() == 1) { | ||
removeV1S3ModelImport("S3ObjectId"); | ||
String comment = "v2 does not have S3ObjectId class. Please manually migrate the code by setting the configs " | ||
+ "directly into the request builder pattern."; | ||
return newClass.withComments(createComments(comment)); | ||
} | ||
|
||
if (type.isAssignableFrom(INITIATE_MPU) && newClass.getArguments().size() == 3) { | ||
String comment = "Transform for ObjectMetadata in initiateMultipartUpload() method is not supported. Please " | ||
+ "manually migrate your code by replacing ObjectMetadata with individual setter methods " | ||
+ "or metadata map in the request builder."; | ||
return newClass.withComments(createComments(comment)); | ||
} | ||
|
||
return newClass; | ||
} | ||
|
||
private void maybeAddV2CannedAclImport(List<Expression> args, boolean isSetObjectAcl, boolean isSetBucketAcl) { | ||
for (Expression expr : args) { | ||
JavaType type = expr.getType(); | ||
if (type == null || !type.isAssignableFrom(CANNED_ACL)) { | ||
continue; | ||
} | ||
removeV1S3ModelImport("CannedAccessControlList"); | ||
if (isSetBucketAcl) { | ||
addV2S3ModelImport("BucketCannedACL"); | ||
} | ||
if (isSetObjectAcl) { | ||
addV2S3ModelImport("ObjectCannedACL"); | ||
} | ||
} | ||
} | ||
|
||
private void removeV1S3ModelImport(String className) { | ||
doAfterVisit(new RemoveImport<>(V1_S3_MODEL_PKG + className, true)); | ||
} | ||
|
||
private void addV2S3ModelImport(String className) { | ||
doAfterVisit(new AddImport<>(V2_S3_MODEL_PKG + className, null, false)); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.