diff --git a/test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/MavenNoCompileTest.java b/test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/MavenNoCompileTest.java new file mode 100644 index 000000000000..2da211c3ba5b --- /dev/null +++ b/test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/MavenNoCompileTest.java @@ -0,0 +1,56 @@ +/* + * 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.v2migrationtests; + +import static software.amazon.awssdk.v2migrationtests.TestUtils.replaceVersion; + +import java.io.File; +import java.io.IOException; +import org.apache.commons.io.FileUtils; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIf; + +/** + * The transformed code will not compile, only testing the transform itself + */ +public class MavenNoCompileTest extends MavenTestBase { + + @BeforeAll + static void setUp() throws IOException { + mavenBefore = new File(MavenNoCompileTest.class.getResource("maven-nocompile/before").getFile()).toPath(); + mavenAfter = new File(MavenNoCompileTest.class.getResource("maven-nocompile/after").getFile()).toPath(); + target = new File(MavenNoCompileTest.class.getResource("/").getFile()).toPath().getParent(); + + mavenActual = target.resolve("maven-nocompile/actual"); + mavenExpected = target.resolve("maven-nocompile/expected"); + + deleteTempDirectories(); + + FileUtils.copyDirectory(mavenBefore.toFile(), mavenActual.toFile()); + FileUtils.copyDirectory(mavenAfter.toFile(), mavenExpected.toFile()); + + replaceVersion(mavenExpected.resolve("pom.xml"), sdkVersion); + replaceVersion(mavenActual.resolve("pom.xml"), sdkVersion); + } + + @Test + @EnabledIf("versionAvailable") + void mavenProject_shouldConvert() throws IOException { + boolean experimental = true; + verifyTransformation(experimental); + } +} diff --git a/test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/MavenProjectTest.java b/test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/MavenProjectTest.java index ac59bc8b847c..3c950c5ee85c 100644 --- a/test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/MavenProjectTest.java +++ b/test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/MavenProjectTest.java @@ -15,36 +15,19 @@ package software.amazon.awssdk.v2migrationtests; -import static java.util.Collections.addAll; -import static software.amazon.awssdk.v2migrationtests.TestUtils.assertTwoDirectoriesHaveSameStructure; -import static software.amazon.awssdk.v2migrationtests.TestUtils.getMigrationToolVersion; -import static software.amazon.awssdk.v2migrationtests.TestUtils.getVersion; import static software.amazon.awssdk.v2migrationtests.TestUtils.replaceVersion; -import static software.amazon.awssdk.v2migrationtests.TestUtils.run; import java.io.File; import java.io.IOException; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; import org.apache.commons.io.FileUtils; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIf; -import software.amazon.awssdk.utils.Logger; -public class MavenProjectTest { - private static final Logger log = Logger.loggerFor(MavenProjectTest.class); - private static String sdkVersion; - private static Path mavenBefore; - private static Path mavenAfter; - private static Path target; - private static Path mavenActual; - private static Path mavenExpected; +public class MavenProjectTest extends MavenTestBase { @BeforeAll static void setUp() throws IOException { - sdkVersion = getVersion(); mavenBefore = new File(MavenProjectTest.class.getResource("maven/before").getFile()).toPath(); mavenAfter = new File(MavenProjectTest.class.getResource("maven/after").getFile()).toPath(); target = new File(MavenProjectTest.class.getResource("/").getFile()).toPath().getParent(); @@ -61,38 +44,11 @@ static void setUp() throws IOException { replaceVersion(mavenActual.resolve("pom.xml"), sdkVersion); } - private static void deleteTempDirectories() throws IOException { - FileUtils.deleteDirectory(mavenActual.toFile()); - FileUtils.deleteDirectory(mavenExpected.toFile()); - } - @Test @EnabledIf("versionAvailable") void mavenProject_shouldConvert() throws IOException { - verifyTransformation(); + boolean experimental = false; + verifyTransformation(experimental); verifyCompilation(); } - - private static void verifyTransformation() throws IOException { - List rewriteArgs = new ArrayList<>(); - // pin version since updates have broken tests - String rewriteMavenPluginVersion = "5.46.0"; - addAll(rewriteArgs, "mvn", "org.openrewrite.maven:rewrite-maven-plugin:" + rewriteMavenPluginVersion + ":run", - "-Drewrite.recipeArtifactCoordinates=software.amazon.awssdk:v2-migration:"+ getMigrationToolVersion() + "-PREVIEW", - "-Drewrite.activeRecipes=software.amazon.awssdk.v2migration.AwsSdkJavaV1ToV2"); - - run(mavenActual, rewriteArgs.toArray(new String[0])); - FileUtils.deleteDirectory(mavenActual.resolve("target").toFile()); - assertTwoDirectoriesHaveSameStructure(mavenActual, mavenExpected); - } - - private static void verifyCompilation() { - List packageArgs = new ArrayList<>(); - addAll(packageArgs, "mvn", "package"); - run(mavenActual, packageArgs.toArray(new String[0])); - } - - boolean versionAvailable() { - return TestUtils.versionAvailable(sdkVersion); - } } diff --git a/test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/MavenTestBase.java b/test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/MavenTestBase.java new file mode 100644 index 000000000000..4df43daf5712 --- /dev/null +++ b/test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/MavenTestBase.java @@ -0,0 +1,77 @@ +/* + * 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.v2migrationtests; + +import static java.util.Collections.addAll; +import static software.amazon.awssdk.v2migrationtests.TestUtils.assertTwoDirectoriesHaveSameStructure; +import static software.amazon.awssdk.v2migrationtests.TestUtils.getMigrationToolVersion; +import static software.amazon.awssdk.v2migrationtests.TestUtils.getVersion; +import static software.amazon.awssdk.v2migrationtests.TestUtils.run; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import org.apache.commons.io.FileUtils; +import org.junit.jupiter.api.BeforeAll; + +public class MavenTestBase { + + protected static String sdkVersion; + protected static Path mavenBefore; + protected static Path mavenAfter; + protected static Path target; + protected static Path mavenActual; + protected static Path mavenExpected; + + @BeforeAll + static void init() throws IOException { + sdkVersion = getVersion(); + } + + protected static void deleteTempDirectories() throws IOException { + FileUtils.deleteDirectory(mavenActual.toFile()); + FileUtils.deleteDirectory(mavenExpected.toFile()); + } + + protected static void verifyTransformation(boolean experimental) throws IOException { + String recipeCmd = "-Drewrite.activeRecipes=software.amazon.awssdk.v2migration.AwsSdkJavaV1ToV2"; + if (experimental) { + recipeCmd += "Experimental"; + } + + List rewriteArgs = new ArrayList<>(); + // pin version since updates have broken tests + String rewriteMavenPluginVersion = "5.46.0"; + addAll(rewriteArgs, "mvn", "org.openrewrite.maven:rewrite-maven-plugin:" + rewriteMavenPluginVersion + ":run", + "-Drewrite.recipeArtifactCoordinates=software.amazon.awssdk:v2-migration:"+ getMigrationToolVersion() + "-PREVIEW", + recipeCmd); + + run(mavenActual, rewriteArgs.toArray(new String[0])); + FileUtils.deleteDirectory(mavenActual.resolve("target").toFile()); + assertTwoDirectoriesHaveSameStructure(mavenActual, mavenExpected); + } + + protected static void verifyCompilation() { + List packageArgs = new ArrayList<>(); + addAll(packageArgs, "mvn", "package"); + run(mavenActual, packageArgs.toArray(new String[0])); + } + + boolean versionAvailable() { + return TestUtils.versionAvailable(sdkVersion); + } +} diff --git a/test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/MavenTransferManagerTest.java b/test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/MavenTransferManagerTest.java index 72abff579e72..a97cb1de231b 100644 --- a/test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/MavenTransferManagerTest.java +++ b/test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/MavenTransferManagerTest.java @@ -15,84 +15,42 @@ package software.amazon.awssdk.v2migrationtests; -import static java.util.Collections.addAll; -import static software.amazon.awssdk.v2migrationtests.TestUtils.assertTwoDirectoriesHaveSameStructure; -import static software.amazon.awssdk.v2migrationtests.TestUtils.getMigrationToolVersion; import static software.amazon.awssdk.v2migrationtests.TestUtils.getVersion; import static software.amazon.awssdk.v2migrationtests.TestUtils.replaceVersion; -import static software.amazon.awssdk.v2migrationtests.TestUtils.run; import java.io.File; import java.io.IOException; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; import org.apache.commons.io.FileUtils; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIf; -import software.amazon.awssdk.utils.Logger; -public class MavenTransferManagerTest { - private static final Logger log = Logger.loggerFor(MavenTransferManagerTest.class); - private static String sdkVersion; - private static Path mavenTmBefore; - private static Path mavenTmAfter; - private static Path target; - private static Path mavenTmActual; - private static Path mavenTmExpected; +public class MavenTransferManagerTest extends MavenTestBase { @BeforeAll static void setUp() throws IOException { sdkVersion = getVersion(); - mavenTmBefore = new File(MavenTransferManagerTest.class.getResource("maven-tm/before").getFile()).toPath(); - mavenTmAfter = new File(MavenTransferManagerTest.class.getResource("maven-tm/after").getFile()).toPath(); + mavenBefore = new File(MavenTransferManagerTest.class.getResource("maven-tm/before").getFile()).toPath(); + mavenAfter = new File(MavenTransferManagerTest.class.getResource("maven-tm/after").getFile()).toPath(); target = new File(MavenTransferManagerTest.class.getResource("/").getFile()).toPath().getParent(); - mavenTmActual = target.resolve("maven-tm/actual"); - mavenTmExpected = target.resolve("maven-tm/expected"); + mavenActual = target.resolve("maven-tm/actual"); + mavenExpected = target.resolve("maven-tm/expected"); deleteTempDirectories(); - FileUtils.copyDirectory(mavenTmBefore.toFile(), mavenTmActual.toFile()); - FileUtils.copyDirectory(mavenTmAfter.toFile(), mavenTmExpected.toFile()); + FileUtils.copyDirectory(mavenBefore.toFile(), mavenActual.toFile()); + FileUtils.copyDirectory(mavenAfter.toFile(), mavenExpected.toFile()); - replaceVersion(mavenTmExpected.resolve("pom.xml"), sdkVersion); - replaceVersion(mavenTmActual.resolve("pom.xml"), sdkVersion); - } - - private static void deleteTempDirectories() throws IOException { - FileUtils.deleteDirectory(mavenTmActual.toFile()); - FileUtils.deleteDirectory(mavenTmExpected.toFile()); + replaceVersion(mavenExpected.resolve("pom.xml"), sdkVersion); + replaceVersion(mavenActual.resolve("pom.xml"), sdkVersion); } @Test @EnabledIf("versionAvailable") void mavenProject_shouldConvert() throws IOException { - verifyTransformation(); + boolean experimental = true; + verifyTransformation(experimental); verifyCompilation(); } - - private static void verifyTransformation() throws IOException { - List rewriteArgs = new ArrayList<>(); - // pin version since updates have broken tests - String rewriteMavenPluginVersion = "5.46.0"; - addAll(rewriteArgs, "mvn", "org.openrewrite.maven:rewrite-maven-plugin:" + rewriteMavenPluginVersion + ":run", - "-Drewrite.recipeArtifactCoordinates=software.amazon.awssdk:v2-migration:"+ getMigrationToolVersion() + "-PREVIEW", - "-Drewrite.activeRecipes=software.amazon.awssdk.v2migration.AwsSdkJavaV1ToV2Experimental"); - - run(mavenTmActual, rewriteArgs.toArray(new String[0])); - FileUtils.deleteDirectory(mavenTmActual.resolve("target").toFile()); - assertTwoDirectoriesHaveSameStructure(mavenTmActual, mavenTmExpected); - } - - private static void verifyCompilation() { - List packageArgs = new ArrayList<>(); - addAll(packageArgs, "mvn", "package"); - run(mavenTmActual, packageArgs.toArray(new String[0])); - } - - boolean versionAvailable() { - return TestUtils.versionAvailable(sdkVersion); - } } diff --git a/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-nocompile/after/pom.xml b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-nocompile/after/pom.xml new file mode 100644 index 000000000000..cd6274a6a517 --- /dev/null +++ b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-nocompile/after/pom.xml @@ -0,0 +1,55 @@ + + + + + 4.0.0 + + software.amazon.awssdk + sample-application + 1.2.3 + + + 1.8 + 1.8 + + + + + + software.amazon.awssdk + bom + V2_VERSION + pom + import + + + + + + + software.amazon.awssdk + s3 + V2_VERSION + + + software.amazon.awssdk + s3-transfer-manager + V2_VERSION + + + \ No newline at end of file diff --git a/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-nocompile/after/src/main/java/foo/bar/S3Transforms.java b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-nocompile/after/src/main/java/foo/bar/S3Transforms.java new file mode 100644 index 000000000000..2d113ed161b3 --- /dev/null +++ b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-nocompile/after/src/main/java/foo/bar/S3Transforms.java @@ -0,0 +1,34 @@ +/* + * 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 foo.bar; + +import software.amazon.awssdk.core.async.AsyncRequestBody; +import software.amazon.awssdk.services.s3.model.PutObjectRequest; +import software.amazon.awssdk.transfer.s3.S3TransferManager; +import software.amazon.awssdk.transfer.s3.model.UploadRequest; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +public class S3Transforms { + + void upload(S3TransferManager tm, String bucket, String key) { + InputStream inputStream = new ByteArrayInputStream(("HelloWorld").getBytes()); + PutObjectRequest requestWithInputStream = PutObjectRequest.builder().bucket(bucket).key(key).websiteRedirectLocation("location") + .build(); + /*AWS SDK for Java v2 migration: When using InputStream to upload with TransferManager, you must specify Content-Length and ExecutorService.*/tm.upload(UploadRequest.builder().putObjectRequest(requestWithInputStream).requestBody(AsyncRequestBody.fromInputStream(inputStream, -1L, newExecutorServiceVariableToDefine)).build()); + } +} diff --git a/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-nocompile/before/pom.xml b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-nocompile/before/pom.xml new file mode 100644 index 000000000000..70c4e8e9b12c --- /dev/null +++ b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-nocompile/before/pom.xml @@ -0,0 +1,49 @@ + + + + + 4.0.0 + + software.amazon.awssdk + sample-application + 1.2.3 + + + 1.8 + 1.8 + + + + + + com.amazonaws + aws-java-sdk-bom + 1.11.1000 + pom + import + + + + + + + com.amazonaws + aws-java-sdk-s3 + + + \ No newline at end of file diff --git a/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-nocompile/before/src/main/java/foo/bar/S3Transforms.java b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-nocompile/before/src/main/java/foo/bar/S3Transforms.java new file mode 100644 index 000000000000..3d6c241b6c62 --- /dev/null +++ b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-nocompile/before/src/main/java/foo/bar/S3Transforms.java @@ -0,0 +1,31 @@ +/* + * 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 foo.bar; + +import com.amazonaws.services.s3.model.PutObjectRequest; +import com.amazonaws.services.s3.transfer.TransferManager; +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +public class S3Transforms { + + void upload(TransferManager tm, String bucket, String key) { + InputStream inputStream = new ByteArrayInputStream(("HelloWorld").getBytes()); + PutObjectRequest requestWithInputStream = new PutObjectRequest(bucket, key, "location"); + requestWithInputStream.setInputStream(inputStream); + tm.upload(requestWithInputStream); + } +} diff --git a/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-tm/after/src/main/java/foo/bar/TransferManagerS3.java b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-tm/after/src/main/java/foo/bar/TransferManagerS3.java index 5cc5bce77059..03a160657e8c 100644 --- a/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-tm/after/src/main/java/foo/bar/TransferManagerS3.java +++ b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-tm/after/src/main/java/foo/bar/TransferManagerS3.java @@ -16,6 +16,7 @@ package foo.bar; import software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration; +import software.amazon.awssdk.core.async.AsyncRequestBody; import software.amazon.awssdk.services.s3.model.CopyObjectRequest; import software.amazon.awssdk.services.s3.model.GetObjectRequest; import software.amazon.awssdk.services.s3.model.PutObjectRequest; @@ -25,6 +26,7 @@ import software.amazon.awssdk.transfer.s3.model.DownloadFileRequest; import software.amazon.awssdk.transfer.s3.model.FileDownload; import software.amazon.awssdk.transfer.s3.model.UploadFileRequest; +import software.amazon.awssdk.transfer.s3.model.UploadRequest; import java.io.File; import java.time.Duration; @@ -56,6 +58,15 @@ void download(S3TransferManager tm, String bucket, String key) { void upload(S3TransferManager tm, String bucket, String key) { tm.uploadFile(UploadFileRequest.builder().putObjectRequest(PutObjectRequest.builder().bucket(bucket).key(key).build()).source(file).build()); + + File file = new File("file1.txt"); + PutObjectRequest requestWithFile = PutObjectRequest.builder().bucket(bucket).key(key) + .build(); + tm.uploadFile(UploadFileRequest.builder().putObjectRequest(requestWithFile).source(file).build()); + + PutObjectRequest requestWithoutPayload = PutObjectRequest.builder().bucket(bucket).key(key).websiteRedirectLocation("location") + .build(); + tm.upload(UploadRequest.builder().putObjectRequest(requestWithoutPayload).requestBody(AsyncRequestBody.empty()).build()); } void copy(S3TransferManager tm, String sourceBucket, String sourceKey, String destinationBucket, String destinationKey) { diff --git a/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-tm/before/src/main/java/foo/bar/TransferManagerS3.java b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-tm/before/src/main/java/foo/bar/TransferManagerS3.java index 1cc8e8b9cce9..49975abfefed 100644 --- a/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-tm/before/src/main/java/foo/bar/TransferManagerS3.java +++ b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven-tm/before/src/main/java/foo/bar/TransferManagerS3.java @@ -17,6 +17,7 @@ import com.amazonaws.services.s3.model.CopyObjectRequest; import com.amazonaws.services.s3.model.GetObjectRequest; +import com.amazonaws.services.s3.model.PutObjectRequest; import com.amazonaws.services.s3.transfer.Copy; import com.amazonaws.services.s3.transfer.Download; import com.amazonaws.services.s3.transfer.TransferManager; @@ -48,6 +49,13 @@ void download(TransferManager tm, String bucket, String key) { void upload(TransferManager tm, String bucket, String key) { tm.upload(bucket, key, file); + + File file = new File("file1.txt"); + PutObjectRequest requestWithFile = new PutObjectRequest(bucket, key, file); + tm.upload(requestWithFile); + + PutObjectRequest requestWithoutPayload = new PutObjectRequest(bucket, key, "location"); + tm.upload(requestWithoutPayload); } void copy(TransferManager tm, String sourceBucket, String sourceKey, String destinationBucket, String destinationKey) { diff --git a/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/after/src/main/java/foo/bar/S3Streaming.java b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/after/src/main/java/foo/bar/S3Streaming.java new file mode 100644 index 000000000000..e699c4904566 --- /dev/null +++ b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/after/src/main/java/foo/bar/S3Streaming.java @@ -0,0 +1,107 @@ +/* + * 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 foo.bar; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.InputStream; +import software.amazon.awssdk.core.ResponseInputStream; +import software.amazon.awssdk.core.sync.RequestBody; +import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.model.GetObjectRequest; +import software.amazon.awssdk.services.s3.model.GetObjectResponse; +import software.amazon.awssdk.services.s3.model.ObjectCannedACL; +import software.amazon.awssdk.services.s3.model.PutObjectRequest; +import software.amazon.awssdk.services.s3.model.RequestPayer; + +public class S3Streaming { + + S3Client s3 = S3Client.create(); + + void getObject(String bucket, String key) throws Exception { + ResponseInputStream s3Object = s3.getObject(GetObjectRequest.builder().bucket(bucket).key(key) + .build()); + s3Object.close(); + } + + void putObject_bucketKeyContent(String bucket, String key, String content) { + s3.putObject(PutObjectRequest.builder().bucket(bucket).key(key) + .build(), RequestBody.fromString(content)); + } + + void putObject_bucketKeyFile(String bucket, String key, File file) { + s3.putObject(PutObjectRequest.builder().bucket(bucket).key(key) + .build(), RequestBody.fromFile(file)); + } + + /** + * Mixed ordering to ensure the files are assigned correctly + */ + void putObject_requestPojoWithFile(String bucket, String key) { + File file4 = new File("file4.txt"); + File file3 = new File("file3.txt"); + File file1 = new File("file1.txt"); + File file2 = new File("file2.txt"); + + PutObjectRequest request1 = PutObjectRequest.builder().bucket(bucket).key(key) + .build(); + + PutObjectRequest request2 = PutObjectRequest.builder().bucket(bucket).key(key).websiteRedirectLocation("location") + .build(); + + s3.putObject(PutObjectRequest.builder().bucket(bucket).key(key).websiteRedirectLocation("location") + .build(), RequestBody.fromFile(file3)); + s3.putObject(request2, RequestBody.fromFile(file2)); + s3.putObject(PutObjectRequest.builder().bucket(bucket).key(key).websiteRedirectLocation("location") + .build(), RequestBody.fromFile(file4)); + s3.putObject(request1, RequestBody.fromFile(file1)); + } + + void putObject_requestPojoWithInputStream(String bucket, String key) { + InputStream inputStream1 = new ByteArrayInputStream(("HelloWorld").getBytes()); + InputStream inputStream2 = new ByteArrayInputStream(("HolaWorld").getBytes()); + + PutObjectRequest request1 = PutObjectRequest.builder().bucket(bucket).key(key).websiteRedirectLocation("location") + .build(); + /*AWS SDK for Java v2 migration: When using InputStream to upload with S3Client, Content-Length should be specified and used with RequestBody.fromInputStream(). Otherwise, the entire stream will be buffered in memory.*/s3.putObject(request1, RequestBody.fromContentProvider(() -> inputStream1, "binary/octet-stream")); + + /*AWS SDK for Java v2 migration: When using InputStream to upload with S3Client, Content-Length should be specified and used with RequestBody.fromInputStream(). Otherwise, the entire stream will be buffered in memory.*/s3.putObject(PutObjectRequest.builder().bucket(bucket).key(key).websiteRedirectLocation("location") + .build(), RequestBody.fromContentProvider(() -> inputStream2, "binary/octet-stream")); + } + + void putObject_requestPojoWithoutPayload(String bucket, String key) { + PutObjectRequest request = PutObjectRequest.builder().bucket(bucket).key(key).websiteRedirectLocation("location") + .build(); + s3.putObject(request, RequestBody.empty()); + } + + + void putObjectSetters() { + PutObjectRequest putObjectRequest = + PutObjectRequest.builder().bucket("bucket").key("key").websiteRedirectLocation("location") + .bucket("bucketName") + .acl(ObjectCannedACL.AWS_EXEC_READ) + .build(); + } + + void putObjectRequesterPaysSetter() { + PutObjectRequest requestWithTrue = PutObjectRequest.builder().bucket("bucket").key("key").websiteRedirectLocation("location").requestPayer(RequestPayer.REQUESTER) + .build(); + + PutObjectRequest requestWithFalse =PutObjectRequest.builder().bucket("bucket").key("key").websiteRedirectLocation("location") + .build(); + } +} \ No newline at end of file diff --git a/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/before/src/main/java/foo/bar/S3Streaming.java b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/before/src/main/java/foo/bar/S3Streaming.java new file mode 100644 index 000000000000..25a6b35582df --- /dev/null +++ b/test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/before/src/main/java/foo/bar/S3Streaming.java @@ -0,0 +1,93 @@ +/* + * 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 foo.bar; + +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.AmazonS3ClientBuilder; +import com.amazonaws.services.s3.model.CannedAccessControlList; +import com.amazonaws.services.s3.model.PutObjectRequest; +import com.amazonaws.services.s3.model.S3Object; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.InputStream; + +public class S3Streaming { + + AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); + + void getObject(String bucket, String key) throws Exception { + S3Object s3Object = s3.getObject(bucket, key); + s3Object.getObjectContent().close(); + } + + void putObject_bucketKeyContent(String bucket, String key, String content) { + s3.putObject(bucket, key, content); + } + + void putObject_bucketKeyFile(String bucket, String key, File file) { + s3.putObject(bucket, key, file); + } + + /** + * Mixed ordering to ensure the files are assigned correctly + */ + void putObject_requestPojoWithFile(String bucket, String key) { + File file4 = new File("file4.txt"); + File file3 = new File("file3.txt"); + File file1 = new File("file1.txt"); + File file2 = new File("file2.txt"); + + PutObjectRequest request1 = new PutObjectRequest(bucket, key, file1); + + PutObjectRequest request2 = new PutObjectRequest(bucket, key, "location"); + request2.setFile(file2); + + s3.putObject(new PutObjectRequest(bucket, key, "location").withFile(file3)); + s3.putObject(request2); + s3.putObject(new PutObjectRequest(bucket, key, "location").withFile(file4)); + s3.putObject(request1); + } + + void putObject_requestPojoWithInputStream(String bucket, String key) { + InputStream inputStream1 = new ByteArrayInputStream(("HelloWorld").getBytes()); + InputStream inputStream2 = new ByteArrayInputStream(("HolaWorld").getBytes()); + + PutObjectRequest request1 = new PutObjectRequest(bucket, key, "location"); + request1.setInputStream(inputStream1); + s3.putObject(request1); + + s3.putObject(new PutObjectRequest(bucket, key, "location").withInputStream(inputStream2)); + } + + void putObject_requestPojoWithoutPayload(String bucket, String key) { + PutObjectRequest request = new PutObjectRequest(bucket, key, "location"); + s3.putObject(request); + } + + + void putObjectSetters() { + PutObjectRequest putObjectRequest = + new PutObjectRequest("bucket", "key", "location") + .withBucketName("bucketName") + .withCannedAcl(CannedAccessControlList.AwsExecRead); + } + + void putObjectRequesterPaysSetter() { + PutObjectRequest requestWithTrue = new PutObjectRequest("bucket", "key", "location").withRequesterPays(true); + + PutObjectRequest requestWithFalse = new PutObjectRequest("bucket", "key", "location").withRequesterPays(false); + } +} \ No newline at end of file diff --git a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/ConstructorToFluent.java b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/ConstructorToFluent.java index fc9ff4809fb6..51e8c726b8dc 100644 --- a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/ConstructorToFluent.java +++ b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/ConstructorToFluent.java @@ -15,6 +15,8 @@ package software.amazon.awssdk.v2migration; +import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.fullyQualified; + import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import java.util.Collections; @@ -78,7 +80,7 @@ private static class Visitor extends JavaVisitor { private final List fluentNames; Visitor(String clzz, List parameterTypes, List fluentNames) { - this.clzz = TypeUtils.asFullyQualified(JavaType.buildType(clzz)); + this.clzz = fullyQualified(clzz); this.parameterTypes = parameterTypes; this.fluentNames = fluentNames; } diff --git a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/HttpSettingsToHttpClient.java b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/HttpSettingsToHttpClient.java index 01e22eec87dd..b29ce8b40791 100644 --- a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/HttpSettingsToHttpClient.java +++ b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/HttpSettingsToHttpClient.java @@ -15,6 +15,8 @@ package software.amazon.awssdk.v2migration; +import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.fullyQualified; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -345,10 +347,8 @@ private JContainer httpClientBuilderInvoke(Map h Class httpClientClassName = httpClientClassNamePair.left(); Class httpClientBuilderClassName = httpClientClassNamePair.right(); - JavaType.FullyQualified httpClientType = - TypeUtils.asFullyQualified(JavaType.buildType(httpClientClassName.getCanonicalName())); - JavaType.FullyQualified httpClientBuilderType = - TypeUtils.asFullyQualified(JavaType.buildType(httpClientBuilderClassName.getCanonicalName())); + JavaType.FullyQualified httpClientType = fullyQualified(httpClientClassName.getCanonicalName()); + JavaType.FullyQualified httpClientBuilderType = fullyQualified(httpClientBuilderClassName.getCanonicalName()); JavaType.Method httpClientBuilderMethodType = new JavaType.Method( null, diff --git a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3NonStreamingRequestToV2.java b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3NonStreamingRequestToV2.java index 9c0f07239381..b64cb7264e0b 100644 --- a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3NonStreamingRequestToV2.java +++ b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3NonStreamingRequestToV2.java @@ -15,6 +15,11 @@ package software.amazon.awssdk.v2migration; +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V1_S3_CLIENT; +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V1_S3_MODEL_PKG; +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.v1S3MethodMatcher; +import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.fullyQualified; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -35,7 +40,6 @@ 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.v2migration.internal.utils.IdentifierUtils; @@ -43,43 +47,37 @@ @SdkInternalApi public class S3NonStreamingRequestToV2 extends Recipe { - private static final String V1_S3_PKG = "com.amazonaws.services.s3"; - - private static final MethodMatcher DELETE_VERSION = - createMethodMatcher("deleteVersion(String, String, String)"); - private static final MethodMatcher COPY_OBJECT = - createMethodMatcher("copyObject(String, String, String, String)"); + private static final MethodMatcher DELETE_VERSION = v1S3MethodMatcher("deleteVersion(String, String, String)"); + private static final MethodMatcher COPY_OBJECT = v1S3MethodMatcher("copyObject(String, String, String, String)"); private static final MethodMatcher LIST_VERSIONS = - createMethodMatcher("listVersions(String, String, String, String, String, Integer)"); - private static final MethodMatcher SET_BUCKET_POLICY = - createMethodMatcher("setBucketPolicy(String, String)"); - private static final MethodMatcher GET_OBJECT_ACL = - createMethodMatcher("getObjectAcl(String, String, String)"); - private static final MethodMatcher SET_BUCKET_ACCELERATE_CONFIGURATION = createMethodMatcher( - String.format("setBucketAccelerateConfiguration(String, %s.model.BucketAccelerateConfiguration)", V1_S3_PKG)); - private static final MethodMatcher SET_BUCKET_CROSS_ORIGIN_CONFIGURATION = createMethodMatcher( - String.format("setBucketCrossOriginConfiguration(String, %s.model.BucketCrossOriginConfiguration)", V1_S3_PKG)); - private static final MethodMatcher SET_BUCKET_ANALYTICS_CONFIGURATION = createMethodMatcher( - String.format("setBucketAnalyticsConfiguration(String, %s.model.analytics.AnalyticsConfiguration)", V1_S3_PKG)); - private static final MethodMatcher SET_BUCKET_INTELLIGENT_TIERING_CONFIGURATION = createMethodMatcher( + v1S3MethodMatcher("listVersions(String, String, String, String, String, Integer)"); + private static final MethodMatcher SET_BUCKET_POLICY = v1S3MethodMatcher("setBucketPolicy(String, String)"); + private static final MethodMatcher GET_OBJECT_ACL = v1S3MethodMatcher("getObjectAcl(String, String, String)"); + private static final MethodMatcher SET_BUCKET_ACCELERATE_CONFIGURATION = v1S3MethodMatcher( + String.format("setBucketAccelerateConfiguration(String, %sBucketAccelerateConfiguration)", V1_S3_MODEL_PKG)); + private static final MethodMatcher SET_BUCKET_CROSS_ORIGIN_CONFIGURATION = v1S3MethodMatcher( + String.format("setBucketCrossOriginConfiguration(String, %sBucketCrossOriginConfiguration)", V1_S3_MODEL_PKG)); + private static final MethodMatcher SET_BUCKET_ANALYTICS_CONFIGURATION = v1S3MethodMatcher( + String.format("setBucketAnalyticsConfiguration(String, %sanalytics.AnalyticsConfiguration)", V1_S3_MODEL_PKG)); + private static final MethodMatcher SET_BUCKET_INTELLIGENT_TIERING_CONFIGURATION = v1S3MethodMatcher( String.format("setBucketIntelligentTieringConfiguration(" - + "String, %s.model.intelligenttiering.IntelligentTieringConfiguration)", V1_S3_PKG)); - private static final MethodMatcher SET_BUCKET_INVENTORY_CONFIGURATION = createMethodMatcher( - String.format("setBucketInventoryConfiguration(String, %s.model.inventory.InventoryConfiguration)", V1_S3_PKG)); - private static final MethodMatcher SET_BUCKET_LIFECYCLE_CONFIGURATION = createMethodMatcher( - String.format("setBucketLifecycleConfiguration(String, %s.model.BucketLifecycleConfiguration)", V1_S3_PKG)); - private static final MethodMatcher SET_BUCKET_METRICS_CONFIGURATION = createMethodMatcher( - String.format("setBucketMetricsConfiguration(String, %s.model.metrics.MetricsConfiguration)", V1_S3_PKG)); - private static final MethodMatcher SET_BUCKET_NOTIFICATION_CONFIGURATION = createMethodMatcher( - String.format("setBucketNotificationConfiguration(String, %s.model.BucketNotificationConfiguration)", V1_S3_PKG)); - private static final MethodMatcher SET_BUCKET_OWNERSHIP_CONTROLS = createMethodMatcher( - String.format("setBucketOwnershipControls(String, %s.model.ownership.OwnershipControls)", V1_S3_PKG)); - private static final MethodMatcher SET_BUCKET_REPLICATION_CONFIGURATION = createMethodMatcher( - String.format("setBucketReplicationConfiguration(String, %s.model.BucketReplicationConfiguration)", V1_S3_PKG)); - private static final MethodMatcher SET_BUCKET_TAGGING_CONFIGURATION = createMethodMatcher( - String.format("setBucketTaggingConfiguration(String, %s.model.BucketTaggingConfiguration)", V1_S3_PKG)); - private static final MethodMatcher SET_BUCKET_WEBSITE_CONFIGURATION = createMethodMatcher( - String.format("setBucketWebsiteConfiguration(String, %s.model.BucketWebsiteConfiguration)", V1_S3_PKG)); + + "String, %sintelligenttiering.IntelligentTieringConfiguration)", V1_S3_MODEL_PKG)); + private static final MethodMatcher SET_BUCKET_INVENTORY_CONFIGURATION = v1S3MethodMatcher( + String.format("setBucketInventoryConfiguration(String, %sinventory.InventoryConfiguration)", V1_S3_MODEL_PKG)); + private static final MethodMatcher SET_BUCKET_LIFECYCLE_CONFIGURATION = v1S3MethodMatcher( + String.format("setBucketLifecycleConfiguration(String, %sBucketLifecycleConfiguration)", V1_S3_MODEL_PKG)); + private static final MethodMatcher SET_BUCKET_METRICS_CONFIGURATION = v1S3MethodMatcher( + String.format("setBucketMetricsConfiguration(String, %smetrics.MetricsConfiguration)", V1_S3_MODEL_PKG)); + private static final MethodMatcher SET_BUCKET_NOTIFICATION_CONFIGURATION = v1S3MethodMatcher( + String.format("setBucketNotificationConfiguration(String, %sBucketNotificationConfiguration)", V1_S3_MODEL_PKG)); + private static final MethodMatcher SET_BUCKET_OWNERSHIP_CONTROLS = v1S3MethodMatcher( + String.format("setBucketOwnershipControls(String, %sownership.OwnershipControls)", V1_S3_MODEL_PKG)); + private static final MethodMatcher SET_BUCKET_REPLICATION_CONFIGURATION = v1S3MethodMatcher( + String.format("setBucketReplicationConfiguration(String, %sBucketReplicationConfiguration)", V1_S3_MODEL_PKG)); + private static final MethodMatcher SET_BUCKET_TAGGING_CONFIGURATION = v1S3MethodMatcher( + String.format("setBucketTaggingConfiguration(String, %sBucketTaggingConfiguration)", V1_S3_MODEL_PKG)); + private static final MethodMatcher SET_BUCKET_WEBSITE_CONFIGURATION = v1S3MethodMatcher( + String.format("setBucketWebsiteConfiguration(String, %sBucketWebsiteConfiguration)", V1_S3_MODEL_PKG)); private static final Map BUCKET_ARG_METHODS = new HashMap<>(); private static final Map BUCKET_KEY_ARGS_METHODS = new HashMap<>(); @@ -87,86 +85,82 @@ public class S3NonStreamingRequestToV2 extends Recipe { private static final Map BUCKET_PREFIX_ARGS_METHODS = new HashMap<>(); static { - BUCKET_ARG_METHODS.put(singleStringArgMethod("createBucket"), fcqn("createBucket")); - BUCKET_ARG_METHODS.put(singleStringArgMethod("deleteBucket"), fcqn("deleteBucket")); - BUCKET_ARG_METHODS.put(singleStringArgMethod("listObjects"), fcqn("listObjects")); - BUCKET_ARG_METHODS.put(singleStringArgMethod("listObjectsV2"), fcqn("listObjectsV2")); + BUCKET_ARG_METHODS.put(singleStringArgMethod("createBucket"), fqcn("createBucket")); + BUCKET_ARG_METHODS.put(singleStringArgMethod("deleteBucket"), fqcn("deleteBucket")); + BUCKET_ARG_METHODS.put(singleStringArgMethod("listObjects"), fqcn("listObjects")); + BUCKET_ARG_METHODS.put(singleStringArgMethod("listObjectsV2"), fqcn("listObjectsV2")); BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketCrossOriginConfiguration"), - fcqn("getBucketCrossOriginConfiguration")); + fqcn("getBucketCrossOriginConfiguration")); BUCKET_ARG_METHODS.put(singleStringArgMethod("deleteBucketCrossOriginConfiguration"), - fcqn("deleteBucketCrossOriginConfiguration")); + fqcn("deleteBucketCrossOriginConfiguration")); BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketVersioningConfiguration"), - fcqn("getBucketVersioningConfiguration")); - BUCKET_ARG_METHODS.put(singleStringArgMethod("deleteBucketEncryption"), fcqn("deleteBucketEncryption")); - BUCKET_ARG_METHODS.put(singleStringArgMethod("deleteBucketPolicy"), fcqn("deleteBucketPolicy")); + fqcn("getBucketVersioningConfiguration")); + BUCKET_ARG_METHODS.put(singleStringArgMethod("deleteBucketEncryption"), fqcn("deleteBucketEncryption")); + BUCKET_ARG_METHODS.put(singleStringArgMethod("deleteBucketPolicy"), fqcn("deleteBucketPolicy")); BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketAccelerateConfiguration"), - fcqn("getBucketAccelerateConfiguration")); - BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketAcl"), fcqn("getBucketAcl")); - BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketEncryption"), fcqn("getBucketEncryption")); + fqcn("getBucketAccelerateConfiguration")); + BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketAcl"), fqcn("getBucketAcl")); + BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketEncryption"), fqcn("getBucketEncryption")); BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketLifecycleConfiguration"), - fcqn("getBucketLifecycleConfiguration")); + fqcn("getBucketLifecycleConfiguration")); BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketNotificationConfiguration"), - fcqn("getBucketNotificationConfiguration")); - BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketPolicy"), fcqn("getBucketPolicy")); - BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketLocation"), fcqn("getBucketLocation")); + fqcn("getBucketNotificationConfiguration")); + BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketPolicy"), fqcn("getBucketPolicy")); + BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketLocation"), fqcn("getBucketLocation")); BUCKET_ARG_METHODS.put(singleStringArgMethod("deleteBucketLifecycleConfiguration"), - fcqn("deleteBucketLifecycleConfiguration")); + fqcn("deleteBucketLifecycleConfiguration")); BUCKET_ARG_METHODS.put(singleStringArgMethod("deleteBucketReplicationConfiguration"), - fcqn("deleteBucketReplicationConfiguration")); + fqcn("deleteBucketReplicationConfiguration")); BUCKET_ARG_METHODS.put(singleStringArgMethod("deleteBucketTaggingConfiguration"), - fcqn("deleteBucketTaggingConfiguration")); + fqcn("deleteBucketTaggingConfiguration")); BUCKET_ARG_METHODS.put(singleStringArgMethod("deleteBucketWebsiteConfiguration"), - fcqn("deleteBucketWebsiteConfiguration")); - BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketLoggingConfiguration"), fcqn("getBucketLoggingConfiguration")); + fqcn("deleteBucketWebsiteConfiguration")); + BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketLoggingConfiguration"), fqcn("getBucketLoggingConfiguration")); BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketReplicationConfiguration"), - fcqn("getBucketReplicationConfiguration")); - BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketTaggingConfiguration"), fcqn("getBucketTaggingConfiguration")); - BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketWebsiteConfiguration"), fcqn("getBucketWebsiteConfiguration")); + fqcn("getBucketReplicationConfiguration")); + BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketTaggingConfiguration"), fqcn("getBucketTaggingConfiguration")); + BUCKET_ARG_METHODS.put(singleStringArgMethod("getBucketWebsiteConfiguration"), fqcn("getBucketWebsiteConfiguration")); - BUCKET_KEY_ARGS_METHODS.put(twoStringArgsMethod("deleteObject"), fcqn("deleteObject")); - BUCKET_KEY_ARGS_METHODS.put(twoStringArgsMethod("getObject"), fcqn("getObject")); - BUCKET_KEY_ARGS_METHODS.put(twoStringArgsMethod("getObjectAcl"), fcqn("getObjectAcl")); - BUCKET_KEY_ARGS_METHODS.put(twoStringArgsMethod("getObjectMetadata"), fcqn("getObjectMetadata")); + BUCKET_KEY_ARGS_METHODS.put(twoStringArgsMethod("deleteObject"), fqcn("deleteObject")); + BUCKET_KEY_ARGS_METHODS.put(twoStringArgsMethod("getObject"), fqcn("getObject")); + BUCKET_KEY_ARGS_METHODS.put(twoStringArgsMethod("getObjectAcl"), fqcn("getObjectAcl")); + BUCKET_KEY_ARGS_METHODS.put(twoStringArgsMethod("getObjectMetadata"), fqcn("getObjectMetadata")); BUCKET_ID_ARGS_METHODS.put(twoStringArgsMethod("deleteBucketAnalyticsConfiguration"), - fcqn("deleteBucketAnalyticsConfiguration")); + fqcn("deleteBucketAnalyticsConfiguration")); BUCKET_ID_ARGS_METHODS.put(twoStringArgsMethod("deleteBucketIntelligentTieringConfiguration"), - fcqn("deleteBucketIntelligentTieringConfiguration")); + fqcn("deleteBucketIntelligentTieringConfiguration")); BUCKET_ID_ARGS_METHODS.put(twoStringArgsMethod("deleteBucketInventoryConfiguration"), - fcqn("deleteBucketInventoryConfiguration")); + fqcn("deleteBucketInventoryConfiguration")); BUCKET_ID_ARGS_METHODS.put(twoStringArgsMethod("deleteBucketMetricsConfiguration"), - fcqn("deleteBucketMetricsConfiguration")); + fqcn("deleteBucketMetricsConfiguration")); BUCKET_ID_ARGS_METHODS.put(twoStringArgsMethod("getBucketAnalyticsConfiguration"), - fcqn("getBucketAnalyticsConfiguration")); + fqcn("getBucketAnalyticsConfiguration")); BUCKET_ID_ARGS_METHODS.put(twoStringArgsMethod("getBucketIntelligentTieringConfiguration"), - fcqn("getBucketIntelligentTieringConfiguration")); + fqcn("getBucketIntelligentTieringConfiguration")); BUCKET_ID_ARGS_METHODS.put(twoStringArgsMethod("getBucketInventoryConfiguration"), - fcqn("getBucketInventoryConfiguration")); - BUCKET_ID_ARGS_METHODS.put(twoStringArgsMethod("getBucketMetricsConfiguration"), fcqn("getBucketMetricsConfiguration")); - - BUCKET_PREFIX_ARGS_METHODS.put(twoStringArgsMethod("listObjects"), fcqn("listObjects")); - BUCKET_PREFIX_ARGS_METHODS.put(twoStringArgsMethod("listObjectsV2"), fcqn("listObjectsV2")); - BUCKET_PREFIX_ARGS_METHODS.put(twoStringArgsMethod("listVersions"), fcqn("listVersions")); - } + fqcn("getBucketInventoryConfiguration")); + BUCKET_ID_ARGS_METHODS.put(twoStringArgsMethod("getBucketMetricsConfiguration"), fqcn("getBucketMetricsConfiguration")); - private static MethodMatcher createMethodMatcher(String methodSignature) { - return new MethodMatcher(V1_S3_PKG + ".AmazonS3 " + methodSignature, true); + BUCKET_PREFIX_ARGS_METHODS.put(twoStringArgsMethod("listObjects"), fqcn("listObjects")); + BUCKET_PREFIX_ARGS_METHODS.put(twoStringArgsMethod("listObjectsV2"), fqcn("listObjectsV2")); + BUCKET_PREFIX_ARGS_METHODS.put(twoStringArgsMethod("listVersions"), fqcn("listVersions")); } private static MethodMatcher singleStringArgMethod(String method) { - String signature = "com.amazonaws.services.s3.AmazonS3 " + method + "(java.lang.String)"; + String signature = V1_S3_CLIENT + " " + method + "(java.lang.String)"; return new MethodMatcher(signature, true); } private static MethodMatcher twoStringArgsMethod(String method) { - String signature = "com.amazonaws.services.s3.AmazonS3 " + method + "(java.lang.String, java.lang.String)"; + String signature = V1_S3_CLIENT + " " + method + "(java.lang.String, java.lang.String)"; return new MethodMatcher(signature, true); } - private static JavaType.FullyQualified fcqn(String method) { + private static JavaType.FullyQualified fqcn(String method) { String methodFirstLetterCaps = method.substring(0, 1).toUpperCase(Locale.ROOT) + method.substring(1); - String typeName = "com.amazonaws.services.s3.model." + methodFirstLetterCaps + "Request"; - return TypeUtils.asFullyQualified(JavaType.buildType(typeName)); + String typeName = V1_S3_MODEL_PKG + methodFirstLetterCaps + "Request"; + return fullyQualified(typeName); } @Override @@ -189,75 +183,75 @@ private static final class Visitor extends JavaIsoVisitor { public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) { if (DELETE_VERSION.matches(method)) { - method = transformMethod(method, fcqn("deleteObject"), "bucket", "key", "versionId"); + method = transformMethod(method, fqcn("deleteObject"), "bucket", "key", "versionId"); return super.visitMethodInvocation(method, executionContext); } if (COPY_OBJECT.matches(method)) { - method = transformMethod(method, fcqn("copyObject"), + method = transformMethod(method, fqcn("copyObject"), "sourceBucket", "sourceKey", "destinationBucket", "destinationKey"); return super.visitMethodInvocation(method, executionContext); } if (LIST_VERSIONS.matches(method)) { - method = transformMethod(method, fcqn("listVersions"), + method = transformMethod(method, fqcn("listVersions"), "bucket", "prefix", "keyMarker", "versionIdMarker", "delimiter", "maxKeys"); return super.visitMethodInvocation(method, executionContext); } if (SET_BUCKET_POLICY.matches(method)) { - method = transformMethod(method, fcqn("putBucketPolicy"), "bucket", "policy"); + method = transformMethod(method, fqcn("putBucketPolicy"), "bucket", "policy"); return super.visitMethodInvocation(method, executionContext); } if (GET_OBJECT_ACL.matches(method)) { - method = transformMethod(method, fcqn("getObjectAcl"), "bucket", "key", "versionId"); + method = transformMethod(method, fqcn("getObjectAcl"), "bucket", "key", "versionId"); return super.visitMethodInvocation(method, executionContext); } if (SET_BUCKET_ACCELERATE_CONFIGURATION.matches(method)) { - method = transformMethod(method, fcqn("setBucketAccelerateConfiguration"), "bucket", "accelerateConfiguration"); + method = transformMethod(method, fqcn("setBucketAccelerateConfiguration"), "bucket", "accelerateConfiguration"); return super.visitMethodInvocation(method, executionContext); } if (SET_BUCKET_CROSS_ORIGIN_CONFIGURATION.matches(method)) { - method = transformMethod(method, fcqn("setBucketCrossOriginConfiguration"), "bucket", "corsConfiguration"); + method = transformMethod(method, fqcn("setBucketCrossOriginConfiguration"), "bucket", "corsConfiguration"); return super.visitMethodInvocation(method, executionContext); } if (SET_BUCKET_ANALYTICS_CONFIGURATION.matches(method)) { - method = transformMethod(method, fcqn("setBucketAnalyticsConfiguration"), "bucket", "analyticsConfiguration"); + method = transformMethod(method, fqcn("setBucketAnalyticsConfiguration"), "bucket", "analyticsConfiguration"); return super.visitMethodInvocation(method, executionContext); } if (SET_BUCKET_INTELLIGENT_TIERING_CONFIGURATION.matches(method)) { - method = transformMethod(method, fcqn("setBucketIntelligentTieringConfiguration"), + method = transformMethod(method, fqcn("setBucketIntelligentTieringConfiguration"), "bucket", "intelligentTieringConfiguration"); return super.visitMethodInvocation(method, executionContext); } if (SET_BUCKET_INVENTORY_CONFIGURATION.matches(method)) { - method = transformMethod(method, fcqn("setBucketInventoryConfiguration"), "bucket", "inventoryConfiguration"); + method = transformMethod(method, fqcn("setBucketInventoryConfiguration"), "bucket", "inventoryConfiguration"); return super.visitMethodInvocation(method, executionContext); } if (SET_BUCKET_LIFECYCLE_CONFIGURATION.matches(method)) { - method = transformMethod(method, fcqn("setBucketLifecycleConfiguration"), "bucket", "lifecycleConfiguration"); + method = transformMethod(method, fqcn("setBucketLifecycleConfiguration"), "bucket", "lifecycleConfiguration"); return super.visitMethodInvocation(method, executionContext); } if (SET_BUCKET_METRICS_CONFIGURATION.matches(method)) { - method = transformMethod(method, fcqn("setBucketMetricsConfiguration"), "bucket", "metricsConfiguration"); + method = transformMethod(method, fqcn("setBucketMetricsConfiguration"), "bucket", "metricsConfiguration"); return super.visitMethodInvocation(method, executionContext); } if (SET_BUCKET_NOTIFICATION_CONFIGURATION.matches(method)) { - method = transformMethod(method, fcqn("setBucketNotificationConfiguration"), + method = transformMethod(method, fqcn("setBucketNotificationConfiguration"), "bucket", "notificationConfiguration"); return super.visitMethodInvocation(method, executionContext); } if (SET_BUCKET_OWNERSHIP_CONTROLS.matches(method)) { - method = transformMethod(method, fcqn("setBucketOwnershipControls"), "bucket", "ownershipControls"); + method = transformMethod(method, fqcn("setBucketOwnershipControls"), "bucket", "ownershipControls"); return super.visitMethodInvocation(method, executionContext); } if (SET_BUCKET_REPLICATION_CONFIGURATION.matches(method)) { - method = transformMethod(method, fcqn("setBucketReplicationConfiguration"), "bucket", "replicationConfiguration"); + method = transformMethod(method, fqcn("setBucketReplicationConfiguration"), "bucket", "replicationConfiguration"); return super.visitMethodInvocation(method, executionContext); } if (SET_BUCKET_TAGGING_CONFIGURATION.matches(method)) { - method = transformMethod(method, fcqn("setBucketTaggingConfiguration"), "bucket", "taggingConfiguration"); + method = transformMethod(method, fqcn("setBucketTaggingConfiguration"), "bucket", "taggingConfiguration"); return super.visitMethodInvocation(method, executionContext); } if (SET_BUCKET_WEBSITE_CONFIGURATION.matches(method)) { - method = transformMethod(method, fcqn("setBucketWebsiteConfiguration"), "bucket", "configuration"); + method = transformMethod(method, fqcn("setBucketWebsiteConfiguration"), "bucket", "configuration"); return super.visitMethodInvocation(method, executionContext); } diff --git a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3NonStreamingRequestToV2Complex.java b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3NonStreamingRequestToV2Complex.java index 6358c1140a61..12a3df97187e 100644 --- a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3NonStreamingRequestToV2Complex.java +++ b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3NonStreamingRequestToV2Complex.java @@ -15,6 +15,10 @@ package software.amazon.awssdk.v2migration; +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_S3_MODEL_PKG; +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.v2S3MethodMatcher; +import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.fullyQualified; + import org.openrewrite.ExecutionContext; import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; @@ -36,24 +40,18 @@ @SdkInternalApi public class S3NonStreamingRequestToV2Complex extends Recipe { - private static final String V2_S3_PKG = "software.amazon.awssdk.services.s3"; - - private static final MethodMatcher DISABLE_REQUESTER_PAYS = createMethodMatcher("disableRequesterPays(String)"); - private static final MethodMatcher ENABLE_REQUESTER_PAYS = createMethodMatcher("enableRequesterPays(String)"); - private static final MethodMatcher IS_REQUESTER_PAYS_ENABLED = createMethodMatcher("isRequesterPaysEnabled(String)"); - private static final MethodMatcher GET_OBJECT_AS_STRING = createMethodMatcher("getObjectAsString(String, String)"); - private static final MethodMatcher GET_URL = createMethodMatcher("getUrl(String, String)"); - private static final MethodMatcher LIST_BUCKETS = createMethodMatcher("listBuckets()"); - private static final MethodMatcher RESTORE_OBJECT = createMethodMatcher("restoreObject(String, String, int)"); + private static final MethodMatcher DISABLE_REQUESTER_PAYS = v2S3MethodMatcher("disableRequesterPays(String)"); + private static final MethodMatcher ENABLE_REQUESTER_PAYS = v2S3MethodMatcher("enableRequesterPays(String)"); + private static final MethodMatcher IS_REQUESTER_PAYS_ENABLED = v2S3MethodMatcher("isRequesterPaysEnabled(String)"); + private static final MethodMatcher GET_OBJECT_AS_STRING = v2S3MethodMatcher("getObjectAsString(String, String)"); + private static final MethodMatcher GET_URL = v2S3MethodMatcher("getUrl(String, String)"); + private static final MethodMatcher LIST_BUCKETS = v2S3MethodMatcher("listBuckets()"); + private static final MethodMatcher RESTORE_OBJECT = v2S3MethodMatcher("restoreObject(String, String, int)"); private static final MethodMatcher SET_OBJECT_REDIRECT_LOCATION = - createMethodMatcher("objectRedirectLocation(String, String, String)"); - private static final MethodMatcher CHANGE_OBJECT_STORAGE_CLASS = createMethodMatcher( - String.format("changeObjectStorageClass(String, String, %s.model.StorageClass)", V2_S3_PKG)); - private static final MethodMatcher CREATE_BUCKET = createMethodMatcher("createBucket(String, String)"); - - private static MethodMatcher createMethodMatcher(String methodSignature) { - return new MethodMatcher(V2_S3_PKG + ".S3Client " + methodSignature, true); - } + v2S3MethodMatcher("objectRedirectLocation(String, String, String)"); + private static final MethodMatcher CHANGE_OBJECT_STORAGE_CLASS = v2S3MethodMatcher( + String.format("changeObjectStorageClass(String, String, %sStorageClass)", V2_S3_MODEL_PKG)); + private static final MethodMatcher CREATE_BUCKET = v2S3MethodMatcher("createBucket(String, String)"); @Override public String getDisplayName() { @@ -124,8 +122,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu } private boolean isCompleteMpuRequestMultipartUploadSetter(J.MethodInvocation method) { - JavaType.FullyQualified completeMpuRequest = TypeUtils.asFullyQualified(JavaType.buildType( - "software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest.Builder")); + JavaType.FullyQualified completeMpuRequest = + fullyQualified("software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest.Builder"); return "multipartUpload".equals(method.getSimpleName()) && TypeUtils.isAssignableTo(completeMpuRequest, method.getSelect().getType()); } @@ -254,7 +252,7 @@ private J.MethodInvocation transformSetRequesterPays(J.MethodInvocation method, } private void addImport(String pojoName) { - String fqcn = "software.amazon.awssdk.services.s3.model." + pojoName; + String fqcn = V2_S3_MODEL_PKG + pojoName; doAfterVisit(new AddImport<>(fqcn, null, false)); } } diff --git a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3PutObjectRequestToV2.java b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3PutObjectRequestToV2.java new file mode 100644 index 000000000000..e13fdf950fa5 --- /dev/null +++ b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3PutObjectRequestToV2.java @@ -0,0 +1,337 @@ +/* + * 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.V2_S3_MODEL_PKG; +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_TM_MODEL_PKG; +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.v2S3MethodMatcher; +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.v2TmMethodMatcher; +import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.isFileType; +import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.isInputStreamType; + +import java.util.ArrayDeque; +import java.util.Collections; +import java.util.List; +import java.util.Queue; +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.JavaTemplate; +import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.tree.Comment; +import org.openrewrite.java.tree.Expression; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.TextComment; +import org.openrewrite.java.tree.TypeUtils; +import org.openrewrite.marker.Markers; +import software.amazon.awssdk.annotations.SdkInternalApi; + +@SdkInternalApi +public class S3PutObjectRequestToV2 extends Recipe { + + private static final MethodMatcher PUT_OBJ_WITH_REQUEST = + v2S3MethodMatcher(String.format("putObject(%sPutObjectRequest)", V2_S3_MODEL_PKG)); + + private static final MethodMatcher UPLOAD_WITH_REQUEST = + v2TmMethodMatcher(String.format("upload(%sPutObjectRequest)", V2_S3_MODEL_PKG)); + + @Override + public String getDisplayName() { + return "V1 S3 PutObjectRequest, AmazonS3.putObject(PutObjectRequest), and TransferManager.upload(PutObjectRequest) to V2"; + } + + @Override + public String getDescription() { + return "Transform V1 S3 PutObjectRequest to V2, as well as methods that take it as an argument."; + } + + @Override + public TreeVisitor getVisitor() { + return new Visitor(); + } + + private static final class Visitor extends JavaIsoVisitor { + + private Queue filesQueue = new ArrayDeque<>(); + + @Override + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) { + + if (isPutObjectRequestBuilderSetter(method)) { + if (isPayloadSetter(method)) { + return transformRequestBuilderPayloadSetter(method, executionContext); + } + if (isRequestPayerSetter(method)) { + return transformRequesterPaysSetter(method); + } + } + if (isPutObjectRequestSetter(method)) { + if (isPayloadSetter(method)) { + return transformRequestPayloadSetter(method, executionContext); + } + } + + if (PUT_OBJ_WITH_REQUEST.matches(method)) { + method = super.visitMethodInvocation(method, executionContext); + method = transformPutObjectWithRequest(method, executionContext); + return method; + } + if (UPLOAD_WITH_REQUEST.matches(method)) { + method = super.visitMethodInvocation(method, executionContext); + method = transformUploadWithRequest(method, executionContext); + return method; + } + + return super.visitMethodInvocation(method, executionContext); + } + + private J.MethodInvocation transformRequestBuilderPayloadSetter(J.MethodInvocation method, + ExecutionContext executionContext) { + Expression payload = method.getArguments().get(0); + + String variableName = retrieveVariableNameIfRequestPojoIsAssigned(); + if (variableName != null) { + executionContext.putMessage(variableName, payload); + } else { + filesQueue.add(payload); + } + + // Remove setter .file(file) + return (J.MethodInvocation) method.getSelect(); + } + + private J.MethodInvocation transformRequestPayloadSetter(J.MethodInvocation method, ExecutionContext executionContext) { + J.Identifier requestPojo = (J.Identifier) method.getSelect(); + String variableName = requestPojo.getSimpleName(); + + Expression payload = method.getArguments().get(0); + executionContext.putMessage(variableName, payload); + + // Remove entire statement e.g., request.setFile(file) + return null; + } + + /** + * Request POJO may be assigned to a variable, or instantiated directly in putObject() API + */ + private String retrieveVariableNameIfRequestPojoIsAssigned() { + J parent = getCursor() + .dropParentUntil(p -> p instanceof J.VariableDeclarations.NamedVariable || p instanceof J.Block) + .getValue(); + + if (parent instanceof J.VariableDeclarations.NamedVariable) { + J.VariableDeclarations.NamedVariable namedVariable = (J.VariableDeclarations.NamedVariable) parent; + return namedVariable.getSimpleName(); + } + return null; + } + + private J.MethodInvocation transformPutObjectWithRequest(J.MethodInvocation method, ExecutionContext executionContext) { + Expression payload = retrieveRequestPayload(method, executionContext); + + if (payload == null) { + method = addEmptyRequestBodyToPutObject(method); + } else if (isFileType(payload.getType())) { + method = addFileToPutObject(method, payload); + } else if (isInputStreamType(payload.getType())) { + method = addInputStreamToPutObject(method, payload); + } + + addRequestBodyImport(); + return method; + } + + private Expression retrieveRequestPayload(J.MethodInvocation method, ExecutionContext executionContext) { + Expression requestPojo = method.getArguments().get(0); + Expression payload; + + if (requestPojo instanceof J.Identifier) { + J.Identifier pojo = (J.Identifier) requestPojo; + payload = executionContext.pollMessage(pojo.getSimpleName()); + } else { + payload = filesQueue.poll(); + } + + return payload; + } + + private J.MethodInvocation transformUploadWithRequest(J.MethodInvocation method, ExecutionContext executionContext) { + Expression payload = retrieveRequestPayload(method, executionContext); + + if (payload == null) { + method = addEmptyAsyncRequestBodyToUpload(method); + } else if (isFileType(payload.getType())) { + method = addFileAndChangeMethodToUploadFile(method, payload); + } else if (isInputStreamType(payload.getType())) { + method = addInputStreamToUpload(method, payload); + } + + return method; + } + + private J.MethodInvocation addEmptyAsyncRequestBodyToUpload(J.MethodInvocation method) { + String v2Method = "UploadRequest.builder().putObjectRequest(#{any()}).requestBody(AsyncRequestBody.empty()).build()"; + addTmImport("UploadRequest"); + addAsyncRequestBodyImport(); + return JavaTemplate.builder(v2Method).build() + .apply(getCursor(), method.getCoordinates().replaceArguments(), method.getArguments().get(0)); + } + + private J.MethodInvocation addEmptyRequestBodyToPutObject(J.MethodInvocation method) { + String v2Method = "#{any()}, RequestBody.empty()"; + return JavaTemplate.builder(v2Method).build() + .apply(getCursor(), method.getCoordinates().replaceArguments(), method.getArguments().get(0)); + } + + private J.MethodInvocation addFileAndChangeMethodToUploadFile(J.MethodInvocation method, Expression file) { + String v2Method = "#{any()}.uploadFile(UploadFileRequest.builder()" + + ".putObjectRequest(#{any()}).source(#{any()}).build())"; + addTmImport("UploadFileRequest"); + return JavaTemplate.builder(v2Method).build() + .apply(getCursor(), method.getCoordinates().replace(), method.getSelect(), + method.getArguments().get(0), file); + } + + private J.MethodInvocation addInputStreamToUpload(J.MethodInvocation method, Expression inputStream) { + long contentLength = extractContentLengthIfSet(method); + + String v2Method = String.format("UploadRequest.builder().putObjectRequest(#{any()})" + + ".requestBody(AsyncRequestBody.fromInputStream(#{any()}, %dL, " + + "newExecutorServiceVariableToDefine)).build()", + contentLength); + + addTmImport("UploadRequest"); + addAsyncRequestBodyImport(); + + String comment = "When using InputStream to upload with TransferManager, you must specify Content-Length and " + + "ExecutorService."; + + return JavaTemplate.builder(v2Method).build() + .apply(getCursor(), method.getCoordinates().replaceArguments(), + method.getArguments().get(0), inputStream) + .withComments(createComments(comment)); + } + + private J.MethodInvocation addFileToPutObject(J.MethodInvocation method, Expression file) { + String v2Method = "#{any()}, RequestBody.fromFile(#{any()})"; + return JavaTemplate.builder(v2Method).build() + .apply(getCursor(), method.getCoordinates().replaceArguments(), + method.getArguments().get(0), file); + } + + private J.MethodInvocation addInputStreamToPutObject(J.MethodInvocation method, Expression inputStream) { + long contentLength = extractContentLengthIfSet(method); + String v2Method; + + if (contentLength < 0) { + String comment = "When using InputStream to upload with S3Client, Content-Length should be specified and used " + + "with RequestBody.fromInputStream(). Otherwise, the entire stream will be buffered in memory."; + // CHECKSTYLE:OFF: Regexp + v2Method = "#{any()}, RequestBody.fromContentProvider(() -> #{any()}, \"binary/octet-stream\")"; + // CHECKSTYLE:ON: Regexp + return JavaTemplate.builder(v2Method).build() + .apply(getCursor(), method.getCoordinates().replaceArguments(), + method.getArguments().get(0), inputStream) + .withComments(createComments(comment)); + } else { + v2Method = String.format("#{any()}, RequestBody.fromInputStream(#{any()}, %d)", contentLength); + return JavaTemplate.builder(v2Method).build() + .apply(getCursor(), method.getCoordinates().replaceArguments(), + method.getArguments().get(0), inputStream); + } + } + + private long extractContentLengthIfSet(J.MethodInvocation method) { + // TODO - check ObjectMetadata and return if set + // Need to implement ObjectMetadata transform + + return -1; + } + + private J.MethodInvocation transformRequesterPaysSetter(J.MethodInvocation method) { + Expression expression = method.getArguments().get(0); + if (expression instanceof J.Literal) { + J.Literal literal = (J.Literal) expression; + if (Boolean.TRUE.equals(literal.getValue())) { + addS3Import("RequestPayer"); + } else { + // Removes method - there is no enum value for false + return (J.MethodInvocation) method.getSelect(); + } + } + + return JavaTemplate.builder("RequestPayer.REQUESTER").build() + .apply(getCursor(), method.getCoordinates().replaceArguments()); + } + + private List createComments(String comment) { + return Collections.singletonList( + new TextComment(true, "AWS SDK for Java v2 migration: " + comment, "", Markers.EMPTY)); + } + + /** Field set during POJO instantiation, e.g., + * PutObjectRequest request = new PutObjectRequest("bucket" "key", "redirectLocation").withFile(file); + */ + private boolean isPutObjectRequestBuilderSetter(J.MethodInvocation method) { + return isSetterForClassType(method, "software.amazon.awssdk.services.s3.model.PutObjectRequest$Builder"); + } + + /** Field set after POJO instantiation, e.g., + * PutObjectRequest request = new PutObjectRequest("bucket" "key", "redirectLocation"); + * request.setFile(file); + */ + private boolean isPutObjectRequestSetter(J.MethodInvocation method) { + return isSetterForClassType(method, "software.amazon.awssdk.services.s3.model.PutObjectRequest"); + } + + private boolean isSetterForClassType(J.MethodInvocation method, String fqcn) { + if (method.getSelect() == null || method.getSelect().getType() == null) { + return false; + } + return TypeUtils.isOfClassType(method.getSelect().getType(), fqcn); + } + + private boolean isPayloadSetter(J.MethodInvocation method) { + return "file".equals(method.getSimpleName()) || "inputStream".equals(method.getSimpleName()); + } + + private boolean isRequestPayerSetter(J.MethodInvocation method) { + return "requestPayer".equals(method.getSimpleName()); + } + + private void addRequestBodyImport() { + String fqcn = "software.amazon.awssdk.core.sync.RequestBody"; + doAfterVisit(new AddImport<>(fqcn, null, false)); + } + + private void addAsyncRequestBodyImport() { + String fqcn = "software.amazon.awssdk.core.async.AsyncRequestBody"; + doAfterVisit(new AddImport<>(fqcn, null, false)); + } + + private void addS3Import(String pojoName) { + String fqcn = V2_S3_MODEL_PKG + pojoName; + doAfterVisit(new AddImport<>(fqcn, null, false)); + } + + private void addTmImport(String pojoName) { + String fqcn = V2_TM_MODEL_PKG + pojoName; + doAfterVisit(new AddImport<>(fqcn, null, false)); + } + } +} diff --git a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3StreamingRequestToV2.java b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3StreamingRequestToV2.java index bc3543ee04f9..9d9c237acc1c 100644 --- a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3StreamingRequestToV2.java +++ b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3StreamingRequestToV2.java @@ -15,6 +15,10 @@ 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.v1S3MethodMatcher; +import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.fullyQualified; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -32,7 +36,6 @@ 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.v2migration.internal.utils.IdentifierUtils; @@ -40,16 +43,12 @@ @SdkInternalApi public class S3StreamingRequestToV2 extends Recipe { private static final MethodMatcher PUT_OBJECT_FILE = - new MethodMatcher("com.amazonaws.services.s3.AmazonS3 " - + "putObject(java.lang.String, java.lang.String, java.io.File)", true); + v1S3MethodMatcher("putObject(java.lang.String, java.lang.String, java.io.File)"); private static final MethodMatcher PUT_OBJECT_STRING = - new MethodMatcher("com.amazonaws.services.s3.AmazonS3 " - + "putObject(java.lang.String, java.lang.String, java.lang.String)", true); + v1S3MethodMatcher("putObject(java.lang.String, java.lang.String, java.lang.String)"); - private static final JavaType.FullyQualified V1_PUT_OBJECT_REQUEST = - TypeUtils.asFullyQualified(JavaType.buildType("com.amazonaws.services.s3.model.PutObjectRequest")); - private static final JavaType.FullyQualified REQUEST_BODY = - TypeUtils.asFullyQualified(JavaType.buildType("software.amazon.awssdk.core.sync.RequestBody")); + private static final JavaType.FullyQualified V1_PUT_OBJECT_REQUEST = fullyQualified(V1_S3_MODEL_PKG + "PutObjectRequest"); + private static final JavaType.FullyQualified REQUEST_BODY = fullyQualified("software.amazon.awssdk.core.sync.RequestBody"); @Override public String getDisplayName() { diff --git a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3StreamingResponseToV2.java b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3StreamingResponseToV2.java index 7e0291d76ea8..3504afb21e51 100644 --- a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3StreamingResponseToV2.java +++ b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3StreamingResponseToV2.java @@ -15,6 +15,10 @@ 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.SdkTypeUtils.fullyQualified; + import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -37,19 +41,18 @@ @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_OBJECT_TYPE = fullyQualified(V1_S3_MODEL_PKG + "S3Object"); private static final JavaType.FullyQualified S3_GET_OBJECT_RESPONSE_TYPE = - TypeUtils.asFullyQualified(JavaType.buildType("software.amazon.awssdk.services.s3.model.GetObjectResponse")); + fullyQualified(V2_S3_MODEL_PKG + "GetObjectResponse"); private static final JavaType.FullyQualified RESPONSE_INPUT_STREAM_TYPE = - TypeUtils.asFullyQualified(JavaType.buildType("software.amazon.awssdk.core.ResponseInputStream")); + fullyQualified("software.amazon.awssdk.core.ResponseInputStream"); private static final MethodMatcher GET_OBJECT_CONTENT = - new MethodMatcher("com.amazonaws.services.s3.model.S3Object getObjectContent()"); + new MethodMatcher(V1_S3_MODEL_PKG + "S3Object getObjectContent()"); private static final MethodMatcher OBJECT_INPUT_STREAM_METHOD = - new MethodMatcher("com.amazonaws.services.s3.model.S3ObjectInputStream *(..)"); + new MethodMatcher(V1_S3_MODEL_PKG + "S3ObjectInputStream *(..)"); @Override public String getDisplayName() { diff --git a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/TransferManagerMethodsToV2.java b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/TransferManagerMethodsToV2.java index 920318010de1..c2c12ff9ed5d 100644 --- a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/TransferManagerMethodsToV2.java +++ b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/TransferManagerMethodsToV2.java @@ -15,6 +15,10 @@ package software.amazon.awssdk.v2migration; +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_S3_MODEL_PKG; +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_TM_MODEL_PKG; +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.v2TmMethodMatcher; + import org.openrewrite.ExecutionContext; import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; @@ -28,27 +32,20 @@ @SdkInternalApi public class TransferManagerMethodsToV2 extends Recipe { - private static final String V2_TM_PKG = "software.amazon.awssdk.transfer.s3."; - private static final String V2_S3_MODEL_PKG = "software.amazon.awssdk.services.s3.model"; - - private static final MethodMatcher DOWNLOAD_BUCKET_KEY_FILE = createMethodMatcher("download(String, String, java.io.File)"); + private static final MethodMatcher DOWNLOAD_BUCKET_KEY_FILE = v2TmMethodMatcher("download(String, String, java.io.File)"); private static final MethodMatcher DOWNLOAD_BUCKET_KEY_FILE_TIMEOUT = - createMethodMatcher("download(String, String, java.io.File, long)"); + v2TmMethodMatcher("download(String, String, java.io.File, long)"); private static final MethodMatcher DOWNLOAD_REQUEST_FILE = - createMethodMatcher(String.format("download(%s.GetObjectRequest, java.io.File)", V2_S3_MODEL_PKG)); + v2TmMethodMatcher(String.format("download(%sGetObjectRequest, java.io.File)", V2_S3_MODEL_PKG)); private static final MethodMatcher DOWNLOAD_REQUEST_FILE_TIMEOUT = - createMethodMatcher(String.format("download(%s.GetObjectRequest, java.io.File, long)", V2_S3_MODEL_PKG)); + v2TmMethodMatcher(String.format("download(%sGetObjectRequest, java.io.File, long)", V2_S3_MODEL_PKG)); - private static final MethodMatcher UPLOAD_BUCKET_KEY_FILE = createMethodMatcher("upload(String, String, java.io.File)"); + private static final MethodMatcher UPLOAD_BUCKET_KEY_FILE = v2TmMethodMatcher("upload(String, String, java.io.File)"); private static final MethodMatcher COPY_REQUEST = - createMethodMatcher(String.format("copy(%s.CopyObjectRequest)", V2_S3_MODEL_PKG)); + v2TmMethodMatcher(String.format("copy(%sCopyObjectRequest)", V2_S3_MODEL_PKG)); private static final MethodMatcher COPY_BUCKET_KEY = - createMethodMatcher("copy(String, String, String, String"); - - private static MethodMatcher createMethodMatcher(String methodSignature) { - return new MethodMatcher(V2_TM_PKG + "S3TransferManager " + methodSignature, true); - } + v2TmMethodMatcher("copy(String, String, String, String"); @Override public String getDisplayName() { @@ -207,12 +204,12 @@ private J.MethodInvocation transformDownloadWithBucketKeyFileTimeout(J.MethodInv } private void addTmImport(String pojoName) { - String fqcn = V2_TM_PKG + "model." + pojoName; + String fqcn = V2_TM_MODEL_PKG + pojoName; doAfterVisit(new AddImport<>(fqcn, null, false)); } private void addS3Import(String pojoName) { - String fqcn = "software.amazon.awssdk.services.s3.model." + pojoName; + String fqcn = V2_S3_MODEL_PKG + pojoName; doAfterVisit(new AddImport<>(fqcn, null, false)); } diff --git a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/internal/utils/NamingConversionUtils.java b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/internal/utils/NamingConversionUtils.java index e7580e0f99b1..5cecb0a5b779 100644 --- a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/internal/utils/NamingConversionUtils.java +++ b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/internal/utils/NamingConversionUtils.java @@ -168,8 +168,6 @@ public final class NamingConversionUtils { S3_POJO_MAPPING.put("SetBucketTaggingConfigurationRequest", "PutBucketTaggingRequest"); S3_POJO_MAPPING.put("SetBucketWebsiteConfigurationRequest", "PutBucketWebsiteRequest"); - S3_POJO_MAPPING.put("ObjectMetadata", "HeadObjectResponse"); - S3_POJO_MAPPING.put("ObjectListing", "ListObjectsResponse"); S3_POJO_MAPPING.put("CorsRule", "CORSRule"); S3_POJO_MAPPING.put("BucketCrossOriginConfiguration", "CORSConfiguration"); S3_POJO_MAPPING.put("BucketAccelerateConfiguration", "AccelerateConfiguration"); @@ -185,6 +183,13 @@ public final class NamingConversionUtils { //Special Enum mapping S3_POJO_MAPPING.put("S3Event", "Event"); + + // TODO - maps 1 to many + // used as input to Request POJOs and returned as Response POJOs + S3_POJO_MAPPING.put("ObjectMetadata", "HeadObjectResponse"); + S3_POJO_MAPPING.put("ObjectListing", "ListObjectsResponse"); + // v2 also has BucketCannedACL + S3_POJO_MAPPING.put("CannedAccessControlList", "ObjectCannedACL"); } static { diff --git a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/internal/utils/S3TransformUtils.java b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/internal/utils/S3TransformUtils.java new file mode 100644 index 000000000000..08fd563a7a37 --- /dev/null +++ b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/internal/utils/S3TransformUtils.java @@ -0,0 +1,48 @@ +/* + * 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.internal.utils; + +import org.openrewrite.java.MethodMatcher; +import software.amazon.awssdk.annotations.SdkInternalApi; + +@SdkInternalApi +public final class S3TransformUtils { + + public static final String V1_S3_CLIENT = "com.amazonaws.services.s3.AmazonS3"; + public static final String V1_S3_MODEL_PKG = "com.amazonaws.services.s3.model."; + + public static final String V2_S3_CLIENT = "software.amazon.awssdk.services.s3.S3Client"; + public static final String V2_S3_MODEL_PKG = "software.amazon.awssdk.services.s3.model."; + + public static final String V2_TM_CLIENT = "software.amazon.awssdk.transfer.s3.S3TransferManager"; + public static final String V2_TM_MODEL_PKG = "software.amazon.awssdk.transfer.s3.model."; + + private S3TransformUtils() { + + } + + public static MethodMatcher v1S3MethodMatcher(String methodSignature) { + return new MethodMatcher(V1_S3_CLIENT + " " + methodSignature, true); + } + + public static MethodMatcher v2S3MethodMatcher(String methodSignature) { + return new MethodMatcher(V2_S3_CLIENT + " " + methodSignature, true); + } + + public static MethodMatcher v2TmMethodMatcher(String methodSignature) { + return new MethodMatcher(V2_TM_CLIENT + " " + methodSignature, true); + } +} diff --git a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/internal/utils/SdkTypeUtils.java b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/internal/utils/SdkTypeUtils.java index 5dd6321a4822..41519ba2f3a8 100644 --- a/v2-migration/src/main/java/software/amazon/awssdk/v2migration/internal/utils/SdkTypeUtils.java +++ b/v2-migration/src/main/java/software/amazon/awssdk/v2migration/internal/utils/SdkTypeUtils.java @@ -538,6 +538,10 @@ public final class SdkTypeUtils { private SdkTypeUtils() { } + public static JavaType.FullyQualified fullyQualified(String clzz) { + return TypeUtils.asFullyQualified(JavaType.buildType(clzz)); + } + public static boolean isCustomSdk(String fullyQualifiedName) { String rootPackage = findRootPackage(fullyQualifiedName, "com.amazonaws.services."); @@ -648,7 +652,7 @@ public static JavaType.FullyQualified v2Builder(JavaType.FullyQualified type) { fqcn = String.format("%s%s", type.getFullyQualifiedName(), "Builder"); } - return TypeUtils.asFullyQualified(JavaType.buildType(fqcn)); + return fullyQualified(fqcn); } public static JavaType.FullyQualified v2ClientFromClientBuilder(JavaType.FullyQualified type) { @@ -657,6 +661,14 @@ public static JavaType.FullyQualified v2ClientFromClientBuilder(JavaType.FullyQu } String builder = type.getFullyQualifiedName().replace("Builder", ""); - return TypeUtils.asFullyQualified(JavaType.buildType(builder)); + return fullyQualified(builder); + } + + public static boolean isInputStreamType(JavaType type) { + return TypeUtils.isAssignableTo("java.io.InputStream", type); + } + + public static boolean isFileType(JavaType type) { + return TypeUtils.isAssignableTo("java.io.File", type); } } diff --git a/v2-migration/src/main/resources/META-INF/rewrite/aws-sdk-java-v1-to-v2-with-tm.yml b/v2-migration/src/main/resources/META-INF/rewrite/aws-sdk-java-v1-to-v2-with-tm.yml index be088ee83306..c5b896c792ff 100644 --- a/v2-migration/src/main/resources/META-INF/rewrite/aws-sdk-java-v1-to-v2-with-tm.yml +++ b/v2-migration/src/main/resources/META-INF/rewrite/aws-sdk-java-v1-to-v2-with-tm.yml @@ -41,5 +41,7 @@ recipeList: - software.amazon.awssdk.v2migration.WrapSdkClientBuilderRegionStr - software.amazon.awssdk.v2migration.EnumCasingToV2 - software.amazon.awssdk.v2migration.SdkBytesToByteBuffer + - software.amazon.awssdk.v2migration.ByteBufferToSdkBytes - software.amazon.awssdk.v2migration.S3NonStreamingRequestToV2Complex + - software.amazon.awssdk.v2migration.S3PutObjectRequestToV2 - software.amazon.awssdk.v2migration.TransferManagerMethodsToV2 \ No newline at end of file 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 3b8353d40dd4..0e3c6a495ab7 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 @@ -40,4 +40,5 @@ recipeList: - software.amazon.awssdk.v2migration.EnumCasingToV2 - software.amazon.awssdk.v2migration.SdkBytesToByteBuffer - software.amazon.awssdk.v2migration.ByteBufferToSdkBytes - - software.amazon.awssdk.v2migration.S3NonStreamingRequestToV2Complex \ No newline at end of file + - software.amazon.awssdk.v2migration.S3NonStreamingRequestToV2Complex + - software.amazon.awssdk.v2migration.S3PutObjectRequestToV2 \ No newline at end of file diff --git a/v2-migration/src/main/resources/META-INF/rewrite/change-s3-methods.yml b/v2-migration/src/main/resources/META-INF/rewrite/change-s3-methods.yml index a111e0d7a9cf..2034463d8b93 100644 --- a/v2-migration/src/main/resources/META-INF/rewrite/change-s3-methods.yml +++ b/v2-migration/src/main/resources/META-INF/rewrite/change-s3-methods.yml @@ -216,4 +216,15 @@ recipeList: newMethodName: withBucket - org.openrewrite.java.ChangeMethodName: methodPattern: com.amazonaws.services.s3.model.CompleteMultipartUploadRequest withPartETags(java.util.List) - newMethodName: withMultipartUpload \ No newline at end of file + newMethodName: withMultipartUpload + + + - org.openrewrite.java.ChangeMethodName: + methodPattern: com.amazonaws.services.s3.model.PutObjectRequest withBucketName(String) + newMethodName: withBucket + - org.openrewrite.java.ChangeMethodName: + methodPattern: com.amazonaws.services.s3.model.PutObjectRequest withCannedAcl(com.amazonaws.services.s3.model.CannedAccessControlList) + newMethodName: withAcl + - org.openrewrite.java.ChangeMethodName: + methodPattern: com.amazonaws.services.s3.model.PutObjectRequest withRequesterPays(boolean) + newMethodName: withRequestPayer \ No newline at end of file diff --git a/v2-migration/src/main/resources/META-INF/rewrite/s3-methods-constructor-to-fluent.yml b/v2-migration/src/main/resources/META-INF/rewrite/s3-methods-constructor-to-fluent.yml index e301669e259c..b1ac43a2dc21 100644 --- a/v2-migration/src/main/resources/META-INF/rewrite/s3-methods-constructor-to-fluent.yml +++ b/v2-migration/src/main/resources/META-INF/rewrite/s3-methods-constructor-to-fluent.yml @@ -34,6 +34,26 @@ recipeList: fluentNames: - withBucket - withKey + - software.amazon.awssdk.v2migration.ConstructorToFluent: + clzzFqcn: com.amazonaws.services.s3.model.PutObjectRequest + parameterTypes: + - java.lang.String + - java.lang.String + - java.lang.String + fluentNames: + - withBucket + - withKey + - withWebsiteRedirectLocation + - software.amazon.awssdk.v2migration.ConstructorToFluent: + clzzFqcn: com.amazonaws.services.s3.model.PutObjectRequest + parameterTypes: + - java.lang.String + - java.lang.String + - java.io.File + fluentNames: + - withBucket + - withKey + - withFile - software.amazon.awssdk.v2migration.ConstructorToFluent: clzzFqcn: com.amazonaws.services.s3.model.CreateBucketRequest parameterTypes: