Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions test/v2-migration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@
<artifactId>test-utils</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import software.amazon.awssdk.testutils.SdkVersionUtils;
import software.amazon.awssdk.utils.Logger;

public class GradleProjectTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected static void verifyTransformation() throws IOException {
String recipeCmd = "-Drewrite.activeRecipes=software.amazon.awssdk.v2migration.AwsSdkJavaV1ToV2";
List<String> rewriteArgs = new ArrayList<>();
// pin version since updates have broken tests
String rewriteMavenPluginVersion = "5.46.0";
String rewriteMavenPluginVersion = "6.17.0";
addAll(rewriteArgs, "mvn", "org.openrewrite.maven:rewrite-maven-plugin:" + rewriteMavenPluginVersion + ":run",
"-Drewrite.recipeArtifactCoordinates=software.amazon.awssdk:v2-migration:"+ getMigrationToolVersion() + "-PREVIEW",
recipeCmd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ initscript {
maven { url "https://plugins.gradle.org/m2" }
}
dependencies {
classpath("org.openrewrite:plugin:6.28.0")
classpath("org.openrewrite:plugin:7.15.0")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class S3EnDateTime {
public void parseEvent(String jsonInput) {
S3EventNotification notification = S3EventNotification.fromJson(jsonInput);

for (S3EventNotification.S3EventNotificationRecord record : notification.getRecords()) {
for (S3EventNotificationRecord record : notification.getRecords()) {
DateTime eventTime = /*AWS SDK for Java v2 migration: getEventTime returns Instant instead of DateTime in v2. AWS SDK v2 does not include org.joda.time as a dependency. If you want to keep using DateTime, you'll need to manually add "org.joda.time:joda-time" dependency to your project after migration.*/new DateTime(record.getEventTime().toEpochMilli());

GlacierEventData glacierEventData = record.getGlacierEventData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public static void main(String... args) {

} catch (SqsException exception) {
System.out.println(String.format("Error code: %s. RequestId: %s. Raw response content: %s",
exception.awsErrorDetails().errorCode(), exception.requestId(),
exception.awsErrorDetails().rawResponse().asUtf8String()));
exception.awsErrorDetails().errorCode(), exception.requestId(),
exception.awsErrorDetails().rawResponse().asUtf8String()));
} catch (AwsServiceException exception) {
System.out.println(String.format("Error message: %s. Service Name: %s",
exception.awsErrorDetails().errorMessage(), exception.awsErrorDetails().serviceName()));
exception.awsErrorDetails().errorMessage(), exception.awsErrorDetails().serviceName()));
} catch (SdkException exception) {
System.out.println("Error message " + exception.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ private void completeMpu(S3Client s3, String bucket, String key) {
.build();

CompleteMultipartUploadRequest completeMpuRequest2 =
CompleteMultipartUploadRequest.builder().bucket(bucket).key(key).uploadId("uploadId").multipartUpload(CompletedMultipartUpload.builder().parts(partETags).build())
.build();
CompleteMultipartUploadRequest.builder().bucket(bucket).key(key).uploadId("uploadId").multipartUpload(CompletedMultipartUpload.builder().parts(partETags).build()).build();
}

private void listObjects(S3Client s3, String bucket) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ public void requestconstructor() {

s3.abortMultipartUpload(AbortMultipartUploadRequest.builder().bucket("name").key("key").uploadId("upload")
.build());
s3.completeMultipartUpload(CompleteMultipartUploadRequest.builder().bucket("name").key("key").uploadId("upload").multipartUpload(CompletedMultipartUpload.builder().parts(new ArrayList<>()).build())
.build());
s3.completeMultipartUpload(CompleteMultipartUploadRequest.builder().bucket("name").key("key").uploadId("upload").multipartUpload(CompletedMultipartUpload.builder().parts(new ArrayList<>()).build()).build());

s3.copyObject(CopyObjectRequest.builder()
.build());
Expand Down Expand Up @@ -458,8 +457,7 @@ public void requestconstructor() {
.build();

List<Tag> tags = new ArrayList<>();
GetObjectTaggingResponse getObjectTaggingResult = GetObjectTaggingResponse.builder().tagSet(tags)
.build();
GetObjectTaggingResponse getObjectTaggingResult = GetObjectTaggingResponse.builder().tagSet(tags).build();

PutBucketVersioningRequest setBucketVersioningConfigurationRequest =
PutBucketVersioningRequest.builder().bucket(bucketName).versioningConfiguration(VersioningConfiguration.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void putObject_bucketKeyStreamMetadata(String bucket, String key, InputStream st
HeadObjectResponse metadataWithLength = HeadObjectResponse.builder()
.build();
s3.putObject(PutObjectRequest.builder().bucket(bucket).key(key).contentLength(22L)
.build(), RequestBody.fromInputStream(stream, 22L));
.build(), RequestBody.fromInputStream(stream, 22));


HeadObjectResponse metadataWithoutLength = HeadObjectResponse.builder()
Expand Down Expand Up @@ -105,7 +105,7 @@ void putObject_requestPojoWithInputStream(String bucket, String key) {
HeadObjectResponse metadata = HeadObjectResponse.builder()
.build();
s3.putObject(PutObjectRequest.builder().bucket(bucket).key(key).websiteRedirectLocation("location").contentLength(11L)
.build(), RequestBody.fromInputStream(inputStream2, 11L));
.build(), RequestBody.fromInputStream(inputStream2, 11));
}

void putObject_requestPojoWithoutPayload(String bucket, String key) {
Expand All @@ -117,8 +117,7 @@ void putObject_requestPojoWithoutPayload(String bucket, String key) {

void putObjectSetters() {
List<Tag> tags = new ArrayList<>();
Tagging objectTagging = Tagging.builder().tagSet(tags)
.build();
Tagging objectTagging = Tagging.builder().tagSet(tags).build();

PutObjectRequest putObjectRequest =
PutObjectRequest.builder().bucket("bucket").key("key").websiteRedirectLocation("location")
Expand Down
4 changes: 2 additions & 2 deletions v2-migration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

<properties>
<awsjavasdk.version>${project.parent.version}</awsjavasdk.version>
<openrewrite.version>2.23.0</openrewrite.version>
<openrewrite.version>3.14.0</openrewrite.version>
<junit.version>5.10.3</junit.version>
<awssdkjavav1.version>1.12.472</awssdkjavav1.version>
</properties>
Expand Down Expand Up @@ -74,7 +74,7 @@
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-java-8</artifactId>
<scope>runtime</scope>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ private static J.MethodInvocation addWither(Expression select, String simpleName
Collections.singletonList(simpleName),
Collections.singletonList(parameterType),
null,
null,
null,
null
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ private J.MethodInvocation addHttpClientBuilderIfNeeded(
Collections.emptyList(),
parametersTypes,
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList()
);

Expand Down Expand Up @@ -359,6 +361,8 @@ private JContainer<Expression> httpClientBuilderInvoke(Map<String, Expression> h
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList()
);

Expand Down Expand Up @@ -403,6 +407,8 @@ private static J.MethodInvocation invokeHttpSetting(Map.Entry<String, Expression
Collections.emptyList(),
parametersTypes,
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ public J visitNewClass(J.NewClass previousNewClass, ExecutionContext executionCo
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList()
);

Expand All @@ -156,6 +158,8 @@ public J visitNewClass(J.NewClass previousNewClass, ExecutionContext executionCo
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ private Expression argsToPojo(JavaType.FullyQualified fqcn, List<String> names,
names,
types,
null,
null,
null,
null
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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.V1_S3_MODEL_PKG;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_S3_MODEL_PKG;

import java.util.List;
import java.util.regex.Pattern;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.AddImport;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import software.amazon.awssdk.annotations.SdkInternalApi;

@SdkInternalApi
public class S3PojoToV2 extends Recipe {

private static final Pattern COMPETE_MPU = Pattern.compile(V1_S3_MODEL_PKG + "CompleteMultipartUploadRequest");
private static final Pattern OBJECT_TAGGING = Pattern.compile(V1_S3_MODEL_PKG + "ObjectTagging");
private static final Pattern GET_OBJECT_TAGGING_RESULT = Pattern.compile(V1_S3_MODEL_PKG + "GetObjectTaggingResult");

@Override
public String getDisplayName() {
return "S3 POJOs to V2";
}

@Override
public String getDescription() {
return "S3 POJOs to V2";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new Visitor();
}

private static final class Visitor extends JavaVisitor<ExecutionContext> {

@Override
public J visitNewClass(J.NewClass newClass, ExecutionContext executionContext) {
JavaType type = newClass.getType();
if (!(type instanceof JavaType.FullyQualified)) {
return newClass;
}

if (type.isAssignableFrom(COMPETE_MPU) && newClass.getArguments().size() == 4) {
addV2S3ModelImport("CompleteMultipartUploadRequest");
addV2S3ModelImport("CompletedMultipartUpload");
List<Expression> params = newClass.getArguments();
String v2Builder = "CompleteMultipartUploadRequest.builder().bucket(#{any()}).key(#{any()}).uploadId(#{any()})"
+ ".multipartUpload(CompletedMultipartUpload.builder().parts(#{any()}).build()).build()";
return JavaTemplate.builder(v2Builder)
.build().apply(getCursor(), newClass.getCoordinates().replace(),
params.get(0), params.get(1), params.get(2), params.get(3));
}
if (type.isAssignableFrom(OBJECT_TAGGING) && newClass.getArguments().size() == 1) {
String v2Builder = "Tagging.builder().tagSet(#{any()}).build();";
return JavaTemplate.builder(v2Builder)
.build().apply(getCursor(), newClass.getCoordinates().replace(),
newClass.getArguments().get(0));
}
if (type.isAssignableFrom(GET_OBJECT_TAGGING_RESULT) && newClass.getArguments().size() == 1) {
String v2Builder = "GetObjectTaggingResponse.builder().tagSet(#{any()}).build();";
return JavaTemplate.builder(v2Builder)
.build().apply(getCursor(), newClass.getCoordinates().replace(),
newClass.getArguments().get(0));
}

return super.visitNewClass(newClass, executionContext);
}

private void addV2S3ModelImport(String className) {
doAfterVisit(new AddImport<>(V2_S3_MODEL_PKG + className, null, false));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,8 @@ private J.MethodInvocation addInputStreamToPutObject(J.MethodInvocation method,
.withComments(inputStreamBufferingWarningComment());
}

StringBuilder sb = new StringBuilder("#{any()}, RequestBody.fromInputStream(#{any()}, #{any()}");
if (contentLen instanceof J.Literal) {
sb.append("L");
}
sb.append(")");

return JavaTemplate.builder(sb.toString()).build()
v2Method = "#{any()}, RequestBody.fromInputStream(#{any()}, #{any()})";
return JavaTemplate.builder(v2Method).build()
.apply(getCursor(), method.getCoordinates().replaceArguments(),
method.getArguments().get(0), inputStream, contentLen);
}
Expand Down Expand Up @@ -346,12 +341,7 @@ private J.MethodInvocation transformPutObjectWithStreamAndMetadata(J.MethodInvoc
.withComments(inputStreamBufferingWarningComment());
}

sb.append(".build(), RequestBody.fromInputStream(#{any()}, #{any()}");

if (contentLen instanceof J.Literal) {
sb.append("L");
}
sb.append(")");
sb.append(".build(), RequestBody.fromInputStream(#{any()}, #{any()})");

params = Arrays.copyOf(params, 4);
params[3] = contentLen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ private J.MethodInvocation stringToRequestBody(Expression fileExpr) {
Collections.singletonList("stringContent"),
Collections.singletonList(JavaType.buildType("java.lang.String")),
null,
null,
null,
null
);

Expand Down Expand Up @@ -186,6 +188,8 @@ private J.MethodInvocation fileToRequestBody(Expression fileExpr) {
Collections.singletonList("file"),
Collections.singletonList(JavaType.buildType("java.io.File")),
null,
null,
null,
null
);

Expand Down Expand Up @@ -217,6 +221,8 @@ private Expression bucketAndKeyToPutObject(Expression bucketExpr, Expression key
Arrays.asList("bucket", "key"),
Arrays.asList(bucketExpr.getType(), keyExpr.getType()),
null,
null,
null,
null
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext execu
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList()
);

Expand Down
Loading