2525import com .google .api .gax .rpc .OperationCallable ;
2626import com .google .api .gax .rpc .RequestParamsBuilder ;
2727import com .google .api .gax .rpc .RequestParamsExtractor ;
28+ import com .google .api .gax .rpc .ResumableUploadCallable ;
2829import com .google .api .gax .rpc .ServerStreamingCallable ;
2930import com .google .api .gax .rpc .UnaryCallable ;
3031import com .google .api .generator .engine .ast .AnnotationNode ;
@@ -106,7 +107,7 @@ public abstract class AbstractTransportServiceStubClassComposer implements Class
106107 private static final List <String > AIP_STANDARDS_METHODS =
107108 ImmutableList .of (
108109 "Get" , "List" , "Create" , "Delete" , "Update" , "Patch" , "Insert" , "AggregatedList" );
109- private static final Statement EMPTY_LINE_STATEMENT = EmptyLineStatement .create ();
110+ protected static final Statement EMPTY_LINE_STATEMENT = EmptyLineStatement .create ();
110111
111112 private static final String METHOD_DESCRIPTOR_NAME_PATTERN = "%sMethodDescriptor" ;
112113 private static final String PAGED_CALLABLE_CLASS_MEMBER_PATTERN = "%sPagedCallable" ;
@@ -115,6 +116,7 @@ public abstract class AbstractTransportServiceStubClassComposer implements Class
115116 private static final String CALLABLE_FACTORY_MEMBER_NAME = "callableFactory" ;
116117 protected static final String CALLABLE_CLASS_MEMBER_PATTERN = "%sCallable" ;
117118 private static final String OPERATION_CALLABLE_CLASS_MEMBER_PATTERN = "%sOperationCallable" ;
119+ protected static final String RESUMABLE_UPLOAD_STUB_MEMBER_NAME = "resumableUploadStub" ;
118120
119121 private static final ImmutableList <String > HEURISTIC_ENABLED_PACKAGES =
120122 ImmutableList .of ("google.cloud.compute" , "google.cloud.sql" , "google.cloud.bigquery" );
@@ -153,6 +155,7 @@ private static TypeStore createStaticTypes() {
153155 TimeUnit .class ,
154156 TypeRegistry .class ,
155157 UnaryCallable .class ,
158+ ResumableUploadCallable .class ,
156159 UnsupportedOperationException .class ,
157160 NullMarked .class );
158161 return new TypeStore (concreteClazzes );
@@ -213,6 +216,22 @@ public GapicClass generate(GapicContext context, Service service) {
213216 }
214217 }
215218
219+ if (service .methods ().stream ().anyMatch (Method ::isResumableUpload )) {
220+ TypeNode resumableUploadStubType =
221+ typeStore .get (String .format ("HttpJson%sResumableUploadStub" , service .name ()));
222+ if (isResumableUploadStubNullable ()) {
223+ resumableUploadStubType =
224+ TypeNode .withReference (resumableUploadStubType .reference ().copyAndSetNullable (true ));
225+ }
226+ classMemberVarExprs .put (
227+ RESUMABLE_UPLOAD_STUB_MEMBER_NAME ,
228+ VariableExpr .withVariable (
229+ Variable .builder ()
230+ .setName (RESUMABLE_UPLOAD_STUB_MEMBER_NAME )
231+ .setType (resumableUploadStubType )
232+ .build ()));
233+ }
234+
216235 classMemberVarExprs .put (
217236 CALLABLE_FACTORY_MEMBER_NAME ,
218237 VariableExpr .withVariable (
@@ -241,8 +260,7 @@ public GapicClass generate(GapicContext context, Service service) {
241260 protoMethodNameToDescriptorVarExprs ,
242261 classStatements );
243262 methodDefinitions .addAll (
244- createStubOverrideMethods (
245- classMemberVarExprs .get (BACKGROUND_RESOURCES_MEMBER_NAME ), service ));
263+ createStubOverrideMethods (classMemberVarExprs , service , typeStore ));
246264
247265 StubCommentComposer commentComposer =
248266 new StubCommentComposer (getTransportContext ().transportNames ().get (0 ));
@@ -425,6 +443,7 @@ protected List<Statement> createMethodDescriptorVariableDecls(
425443 boolean restNumericEnumsEnabled ) {
426444 return service .methods ().stream ()
427445 .filter (x -> x .isSupportedByTransport (getTransportContext ().transport ()))
446+ .filter (x -> !x .isResumableUpload ())
428447 .map (
429448 m ->
430449 createMethodDescriptorVariableDecl (
@@ -454,6 +473,7 @@ protected Map<String, VariableExpr> createProtoMethodNameToDescriptorClassMember
454473 Service service , Class <?> descriptorClass ) {
455474 return service .methods ().stream ()
456475 .filter (x -> x .isSupportedByTransport (getTransportContext ().transport ()))
476+ .filter (x -> !x .isResumableUpload ())
457477 .collect (
458478 Collectors .toMap (
459479 Method ::name ,
@@ -488,6 +508,9 @@ private Map<String, VariableExpr> createCallableClassMembers(
488508 if (!protoMethod .isSupportedByTransport (getTransportContext ().transport ())) {
489509 continue ;
490510 }
511+ if (protoMethod .isResumableUpload ()) {
512+ continue ;
513+ }
491514 String javaStyleProtoMethodName = JavaStyle .toLowerCamelCase (protoMethod .name ());
492515 String callableName = String .format (CALLABLE_CLASS_MEMBER_PATTERN , javaStyleProtoMethodName );
493516 callableClassMembers .put (callableName , getCallableExpr (protoMethod , callableName ));
@@ -592,6 +615,10 @@ protected List<MethodDefinition> createClassMethods(
592615 service ,
593616 classMemberVarExprs .get (getTransportContext ().transportOperationsStubNames ().get (0 ))));
594617 javaMethods .addAll (createCallableGetterMethods (callableClassMemberVarExprs ));
618+ if (service .methods ().stream ().anyMatch (Method ::isResumableUpload )) {
619+ javaMethods .addAll (
620+ createResumableUploadCallableGetterMethods (service , typeStore , classMemberVarExprs ));
621+ }
595622 return javaMethods ;
596623 }
597624
@@ -778,6 +805,7 @@ protected List<MethodDefinition> createConstructorMethods(
778805 Map <String , VariableExpr > javaStyleMethodNameToTransportSettingsVarExprs =
779806 service .methods ().stream ()
780807 .filter (x -> x .isSupportedByTransport (getTransportContext ().transport ()))
808+ .filter (x -> !x .isResumableUpload ())
781809 .collect (
782810 Collectors .toMap (
783811 m -> JavaStyle .toLowerCamelCase (m .name ()),
@@ -803,6 +831,7 @@ protected List<MethodDefinition> createConstructorMethods(
803831 secondCtorExprs .addAll (
804832 service .methods ().stream ()
805833 .filter (x -> x .isSupportedByTransport (getTransportContext ().transport ()))
834+ .filter (x -> !x .isResumableUpload ())
806835 .map (
807836 m ->
808837 createTransportSettingsInitExpr (
@@ -835,6 +864,9 @@ protected List<MethodDefinition> createConstructorMethods(
835864 if (!method .isSupportedByTransport (getTransportContext ().transport ())) {
836865 continue ;
837866 }
867+ if (method .isResumableUpload ()) {
868+ continue ;
869+ }
838870 secondCtorExprs .addAll (
839871 createCallableInitExprs (
840872 context ,
@@ -853,6 +885,17 @@ protected List<MethodDefinition> createConstructorMethods(
853885 secondCtorExprs .clear ();
854886 secondCtorStatements .add (EMPTY_LINE_STATEMENT );
855887
888+ if (service .methods ().stream ().anyMatch (Method ::isResumableUpload )) {
889+ secondCtorStatements .addAll (
890+ createResumableUploadStubInitStatements (
891+ service ,
892+ typeStore ,
893+ thisExpr ,
894+ classMemberVarExprs .get (RESUMABLE_UPLOAD_STUB_MEMBER_NAME ),
895+ clientContextVarExpr ,
896+ settingsVarExpr ));
897+ }
898+
856899 secondCtorStatements .addAll (createLongRunningClient (service , typeStore ));
857900
858901 // Instantiate backgroundResources.
@@ -908,6 +951,36 @@ protected List<Expr> createOperationsStubInitExpr(
908951 .build ());
909952 }
910953
954+ protected List <Statement > createResumableUploadStubInitStatements (
955+ Service service ,
956+ TypeStore typeStore ,
957+ Expr thisExpr ,
958+ VariableExpr resumableUploadStubVarExpr ,
959+ VariableExpr clientContextVarExpr ,
960+ VariableExpr settingsVarExpr ) {
961+ TypeNode stubType =
962+ typeStore .get (String .format ("HttpJson%sResumableUploadStub" , service .name ()));
963+ Expr createStubExpr =
964+ MethodInvocationExpr .builder ()
965+ .setStaticReferenceType (stubType )
966+ .setMethodName ("create" )
967+ .setArguments (Arrays .asList (clientContextVarExpr , settingsVarExpr ))
968+ .setReturnType (stubType )
969+ .build ();
970+ return Arrays .asList (
971+ ExprStatement .withExpr (
972+ AssignmentExpr .builder ()
973+ .setVariableExpr (
974+ resumableUploadStubVarExpr .toBuilder ().setExprReferenceExpr (thisExpr ).build ())
975+ .setValueExpr (createStubExpr )
976+ .build ()),
977+ EMPTY_LINE_STATEMENT );
978+ }
979+
980+ protected boolean isResumableUploadStubNullable () {
981+ return false ;
982+ }
983+
911984 protected List <Statement > createLongRunningClient (Service service , TypeStore typeStore ) {
912985 return ImmutableList .of ();
913986 }
@@ -1094,8 +1167,39 @@ private static List<MethodDefinition> createCallableGetterMethods(
10941167 .collect (Collectors .toList ());
10951168 }
10961169
1170+ protected List <MethodDefinition > createResumableUploadCallableGetterMethods (
1171+ Service service , TypeStore typeStore , Map <String , VariableExpr > classMemberVarExprs ) {
1172+ VariableExpr resumableUploadStubVarExpr =
1173+ classMemberVarExprs .get (RESUMABLE_UPLOAD_STUB_MEMBER_NAME );
1174+ return service .methods ().stream ()
1175+ .filter (Method ::isResumableUpload )
1176+ .map (
1177+ m -> {
1178+ String javaStyleMethodName = JavaStyle .toLowerCamelCase (m .name ());
1179+ String callableMethodName =
1180+ String .format (CALLABLE_CLASS_MEMBER_PATTERN , javaStyleMethodName );
1181+ TypeNode callableType = getCallableType (m );
1182+ Expr returnExpr =
1183+ MethodInvocationExpr .builder ()
1184+ .setExprReferenceExpr (resumableUploadStubVarExpr )
1185+ .setMethodName (callableMethodName )
1186+ .setReturnType (callableType )
1187+ .build ();
1188+ return MethodDefinition .builder ()
1189+ .setIsOverride (true )
1190+ .setScope (ScopeNode .PUBLIC )
1191+ .setReturnType (callableType )
1192+ .setName (callableMethodName )
1193+ .setReturnExpr (returnExpr )
1194+ .build ();
1195+ })
1196+ .collect (Collectors .toList ());
1197+ }
1198+
10971199 private List <MethodDefinition > createStubOverrideMethods (
1098- VariableExpr backgroundResourcesVarExpr , Service service ) {
1200+ Map <String , VariableExpr > classMemberVarExprs , Service service , TypeStore typeStore ) {
1201+ VariableExpr backgroundResourcesVarExpr =
1202+ classMemberVarExprs .get (BACKGROUND_RESOURCES_MEMBER_NAME );
10991203 Function <String , MethodDefinition .Builder > methodMakerStarterFn =
11001204 methodName ->
11011205 MethodDefinition .builder ()
@@ -1135,6 +1239,9 @@ private List<MethodDefinition> createStubOverrideMethods(
11351239 // public final void close() {
11361240 // try {
11371241 // backgroundResources.close();
1242+ // if (resumableUploadStub != null) {
1243+ // resumableUploadStub.close();
1244+ // }
11381245 // } catch (RuntimeException e) {
11391246 // throw e;
11401247 // } catch (Exception e) {
@@ -1162,6 +1269,36 @@ private List<MethodDefinition> createStubOverrideMethods(
11621269 if (service .operationPollingMethod () != null ) {
11631270 javaMethods .addAll (createLongRunningClientGetters ());
11641271 }
1272+
1273+ List <Statement > tryBodyStatements = new ArrayList <>();
1274+ tryBodyStatements .add (
1275+ ExprStatement .withExpr (
1276+ MethodInvocationExpr .builder ()
1277+ .setExprReferenceExpr (backgroundResourcesVarExpr )
1278+ .setMethodName ("close" )
1279+ .build ()));
1280+ if (service .methods ().stream ().anyMatch (Method ::isResumableUpload )) {
1281+ VariableExpr resumableUploadStubVarExpr =
1282+ classMemberVarExprs .get (RESUMABLE_UPLOAD_STUB_MEMBER_NAME );
1283+ Statement closeResumableUploadStubStmt =
1284+ ExprStatement .withExpr (
1285+ MethodInvocationExpr .builder ()
1286+ .setExprReferenceExpr (resumableUploadStubVarExpr )
1287+ .setMethodName ("close" )
1288+ .build ());
1289+ if (isResumableUploadStubNullable ()) {
1290+ tryBodyStatements .add (
1291+ IfStatement .builder ()
1292+ .setConditionExpr (
1293+ RelationalOperationExpr .notEqualToWithExprs (
1294+ resumableUploadStubVarExpr , ValueExpr .createNullExpr ()))
1295+ .setBody (Arrays .asList (closeResumableUploadStubStmt ))
1296+ .build ());
1297+ } else {
1298+ tryBodyStatements .add (closeResumableUploadStubStmt );
1299+ }
1300+ }
1301+
11651302 javaMethods .add (
11661303 methodMakerStarterFn
11671304 .apply ("close" )
@@ -1170,13 +1307,7 @@ private List<MethodDefinition> createStubOverrideMethods(
11701307 .setBody (
11711308 Arrays .asList (
11721309 TryCatchStatement .builder ()
1173- .setTryBody (
1174- Arrays .asList (
1175- ExprStatement .withExpr (
1176- MethodInvocationExpr .builder ()
1177- .setExprReferenceExpr (backgroundResourcesVarExpr )
1178- .setMethodName ("close" )
1179- .build ())))
1310+ .setTryBody (tryBodyStatements )
11801311 .addCatch (
11811312 catchRuntimeExceptionVarExpr .toBuilder ().setIsDecl (true ).build (),
11821313 Arrays .asList (
@@ -1281,10 +1412,22 @@ private TypeStore createDynamicTypes(Service service, String stubPakkage) {
12811412 .collect (Collectors .toList ()),
12821413 true ,
12831414 getTransportContext ().classNames ().getServiceClientClassName (service ));
1415+ if (service .methods ().stream ().anyMatch (Method ::isResumableUpload )) {
1416+ typeStore .put (stubPakkage , String .format ("HttpJson%sResumableUploadStub" , service .name ()));
1417+ }
12841418 return typeStore ;
12851419 }
12861420
12871421 protected static TypeNode getCallableType (Method protoMethod ) {
1422+ if (protoMethod .isResumableUpload ()) {
1423+ return TypeNode .withReference (
1424+ ConcreteReference .builder ()
1425+ .setClazz (ResumableUploadCallable .class )
1426+ .setGenerics (
1427+ Arrays .asList (
1428+ protoMethod .inputType ().reference (), protoMethod .outputType ().reference ()))
1429+ .build ());
1430+ }
12881431 TypeNode callableType = FIXED_TYPESTORE .get ("UnaryCallable" );
12891432 switch (protoMethod .stream ()) {
12901433 case CLIENT :
0 commit comments