|
43 | 43 | @SdkInternalApi
|
44 | 44 | public class S3NonStreamingRequestToV2 extends Recipe {
|
45 | 45 |
|
| 46 | + private static final MethodMatcher DELETE_VERSION = |
| 47 | + new MethodMatcher("com.amazonaws.services.s3.AmazonS3 deleteVersion(String, String, String)", true); |
| 48 | + private static final MethodMatcher COPY_OBJECT = |
| 49 | + new MethodMatcher("com.amazonaws.services.s3.AmazonS3 copyObject(String, String, String, String)", true); |
| 50 | + private static final MethodMatcher LIST_VERSIONS = |
| 51 | + new MethodMatcher("com.amazonaws.services.s3.AmazonS3 " |
| 52 | + + "listVersions(String, String, String, String, String, Integer)", true); |
| 53 | + private static final MethodMatcher SET_BUCKET_POLICY = |
| 54 | + new MethodMatcher("com.amazonaws.services.s3.AmazonS3 setBucketPolicy(String, String)", true); |
| 55 | + |
46 | 56 | private static final Map<MethodMatcher, JavaType.FullyQualified> BUCKET_ARG_METHODS = new HashMap<>();
|
47 | 57 | private static final Map<MethodMatcher, JavaType.FullyQualified> BUCKET_KEY_ARGS_METHODS = new HashMap<>();
|
48 | 58 | private static final Map<MethodMatcher, JavaType.FullyQualified> BUCKET_ID_ARGS_METHODS = new HashMap<>();
|
@@ -146,74 +156,72 @@ private static final class Visitor extends JavaIsoVisitor<ExecutionContext> {
|
146 | 156 | @Override
|
147 | 157 | public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
|
148 | 158 |
|
| 159 | + if (DELETE_VERSION.matches(method)) { |
| 160 | + method = transformMethod(method, fcqn("deleteObject"), "bucket", "key", "versionId"); |
| 161 | + return super.visitMethodInvocation(method, executionContext); |
| 162 | + } |
| 163 | + if (COPY_OBJECT.matches(method)) { |
| 164 | + method = transformMethod(method, fcqn("copyObject"), |
| 165 | + "sourceBucket", "sourceKey", "destinationBucket", "destinationKey"); |
| 166 | + return super.visitMethodInvocation(method, executionContext); |
| 167 | + } |
| 168 | + if (LIST_VERSIONS.matches(method)) { |
| 169 | + method = transformMethod(method, fcqn("listVersions"), |
| 170 | + "bucket", "prefix", "keyMarker", "versionIdMarker", "delimiter", "maxKeys"); |
| 171 | + return super.visitMethodInvocation(method, executionContext); |
| 172 | + } |
| 173 | + if (SET_BUCKET_POLICY.matches(method)) { |
| 174 | + method = transformMethod(method, fcqn("putBucketPolicy"), "bucket", "policy"); |
| 175 | + return super.visitMethodInvocation(method, executionContext); |
| 176 | + } |
149 | 177 | for (Map.Entry<MethodMatcher, JavaType.FullyQualified> entry : BUCKET_ARG_METHODS.entrySet()) {
|
150 | 178 | if (entry.getKey().matches(method)) {
|
151 |
| - method = transformBucketArgOverload(method, entry.getValue()); |
| 179 | + method = transformMethod(method, entry.getValue(), "bucket"); |
152 | 180 | return super.visitMethodInvocation(method, executionContext);
|
153 | 181 | }
|
154 | 182 | }
|
155 | 183 | for (Map.Entry<MethodMatcher, JavaType.FullyQualified> entry : BUCKET_KEY_ARGS_METHODS.entrySet()) {
|
156 | 184 | if (entry.getKey().matches(method)) {
|
157 |
| - method = transformTwoStringArgOverload(method, entry.getValue(), "key"); |
| 185 | + method = transformMethod(method, entry.getValue(), "bucket", "key"); |
158 | 186 | return super.visitMethodInvocation(method, executionContext);
|
159 | 187 | }
|
160 | 188 | }
|
161 | 189 | for (Map.Entry<MethodMatcher, JavaType.FullyQualified> entry : BUCKET_ID_ARGS_METHODS.entrySet()) {
|
162 | 190 | if (entry.getKey().matches(method)) {
|
163 |
| - method = transformTwoStringArgOverload(method, entry.getValue(), "id"); |
| 191 | + method = transformMethod(method, entry.getValue(), "bucket", "id"); |
164 | 192 | return super.visitMethodInvocation(method, executionContext);
|
165 | 193 | }
|
166 | 194 | }
|
167 | 195 | for (Map.Entry<MethodMatcher, JavaType.FullyQualified> entry : BUCKET_PREFIX_ARGS_METHODS.entrySet()) {
|
168 | 196 | if (entry.getKey().matches(method)) {
|
169 |
| - method = transformTwoStringArgOverload(method, entry.getValue(), "prefix"); |
| 197 | + method = transformMethod(method, entry.getValue(), "bucket", "prefix"); |
170 | 198 | return super.visitMethodInvocation(method, executionContext);
|
171 | 199 | }
|
172 | 200 | }
|
173 | 201 |
|
174 | 202 | return super.visitMethodInvocation(method, executionContext);
|
175 | 203 | }
|
176 | 204 |
|
177 |
| - private J.MethodInvocation transformBucketArgOverload(J.MethodInvocation method, JavaType.FullyQualified fqcn) { |
| 205 | + private J.MethodInvocation transformMethod(J.MethodInvocation method, JavaType.FullyQualified fqcn, |
| 206 | + String... args) { |
178 | 207 | JavaType.Method methodType = method.getMethodType();
|
179 | 208 | if (methodType == null) {
|
180 | 209 | return method;
|
181 | 210 | }
|
182 | 211 |
|
183 |
| - Expression bucketExpr = method.getArguments().get(0); |
184 |
| - List<Expression> newArgs = new ArrayList<>(); |
185 |
| - |
186 |
| - Expression expr = argsToPojo(fqcn, Collections.singletonList("bucket"), |
187 |
| - Collections.singletonList(bucketExpr.getType()), |
188 |
| - JContainer.build(Collections.singletonList(JRightPadded.build(bucketExpr)))); |
189 |
| - newArgs.add(expr); |
190 |
| - |
191 |
| - methodType = addParamsToMethod(methodType, newArgs); |
192 |
| - |
193 |
| - return method.withMethodType(methodType).withArguments(newArgs); |
194 |
| - } |
| 212 | + List<String> names = Arrays.asList(args); |
| 213 | + List<JavaType> types = new ArrayList<>(); |
| 214 | + List<JRightPadded<Expression>> expressions = new ArrayList<>(); |
195 | 215 |
|
196 |
| - private J.MethodInvocation transformTwoStringArgOverload(J.MethodInvocation method, JavaType.FullyQualified fqcn, |
197 |
| - String secondArgName) { |
198 |
| - JavaType.Method methodType = method.getMethodType(); |
199 |
| - if (methodType == null) { |
200 |
| - return method; |
| 216 | + for (int i = 0; i < names.size(); i++) { |
| 217 | + Expression expr = method.getArguments().get(i); |
| 218 | + types.add(expr.getType()); |
| 219 | + expressions.add(JRightPadded.build(expr)); |
201 | 220 | }
|
202 | 221 |
|
203 |
| - Expression bucketExpr = method.getArguments().get(0); |
204 |
| - Expression stringExpr = method.getArguments().get(1); |
205 |
| - List<Expression> newArgs = new ArrayList<>(); |
206 |
| - |
207 |
| - List<String> names = Arrays.asList("bucket", secondArgName); |
208 |
| - List<JavaType> types = Arrays.asList(bucketExpr.getType(), stringExpr.getType()); |
209 |
| - JContainer<Expression> args = JContainer.build(Arrays.asList(JRightPadded.build(bucketExpr), |
210 |
| - JRightPadded.build(stringExpr))); |
211 |
| - |
212 |
| - Expression expr = argsToPojo(fqcn, names, types, args); |
213 |
| - newArgs.add(expr); |
214 |
| - |
| 222 | + Expression newPojo = argsToPojo(fqcn, names, types, JContainer.build(expressions)); |
| 223 | + List<Expression> newArgs = Collections.singletonList(newPojo); |
215 | 224 | methodType = addParamsToMethod(methodType, newArgs);
|
216 |
| - |
217 | 225 | return method.withMethodType(methodType).withArguments(newArgs);
|
218 | 226 | }
|
219 | 227 |
|
|
0 commit comments