-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migration Tool Client Config Part 1: map builder variations to the st…
…andard builder (#5165) * WIP * Add recipe to convert builder varitions to standard builder method
- Loading branch information
Showing
12 changed files
with
494 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
...src/main/java/software/amazon/awssdk/migration/recipe/V1BuilderVariationsToV2Builder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/* | ||
* 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.migration.recipe; | ||
|
||
import static software.amazon.awssdk.migration.internal.utils.SdkTypeUtils.isV2AsyncClientClass; | ||
import static software.amazon.awssdk.migration.internal.utils.SdkTypeUtils.isV2ClientBuilder; | ||
|
||
import java.util.Collections; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.Tree; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.tree.Expression; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.JContainer; | ||
import org.openrewrite.java.tree.JRightPadded; | ||
import org.openrewrite.java.tree.JavaType; | ||
import org.openrewrite.java.tree.Space; | ||
import org.openrewrite.java.tree.TypeUtils; | ||
import org.openrewrite.marker.Markers; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.migration.internal.utils.SdkTypeUtils; | ||
|
||
/** | ||
* Internal recipe that renames V1 client builder variations. For example: {@code SqsClientBuilder.standard().build()} to | ||
* {@code SqsClient.builder().build()} and {@code SqsAsyncClient.asyncBuilder().build()} to | ||
* {@code SqsAsyncClient.builder().build()}. | ||
*/ | ||
@SdkInternalApi | ||
public class V1BuilderVariationsToV2Builder extends Recipe { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "V1 client builder variations to builder()"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Transforms V1 builder variations to builder()"; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new Visitor(); | ||
} | ||
|
||
private static class Visitor extends JavaIsoVisitor<ExecutionContext> { | ||
|
||
@Override | ||
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) { | ||
method = super.visitMethodInvocation(method, executionContext); | ||
|
||
JavaType selectType = null; | ||
|
||
Expression select = method.getSelect(); | ||
if (select != null) { | ||
selectType = select.getType(); | ||
} | ||
|
||
if (selectType == null || !shouldChangeMethod(selectType)) { | ||
return method; | ||
} | ||
|
||
if (isV2AsyncClientClass(selectType)) { | ||
return renameAsyncBuilderToBuilder(method, selectType); | ||
} | ||
|
||
if (isV2ClientBuilder(selectType)) { | ||
return renameStandardToBuilder(method, selectType); | ||
} | ||
|
||
return method; | ||
} | ||
|
||
private static boolean shouldChangeMethod(JavaType selectType) { | ||
return isV2ClientBuilder(selectType) || isV2AsyncClientClass(selectType); | ||
} | ||
|
||
private J.MethodInvocation renameStandardToBuilder(J.MethodInvocation method, JavaType selectType) { | ||
String methodName = method.getSimpleName(); | ||
JavaType.Method mt = method.getMethodType(); | ||
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(selectType); | ||
|
||
if (mt == null || !"standard".equals(methodName) || fullyQualified == null) { | ||
return method; | ||
} | ||
|
||
methodName = "builder"; | ||
|
||
JavaType.FullyQualified v2Client = SdkTypeUtils.v2ClientFromClientBuilder(fullyQualified); | ||
J.Identifier id = new J.Identifier( | ||
Tree.randomId(), | ||
Space.EMPTY, | ||
Markers.EMPTY, | ||
Collections.emptyList(), | ||
v2Client.getClassName(), | ||
v2Client, | ||
null | ||
); | ||
|
||
J.Identifier builderMethod = new J.Identifier( | ||
Tree.randomId(), | ||
Space.EMPTY, | ||
Markers.EMPTY, | ||
Collections.emptyList(), | ||
methodName, | ||
null, | ||
null | ||
); | ||
|
||
JavaType.Method methodType = new JavaType.Method( | ||
null, | ||
0L, | ||
v2Client, | ||
methodName, | ||
v2Client, | ||
Collections.emptyList(), | ||
Collections.emptyList(), | ||
Collections.emptyList(), | ||
Collections.emptyList() | ||
); | ||
|
||
J.MethodInvocation builderInvoke = new J.MethodInvocation( | ||
Tree.randomId(), | ||
Space.EMPTY, | ||
Markers.EMPTY, | ||
JRightPadded.build(id), | ||
null, | ||
builderMethod, | ||
JContainer.empty(), | ||
methodType | ||
); | ||
|
||
maybeRemoveImport(fullyQualified); | ||
maybeAddImport(v2Client); | ||
return builderInvoke; | ||
} | ||
|
||
private J.MethodInvocation renameAsyncBuilderToBuilder(J.MethodInvocation method, JavaType selectType) { | ||
String methodName = method.getSimpleName(); | ||
JavaType.Method mt = method.getMethodType(); | ||
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(selectType); | ||
|
||
if (mt == null || fullyQualified == null) { | ||
return method; | ||
} | ||
|
||
if ("asyncBuilder".equals(methodName)) { | ||
methodName = "builder"; | ||
} | ||
|
||
mt = mt.withName(methodName) | ||
.withReturnType(selectType) | ||
.withDeclaringType(fullyQualified); | ||
|
||
method = method.withName(method.getName() | ||
.withSimpleName(methodName) | ||
.withType(mt)) | ||
.withMethodType(mt); | ||
return method; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.