1616package software .amazon .awssdk .v2migration ;
1717
1818import 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 ;
1920import static software .amazon .awssdk .v2migration .internal .utils .S3TransformUtils .V2_TM_MODEL_PKG ;
2021import static software .amazon .awssdk .v2migration .internal .utils .S3TransformUtils .v2TmMethodMatcher ;
2122
23+ import java .util .regex .Pattern ;
2224import org .openrewrite .ExecutionContext ;
2325import org .openrewrite .Recipe ;
2426import org .openrewrite .TreeVisitor ;
2527import org .openrewrite .java .AddImport ;
26- import org .openrewrite .java .JavaIsoVisitor ;
2728import org .openrewrite .java .JavaTemplate ;
29+ import org .openrewrite .java .JavaVisitor ;
2830import org .openrewrite .java .MethodMatcher ;
31+ import org .openrewrite .java .tree .Expression ;
2932import org .openrewrite .java .tree .J ;
33+ import org .openrewrite .java .tree .JavaType ;
3034import software .amazon .awssdk .annotations .SdkInternalApi ;
3135
3236@ SdkInternalApi
@@ -47,6 +51,13 @@ public class TransferManagerMethodsToV2 extends Recipe {
4751 private static final MethodMatcher COPY_BUCKET_KEY =
4852 v2TmMethodMatcher ("copy(String, String, String, String" );
4953
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+
5061 @ Override
5162 public String getDisplayName () {
5263 return "Transfer Manager Methods to V2" ;
@@ -62,10 +73,10 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
6273 return new Visitor ();
6374 }
6475
65- private static final class Visitor extends JavaIsoVisitor <ExecutionContext > {
76+ private static final class Visitor extends JavaVisitor <ExecutionContext > {
6677
6778 @ Override
68- public J . MethodInvocation visitMethodInvocation (J .MethodInvocation method , ExecutionContext executionContext ) {
79+ public J visitMethodInvocation (J .MethodInvocation method , ExecutionContext executionContext ) {
6980
7081 if (DOWNLOAD_BUCKET_KEY_FILE .matches (method , false )) {
7182 method = transformDownloadWithBucketKeyFile (method );
@@ -95,10 +106,70 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
95106 method = transformUploadWithBucketKeyFile (method );
96107 return super .visitMethodInvocation (method , executionContext );
97108 }
109+ if (DOWNLOAD_DIR .matches (method , false )) {
110+ method = transformDownloadDirectory (method );
111+ return super .visitMethodInvocation (method , executionContext );
112+ }
98113
99114 return super .visitMethodInvocation (method , executionContext );
100115 }
101116
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+
102173 private J .MethodInvocation transformUploadWithBucketKeyFile (J .MethodInvocation method ) {
103174 String v2Method = "#{any()}.uploadFile(UploadFileRequest.builder()"
104175 + ".putObjectRequest(PutObjectRequest.builder().bucket(#{any()}).key(#{any()}).build())"
@@ -220,5 +291,13 @@ private void addDurationImport() {
220291 private void addRequestOverrideConfigImport () {
221292 doAfterVisit (new AddImport <>("software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration" , null , false ));
222293 }
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+ }
223302 }
224303}
0 commit comments