39
39
40
40
@ SdkInternalApi
41
41
public class S3StreamingRequestToV2 extends Recipe {
42
- private static final MethodMatcher PUT_OBJECT =
42
+ private static final MethodMatcher PUT_OBJECT_FILE =
43
43
new MethodMatcher ("com.amazonaws.services.s3.AmazonS3 "
44
- + "putObject(java.lang.String, java.lang.String, java.io.File)" ,
45
- true );
44
+ + "putObject(java.lang.String, java.lang.String, java.io.File)" , true );
45
+ private static final MethodMatcher PUT_OBJECT_STRING =
46
+ new MethodMatcher ("com.amazonaws.services.s3.AmazonS3 "
47
+ + "putObject(java.lang.String, java.lang.String, java.lang.String)" , true );
48
+
46
49
private static final JavaType .FullyQualified V1_PUT_OBJECT_REQUEST =
47
50
TypeUtils .asFullyQualified (JavaType .buildType ("com.amazonaws.services.s3.model.PutObjectRequest" ));
48
51
private static final JavaType .FullyQualified REQUEST_BODY =
@@ -66,12 +69,79 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
66
69
private static final class Visitor extends JavaIsoVisitor <ExecutionContext > {
67
70
@ Override
68
71
public J .MethodInvocation visitMethodInvocation (J .MethodInvocation method , ExecutionContext executionContext ) {
69
- if (PUT_OBJECT .matches (method , false )) {
72
+ if (PUT_OBJECT_FILE .matches (method , false )) {
70
73
method = transformPutFileOverload (method );
71
74
}
75
+ if (PUT_OBJECT_STRING .matches (method , false )) {
76
+ method = transformPutStringOverload (method );
77
+ }
72
78
return super .visitMethodInvocation (method , executionContext );
73
79
}
74
80
81
+ private J .MethodInvocation transformPutStringOverload (J .MethodInvocation method ) {
82
+ JavaType .Method methodType = method .getMethodType ();
83
+ if (methodType == null ) {
84
+ return method ;
85
+ }
86
+
87
+ List <Expression > originalArgs = method .getArguments ();
88
+
89
+ Expression bucketExpr = originalArgs .get (0 );
90
+ Expression keyExpr = originalArgs .get (1 );
91
+ Expression stringExpr = originalArgs .get (2 );
92
+
93
+ List <Expression > newArgs = new ArrayList <>();
94
+ Expression getObjectExpr = bucketAndKeyToPutObject (bucketExpr , keyExpr );
95
+ newArgs .add (getObjectExpr );
96
+
97
+ // This is to maintain the formatting/spacing of original code, getPrefix() retrieves the leading whitespace
98
+ Space stringArgPrefix = stringExpr .getPrefix ();
99
+ stringExpr = stringToRequestBody (stringExpr .withPrefix (Space .EMPTY )).withPrefix (stringArgPrefix );
100
+ newArgs .add (stringExpr );
101
+
102
+ List <String > paramNames = Arrays .asList ("request" , "stringContent" );
103
+ List <JavaType > paramTypes = newArgs .stream ()
104
+ .map (Expression ::getType )
105
+ .collect (Collectors .toList ());
106
+
107
+
108
+ methodType = methodType .withParameterTypes (paramTypes )
109
+ .withParameterNames (paramNames );
110
+
111
+ return method .withMethodType (methodType ).withArguments (newArgs );
112
+ }
113
+
114
+ private J .MethodInvocation stringToRequestBody (Expression fileExpr ) {
115
+ maybeAddImport (REQUEST_BODY );
116
+
117
+ J .Identifier requestBodyId = IdentifierUtils .makeId (REQUEST_BODY .getClassName (), REQUEST_BODY );
118
+
119
+ JavaType .Method fromStringType = new JavaType .Method (
120
+ null ,
121
+ 0L ,
122
+ REQUEST_BODY ,
123
+ "fromString" ,
124
+ REQUEST_BODY ,
125
+ Collections .singletonList ("stringContent" ),
126
+ Collections .singletonList (JavaType .buildType ("java.lang.String" )),
127
+ null ,
128
+ null
129
+ );
130
+
131
+ J .Identifier fromFileId = IdentifierUtils .makeId ("fromString" , fromStringType );
132
+
133
+ return new J .MethodInvocation (
134
+ Tree .randomId (),
135
+ Space .EMPTY ,
136
+ Markers .EMPTY ,
137
+ JRightPadded .build (requestBodyId ),
138
+ null ,
139
+ fromFileId ,
140
+ JContainer .build (Collections .singletonList (JRightPadded .build (fileExpr ))),
141
+ fromStringType
142
+ );
143
+ }
144
+
75
145
private J .MethodInvocation transformPutFileOverload (J .MethodInvocation method ) {
76
146
JavaType .Method methodType = method .getMethodType ();
77
147
if (methodType == null ) {
0 commit comments