15
15
16
16
package software .amazon .awssdk .codegen .poet .client ;
17
17
18
- import static com .squareup .javapoet .TypeSpec .Builder ;
19
18
import static java .util .Collections .singletonList ;
19
+ import static java .util .stream .Collectors .toList ;
20
20
import static javax .lang .model .element .Modifier .FINAL ;
21
21
import static javax .lang .model .element .Modifier .PRIVATE ;
22
+ import static javax .lang .model .element .Modifier .PROTECTED ;
23
+ import static javax .lang .model .element .Modifier .PUBLIC ;
22
24
import static javax .lang .model .element .Modifier .STATIC ;
23
25
import static software .amazon .awssdk .codegen .internal .Constant .EVENT_PUBLISHER_PARAM_NAME ;
24
26
import static software .amazon .awssdk .codegen .poet .client .ClientClassUtils .addS3ArnableFieldCode ;
35
37
import com .squareup .javapoet .TypeSpec ;
36
38
import java .net .URI ;
37
39
import java .nio .ByteBuffer ;
40
+ import java .util .ArrayList ;
38
41
import java .util .Collections ;
42
+ import java .util .Comparator ;
39
43
import java .util .List ;
40
44
import java .util .concurrent .CompletableFuture ;
41
45
import java .util .concurrent .Executor ;
42
46
import java .util .concurrent .ScheduledExecutorService ;
43
47
import java .util .stream .Collectors ;
44
- import javax . lang . model . element . Modifier ;
48
+ import java . util . stream . Stream ;
45
49
import org .reactivestreams .Publisher ;
46
50
import org .slf4j .Logger ;
47
51
import org .slf4j .LoggerFactory ;
@@ -99,66 +103,105 @@ public AsyncClientClass(GeneratorTaskParams dependencies) {
99
103
}
100
104
101
105
@ Override
102
- public TypeSpec poetSpec () {
106
+ protected TypeSpec .Builder createTypeSpec () {
107
+ return PoetUtils .createClassBuilder (className );
108
+ }
109
+
110
+ @ Override
111
+ protected void addInterfaceClass (TypeSpec .Builder type ) {
103
112
ClassName interfaceClass = poetExtensions .getClientClass (model .getMetadata ().getAsyncInterface ());
104
- Builder classBuilder = PoetUtils .createClassBuilder (className );
105
- classBuilder .addAnnotation (SdkInternalApi .class )
106
- .addModifiers (Modifier .FINAL )
107
- .addField (FieldSpec .builder (ClassName .get (Logger .class ), "log" )
108
- .addModifiers (Modifier .PRIVATE , Modifier .STATIC , Modifier .FINAL )
109
- .initializer ("$T.getLogger($T.class)" , LoggerFactory .class ,
110
- className )
111
- .build ())
112
- .addField (AsyncClientHandler .class , "clientHandler" , Modifier .PRIVATE , Modifier .FINAL )
113
- .addField (protocolSpec .protocolFactory (model ))
114
- .addField (SdkClientConfiguration .class , "clientConfiguration" , Modifier .PRIVATE , Modifier .FINAL )
115
- .addSuperinterface (interfaceClass )
116
- .addJavadoc ("Internal implementation of {@link $1T}.\n \n @see $1T#builder()" ,
117
- interfaceClass )
118
- .addMethod (constructor (classBuilder ))
119
- .addMethod (nameMethod ())
120
- .addMethods (operations ())
121
- .addMethod (closeMethod ())
122
- .addMethods (protocolSpec .additionalMethods ())
123
- .addMethod (protocolSpec .initProtocolFactory (model ))
124
- .addMethod (resolveMetricPublishersMethod ());
113
+ type .addSuperinterface (interfaceClass )
114
+ .addJavadoc ("Internal implementation of {@link $1T}.\n \n @see $1T#builder()" , interfaceClass );
115
+ }
116
+
117
+ @ Override
118
+ protected void addAnnotations (TypeSpec .Builder type ) {
119
+ type .addAnnotation (SdkInternalApi .class );
120
+ }
121
+
122
+ @ Override
123
+ protected void addModifiers (TypeSpec .Builder type ) {
124
+ type .addModifiers (FINAL );
125
+ }
126
+
127
+ @ Override
128
+ protected void addFields (TypeSpec .Builder type ) {
129
+ type .addField (FieldSpec .builder (ClassName .get (Logger .class ), "log" )
130
+ .addModifiers (PRIVATE , STATIC , FINAL )
131
+ .initializer ("$T.getLogger($T.class)" , LoggerFactory .class ,
132
+ className )
133
+ .build ())
134
+ .addField (AsyncClientHandler .class , "clientHandler" , PRIVATE , FINAL )
135
+ .addField (protocolSpec .protocolFactory (model ))
136
+ .addField (SdkClientConfiguration .class , "clientConfiguration" , PRIVATE , FINAL );
125
137
126
138
// Kinesis doesn't support CBOR for STS yet so need another protocol factory for JSON
127
139
if (model .getMetadata ().isCborProtocol ()) {
128
- classBuilder .addField (AwsJsonProtocolFactory .class , "jsonProtocolFactory" , Modifier . PRIVATE , Modifier . FINAL );
140
+ type .addField (AwsJsonProtocolFactory .class , "jsonProtocolFactory" , PRIVATE , FINAL );
129
141
}
130
142
143
+ model .getEndpointOperation ().ifPresent (
144
+ o -> type .addField (EndpointDiscoveryRefreshCache .class , "endpointDiscoveryCache" , PRIVATE ));
145
+ }
146
+
147
+ @ Override
148
+ protected void addAdditionalMethods (TypeSpec .Builder type ) {
149
+ type .addMethod (constructor (type ))
150
+ .addMethod (nameMethod ())
151
+ .addMethods (protocolSpec .additionalMethods ())
152
+ .addMethod (protocolSpec .initProtocolFactory (model ))
153
+ .addMethod (resolveMetricPublishersMethod ());
154
+
131
155
if (model .hasPaginators ()) {
132
- classBuilder .addMethod (applyPaginatorUserAgentMethod (poetExtensions , model ));
156
+ type .addMethod (applyPaginatorUserAgentMethod (poetExtensions , model ));
133
157
}
134
158
135
159
if (model .containsRequestSigners () || model .containsRequestEventStreams () || hasStreamingV4AuthOperations ()) {
136
- classBuilder .addMethod (applySignerOverrideMethod (poetExtensions , model ));
137
- classBuilder .addMethod (isSignerOverriddenOnClientMethod ());
160
+ type .addMethod (applySignerOverrideMethod (poetExtensions , model ));
161
+ type .addMethod (isSignerOverriddenOnClientMethod ());
138
162
}
139
163
140
- if (model .getCustomizationConfig ().getUtilitiesMethod () != null ) {
141
- classBuilder .addMethod (utilitiesMethod ());
142
- }
164
+ protocolSpec .createErrorResponseHandler ().ifPresent (type ::addMethod );
165
+ }
143
166
144
- model .getEndpointOperation ().ifPresent (
145
- o -> classBuilder .addField (EndpointDiscoveryRefreshCache .class , "endpointDiscoveryCache" , PRIVATE ));
167
+ @ Override
168
+ protected void addWaiterMethod (TypeSpec .Builder type ) {
169
+ type .addField (FieldSpec .builder (ClassName .get (ScheduledExecutorService .class ), "executorService" )
170
+ .addModifiers (PRIVATE , FINAL )
171
+ .build ());
172
+
173
+ MethodSpec waiter = MethodSpec .methodBuilder ("waiter" )
174
+ .addModifiers (PUBLIC )
175
+ .addAnnotation (Override .class )
176
+ .addStatement ("return $T.builder().client(this)"
177
+ + ".scheduledExecutorService(executorService).build()" ,
178
+ poetExtensions .getAsyncWaiterInterface ())
179
+ .returns (poetExtensions .getAsyncWaiterInterface ())
180
+ .build ();
181
+
182
+ type .addMethod (waiter );
183
+ }
146
184
147
- protocolSpec .createErrorResponseHandler ().ifPresent (classBuilder ::addMethod );
185
+ @ Override
186
+ protected List <MethodSpec > operations () {
187
+ return model .getOperations ().values ().stream ()
188
+ .flatMap (this ::operations )
189
+ .sorted (Comparator .comparing (m -> m .name ))
190
+ .collect (toList ());
191
+ }
148
192
149
- if ( model . hasWaiters () ) {
150
- classBuilder . addField ( FieldSpec . builder ( ClassName . get ( ScheduledExecutorService . class ), "executorService" )
151
- . addModifiers ( PRIVATE , FINAL )
152
- . build ());
153
- classBuilder . addMethod ( waiterImplMethod ( ));
193
+ private Stream < MethodSpec > operations ( OperationModel opModel ) {
194
+ List < MethodSpec > methods = new ArrayList <>();
195
+ methods . add ( traditionalMethod ( opModel ));
196
+ if ( opModel . isPaginated ()) {
197
+ methods . add ( paginatedTraditionalMethod ( opModel ));
154
198
}
155
-
156
- return classBuilder .build ();
199
+ return methods .stream ();
157
200
}
158
201
159
- private MethodSpec constructor (Builder classBuilder ) {
202
+ private MethodSpec constructor (TypeSpec . Builder classBuilder ) {
160
203
MethodSpec .Builder builder = MethodSpec .constructorBuilder ()
161
- .addModifiers (Modifier . PROTECTED )
204
+ .addModifiers (PROTECTED )
162
205
.addParameter (SdkClientConfiguration .class , "clientConfiguration" )
163
206
.addStatement ("this.clientHandler = new $T(clientConfiguration)" ,
164
207
AwsAsyncClientHandler .class )
@@ -174,8 +217,7 @@ private MethodSpec constructor(Builder classBuilder) {
174
217
builder .addStatement ("this.jsonProtocolFactory = init($T.builder()).build()" , AwsJsonProtocolFactory .class );
175
218
}
176
219
if (hasOperationWithEventStreamOutput ()) {
177
- classBuilder .addField (FieldSpec .builder (ClassName .get (Executor .class ), "executor" ,
178
- Modifier .PRIVATE , Modifier .FINAL )
220
+ classBuilder .addField (FieldSpec .builder (ClassName .get (Executor .class ), "executor" , PRIVATE , FINAL )
179
221
.build ());
180
222
builder .addStatement ("this.executor = clientConfiguration.option($T.FUTURE_COMPLETION_EXECUTOR)" ,
181
223
SdkAdvancedAsyncClientOption .class );
@@ -216,24 +258,27 @@ private boolean hasOperationWithEventStreamOutput() {
216
258
private MethodSpec nameMethod () {
217
259
return MethodSpec .methodBuilder ("serviceName" )
218
260
.addAnnotation (Override .class )
219
- .addModifiers (Modifier . PUBLIC , Modifier . FINAL )
261
+ .addModifiers (PUBLIC , FINAL )
220
262
.returns (String .class )
221
263
.addStatement ("return SERVICE_NAME" )
222
264
.build ();
223
265
}
224
266
225
- private MethodSpec closeMethod () {
226
- return MethodSpec .methodBuilder ("close" )
227
- .addAnnotation (Override .class )
228
- .addModifiers (Modifier .PUBLIC )
229
- .addStatement ("$N.close()" , "clientHandler" )
230
- .build ();
267
+ @ Override
268
+ protected void addCloseMethod (TypeSpec .Builder type ) {
269
+ MethodSpec method = MethodSpec .methodBuilder ("close" )
270
+ .addAnnotation (Override .class )
271
+ .addModifiers (PUBLIC )
272
+ .addStatement ("$N.close()" , "clientHandler" )
273
+ .build ();
274
+
275
+ type .addMethod (method );
231
276
}
232
277
233
278
@ Override
234
279
protected MethodSpec .Builder operationBody (MethodSpec .Builder builder , OperationModel opModel ) {
235
280
236
- builder .addModifiers (Modifier . PUBLIC )
281
+ builder .addModifiers (PUBLIC )
237
282
.addAnnotation (Override .class );
238
283
239
284
builder .addStatement ("$T<$T> metricPublishers = "
@@ -362,7 +407,7 @@ protected MethodSpec.Builder operationBody(MethodSpec.Builder builder, Operation
362
407
363
408
@ Override
364
409
protected MethodSpec .Builder paginatedMethodBody (MethodSpec .Builder builder , OperationModel opModel ) {
365
- return builder .addModifiers (Modifier . PUBLIC )
410
+ return builder .addModifiers (PUBLIC )
366
411
.addStatement ("return new $T(this, applyPaginatorUserAgent($L))" ,
367
412
poetExtensions .getResponseClassForPaginatedAsyncOperation (opModel .getOperationName ()),
368
413
opModel .getInput ().getVariableName ());
@@ -434,25 +479,14 @@ protected MethodSpec utilitiesMethod() {
434
479
435
480
return MethodSpec .methodBuilder (UtilitiesMethod .METHOD_NAME )
436
481
.returns (returnType )
437
- .addModifiers (Modifier . PUBLIC )
482
+ .addModifiers (PUBLIC )
438
483
.addAnnotation (Override .class )
439
484
.addStatement ("return $T.create($L)" ,
440
485
instanceType ,
441
486
String .join ("," , config .getCreateMethodParams ()))
442
487
.build ();
443
488
}
444
489
445
- private MethodSpec waiterImplMethod () {
446
- return MethodSpec .methodBuilder ("waiter" )
447
- .addModifiers (Modifier .PUBLIC )
448
- .addAnnotation (Override .class )
449
- .addStatement ("return $T.builder().client(this)"
450
- + ".scheduledExecutorService(executorService).build()" ,
451
- poetExtensions .getAsyncWaiterInterface ())
452
- .returns (poetExtensions .getAsyncWaiterInterface ())
453
- .build ();
454
- }
455
-
456
490
private MethodSpec resolveMetricPublishersMethod () {
457
491
String clientConfigName = "clientConfiguration" ;
458
492
String requestOverrideConfigName = "requestOverrideConfiguration" ;
0 commit comments