|
| 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 com.fasterxml.jackson.annotation.JsonCreator; |
| 19 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | +import java.util.stream.Collectors; |
| 23 | +import org.openrewrite.ExecutionContext; |
| 24 | +import org.openrewrite.Recipe; |
| 25 | +import org.openrewrite.Tree; |
| 26 | +import org.openrewrite.TreeVisitor; |
| 27 | +import org.openrewrite.java.JavaVisitor; |
| 28 | +import org.openrewrite.java.tree.Expression; |
| 29 | +import org.openrewrite.java.tree.J; |
| 30 | +import org.openrewrite.java.tree.JContainer; |
| 31 | +import org.openrewrite.java.tree.JRightPadded; |
| 32 | +import org.openrewrite.java.tree.JavaType; |
| 33 | +import org.openrewrite.java.tree.Space; |
| 34 | +import org.openrewrite.java.tree.TypeUtils; |
| 35 | +import org.openrewrite.marker.Markers; |
| 36 | +import software.amazon.awssdk.annotations.SdkInternalApi; |
| 37 | + |
| 38 | +@SdkInternalApi |
| 39 | +public class ConstructorToFluent extends Recipe { |
| 40 | + private final String clzzFqcn; |
| 41 | + private final List<String> parameterTypes; |
| 42 | + private final List<String> fluentNames; |
| 43 | + |
| 44 | + @JsonCreator |
| 45 | + public ConstructorToFluent(@JsonProperty("clzzFqcn") String clzzFqcn, |
| 46 | + @JsonProperty("parameterTypes") List<String> parameterTypes, |
| 47 | + @JsonProperty("fluentNames") List<String> fluentNames) { |
| 48 | + this.clzzFqcn = clzzFqcn; |
| 49 | + this.parameterTypes = parameterTypes; |
| 50 | + this.fluentNames = fluentNames; |
| 51 | + |
| 52 | + if (fluentNames.size() != parameterTypes.size()) { |
| 53 | + throw new IllegalArgumentException("parameterTypes and fluentNames must be the same length."); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public String getDisplayName() { |
| 59 | + return "Moves constructor arguments to fluent setters"; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public String getDescription() { |
| 64 | + return "A recipe that takes constructor arguments and moves them to the specified fluent setters on the object."; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 69 | + List<JavaType> paramJavaTypes = parameterTypes.stream() |
| 70 | + .map(JavaType::buildType) |
| 71 | + .collect(Collectors.toList()); |
| 72 | + return new Visitor(clzzFqcn, paramJavaTypes, fluentNames); |
| 73 | + } |
| 74 | + |
| 75 | + private static class Visitor extends JavaVisitor<ExecutionContext> { |
| 76 | + private final JavaType.FullyQualified clzz; |
| 77 | + private final List<JavaType> parameterTypes; |
| 78 | + private final List<String> fluentNames; |
| 79 | + |
| 80 | + Visitor(String clzz, List<JavaType> parameterTypes, List<String> fluentNames) { |
| 81 | + this.clzz = TypeUtils.asFullyQualified(JavaType.buildType(clzz)); |
| 82 | + this.parameterTypes = parameterTypes; |
| 83 | + this.fluentNames = fluentNames; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public J visitNewClass(J.NewClass newClass, ExecutionContext executionContext) { |
| 88 | + JavaType.Method ctorType = newClass.getMethodType(); |
| 89 | + |
| 90 | + if (ctorType == null) { |
| 91 | + return newClass; |
| 92 | + } |
| 93 | + |
| 94 | + if (!clzz.isAssignableFrom(ctorType.getDeclaringType())) { |
| 95 | + return newClass; |
| 96 | + } |
| 97 | + |
| 98 | + List<JavaType> paramTypes = ctorType.getParameterTypes(); |
| 99 | + |
| 100 | + if (paramTypes.size() != this.parameterTypes.size()) { |
| 101 | + return newClass; |
| 102 | + } |
| 103 | + |
| 104 | + for (int i = 0; i < parameterTypes.size(); ++i) { |
| 105 | + JavaType expected = this.parameterTypes.get(i); |
| 106 | + if (!TypeUtils.isAssignableTo(expected, paramTypes.get(i))) { |
| 107 | + return newClass; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // Remove all the arguments from the constructor |
| 112 | + List<Expression> arguments = newClass.getArguments(); |
| 113 | + |
| 114 | + ctorType = ctorType.withParameterTypes(Collections.emptyList()) |
| 115 | + .withParameterNames(Collections.emptyList()); |
| 116 | + |
| 117 | + newClass = newClass.withArguments(Collections.emptyList()) |
| 118 | + .withMethodType(ctorType); |
| 119 | + |
| 120 | + JavaType.FullyQualified declaringType = ctorType.getDeclaringType(); |
| 121 | + Expression select = newClass; |
| 122 | + for (int i = 0; i < parameterTypes.size(); ++i) { |
| 123 | + JavaType paramType = parameterTypes.get(i); |
| 124 | + String name = fluentNames.get(i); |
| 125 | + // Note we don't preserve prefix so we don't end up with |
| 126 | + // extra spaces after the opening paren like 'withFoo( arg)' |
| 127 | + Expression argExpr = arguments.get(i).withPrefix(Space.EMPTY); |
| 128 | + |
| 129 | + select = addWither(select, name, paramType, argExpr, declaringType); |
| 130 | + } |
| 131 | + |
| 132 | + return select; |
| 133 | + } |
| 134 | + |
| 135 | + private static J.MethodInvocation addWither(Expression select, String simpleName, |
| 136 | + JavaType parameterType, |
| 137 | + Expression paramExpr, |
| 138 | + JavaType.FullyQualified declaringType) { |
| 139 | + JavaType.Method methodType = new JavaType.Method( |
| 140 | + null, |
| 141 | + 0L, |
| 142 | + declaringType, |
| 143 | + simpleName, |
| 144 | + declaringType, |
| 145 | + Collections.singletonList(simpleName), |
| 146 | + Collections.singletonList(parameterType), |
| 147 | + null, |
| 148 | + null |
| 149 | + ); |
| 150 | + |
| 151 | + J.Identifier witherId = new J.Identifier( |
| 152 | + Tree.randomId(), |
| 153 | + Space.EMPTY, |
| 154 | + Markers.EMPTY, |
| 155 | + Collections.emptyList(), |
| 156 | + simpleName, |
| 157 | + methodType, |
| 158 | + null |
| 159 | + ); |
| 160 | + |
| 161 | + return new J.MethodInvocation( |
| 162 | + Tree.randomId(), |
| 163 | + Space.EMPTY, |
| 164 | + Markers.EMPTY, |
| 165 | + JRightPadded.build(select), |
| 166 | + null, |
| 167 | + witherId, |
| 168 | + JContainer.build(Collections.singletonList(JRightPadded.build(paramExpr))), |
| 169 | + methodType |
| 170 | + ); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments