Skip to content

Commit e8e3fca

Browse files
authored
Add V1GetterToV2 Recipe (#5260)
* Add V1GetterToV2 Recipe * Add check for model class * Update recipe path
1 parent 8f14af3 commit e8e3fca

File tree

6 files changed

+194
-1
lines changed

6 files changed

+194
-1
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.migration.internal.recipe;
17+
18+
import static software.amazon.awssdk.migration.internal.utils.SdkTypeUtils.isV2ModelClass;
19+
20+
import org.openrewrite.ExecutionContext;
21+
import org.openrewrite.Recipe;
22+
import org.openrewrite.TreeVisitor;
23+
import org.openrewrite.java.JavaIsoVisitor;
24+
import org.openrewrite.java.tree.Expression;
25+
import org.openrewrite.java.tree.J;
26+
import org.openrewrite.java.tree.JavaType;
27+
import org.openrewrite.java.tree.TypeUtils;
28+
import software.amazon.awssdk.annotations.SdkInternalApi;
29+
import software.amazon.awssdk.migration.internal.utils.NamingUtils;
30+
31+
@SdkInternalApi
32+
public class V1GetterToV2 extends Recipe {
33+
@Override
34+
public String getDisplayName() {
35+
return "V1 Getter to V2";
36+
}
37+
38+
@Override
39+
public String getDescription() {
40+
return "Transforms V1 getter to fluent getter in V2.";
41+
}
42+
43+
@Override
44+
public TreeVisitor<?, ExecutionContext> getVisitor() {
45+
return new V1GetterToV2Visitor();
46+
}
47+
48+
private static class V1GetterToV2Visitor extends JavaIsoVisitor<ExecutionContext> {
49+
50+
@Override
51+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
52+
method = super.visitMethodInvocation(method, executionContext);
53+
54+
JavaType selectType;
55+
56+
Expression select = method.getSelect();
57+
58+
if (select == null || select.getType() == null) {
59+
return method;
60+
}
61+
selectType = select.getType();
62+
63+
String methodName = method.getSimpleName();
64+
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(selectType);
65+
66+
if (!shouldChangeGetter(fullyQualified)) {
67+
return method;
68+
}
69+
70+
if (NamingUtils.isGetter(methodName)) {
71+
methodName = NamingUtils.removeGet(methodName);
72+
}
73+
74+
JavaType.Method methodType = method.getMethodType();
75+
76+
if (methodType != null) {
77+
methodType = methodType.withName(methodName)
78+
.withReturnType(selectType);
79+
80+
if (fullyQualified != null) {
81+
methodType = methodType.withDeclaringType(fullyQualified);
82+
}
83+
84+
method = method.withName(method.getName()
85+
.withSimpleName(methodName)
86+
.withType(methodType))
87+
.withMethodType(methodType);
88+
}
89+
90+
return method;
91+
}
92+
93+
private static boolean shouldChangeGetter(JavaType.FullyQualified selectType) {
94+
return isV2ModelClass(selectType);
95+
}
96+
}
97+
}

migration-tool/src/main/java/software/amazon/awssdk/migration/internal/utils/NamingUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public static String removeSet(String name) {
3232
return removePrefix(name, "set");
3333
}
3434

35+
public static String removeGet(String name) {
36+
return removePrefix(name, "get");
37+
}
38+
3539
private static String removePrefix(String name, String prefix) {
3640
if (StringUtils.isBlank(name)) {
3741
return name;
@@ -53,4 +57,8 @@ public static boolean isWither(String name) {
5357
public static boolean isSetter(String name) {
5458
return !StringUtils.isBlank(name) && name.startsWith("set");
5559
}
60+
61+
public static boolean isGetter(String name) {
62+
return !StringUtils.isBlank(name) && name.startsWith("get");
63+
}
5664
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ recipeList:
3030
# At this point, all classes should be changed to v2 equivalents
3131
- software.amazon.awssdk.migration.recipe.V1BuilderVariationsToV2Builder
3232
- software.amazon.awssdk.migration.recipe.NewClassToBuilderPattern
33-
- software.amazon.awssdk.migration.recipe.NewClassToStaticFactory
33+
- software.amazon.awssdk.migration.recipe.NewClassToStaticFactory
34+
- software.amazon.awssdk.migration.internal.recipe.V1GetterToV2
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package software.amazon.awssdk.migration.recipe;
2+
3+
import static org.openrewrite.java.Assertions.java;
4+
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.api.condition.EnabledOnJre;
7+
import org.junit.jupiter.api.condition.JRE;
8+
import org.openrewrite.java.Java8Parser;
9+
import org.openrewrite.test.RecipeSpec;
10+
import org.openrewrite.test.RewriteTest;
11+
import software.amazon.awssdk.migration.internal.recipe.V1GetterToV2;
12+
13+
public class V1GetterToV2Test implements RewriteTest {
14+
@Override
15+
public void defaults(RecipeSpec spec) {
16+
spec.recipes(new ChangeSdkType(), new NewClassToBuilderPattern(), new V1GetterToV2());
17+
spec.parser(Java8Parser.builder().classpath("sqs",
18+
"aws-java-sdk-sqs",
19+
"sqs",
20+
"sdk-core"));
21+
}
22+
23+
@Test
24+
@EnabledOnJre({JRE.JAVA_8})
25+
void v1ModelClassGetter_isRewrittenToFluent() {
26+
rewriteRun(
27+
java(
28+
"import com.amazonaws.services.sqs.AmazonSQS;\n"
29+
+ "import com.amazonaws.services.sqs.AmazonSQSClient;\n"
30+
+ "import com.amazonaws.services.sqs.model.ReceiveMessageRequest;\n"
31+
+ "import com.amazonaws.services.sqs.model.ReceiveMessageResult;\n"
32+
+ "import com.amazonaws.services.sqs.model.Message;\n"
33+
+ "\n"
34+
+ "public class SqsExample {\n"
35+
+ " public static void main(String[] args) {\n"
36+
+ " AmazonSQS sqs = new AmazonSQSClient();\n"
37+
+ " ReceiveMessageRequest request = new ReceiveMessageRequest().withQueueUrl(\"url\");\n"
38+
+ " ReceiveMessageResult receiveMessage = sqs.receiveMessage(request);\n"
39+
+ " List<Message> messages = receiveMessage.getMessages();\n"
40+
+ " }\n"
41+
+ "}\n",
42+
"import software.amazon.awssdk.services.sqs.SqsClient;\n"
43+
+ "import software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest;\n"
44+
+ "import software.amazon.awssdk.services.sqs.model.ReceiveMessageResponse;\n"
45+
+ "import software.amazon.awssdk.services.sqs.model.Message;\n"
46+
+ "\n"
47+
+ "public class SqsExample {\n"
48+
+ " public static void main(String[] args) {\n"
49+
+ " SqsClient sqs = SqsClient.builder().build();\n"
50+
+ " ReceiveMessageRequest request = ReceiveMessageRequest.builder().queueUrl(\"url\").build();\n"
51+
+ " ReceiveMessageResponse receiveMessage = sqs.receiveMessage(request);\n"
52+
+ " List<Message> messages = receiveMessage.messages();\n"
53+
+ " }\n"
54+
+ "}"
55+
)
56+
);
57+
}
58+
59+
@Test
60+
@EnabledOnJre({JRE.JAVA_8})
61+
void nonV1ModelClass_shouldNotChangeGetter() {
62+
rewriteRun(
63+
java(
64+
"import java.util.Locale;\n"
65+
+ "\n"
66+
+ "public class NonV1ModelClassExample {\n"
67+
+ " public static void main(String[] args) {\n"
68+
+ " Locale locale = Locale.getDefault();\n"
69+
+ " String path = System.getenv(\"PATH\");\n"
70+
+ " String className = String.class.getName();\n"
71+
+ " }\n"
72+
+ "}\n",
73+
"import java.util.Locale;\n"
74+
+ "\n"
75+
+ "public class NonV1ModelClassExample {\n"
76+
+ " public static void main(String[] args) {\n"
77+
+ " Locale locale = Locale.getDefault();\n"
78+
+ " String path = System.getenv(\"PATH\");\n"
79+
+ " String className = String.class.getName();\n"
80+
+ " }\n"
81+
+ "}"
82+
)
83+
);
84+
}
85+
}

test/migration-tool-tests/src/test/resources/after/src/main/java/foo/bar/Application.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public static void main(String... args) {
4242
.queueNamePrefix("MyQueue-")
4343
.nextToken("token").build();
4444
ListQueuesResponse listQueuesResult = sqs.listQueues(request);
45+
String token = listQueuesResult.nextToken();
4546
System.out.println(listQueuesResult);
4647
}
4748

test/migration-tool-tests/src/test/resources/before/src/main/java/foo/bar/Application.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static void main(String... args) {
4141
.withQueueNamePrefix("MyQueue-")
4242
.withNextToken("token");
4343
ListQueuesResult listQueuesResult = sqs.listQueues(request);
44+
String token = listQueuesResult.getNextToken();
4445
System.out.println(listQueuesResult);
4546
}
4647

0 commit comments

Comments
 (0)