Skip to content

Commit 3b0ddf1

Browse files
zoewanggdavidh44
andauthored
Convert SdkBytes to ByteBuffer for POJO getters (#5791)
* Convert SdkBytes to ByteBuffer for POJO getters * Update v2-migration/src/main/java/software/amazon/awssdk/v2migration/SdkBytesToByteBuffer.java * Update v2-migration/src/main/java/software/amazon/awssdk/v2migration/SdkBytesToByteBuffer.java * Update v2-migration/src/main/java/software/amazon/awssdk/v2migration/SdkBytesToByteBuffer.java * Fix build failure --------- Co-authored-by: David Ho <70000000+davidh44@users.noreply.github.com>
1 parent 5a6d4b5 commit 3b0ddf1

File tree

5 files changed

+153
-1
lines changed

5 files changed

+153
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS SDK for Java v2 Migration Tool",
4+
"contributor": "",
5+
"description": "Transform the getter methods on the service model classes that return SdkBytes to return ByteBuffer to be compatible with v1 style getters"
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 foo.bar;
17+
18+
import java.nio.ByteBuffer;
19+
import software.amazon.awssdk.services.sqs.model.MessageAttributeValue;
20+
21+
public class SdkBytes {
22+
23+
void sdkBytesGetters() {
24+
MessageAttributeValue messageAttributeValue = MessageAttributeValue.builder()
25+
.build();
26+
ByteBuffer binaryValue = messageAttributeValue.binaryValue().asByteBuffer();
27+
String binaryString = new String(messageAttributeValue.binaryValue().asByteBuffer().array());
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 foo.bar;
17+
18+
import com.amazonaws.services.sqs.model.MessageAttributeValue;
19+
import java.nio.ByteBuffer;
20+
21+
public class SdkBytes {
22+
23+
void sdkBytesGetters() {
24+
MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
25+
ByteBuffer binaryValue = messageAttributeValue.getBinaryValue();
26+
String binaryString = new String(messageAttributeValue.getBinaryValue().array());
27+
}
28+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 java.nio.ByteBuffer;
19+
import java.util.regex.Pattern;
20+
import org.openrewrite.ExecutionContext;
21+
import org.openrewrite.NlsRewrite;
22+
import org.openrewrite.Recipe;
23+
import org.openrewrite.TreeVisitor;
24+
import org.openrewrite.java.JavaIsoVisitor;
25+
import org.openrewrite.java.JavaTemplate;
26+
import org.openrewrite.java.tree.J;
27+
import org.openrewrite.java.tree.JavaType.FullyQualified;
28+
import org.openrewrite.java.tree.JavaType.Method;
29+
import software.amazon.awssdk.annotations.SdkInternalApi;
30+
import software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils;
31+
32+
@SdkInternalApi
33+
public class SdkBytesToByteBuffer extends Recipe {
34+
private static final Pattern BYTE_BUFFER_PATTERN = Pattern.compile(ByteBuffer.class.getCanonicalName());
35+
36+
@Override
37+
public @NlsRewrite.DisplayName String getDisplayName() {
38+
return "Convert SdkBytes to ByteBuffer";
39+
}
40+
41+
@Override
42+
public @NlsRewrite.Description String getDescription() {
43+
return "Convert SdkBytes to ByteBuffer by calling SdkBytes#asByteBuffer()";
44+
}
45+
46+
@Override
47+
public TreeVisitor<?, ExecutionContext> getVisitor() {
48+
return new SdkBytesToBufferVisitor();
49+
}
50+
51+
private static boolean isV2ModelGetterReturningByteBuffer(J.MethodInvocation method) {
52+
Method mt = method.getMethodType();
53+
54+
if (mt != null) {
55+
FullyQualified declaringType = mt.getDeclaringType();
56+
boolean isByteBuffer = mt.getReturnType().isAssignableFrom(BYTE_BUFFER_PATTERN);
57+
if (SdkTypeUtils.isV2ModelClass(declaringType) && isByteBuffer) {
58+
return true;
59+
}
60+
}
61+
return false;
62+
}
63+
64+
private static final class SdkBytesToBufferVisitor extends JavaIsoVisitor<ExecutionContext> {
65+
@Override
66+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation originalMethod,
67+
ExecutionContext executionContext) {
68+
J.MethodInvocation method =
69+
super.visitMethodInvocation(originalMethod, executionContext);
70+
if (!isV2ModelGetterReturningByteBuffer(method)) {
71+
return method;
72+
}
73+
74+
String methodName = method.getSimpleName();
75+
JavaTemplate template = JavaTemplate
76+
.builder(method.getSelect() + "." + methodName + "()." + "asByteBuffer()")
77+
.contextSensitive()
78+
.build();
79+
80+
method = template.apply(
81+
updateCursor(method),
82+
method.getCoordinates().replace()
83+
);
84+
85+
return method;
86+
}
87+
}
88+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ recipeList:
3737
- software.amazon.awssdk.v2migration.V1GetterToV2
3838
- software.amazon.awssdk.v2migration.HttpSettingsToHttpClient
3939
- software.amazon.awssdk.v2migration.WrapSdkClientBuilderRegionStr
40-
- software.amazon.awssdk.v2migration.EnumCasingToV2
40+
- software.amazon.awssdk.v2migration.EnumCasingToV2
41+
- software.amazon.awssdk.v2migration.SdkBytesToByteBuffer

0 commit comments

Comments
 (0)