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.AwsSessionCredentials" ,
65
+ "software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider" ,
66
+ "software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider" ,
67
+ "software.amazon.awssdk.auth.credentials.ContainerCredentialsProvider" ,
68
+ "software.amazon.awssdk.auth.credentials.InstanceProfileCredentialsProvider" ,
69
+ "software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider" ,
70
+ "software.amazon.awssdk.services.sts.auth.StsGetSessionTokenCredentialsProvider" ,
71
+ "software.amazon.awssdk.services.sts.auth.StsAssumeRoleWithWebIdentityCredentialsProvider" ,
72
+ "software.amazon.awssdk.auth.credentials.ProcessCredentialsProvider" ));
73
+
74
+ private static final Pattern V2_CLIENT_BUILDER_PATTERN = Pattern .compile (
75
+ "software\\ .amazon\\ .awssdk\\ .services\\ .[a-zA-Z0-9]+\\ .[a-zA-Z0-9]+Builder" );
37
76
38
77
private SdkTypeUtils () {
39
78
}
@@ -48,6 +87,12 @@ public static boolean isV1ModelClass(JavaType type) {
48
87
&& type .isAssignableFrom (V1_SERVICE_MODEL_CLASS_PATTERN );
49
88
}
50
89
90
+ public static boolean isV1ClientClass (JavaType type ) {
91
+ return type != null
92
+ && type instanceof JavaType .FullyQualified
93
+ && type .isAssignableFrom (V1_SERVICE_CLIENT_CLASS_PATTERN );
94
+ }
95
+
51
96
public static boolean isV2ModelBuilder (JavaType type ) {
52
97
return type != null
53
98
&& type .isAssignableFrom (V2_MODEL_BUILDER_PATTERN );
@@ -58,26 +103,39 @@ public static boolean isV2ModelClass(JavaType type) {
58
103
&& type .isAssignableFrom (V2_MODEL_CLASS_PATTERN );
59
104
}
60
105
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 ();
106
+ public static boolean isV2ClientClass (JavaType type ) {
107
+ return type != null
108
+ && type .isAssignableFrom (V2_CLIENT_CLASS_PATTERN );
109
+ }
68
110
69
- packageName = StringUtils .replaceOnce (packageName , "com.amazonaws" , "software.amazon.awssdk" );
111
+ public static boolean isV2ClientBuilder (JavaType type ) {
112
+ return type != null
113
+ && type .isAssignableFrom (V2_CLIENT_BUILDER_PATTERN );
114
+ }
70
115
71
- return TypeUtils .asFullyQualified (JavaType .buildType (String .format ("%s.%s" , packageName , className )));
116
+ public static boolean isEligibleToConvertToBuilder (JavaType .FullyQualified type ) {
117
+ return isV2ModelClass (type ) || isV2ClientClass (type ) || isV2CoreClassesWithBuilder (type .getFullyQualifiedName ());
72
118
}
73
119
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
- }
120
+ public static boolean isEligibleToConvertToStaticFactory (JavaType .FullyQualified type ) {
121
+ return type != null && V2_CORE_CLASSES_WITH_STATIC_FACTORY .containsKey (type .getFullyQualifiedName ());
122
+ }
78
123
79
- String fqcn = String .format ("%s.%s" , type .getFullyQualifiedName (), "Builder" );
124
+ private static boolean isV2CoreClassesWithBuilder (String fqcn ) {
125
+ return V2_CORE_CLASSES_WITH_BUILDER .contains (fqcn );
126
+ }
80
127
128
+ public static JavaType .FullyQualified v2Builder (JavaType .FullyQualified type ) {
129
+ if (!isEligibleToConvertToBuilder (type )) {
130
+ throw new IllegalArgumentException (String .format ("%s cannot be converted to builder" , type ));
131
+ }
132
+ String fqcn ;
133
+ if (isV2ModelClass (type )) {
134
+ fqcn = String .format ("%s.%s" , type .getFullyQualifiedName (), "Builder" );
135
+ } else {
136
+ fqcn = String .format ("%s%s" , type .getFullyQualifiedName (), "Builder" );
137
+ }
138
+
81
139
return TypeUtils .asFullyQualified (JavaType .buildType (fqcn ));
82
140
}
83
141
}
0 commit comments