16
16
package software .amazon .awssdk .v2migration ;
17
17
18
18
import static software .amazon .awssdk .v2migration .internal .utils .S3TransformUtils .V2_S3_MODEL_PKG ;
19
+ import static software .amazon .awssdk .v2migration .internal .utils .S3TransformUtils .V2_TM_CLIENT ;
19
20
import static software .amazon .awssdk .v2migration .internal .utils .S3TransformUtils .V2_TM_MODEL_PKG ;
20
21
import static software .amazon .awssdk .v2migration .internal .utils .S3TransformUtils .v2TmMethodMatcher ;
21
22
23
+ import java .util .regex .Pattern ;
22
24
import org .openrewrite .ExecutionContext ;
23
25
import org .openrewrite .Recipe ;
24
26
import org .openrewrite .TreeVisitor ;
25
27
import org .openrewrite .java .AddImport ;
26
- import org .openrewrite .java .JavaIsoVisitor ;
27
28
import org .openrewrite .java .JavaTemplate ;
29
+ import org .openrewrite .java .JavaVisitor ;
28
30
import org .openrewrite .java .MethodMatcher ;
31
+ import org .openrewrite .java .tree .Expression ;
29
32
import org .openrewrite .java .tree .J ;
33
+ import org .openrewrite .java .tree .JavaType ;
30
34
import software .amazon .awssdk .annotations .SdkInternalApi ;
31
35
32
36
@ SdkInternalApi
@@ -47,6 +51,13 @@ public class TransferManagerMethodsToV2 extends Recipe {
47
51
private static final MethodMatcher COPY_BUCKET_KEY =
48
52
v2TmMethodMatcher ("copy(String, String, String, String" );
49
53
54
+ private static final MethodMatcher DOWNLOAD_DIR = v2TmMethodMatcher ("downloadDirectory(String, String, java.io.File)" );
55
+
56
+ private static final Pattern S3_TM_CREDENTIAL = Pattern .compile (V2_TM_CLIENT );
57
+ private static final Pattern V2_AWSCREDENTAIL = Pattern .compile ("software.amazon.awssdk.auth.credentials.AwsCredentials" );
58
+ private static final Pattern V2_CREDENTIAL_PROVIDER = Pattern .compile ("software.amazon.awssdk.auth.credentials"
59
+ + ".AwsCredentialsProvider" );
60
+
50
61
@ Override
51
62
public String getDisplayName () {
52
63
return "Transfer Manager Methods to V2" ;
@@ -62,10 +73,10 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
62
73
return new Visitor ();
63
74
}
64
75
65
- private static final class Visitor extends JavaIsoVisitor <ExecutionContext > {
76
+ private static final class Visitor extends JavaVisitor <ExecutionContext > {
66
77
67
78
@ Override
68
- public J . MethodInvocation visitMethodInvocation (J .MethodInvocation method , ExecutionContext executionContext ) {
79
+ public J visitMethodInvocation (J .MethodInvocation method , ExecutionContext executionContext ) {
69
80
70
81
if (DOWNLOAD_BUCKET_KEY_FILE .matches (method , false )) {
71
82
method = transformDownloadWithBucketKeyFile (method );
@@ -95,10 +106,70 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
95
106
method = transformUploadWithBucketKeyFile (method );
96
107
return super .visitMethodInvocation (method , executionContext );
97
108
}
109
+ if (DOWNLOAD_DIR .matches (method , false )) {
110
+ method = transformDownloadDirectory (method );
111
+ return super .visitMethodInvocation (method , executionContext );
112
+ }
98
113
99
114
return super .visitMethodInvocation (method , executionContext );
100
115
}
101
116
117
+ @ Override
118
+ public J visitNewClass (J .NewClass newClass , ExecutionContext executionContext ) {
119
+ JavaType type = newClass .getType ();
120
+ if (!(type instanceof JavaType .FullyQualified )) {
121
+ return newClass ;
122
+ }
123
+
124
+ if (type .isAssignableFrom (S3_TM_CREDENTIAL ) &&
125
+ newClass .getArguments ().size () == 1 &&
126
+ newClass .getArguments ().get (0 ).getType () != null ) {
127
+ Expression arg = newClass .getArguments ().get (0 );
128
+ if (arg .getType ().isAssignableFrom (V2_AWSCREDENTAIL )) {
129
+ addS3AsyncClientImport ();
130
+ addStaticCredentialsProviderImport ();
131
+
132
+ return JavaTemplate
133
+ .builder ("S3TransferManager.builder()" +
134
+ ".s3Client(S3AsyncClient.builder()" +
135
+ ".credentialsProvider(StaticCredentialsProvider.create(#{any()}))" +
136
+ ".build())" +
137
+ ".build()" )
138
+ .build ()
139
+ .apply (getCursor (), newClass .getCoordinates ().replace (), arg );
140
+ }
141
+ if (arg .getType ().isAssignableFrom (V2_CREDENTIAL_PROVIDER )) {
142
+ addS3AsyncClientImport ();
143
+
144
+ return JavaTemplate
145
+ .builder ("S3TransferManager.builder()" +
146
+ ".s3Client(S3AsyncClient.builder()" +
147
+ ".credentialsProvider(#{any()})" +
148
+ ".build())" +
149
+ ".build()" )
150
+ .build ()
151
+ .apply (getCursor (), newClass .getCoordinates ().replace (), arg );
152
+ }
153
+ }
154
+
155
+ return super .visitNewClass (newClass , executionContext );
156
+ }
157
+
158
+ private J .MethodInvocation transformDownloadDirectory (J .MethodInvocation method ) {
159
+ String v2Method = "#{any()}.downloadDirectory(DownloadDirectoryRequest.builder()"
160
+ + ".bucket(#{any()}).listObjectsV2RequestTransformer(builder -> builder.prefix(#{any()}))"
161
+ + ".destination(#{any()}.toPath()).build())" ;
162
+
163
+ method = JavaTemplate .builder (v2Method ).build ()
164
+ .apply (getCursor (), method .getCoordinates ().replace (), method .getSelect (),
165
+ method .getArguments ().get (0 ), method .getArguments ().get (1 ),
166
+ method .getArguments ().get (2 ));
167
+
168
+ addTmImport ("DirectoryDownload" );
169
+ addTmImport ("DownloadDirectoryRequest" );
170
+ return method ;
171
+ }
172
+
102
173
private J .MethodInvocation transformUploadWithBucketKeyFile (J .MethodInvocation method ) {
103
174
String v2Method = "#{any()}.uploadFile(UploadFileRequest.builder()"
104
175
+ ".putObjectRequest(PutObjectRequest.builder().bucket(#{any()}).key(#{any()}).build())"
@@ -220,5 +291,13 @@ private void addDurationImport() {
220
291
private void addRequestOverrideConfigImport () {
221
292
doAfterVisit (new AddImport <>("software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration" , null , false ));
222
293
}
294
+
295
+ private void addS3AsyncClientImport () {
296
+ doAfterVisit (new AddImport <>("software.amazon.awssdk.services.s3.S3AsyncClient" , null , false ));
297
+ }
298
+
299
+ private void addStaticCredentialsProviderImport () {
300
+ doAfterVisit (new AddImport <>("software.amazon.awssdk.auth.credentials.StaticCredentialsProvider" , null , false ));
301
+ }
223
302
}
224
303
}
0 commit comments