16
16
package software .amazon .awssdk .migration .recipe ;
17
17
18
18
import static software .amazon .awssdk .migration .internal .utils .NamingConversionUtils .getV2Equivalent ;
19
+ import static software .amazon .awssdk .migration .internal .utils .NamingConversionUtils .getV2ModelPackageWildCardEquivalent ;
19
20
import static software .amazon .awssdk .migration .internal .utils .SdkTypeUtils .isV1ClientClass ;
20
21
import static software .amazon .awssdk .migration .internal .utils .SdkTypeUtils .isV1ModelClass ;
21
22
23
+ import java .util .ArrayList ;
22
24
import java .util .HashMap ;
23
25
import java .util .HashSet ;
24
26
import java .util .IdentityHashMap ;
27
+ import java .util .List ;
25
28
import java .util .Map ;
26
29
import java .util .Optional ;
27
30
import java .util .Set ;
55
58
*/
56
59
@ SdkInternalApi
57
60
public class ChangeSdkType extends Recipe {
58
-
59
- private static final String V1_PATTERN = "com.amazonaws.services." ;
61
+ private static final String V1_SERVICE_MODEL_WILD_CARD_CLASS_PATTERN =
62
+ "com\\ .amazonaws\\ .services\\ .[a-zA-Z0-9]+\\ .model\\ .\\ *" ;
63
+ private static final String V1_SERVICE_WILD_CARD_CLASS_PATTERN = "com\\ .amazonaws\\ .services\\ .[a-zA-Z0-9]+\\ .\\ *" ;
60
64
61
65
@ Override
62
66
public String getDisplayName () {
@@ -79,6 +83,7 @@ private static class ChangeTypeVisitor extends JavaVisitor<ExecutionContext> {
79
83
80
84
private final Map <JavaType , JavaType > oldNameToChangedType = new IdentityHashMap <>();
81
85
private final Set <String > topLevelClassnames = new HashSet <>();
86
+ private final List <String > wildcardImports = new ArrayList <>();
82
87
83
88
private Map <String , Pair <JavaType .Class , JavaType >> oldTypeToNewType = new HashMap <>();
84
89
@@ -99,19 +104,19 @@ public J visitImport(J.Import anImport, ExecutionContext ctx) {
99
104
.map (TypeUtils ::asFullyQualified )
100
105
.orElse (null );
101
106
102
- if (fullyQualified == null ) {
107
+ if (fullyQualified == null ) {
108
+ String fullName = anImport .getTypeName ();
109
+ if (isWildcard (fullName )) {
110
+ maybeAddImport (getV2ModelPackageWildCardEquivalent (fullName ));
111
+ wildcardImports .add (fullName );
112
+ }
103
113
return anImport ;
104
114
}
105
115
106
116
String currentFqcn = fullyQualified .getFullyQualifiedName ();
107
117
108
- if (isV1ModelClass (fullyQualified ) || isV1ClientClass (fullyQualified )) {
109
- JavaType .ShallowClass originalType = JavaType .ShallowClass .build (currentFqcn );
110
- String v2Equivalent = getV2Equivalent (currentFqcn );
111
-
112
- JavaType targetType = JavaType .buildType (v2Equivalent );
113
-
114
- oldTypeToNewType .put (currentFqcn , Pair .of (originalType , targetType ));
118
+ if (isV1Class (fullyQualified )) {
119
+ storeV1ClassMetadata (currentFqcn );
115
120
if (anImport .getAlias () != null ) {
116
121
importAlias = anImport .getAlias ();
117
122
}
@@ -120,9 +125,13 @@ public J visitImport(J.Import anImport, ExecutionContext ctx) {
120
125
return anImport ;
121
126
}
122
127
123
- @ Override
124
- public J visitCompilationUnit (J .CompilationUnit cu , ExecutionContext executionContext ) {
125
- return super .visitCompilationUnit (cu , executionContext );
128
+ private static boolean isWildcard (String fullName ) {
129
+ return fullName .matches (V1_SERVICE_MODEL_WILD_CARD_CLASS_PATTERN ) ||
130
+ fullName .matches (V1_SERVICE_WILD_CARD_CLASS_PATTERN );
131
+ }
132
+
133
+ private static boolean isV1Class (JavaType .FullyQualified fullyQualified ) {
134
+ return isV1ModelClass (fullyQualified ) || isV1ClientClass (fullyQualified );
126
135
}
127
136
128
137
@ Override
@@ -230,6 +239,23 @@ private J postVisitSourceFile(JavaSourceFile tree, ExecutionContext ctx, J curre
230
239
231
240
currentTree = sourceFile ;
232
241
}
242
+
243
+ return removeWildcardImports (ctx , currentTree , sourceFile );
244
+ }
245
+
246
+ private J removeWildcardImports (ExecutionContext ctx , J currentTree , JavaSourceFile sourceFile ) {
247
+ for (String fqcn : wildcardImports ) {
248
+ sourceFile = (JavaSourceFile ) new RemoveImport <ExecutionContext >(fqcn )
249
+ .visit (sourceFile , ctx , getCursor ().getParentOrThrow ());
250
+
251
+ if (sourceFile != null ) {
252
+ sourceFile = sourceFile .withImports (
253
+ ListUtils .map (sourceFile .getImports (), i -> visitAndCast (i , ctx ,
254
+ super ::visitImport )));
255
+ }
256
+
257
+ currentTree = sourceFile ;
258
+ }
233
259
return currentTree ;
234
260
}
235
261
@@ -281,33 +307,50 @@ public J visitFieldAccess(J.FieldAccess fieldAccess, ExecutionContext ctx) {
281
307
public J visitIdentifier (J .Identifier ident , ExecutionContext ctx ) {
282
308
283
309
JavaType currentType = ident .getType ();
284
- if (currentType instanceof JavaType .FullyQualified ) {
285
- JavaType .FullyQualified original = TypeUtils .asFullyQualified (currentType );
310
+ if (!(currentType instanceof JavaType .FullyQualified )) {
311
+ return visitAndCast (ident , ctx , super ::visitIdentifier );
312
+ }
313
+
314
+ JavaType .FullyQualified original = TypeUtils .asFullyQualified (currentType );
286
315
287
- if (original != null && TypeUtils .isOfClassType (ident .getType (), original .getFullyQualifiedName ())) {
288
- String fullyQualifiedName = original .getFullyQualifiedName ();
316
+ if (original != null && TypeUtils .isOfClassType (ident .getType (), original .getFullyQualifiedName ())) {
317
+ String fullyQualifiedName = original .getFullyQualifiedName ();
289
318
290
- if (oldTypeToNewType .containsKey (fullyQualifiedName )) {
291
- JavaType .Class originalType = oldTypeToNewType .get (fullyQualifiedName ).left ();
292
- String className = originalType .getClassName ();
319
+ if (isV1Class (original )) {
320
+ storeV1ClassMetadata (fullyQualifiedName );
321
+ JavaType .Class originalType = oldTypeToNewType .get (fullyQualifiedName ).left ();
322
+ String className = originalType .getClassName ();
293
323
294
- if (ident .getSimpleName ().equals (className )) {
295
- JavaType targetType = oldTypeToNewType .get (fullyQualifiedName ).right ();
296
- ident = ident .withSimpleName (((JavaType .FullyQualified ) targetType ).getClassName ());
297
- ident = ident .withType (updateType (currentType ));
298
- }
324
+ if (ident .getSimpleName ().equals (className )) {
325
+ JavaType targetType = oldTypeToNewType .get (fullyQualifiedName ).right ();
326
+ ident = ident .withSimpleName (((JavaType .FullyQualified ) targetType ).getClassName ());
327
+ ident = ident .withType (updateType (currentType ));
299
328
}
300
329
}
301
330
}
302
331
303
332
return visitAndCast (ident , ctx , super ::visitIdentifier );
304
333
}
305
334
335
+ private void storeV1ClassMetadata (String currentFqcn ) {
336
+ JavaType .ShallowClass originalType = JavaType .ShallowClass .build (currentFqcn );
337
+ String v2Equivalent = getV2Equivalent (currentFqcn );
338
+
339
+ JavaType targetType = JavaType .buildType (v2Equivalent );
340
+
341
+ oldTypeToNewType .put (currentFqcn , Pair .of (originalType , targetType ));
342
+ }
343
+
306
344
@ Override
307
345
public J visitMethodInvocation (J .MethodInvocation method , ExecutionContext ctx ) {
308
- for (Pair <JavaType .Class , JavaType > entry : oldTypeToNewType .values ()) {
309
- JavaType .Class originalType = entry .left ();
310
- JavaType targetType = entry .right ();
346
+ JavaType .FullyQualified declaringType = method .getMethodType ().getDeclaringType ();
347
+ if (isV1Class (declaringType )) {
348
+ String fullyQualifiedName = declaringType .getFullyQualifiedName ();
349
+ storeV1ClassMetadata (fullyQualifiedName );
350
+
351
+ Pair <JavaType .Class , JavaType > oldTypeToNewTypePair = oldTypeToNewType .get (fullyQualifiedName );
352
+ JavaType .Class originalType = oldTypeToNewTypePair .left ();
353
+ JavaType targetType = oldTypeToNewTypePair .right ();
311
354
if (method .getMethodType () != null && method .getMethodType ().hasFlags (Flag .Static )) {
312
355
if (method .getMethodType ().getDeclaringType ().isAssignableFrom (originalType )) {
313
356
JavaSourceFile cu = getCursor ().firstEnclosingOrThrow (JavaSourceFile .class );
@@ -329,6 +372,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
329
372
}
330
373
}
331
374
}
375
+
332
376
return super .visitMethodInvocation (method , ctx );
333
377
}
334
378
0 commit comments