Skip to content

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor

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.

Copy link
Contributor Author

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).


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);
// 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);
}
}
}
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -845,12 +845,6 @@ recipeList:
newGroupId: software.amazon.awssdk
newArtifactId: transfer
newVersion: 2.23.16-SNAPSHOT
- org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId:
oldGroupId: com.amazonaws
oldArtifactId: aws-java-sdk-deadline
newGroupId: software.amazon.awssdk
newArtifactId: deadline
newVersion: 2.23.16-SNAPSHOT
- org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId:
oldGroupId: com.amazonaws
oldArtifactId: aws-java-sdk-braket
Expand Down Expand Up @@ -2177,12 +2171,6 @@ recipeList:
newGroupId: software.amazon.awssdk
newArtifactId: ram
newVersion: 2.23.16-SNAPSHOT
- org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId:
oldGroupId: com.amazonaws
oldArtifactId: aws-java-sdk-codeconnections
newGroupId: software.amazon.awssdk
newArtifactId: codeconnections
newVersion: 2.23.16-SNAPSHOT
- org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId:
oldGroupId: com.amazonaws
oldArtifactId: aws-java-sdk-efs
Expand Down
Loading