-
Notifications
You must be signed in to change notification settings - Fork 914
Add S3StreamingResponseToV2 Recipe #5173
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
dagnir
merged 2 commits into
feature/master/migration-tool
from
dongie/migration-tool/s3-getobj-response
May 2, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
182 changes: 182 additions & 0 deletions
182
...c/main/java/software/amazon/awssdk/migration/internal/recipe/S3StreamingResponseToV2.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,182 @@ | ||
/* | ||
* 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.migration.internal.recipe; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.Tree; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaVisitor; | ||
import org.openrewrite.java.MethodMatcher; | ||
import org.openrewrite.java.tree.Expression; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.JContainer; | ||
import org.openrewrite.java.tree.JRightPadded; | ||
import org.openrewrite.java.tree.JavaType; | ||
import org.openrewrite.java.tree.Space; | ||
import org.openrewrite.java.tree.TypeUtils; | ||
import org.openrewrite.marker.Markers; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.migration.internal.utils.IdentifierUtils; | ||
|
||
@SdkInternalApi | ||
public class S3StreamingResponseToV2 extends Recipe { | ||
private static final JavaType.FullyQualified S3_OBJECT_TYPE = | ||
TypeUtils.asFullyQualified(JavaType.buildType("com.amazonaws.services.s3.model.S3Object")); | ||
private static final JavaType.FullyQualified S3_GET_OBJECT_RESPONSE_TYPE = | ||
TypeUtils.asFullyQualified(JavaType.buildType("software.amazon.awssdk.services.s3.model.GetObjectResponse")); | ||
|
||
private static final JavaType.FullyQualified RESPONSE_INPUT_STREAM_TYPE = | ||
TypeUtils.asFullyQualified(JavaType.buildType("software.amazon.awssdk.core.ResponseInputStream")); | ||
|
||
private static final MethodMatcher GET_OBJECT_CONTENT = | ||
new MethodMatcher("com.amazonaws.services.s3.model.S3Object getObjectContent()"); | ||
|
||
private static final MethodMatcher OBJECT_INPUT_STREAM_METHOD = | ||
new MethodMatcher("com.amazonaws.services.s3.model.S3ObjectInputStream *(..)"); | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "V1 S3Object to V2"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Transform usage of V1 S3Object to V2."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new Visitor(); | ||
} | ||
|
||
private static class Visitor extends JavaVisitor<ExecutionContext> { | ||
|
||
// Transform an S3Object myObject = ... | ||
// to | ||
// ResponseInputStream<GetObjectResponse> myObject = ... | ||
@Override | ||
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, | ||
ExecutionContext executionContext) { | ||
if (TypeUtils.isAssignableTo(S3_OBJECT_TYPE, multiVariable.getType())) { | ||
JavaType.Parameterized newType = | ||
new JavaType.Parameterized(null, RESPONSE_INPUT_STREAM_TYPE, | ||
Collections.singletonList(S3_GET_OBJECT_RESPONSE_TYPE)); | ||
|
||
maybeAddS3ResponseImports(); | ||
|
||
multiVariable = multiVariable.withType(newType) | ||
.withTypeExpression(IdentifierUtils.makeId(IdentifierUtils.simpleName(newType), | ||
newType)); | ||
|
||
List<J.VariableDeclarations.NamedVariable> variables = multiVariable.getVariables().stream() | ||
.map(nv -> nv.withType(newType)) | ||
.collect(Collectors.toList()); | ||
|
||
multiVariable = multiVariable.withVariables(variables); | ||
} | ||
|
||
multiVariable = super.visitVariableDeclarations(multiVariable, executionContext).cast(); | ||
|
||
return multiVariable; | ||
} | ||
|
||
private void maybeAddS3ResponseImports() { | ||
maybeAddImport(RESPONSE_INPUT_STREAM_TYPE); | ||
dagnir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Note: 'onlyIfReferenced' set to false because OpenRewrite does not seem to consider just having the type as a type | ||
// parameter as being in use, so will not add the import if set to true. | ||
maybeAddImport(S3_GET_OBJECT_RESPONSE_TYPE.getFullyQualifiedName(), null, false); | ||
} | ||
|
||
// In v2, the request is inverted. Whereas S3Object contains the response stream, in V2, a response stream object | ||
// wraps the response object. so the InputStream methods are directly on the "response" object now, and to access the | ||
// non-streaming members, we need to insert a call to response(). | ||
@Override | ||
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) { | ||
method = super.visitMethodInvocation(method, executionContext).cast(); | ||
|
||
Expression select = method.getSelect(); | ||
|
||
if (select == null) { | ||
return method; | ||
} | ||
|
||
if (GET_OBJECT_CONTENT.matches(method)) { | ||
return select; | ||
} | ||
|
||
JavaType.Method methodType = method.getMethodType(); | ||
|
||
if (methodType == null) { | ||
return method; | ||
} | ||
|
||
if (!TypeUtils.isAssignableTo(S3_OBJECT_TYPE, methodType.getDeclaringType())) { | ||
return method; | ||
} | ||
|
||
// Calling a method on the stream, just change the declaring type | ||
if (isObjectContentMethod(method)) { | ||
method = method.withMethodType(methodType.withDeclaringType(RESPONSE_INPUT_STREAM_TYPE)); | ||
return method; | ||
} | ||
|
||
JavaType.Method responseGetterType = new JavaType.Method( | ||
null, | ||
0L, | ||
RESPONSE_INPUT_STREAM_TYPE, | ||
"response", | ||
S3_GET_OBJECT_RESPONSE_TYPE, | ||
Collections.emptyList(), | ||
Collections.emptyList(), | ||
Collections.emptyList(), | ||
Collections.emptyList() | ||
); | ||
|
||
// Calling a method on the response, insert a response() getter | ||
J.Identifier responseGetterId = IdentifierUtils.makeId("response", responseGetterType); | ||
|
||
J.MethodInvocation getResponse = new J.MethodInvocation( | ||
Tree.randomId(), | ||
Space.EMPTY, | ||
Markers.EMPTY, | ||
JRightPadded.build(select), | ||
null, | ||
responseGetterId, | ||
JContainer.empty(), | ||
responseGetterType | ||
); | ||
|
||
methodType = methodType.withDeclaringType(S3_GET_OBJECT_RESPONSE_TYPE); | ||
method = method.withSelect(getResponse) | ||
.withName(method.getName().withType(methodType)) | ||
.withMethodType(methodType); | ||
|
||
return method; | ||
} | ||
|
||
private static boolean isObjectContentMethod(J.MethodInvocation method) { | ||
Expression select = method.getSelect(); | ||
if (select == null || !TypeUtils.isAssignableTo(S3_OBJECT_TYPE, select.getType())) { | ||
return false; | ||
} | ||
return OBJECT_INPUT_STREAM_METHOD.matches(method); | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...n-tool/src/main/java/software/amazon/awssdk/migration/internal/utils/IdentifierUtils.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,73 @@ | ||
/* | ||
* 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.migration.internal.utils; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import org.openrewrite.Tree; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.JavaType; | ||
import org.openrewrite.java.tree.Space; | ||
import org.openrewrite.java.tree.TypeUtils; | ||
import org.openrewrite.marker.Markers; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
|
||
@SdkInternalApi | ||
public final class IdentifierUtils { | ||
private IdentifierUtils() { | ||
} | ||
|
||
/** | ||
* Utility method for creating a {@link J.Identifier}. | ||
*/ | ||
public static J.Identifier makeId(String simpleName, JavaType type) { | ||
return new J.Identifier( | ||
Tree.randomId(), | ||
Space.EMPTY, | ||
Markers.EMPTY, | ||
Collections.emptyList(), | ||
simpleName, | ||
type, | ||
null | ||
); | ||
} | ||
|
||
/** | ||
* Simple method for creating a simple name for a {@link JavaType.Parameterized} instance. This is a naive implementation | ||
* that currently requires all type parameters to be {@link JavaType.FullyQualified}, and does not handle nested | ||
* parameterized types. | ||
*/ | ||
public static String simpleName(JavaType.Parameterized parameterized) { | ||
StringBuilder sb = new StringBuilder(parameterized.getClassName()) | ||
.append('<'); | ||
|
||
List<JavaType> typeParams = parameterized.getTypeParameters(); | ||
|
||
for (int i = 0; i < typeParams.size(); ++i) { | ||
JavaType.FullyQualified fqParamType = TypeUtils.asFullyQualified(typeParams.get(i)); | ||
if (fqParamType == null) { | ||
throw new RuntimeException("Encountered non fully qualified type"); | ||
} | ||
sb.append(fqParamType.getClassName()); | ||
|
||
if (i + 1 != typeParams.size()) { | ||
sb.append(", "); | ||
} | ||
} | ||
|
||
return sb.append('>').toString(); | ||
} | ||
} |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit : Do we need the maybe ? I thought we could just do AddS3ResponseImports and possibly specify a comment that explains under what conditions this happens.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to keep the
maybe
since these visitor methods can be called any number of times and it clearly signifies that we only conditionally add them (i.e. if they aren't already present).