From 0eed4fab0fb967d8a6edbd17f6a227e13c434db7 Mon Sep 17 00:00:00 2001 From: Zoe Wang <33073555+zoewangg@users.noreply.github.com> Date: Wed, 15 Jan 2025 11:35:36 -0800 Subject: [PATCH] Convert ByteBuffer to SdkBytes --- ...-AWSSDKforJavav2MigrationTool-21fc092.json | 6 + ...dkBytes.java => SdkBytesGetterSetter.java} | 12 +- ...dkBytes.java => SdkBytesGetterSetter.java} | 10 +- .../v2migration/ByteBufferToSdkBytes.java | 103 ++++++++++++++++++ .../rewrite/aws-sdk-java-v1-to-v2.yml | 3 +- 5 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 .changes/next-release/bugfix-AWSSDKforJavav2MigrationTool-21fc092.json rename test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/after/src/main/java/foo/bar/{SdkBytes.java => SdkBytesGetterSetter.java} (71%) rename test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/before/src/main/java/foo/bar/{SdkBytes.java => SdkBytesGetterSetter.java} (74%) create mode 100644 v2-migration/src/main/java/software/amazon/awssdk/v2migration/ByteBufferToSdkBytes.java diff --git a/.changes/next-release/bugfix-AWSSDKforJavav2MigrationTool-21fc092.json b/.changes/next-release/bugfix-AWSSDKforJavav2MigrationTool-21fc092.json new file mode 100644 index 00000000000..f1c284151c8 --- /dev/null +++ b/.changes/next-release/bugfix-AWSSDKforJavav2MigrationTool-21fc092.json @@ -0,0 +1,6 @@ +{ + "type": "bugfix", + "category": "AWS SDK for Java v2 Migration Tool", + "contributor": "", + "description": "Transform the setter methods on the service model classes that take SdkBytes to take ByteBuffer to be compatible with v1 style setters" +} diff --git a/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/after/src/main/java/foo/bar/SdkBytes.java b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/after/src/main/java/foo/bar/SdkBytesGetterSetter.java similarity index 71% rename from test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/after/src/main/java/foo/bar/SdkBytes.java rename to test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/after/src/main/java/foo/bar/SdkBytesGetterSetter.java index a91289ce025..866092e40c6 100644 --- a/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/after/src/main/java/foo/bar/SdkBytes.java +++ b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/after/src/main/java/foo/bar/SdkBytesGetterSetter.java @@ -16,13 +16,23 @@ package foo.bar; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.services.sqs.model.MessageAttributeValue; -public class SdkBytes { +public class SdkBytesGetterSetter { + + void byteBufferSetter() { + ByteBuffer buffer = ByteBuffer.wrap("helloworld".getBytes(StandardCharsets.UTF_8)); + MessageAttributeValue messageAttributeValue = MessageAttributeValue.builder() + .binaryValue(SdkBytes.fromByteBuffer(buffer)) + .build(); + } void sdkBytesGetters() { MessageAttributeValue messageAttributeValue = MessageAttributeValue.builder() .build(); + ByteBuffer binaryValue = messageAttributeValue.binaryValue().asByteBuffer(); String binaryString = new String(messageAttributeValue.binaryValue().asByteBuffer().array()); } diff --git a/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/before/src/main/java/foo/bar/SdkBytes.java b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/before/src/main/java/foo/bar/SdkBytesGetterSetter.java similarity index 74% rename from test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/before/src/main/java/foo/bar/SdkBytes.java rename to test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/before/src/main/java/foo/bar/SdkBytesGetterSetter.java index 36bad73c817..b229ec9e8c5 100644 --- a/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/before/src/main/java/foo/bar/SdkBytes.java +++ b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/before/src/main/java/foo/bar/SdkBytesGetterSetter.java @@ -17,11 +17,19 @@ import com.amazonaws.services.sqs.model.MessageAttributeValue; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; -public class SdkBytes { +public class SdkBytesGetterSetter { + + void byteBufferSetter() { + ByteBuffer buffer = ByteBuffer.wrap("helloworld".getBytes(StandardCharsets.UTF_8)); + MessageAttributeValue messageAttributeValue = new MessageAttributeValue() + .withBinaryValue(buffer); + } void sdkBytesGetters() { MessageAttributeValue messageAttributeValue = new MessageAttributeValue(); + ByteBuffer binaryValue = messageAttributeValue.getBinaryValue(); String binaryString = new String(messageAttributeValue.getBinaryValue().array()); } diff --git a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/ByteBufferToSdkBytes.java b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/ByteBufferToSdkBytes.java new file mode 100644 index 00000000000..161ffcdba8a --- /dev/null +++ b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/ByteBufferToSdkBytes.java @@ -0,0 +1,103 @@ +/* + * 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 java.nio.ByteBuffer; +import java.util.List; +import java.util.regex.Pattern; +import org.openrewrite.ExecutionContext; +import org.openrewrite.NlsRewrite; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; +import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.JavaTemplate; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.JavaType; +import org.openrewrite.java.tree.JavaType.FullyQualified; +import org.openrewrite.java.tree.JavaType.Method; +import software.amazon.awssdk.annotations.SdkInternalApi; +import software.amazon.awssdk.core.SdkBytes; +import software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils; + +@SdkInternalApi +public class ByteBufferToSdkBytes extends Recipe { + private static final Pattern BYTE_BUFFER_PATTERN = Pattern.compile(ByteBuffer.class.getCanonicalName()); + + @Override + public @NlsRewrite.DisplayName String getDisplayName() { + return "Convert ByteBuffer to SdkBytes"; + } + + @Override + public @NlsRewrite.Description String getDescription() { + return "Convert ByteBuffer to SdkBytes by calling SdkBytes#fromByteBuffer"; + } + + @Override + public TreeVisitor getVisitor() { + return new SdkBytesToBufferVisitor(); + } + + private static final class SdkBytesToBufferVisitor extends JavaIsoVisitor { + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation originalMethod, + ExecutionContext executionContext) { + J.MethodInvocation method = + super.visitMethodInvocation(originalMethod, executionContext); + if (!isV2ModelSetterWithByteBufferParam(method)) { + return method; + } + + JavaTemplate template = JavaTemplate + .builder("SdkBytes.fromByteBuffer(#{any()})") + .contextSensitive() + .imports(SdkBytes.class.getCanonicalName()) + .build(); + + method = template.apply( + updateCursor(method), + method.getCoordinates().replaceArguments(), + method.getArguments().toArray() + ); + + maybeAddImport(SdkBytes.class.getCanonicalName(), false); + return method; + } + + private static boolean isV2ModelSetterWithByteBufferParam(J.MethodInvocation method) { + Method mt = method.getMethodType(); + + if (mt != null) { + FullyQualified declaringType = mt.getDeclaringType(); + List parameterTypes = mt.getParameterTypes(); + if (parameterTypes.size() != 1) { + return false; + } + + JavaType javaType = parameterTypes.get(0); + if (javaType == null) { + return false; + } + + boolean isByteBufferParam = javaType.isAssignableFrom(BYTE_BUFFER_PATTERN); + if (SdkTypeUtils.isV2ModelBuilder(declaringType) && isByteBufferParam) { + return true; + } + } + return false; + } + } +} diff --git a/v2-migration/src/main/resources/META-INF/rewrite/aws-sdk-java-v1-to-v2.yml b/v2-migration/src/main/resources/META-INF/rewrite/aws-sdk-java-v1-to-v2.yml index 84a2dee8655..edccbf38408 100644 --- a/v2-migration/src/main/resources/META-INF/rewrite/aws-sdk-java-v1-to-v2.yml +++ b/v2-migration/src/main/resources/META-INF/rewrite/aws-sdk-java-v1-to-v2.yml @@ -38,4 +38,5 @@ recipeList: - software.amazon.awssdk.v2migration.HttpSettingsToHttpClient - software.amazon.awssdk.v2migration.WrapSdkClientBuilderRegionStr - software.amazon.awssdk.v2migration.EnumCasingToV2 - - software.amazon.awssdk.v2migration.SdkBytesToByteBuffer \ No newline at end of file + - software.amazon.awssdk.v2migration.SdkBytesToByteBuffer + - software.amazon.awssdk.v2migration.ByteBufferToSdkBytes \ No newline at end of file