15
15
16
16
package software .amazon .awssdk .migration .internal .utils ;
17
17
18
+ import java .util .Arrays ;
19
+ import java .util .HashSet ;
20
+ import java .util .Map ;
21
+ import java .util .Set ;
18
22
import java .util .regex .Pattern ;
19
23
import org .openrewrite .java .tree .JavaType ;
20
24
import org .openrewrite .java .tree .TypeUtils ;
21
25
import software .amazon .awssdk .annotations .SdkInternalApi ;
22
- import software .amazon .awssdk .utils .StringUtils ;
26
+ import software .amazon .awssdk .utils .ImmutableMap ;
23
27
24
28
/**
25
29
* Type creation and checking utilities.
26
30
*/
27
31
@ SdkInternalApi
28
32
public final class SdkTypeUtils {
33
+ /**
34
+ * V2 core classes with a static factory method
35
+ */
36
+ public static final Map <String , Integer > V2_CORE_CLASSES_WITH_STATIC_FACTORY =
37
+ ImmutableMap .<String , Integer >builder ()
38
+ .put ("software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider" , 0 )
39
+ .put ("software.amazon.awssdk.auth.credentials.InstanceProfileCredentialsProvider" , 0 )
40
+ .put ("software.amazon.awssdk.auth.credentials.AwsBasicCredentials" , 2 )
41
+ .put ("software.amazon.awssdk.auth.credentials.AwsSessionCredentials" , 3 )
42
+ .put ("software.amazon.awssdk.auth.credentials.StaticCredentialsProvider" , 1 )
43
+ .build ();
44
+
29
45
private static final Pattern V1_SERVICE_CLASS_PATTERN =
30
46
Pattern .compile ("com\\ .amazonaws\\ .services\\ .[a-zA-Z0-9]+\\ .[a-zA-Z0-9]+" );
31
47
private static final Pattern V1_SERVICE_MODEL_CLASS_PATTERN =
32
48
Pattern .compile ("com\\ .amazonaws\\ .services\\ .[a-zA-Z0-9]+\\ .model\\ .[a-zA-Z0-9]+" );
49
+
50
+ private static final Pattern V1_SERVICE_CLIENT_CLASS_PATTERN =
51
+ Pattern .compile ("com\\ .amazonaws\\ .services\\ .[a-zA-Z0-9]+\\ .[a-zA-Z0-9]+" );
33
52
private static final Pattern V2_MODEL_BUILDER_PATTERN =
34
53
Pattern .compile ("software\\ .amazon\\ .awssdk\\ .services\\ .[a-zA-Z0-9]+\\ .model\\ .[a-zA-Z0-9]+\\ .Builder" );
35
54
private static final Pattern V2_MODEL_CLASS_PATTERN = Pattern .compile (
36
55
"software\\ .amazon\\ .awssdk\\ .services\\ .[a-zA-Z0-9]+\\ .model\\ ..[a-zA-Z0-9]+" );
56
+ private static final Pattern V2_CLIENT_CLASS_PATTERN = Pattern .compile (
57
+ "software\\ .amazon\\ .awssdk\\ .services\\ .[a-zA-Z0-9]+\\ .[a-zA-Z0-9]+" );
58
+
59
+ /**
60
+ * V2 core classes with a builder
61
+ */
62
+ private static final Set <String > V2_CORE_CLASSES_WITH_BUILDER =
63
+ new HashSet <>(Arrays .asList ("software.amazon.awssdk.core.client.ClientOverrideConfiguration" ,
64
+ "software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider" ,
65
+ "software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider" ,
66
+ "software.amazon.awssdk.auth.credentials.ContainerCredentialsProvider" ,
67
+ "software.amazon.awssdk.auth.credentials.InstanceProfileCredentialsProvider" ,
68
+ "software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider" ,
69
+ "software.amazon.awssdk.services.sts.auth.StsGetSessionTokenCredentialsProvider" ,
70
+ "software.amazon.awssdk.services.sts.auth.StsAssumeRoleWithWebIdentityCredentialsProvider" ,
71
+ "software.amazon.awssdk.auth.credentials.ProcessCredentialsProvider" ));
72
+
73
+ private static final Pattern V2_CLIENT_BUILDER_PATTERN = Pattern .compile (
74
+ "software\\ .amazon\\ .awssdk\\ .services\\ .[a-zA-Z0-9]+\\ .[a-zA-Z0-9]+Builder" );
37
75
38
76
private SdkTypeUtils () {
39
77
}
@@ -48,6 +86,12 @@ public static boolean isV1ModelClass(JavaType type) {
48
86
&& type .isAssignableFrom (V1_SERVICE_MODEL_CLASS_PATTERN );
49
87
}
50
88
89
+ public static boolean isV1ClientClass (JavaType type ) {
90
+ return type != null
91
+ && type instanceof JavaType .FullyQualified
92
+ && type .isAssignableFrom (V1_SERVICE_CLIENT_CLASS_PATTERN );
93
+ }
94
+
51
95
public static boolean isV2ModelBuilder (JavaType type ) {
52
96
return type != null
53
97
&& type .isAssignableFrom (V2_MODEL_BUILDER_PATTERN );
@@ -58,26 +102,39 @@ public static boolean isV2ModelClass(JavaType type) {
58
102
&& type .isAssignableFrom (V2_MODEL_CLASS_PATTERN );
59
103
}
60
104
61
- public static JavaType .FullyQualified asV2Type (JavaType .FullyQualified type ) {
62
- if (!isV1ModelClass (type )) {
63
- throw new IllegalArgumentException (String .format ("%s is not a V1 SDK model type" , type ));
64
- }
65
-
66
- String className = type .getClassName ();
67
- String packageName = type .getPackageName ();
105
+ public static boolean isV2ClientClass (JavaType type ) {
106
+ return type != null
107
+ && type .isAssignableFrom (V2_CLIENT_CLASS_PATTERN );
108
+ }
68
109
69
- packageName = StringUtils .replaceOnce (packageName , "com.amazonaws" , "software.amazon.awssdk" );
110
+ public static boolean isV2ClientBuilder (JavaType type ) {
111
+ return type != null
112
+ && type .isAssignableFrom (V2_CLIENT_BUILDER_PATTERN );
113
+ }
70
114
71
- return TypeUtils .asFullyQualified (JavaType .buildType (String .format ("%s.%s" , packageName , className )));
115
+ public static boolean isEligibleToConvertToBuilder (JavaType .FullyQualified type ) {
116
+ return isV2ModelClass (type ) || isV2ClientClass (type ) || isV2CoreClassesWithBuilder (type .getFullyQualifiedName ());
72
117
}
73
118
74
- public static JavaType .FullyQualified v2ModelBuilder (JavaType .FullyQualified type ) {
75
- if (!isV2ModelClass (type )) {
76
- throw new IllegalArgumentException (String .format ("%s is not a V2 model class" , type ));
77
- }
119
+ public static boolean isEligibleToConvertToStaticFactory (JavaType .FullyQualified type ) {
120
+ return type != null && V2_CORE_CLASSES_WITH_STATIC_FACTORY .containsKey (type .getFullyQualifiedName ());
121
+ }
78
122
79
- String fqcn = String .format ("%s.%s" , type .getFullyQualifiedName (), "Builder" );
123
+ private static boolean isV2CoreClassesWithBuilder (String fqcn ) {
124
+ return V2_CORE_CLASSES_WITH_BUILDER .contains (fqcn );
125
+ }
80
126
127
+ public static JavaType .FullyQualified v2Builder (JavaType .FullyQualified type ) {
128
+ if (!isEligibleToConvertToBuilder (type )) {
129
+ throw new IllegalArgumentException (String .format ("%s cannot be converted to builder" , type ));
130
+ }
131
+ String fqcn ;
132
+ if (isV2ModelClass (type )) {
133
+ fqcn = String .format ("%s.%s" , type .getFullyQualifiedName (), "Builder" );
134
+ } else {
135
+ fqcn = String .format ("%s%s" , type .getFullyQualifiedName (), "Builder" );
136
+ }
137
+
81
138
return TypeUtils .asFullyQualified (JavaType .buildType (fqcn ));
82
139
}
83
140
}
0 commit comments