Skip to content

Commit 23c2ad6

Browse files
authored
Add Region class mapping (#5118)
1 parent 55f165a commit 23c2ad6

File tree

4 files changed

+117
-1
lines changed

4 files changed

+117
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
type: specs.openrewrite.org/v1beta/recipe
17+
name: software.amazon.awssdk.ChangeRegionTypes
18+
displayName: Change region related classes
19+
recipeList:
20+
- org.openrewrite.java.ChangeMethodName:
21+
methodPattern: com.amazonaws.regions.Regions fromName(String)
22+
newMethodName: of
23+
- org.openrewrite.java.ChangeMethodName:
24+
methodPattern: com.amazonaws.regions.Regions getName()
25+
newMethodName: id
26+
- org.openrewrite.java.ChangeType:
27+
oldFullyQualifiedTypeName: com.amazonaws.regions.Regions
28+
newFullyQualifiedTypeName: software.amazon.awssdk.regions.Region
29+
30+
## TODO: handle RegionUtils, Region
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
type: specs.openrewrite.org/v1beta/recipe
17+
name: software.amazon.awssdk.ChangeSdkCoreTypes
18+
displayName: Change Maven dependency groupId, artifactId and/or the version example
19+
recipeList:
20+
- software.amazon.awssdk.ChangeRegionTypes

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
@@ -22,4 +22,5 @@ tags:
2222
- sdk
2323
recipeList:
2424
- software.amazon.awssdk.UpgradeSdkDependencies
25-
- software.amazon.awssdk.migration.recipe.ChangeSdkType
25+
- software.amazon.awssdk.migration.recipe.ChangeSdkType
26+
- software.amazon.awssdk.ChangeSdkCoreTypes
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.recipe;
17+
18+
import static org.openrewrite.java.Assertions.java;
19+
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.condition.EnabledOnJre;
24+
import org.junit.jupiter.api.condition.JRE;
25+
import org.openrewrite.java.Java8Parser;
26+
import org.openrewrite.test.RecipeSpec;
27+
import org.openrewrite.test.RewriteTest;
28+
29+
30+
public class ChangeRegionTypesTest implements RewriteTest {
31+
32+
@Override
33+
public void defaults(RecipeSpec spec) {
34+
try (InputStream stream = getClass().getResourceAsStream("/META-INF/rewrite/change-region-types.yml")) {
35+
spec.recipe(stream, "software.amazon.awssdk.ChangeRegionTypes");
36+
} catch (IOException e) {
37+
throw new RuntimeException(e);
38+
}
39+
}
40+
41+
@Test
42+
@EnabledOnJre({JRE.JAVA_8})
43+
void shouldChangeRegions() {
44+
rewriteRun(
45+
spec -> spec.parser(Java8Parser.builder().classpath("aws-java-sdk-core")),
46+
java(
47+
"import com.amazonaws.regions.Regions;\n" +
48+
"class Test {\n" +
49+
" static void method() {\n" +
50+
" Regions region = Regions.fromName(\"us-east-1\");\n" +
51+
" Regions region2 = Regions.US_EAST_1;\n" +
52+
" }\n" +
53+
"}\n",
54+
"import software.amazon.awssdk.regions.Region;\n" +
55+
"\n" +
56+
"class Test {\n" +
57+
" static void method() {\n" +
58+
" Region region = Region.of(\"us-east-1\");\n" +
59+
" Region region2 = Region.US_EAST_1;\n" +
60+
" }\n" +
61+
"}\n"
62+
)
63+
);
64+
}
65+
}

0 commit comments

Comments
 (0)