Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration tool - Date to Instant #5986

Merged
merged 1 commit into from
Mar 25, 2025
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
11 changes: 11 additions & 0 deletions test/v2-migration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
<artifactId>aws-java-sdk-s3</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-cloudwatch</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
Expand All @@ -82,6 +87,12 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>cloudwatch</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>v2-migration</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,10 @@
<artifactId>s3</artifactId>
<version>V2_VERSION</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>cloudwatch</artifactId>
<version>V2_VERSION</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Date;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.core.ResponseInputStream;
import software.amazon.awssdk.core.exception.SdkException;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.cloudwatch.model.GetMetricStatisticsRequest;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
Expand Down Expand Up @@ -105,4 +107,11 @@ private static PutObjectResponse uploadString(S3Client s3, String bucket, String

return result;
}

void dateToInstant(Date start, Date end) {
GetMetricStatisticsRequest getMetricStatisticsRequest = GetMetricStatisticsRequest.builder()
.startTime(start.toInstant())
.endTime(end.toInstant())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,9 @@
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-cloudwatch</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;
Expand All @@ -33,6 +34,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Date;

public class Application {

Expand Down Expand Up @@ -100,4 +102,10 @@ private static PutObjectResult uploadString(AmazonS3 s3, String bucket, String k

return result;
}

void dateToInstant(Date start, Date end) {
GetMetricStatisticsRequest getMetricStatisticsRequest = new GetMetricStatisticsRequest()
.withStartTime(start)
.withEndTime(end);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.v2migration;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
import org.openrewrite.ExecutionContext;
import org.openrewrite.NlsRewrite;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils;

@SdkInternalApi
public class DateToInstant extends Recipe {
private static final Pattern DATE_PATTERN = Pattern.compile(Date.class.getCanonicalName());

@Override
public @NlsRewrite.DisplayName String getDisplayName() {
return "Convert Date to Instant";
}

@Override
public @NlsRewrite.Description String getDescription() {
return "Convert Date to Instant by calling Date#toInstant";
}

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

private static final class DateToInstantVisitor extends JavaIsoVisitor<ExecutionContext> {

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation originalMethod, ExecutionContext ctx) {
J.MethodInvocation method = super.visitMethodInvocation(originalMethod, ctx);

if (!isV2ModelSetterWithDateParam(method)) {
return method;
}

JavaTemplate template = JavaTemplate.builder("#{any()}.toInstant()").contextSensitive().build();

return template.apply(updateCursor(method), method.getCoordinates().replaceArguments(), method.getArguments().get(0));
}

private static boolean isV2ModelSetterWithDateParam(J.MethodInvocation method) {
JavaType.Method mt = method.getMethodType();

if (mt == null) {
return false;
}

JavaType.FullyQualified declaringType = mt.getDeclaringType();
List<JavaType> parameterTypes = mt.getParameterTypes();
if (parameterTypes.size() != 1) {
return false;
}

JavaType javaType = parameterTypes.get(0);
if (javaType == null) {
return false;
}

boolean isDateParam = javaType.isAssignableFrom(DATE_PATTERN);
return SdkTypeUtils.isV2ModelBuilder(declaringType) && isDateParam;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have special cases where Date objects should NOT be transformed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No Date in v2, will all be Instant afaik. At least for POJO setters

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ recipeList:
- software.amazon.awssdk.v2migration.EnumCasingToV2
- software.amazon.awssdk.v2migration.SdkBytesToByteBuffer
- software.amazon.awssdk.v2migration.ByteBufferToSdkBytes
- software.amazon.awssdk.v2migration.DateToInstant
- software.amazon.awssdk.v2migration.S3NonStreamingRequestToV2Complex
- software.amazon.awssdk.v2migration.S3PutObjectRequestToV2
- software.amazon.awssdk.v2migration.TransferManagerMethodsToV2
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ recipeList:
- software.amazon.awssdk.v2migration.EnumCasingToV2
- software.amazon.awssdk.v2migration.SdkBytesToByteBuffer
- software.amazon.awssdk.v2migration.ByteBufferToSdkBytes
- software.amazon.awssdk.v2migration.DateToInstant
- software.amazon.awssdk.v2migration.S3NonStreamingRequestToV2Complex
- software.amazon.awssdk.v2migration.S3PutObjectRequestToV2
Loading