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 = "com \\ .amazonaws \\ .services \\ .[a-zA-Z0-9]+ \\ .model \\ "
62
+ + ". \\ * " ;
60
63
61
64
@ Override
62
65
public String getDisplayName () {
@@ -79,6 +82,7 @@ private static class ChangeTypeVisitor extends JavaVisitor<ExecutionContext> {
79
82
80
83
private final Map <JavaType , JavaType > oldNameToChangedType = new IdentityHashMap <>();
81
84
private final Set <String > topLevelClassnames = new HashSet <>();
85
+ private final List <String > wildcardImports = new ArrayList <>();
82
86
83
87
private Map <String , Pair <JavaType .Class , JavaType >> oldTypeToNewType = new HashMap <>();
84
88
@@ -99,19 +103,19 @@ public J visitImport(J.Import anImport, ExecutionContext ctx) {
99
103
.map (TypeUtils ::asFullyQualified )
100
104
.orElse (null );
101
105
102
- if (fullyQualified == null ) {
106
+ if (fullyQualified == null ) {
107
+ String fullName = anImport .getTypeName ();
108
+ if (fullName .matches (V1_SERVICE_MODEL_WILD_CARD_CLASS_PATTERN )) {
109
+ maybeAddImport (getV2ModelPackageWildCardEquivalent (fullName ), false );
110
+ wildcardImports .add (fullName );
111
+ }
103
112
return anImport ;
104
113
}
105
114
106
115
String currentFqcn = fullyQualified .getFullyQualifiedName ();
107
116
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 ));
117
+ if (isV1Class (fullyQualified )) {
118
+ storeV1ClassMetadata (currentFqcn );
115
119
if (anImport .getAlias () != null ) {
116
120
importAlias = anImport .getAlias ();
117
121
}
@@ -120,6 +124,10 @@ public J visitImport(J.Import anImport, ExecutionContext ctx) {
120
124
return anImport ;
121
125
}
122
126
127
+ private static boolean isV1Class (JavaType .FullyQualified fullyQualified ) {
128
+ return isV1ModelClass (fullyQualified ) || isV1ClientClass (fullyQualified );
129
+ }
130
+
123
131
@ Override
124
132
public J visitCompilationUnit (J .CompilationUnit cu , ExecutionContext executionContext ) {
125
133
return super .visitCompilationUnit (cu , executionContext );
@@ -230,6 +238,11 @@ private J postVisitSourceFile(JavaSourceFile tree, ExecutionContext ctx, J curre
230
238
231
239
currentTree = sourceFile ;
232
240
}
241
+
242
+ for (String fqcn : wildcardImports ) {
243
+ currentTree = new RemoveImport <ExecutionContext >(fqcn ).visit (sourceFile , ctx , getCursor ().getParentOrThrow ());
244
+ }
245
+
233
246
return currentTree ;
234
247
}
235
248
@@ -281,33 +294,50 @@ public J visitFieldAccess(J.FieldAccess fieldAccess, ExecutionContext ctx) {
281
294
public J visitIdentifier (J .Identifier ident , ExecutionContext ctx ) {
282
295
283
296
JavaType currentType = ident .getType ();
284
- if (currentType instanceof JavaType .FullyQualified ) {
285
- JavaType .FullyQualified original = TypeUtils .asFullyQualified (currentType );
297
+ if (!(currentType instanceof JavaType .FullyQualified )) {
298
+ return visitAndCast (ident , ctx , super ::visitIdentifier );
299
+ }
286
300
287
- if (original != null && TypeUtils .isOfClassType (ident .getType (), original .getFullyQualifiedName ())) {
288
- String fullyQualifiedName = original .getFullyQualifiedName ();
301
+ JavaType .FullyQualified original = TypeUtils .asFullyQualified (currentType );
289
302
290
- if (oldTypeToNewType .containsKey (fullyQualifiedName )) {
291
- JavaType .Class originalType = oldTypeToNewType .get (fullyQualifiedName ).left ();
292
- String className = originalType .getClassName ();
303
+ if (original != null && TypeUtils .isOfClassType (ident .getType (), original .getFullyQualifiedName ())) {
304
+ String fullyQualifiedName = original .getFullyQualifiedName ();
293
305
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
- }
306
+ if (isV1Class (original )) {
307
+ storeV1ClassMetadata (fullyQualifiedName );
308
+ JavaType .Class originalType = oldTypeToNewType .get (fullyQualifiedName ).left ();
309
+ String className = originalType .getClassName ();
310
+
311
+ if (ident .getSimpleName ().equals (className )) {
312
+ JavaType targetType = oldTypeToNewType .get (fullyQualifiedName ).right ();
313
+ ident = ident .withSimpleName (((JavaType .FullyQualified ) targetType ).getClassName ());
314
+ ident = ident .withType (updateType (currentType ));
299
315
}
300
316
}
301
317
}
302
318
303
319
return visitAndCast (ident , ctx , super ::visitIdentifier );
304
320
}
305
321
322
+ private void storeV1ClassMetadata (String currentFqcn ) {
323
+ JavaType .ShallowClass originalType = JavaType .ShallowClass .build (currentFqcn );
324
+ String v2Equivalent = getV2Equivalent (currentFqcn );
325
+
326
+ JavaType targetType = JavaType .buildType (v2Equivalent );
327
+
328
+ oldTypeToNewType .put (currentFqcn , Pair .of (originalType , targetType ));
329
+ }
330
+
306
331
@ Override
307
332
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 ();
333
+ JavaType .FullyQualified declaringType = method .getMethodType ().getDeclaringType ();
334
+ if (isV1Class (declaringType )) {
335
+ String fullyQualifiedName = declaringType .getFullyQualifiedName ();
336
+ storeV1ClassMetadata (fullyQualifiedName );
337
+
338
+ Pair <JavaType .Class , JavaType > oldTypeToNewTypePair = oldTypeToNewType .get (fullyQualifiedName );
339
+ JavaType .Class originalType = oldTypeToNewTypePair .left ();
340
+ JavaType targetType = oldTypeToNewTypePair .right ();
311
341
if (method .getMethodType () != null && method .getMethodType ().hasFlags (Flag .Static )) {
312
342
if (method .getMethodType ().getDeclaringType ().isAssignableFrom (originalType )) {
313
343
JavaSourceFile cu = getCursor ().firstEnclosingOrThrow (JavaSourceFile .class );
@@ -329,6 +359,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
329
359
}
330
360
}
331
361
}
362
+
332
363
return super .visitMethodInvocation (method , ctx );
333
364
}
334
365
0 commit comments