Skip to content

Commit 6451fbd

Browse files
authored
Migration tool - Date to Instant (#5986)
1 parent a6573d7 commit 6451fbd

File tree

8 files changed

+127
-0
lines changed

8 files changed

+127
-0
lines changed

test/v2-migration-tests/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
<artifactId>aws-java-sdk-s3</artifactId>
7171
<scope>test</scope>
7272
</dependency>
73+
<dependency>
74+
<groupId>com.amazonaws</groupId>
75+
<artifactId>aws-java-sdk-cloudwatch</artifactId>
76+
<scope>test</scope>
77+
</dependency>
7378
<dependency>
7479
<groupId>software.amazon.awssdk</groupId>
7580
<artifactId>s3</artifactId>
@@ -82,6 +87,12 @@
8287
<version>${project.version}</version>
8388
<scope>test</scope>
8489
</dependency>
90+
<dependency>
91+
<groupId>software.amazon.awssdk</groupId>
92+
<artifactId>cloudwatch</artifactId>
93+
<version>${project.version}</version>
94+
<scope>test</scope>
95+
</dependency>
8596
<dependency>
8697
<groupId>software.amazon.awssdk</groupId>
8798
<artifactId>v2-migration</artifactId>

test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/after/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,10 @@
6262
<artifactId>s3</artifactId>
6363
<version>V2_VERSION</version>
6464
</dependency>
65+
<dependency>
66+
<groupId>software.amazon.awssdk</groupId>
67+
<artifactId>cloudwatch</artifactId>
68+
<version>V2_VERSION</version>
69+
</dependency>
6570
</dependencies>
6671
</project>

test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/after/src/main/java/foo/bar/Application.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
import java.nio.file.Files;
2121
import java.nio.file.Path;
2222
import java.nio.file.StandardCopyOption;
23+
import java.util.Date;
2324
import software.amazon.awssdk.awscore.exception.AwsServiceException;
2425
import software.amazon.awssdk.core.ResponseInputStream;
2526
import software.amazon.awssdk.core.exception.SdkException;
2627
import software.amazon.awssdk.core.sync.RequestBody;
28+
import software.amazon.awssdk.services.cloudwatch.model.GetMetricStatisticsRequest;
2729
import software.amazon.awssdk.services.s3.S3Client;
2830
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
2931
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
@@ -105,4 +107,11 @@ private static PutObjectResponse uploadString(S3Client s3, String bucket, String
105107

106108
return result;
107109
}
110+
111+
void dateToInstant(Date start, Date end) {
112+
GetMetricStatisticsRequest getMetricStatisticsRequest = GetMetricStatisticsRequest.builder()
113+
.startTime(start.toInstant())
114+
.endTime(end.toInstant())
115+
.build();
116+
}
108117
}

test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/before/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,9 @@
5555
<groupId>com.amazonaws</groupId>
5656
<artifactId>aws-java-sdk-s3</artifactId>
5757
</dependency>
58+
<dependency>
59+
<groupId>com.amazonaws</groupId>
60+
<artifactId>aws-java-sdk-cloudwatch</artifactId>
61+
</dependency>
5862
</dependencies>
5963
</project>

test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/before/src/main/java/foo/bar/Application.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.amazonaws.AmazonClientException;
1919
import com.amazonaws.AmazonServiceException;
20+
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest;
2021
import com.amazonaws.services.s3.AmazonS3;
2122
import com.amazonaws.services.s3.model.GetObjectRequest;
2223
import com.amazonaws.services.s3.model.PutObjectRequest;
@@ -33,6 +34,7 @@
3334
import java.nio.file.Files;
3435
import java.nio.file.Path;
3536
import java.nio.file.StandardCopyOption;
37+
import java.util.Date;
3638

3739
public class Application {
3840

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

101103
return result;
102104
}
105+
106+
void dateToInstant(Date start, Date end) {
107+
GetMetricStatisticsRequest getMetricStatisticsRequest = new GetMetricStatisticsRequest()
108+
.withStartTime(start)
109+
.withEndTime(end);
110+
}
103111
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.v2migration;
17+
18+
import java.util.Date;
19+
import java.util.List;
20+
import java.util.regex.Pattern;
21+
import org.openrewrite.ExecutionContext;
22+
import org.openrewrite.NlsRewrite;
23+
import org.openrewrite.Recipe;
24+
import org.openrewrite.TreeVisitor;
25+
import org.openrewrite.java.JavaIsoVisitor;
26+
import org.openrewrite.java.JavaTemplate;
27+
import org.openrewrite.java.tree.J;
28+
import org.openrewrite.java.tree.JavaType;
29+
import software.amazon.awssdk.annotations.SdkInternalApi;
30+
import software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils;
31+
32+
@SdkInternalApi
33+
public class DateToInstant extends Recipe {
34+
private static final Pattern DATE_PATTERN = Pattern.compile(Date.class.getCanonicalName());
35+
36+
@Override
37+
public @NlsRewrite.DisplayName String getDisplayName() {
38+
return "Convert Date to Instant";
39+
}
40+
41+
@Override
42+
public @NlsRewrite.Description String getDescription() {
43+
return "Convert Date to Instant by calling Date#toInstant";
44+
}
45+
46+
@Override
47+
public TreeVisitor<?, ExecutionContext> getVisitor() {
48+
return new DateToInstantVisitor();
49+
}
50+
51+
private static final class DateToInstantVisitor extends JavaIsoVisitor<ExecutionContext> {
52+
53+
@Override
54+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation originalMethod, ExecutionContext ctx) {
55+
J.MethodInvocation method = super.visitMethodInvocation(originalMethod, ctx);
56+
57+
if (!isV2ModelSetterWithDateParam(method)) {
58+
return method;
59+
}
60+
61+
JavaTemplate template = JavaTemplate.builder("#{any()}.toInstant()").contextSensitive().build();
62+
63+
return template.apply(updateCursor(method), method.getCoordinates().replaceArguments(), method.getArguments().get(0));
64+
}
65+
66+
private static boolean isV2ModelSetterWithDateParam(J.MethodInvocation method) {
67+
JavaType.Method mt = method.getMethodType();
68+
69+
if (mt == null) {
70+
return false;
71+
}
72+
73+
JavaType.FullyQualified declaringType = mt.getDeclaringType();
74+
List<JavaType> parameterTypes = mt.getParameterTypes();
75+
if (parameterTypes.size() != 1) {
76+
return false;
77+
}
78+
79+
JavaType javaType = parameterTypes.get(0);
80+
if (javaType == null) {
81+
return false;
82+
}
83+
84+
boolean isDateParam = javaType.isAssignableFrom(DATE_PATTERN);
85+
return SdkTypeUtils.isV2ModelBuilder(declaringType) && isDateParam;
86+
}
87+
}
88+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ recipeList:
4242
- software.amazon.awssdk.v2migration.EnumCasingToV2
4343
- software.amazon.awssdk.v2migration.SdkBytesToByteBuffer
4444
- software.amazon.awssdk.v2migration.ByteBufferToSdkBytes
45+
- software.amazon.awssdk.v2migration.DateToInstant
4546
- software.amazon.awssdk.v2migration.S3NonStreamingRequestToV2Complex
4647
- software.amazon.awssdk.v2migration.S3PutObjectRequestToV2
4748
- software.amazon.awssdk.v2migration.TransferManagerMethodsToV2

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ recipeList:
4040
- software.amazon.awssdk.v2migration.EnumCasingToV2
4141
- software.amazon.awssdk.v2migration.SdkBytesToByteBuffer
4242
- software.amazon.awssdk.v2migration.ByteBufferToSdkBytes
43+
- software.amazon.awssdk.v2migration.DateToInstant
4344
- software.amazon.awssdk.v2migration.S3NonStreamingRequestToV2Complex
4445
- software.amazon.awssdk.v2migration.S3PutObjectRequestToV2

0 commit comments

Comments
 (0)