@@ -105,13 +105,9 @@ public abstract class AbstractTransportServiceStubClassComposer implements Class
105
105
private static final String PAGED_CALLABLE_CLASS_MEMBER_PATTERN = "%sPagedCallable" ;
106
106
107
107
private static final String BACKGROUND_RESOURCES_MEMBER_NAME = "backgroundResources" ;
108
- private static final String CALLABLE_NAME = "Callable" ;
109
108
private static final String CALLABLE_FACTORY_MEMBER_NAME = "callableFactory" ;
110
109
protected static final String CALLABLE_CLASS_MEMBER_PATTERN = "%sCallable" ;
111
110
private static final String OPERATION_CALLABLE_CLASS_MEMBER_PATTERN = "%sOperationCallable" ;
112
- private static final String OPERATION_CALLABLE_NAME = "OperationCallable" ;
113
- // private static final String OPERATIONS_STUB_MEMBER_NAME = "operationsStub";
114
- protected static final String PAGED_CALLABLE_NAME = "PagedCallable" ;
115
111
116
112
protected static final TypeStore FIXED_TYPESTORE = createStaticTypes ();
117
113
@@ -460,59 +456,63 @@ private Map<String, VariableExpr> createCallableClassMembers(
460
456
}
461
457
String javaStyleProtoMethodName = JavaStyle .toLowerCamelCase (protoMethod .name ());
462
458
String callableName = String .format (CALLABLE_CLASS_MEMBER_PATTERN , javaStyleProtoMethodName );
463
- callableClassMembers .put (
464
- callableName ,
465
- VariableExpr .withVariable (
466
- Variable .builder ()
467
- .setName (callableName )
468
- .setType (getCallableType (protoMethod ))
469
- .build ()));
459
+ callableClassMembers .put (callableName , getCallableExpr (protoMethod , callableName ));
470
460
if (protoMethod .hasLro ()) {
471
461
callableName =
472
462
String .format (OPERATION_CALLABLE_CLASS_MEMBER_PATTERN , javaStyleProtoMethodName );
473
- callableClassMembers .put (
474
- callableName ,
475
- VariableExpr .withVariable (
476
- Variable .builder ()
477
- .setName (callableName )
478
- .setType (
479
- TypeNode .withReference (
480
- ConcreteReference .builder ()
481
- .setClazz (OperationCallable .class )
482
- .setGenerics (
483
- Arrays .asList (
484
- protoMethod .inputType ().reference (),
485
- protoMethod .lro ().responseType ().reference (),
486
- protoMethod .lro ().metadataType ().reference ()))
487
- .build ()))
488
- .build ()));
463
+ callableClassMembers .put (callableName , getOperationCallableExpr (protoMethod , callableName ));
489
464
}
490
465
if (protoMethod .isPaged ()) {
491
466
callableName = String .format (PAGED_CALLABLE_CLASS_MEMBER_PATTERN , javaStyleProtoMethodName );
492
467
callableClassMembers .put (
493
- callableName ,
494
- VariableExpr .withVariable (
495
- Variable .builder ()
496
- .setName (callableName )
497
- .setType (
498
- TypeNode .withReference (
499
- getCallableType (protoMethod )
500
- .reference ()
501
- .copyAndSetGenerics (
502
- Arrays .asList (
503
- protoMethod .inputType ().reference (),
504
- typeStore
505
- .get (
506
- String .format (
507
- PAGED_RESPONSE_TYPE_NAME_PATTERN ,
508
- protoMethod .name ()))
509
- .reference ()))))
510
- .build ()));
468
+ callableName , getPagedCallableExpr (typeStore , protoMethod , callableName ));
511
469
}
512
470
}
513
471
return callableClassMembers ;
514
472
}
515
473
474
+ private VariableExpr getCallableExpr (Method protoMethod , String callableName ) {
475
+ return VariableExpr .withVariable (
476
+ Variable .builder ().setName (callableName ).setType (getCallableType (protoMethod )).build ());
477
+ }
478
+
479
+ private VariableExpr getPagedCallableExpr (
480
+ TypeStore typeStore , Method protoMethod , String callableName ) {
481
+ return VariableExpr .withVariable (
482
+ Variable .builder ()
483
+ .setName (callableName )
484
+ .setType (
485
+ TypeNode .withReference (
486
+ getCallableType (protoMethod )
487
+ .reference ()
488
+ .copyAndSetGenerics (
489
+ Arrays .asList (
490
+ protoMethod .inputType ().reference (),
491
+ typeStore
492
+ .get (
493
+ String .format (
494
+ PAGED_RESPONSE_TYPE_NAME_PATTERN , protoMethod .name ()))
495
+ .reference ()))))
496
+ .build ());
497
+ }
498
+
499
+ private VariableExpr getOperationCallableExpr (Method protoMethod , String callableName ) {
500
+ return VariableExpr .withVariable (
501
+ Variable .builder ()
502
+ .setName (callableName )
503
+ .setType (
504
+ TypeNode .withReference (
505
+ ConcreteReference .builder ()
506
+ .setClazz (OperationCallable .class )
507
+ .setGenerics (
508
+ Arrays .asList (
509
+ protoMethod .inputType ().reference (),
510
+ protoMethod .lro ().responseType ().reference (),
511
+ protoMethod .lro ().metadataType ().reference ()))
512
+ .build ()))
513
+ .build ());
514
+ }
515
+
516
516
protected List <AnnotationNode > createClassAnnotations (Service service ) {
517
517
List <AnnotationNode > annotations = new ArrayList <>();
518
518
if (!PackageChecker .isGaApi (service .pakkage ())) {
@@ -547,7 +547,6 @@ protected List<MethodDefinition> createClassMethods(
547
547
service ,
548
548
typeStore ,
549
549
classMemberVarExprs ,
550
- callableClassMemberVarExprs ,
551
550
protoMethodNameToDescriptorVarExprs ,
552
551
classStatements ));
553
552
javaMethods .addAll (
@@ -646,7 +645,6 @@ protected List<MethodDefinition> createConstructorMethods(
646
645
Service service ,
647
646
TypeStore typeStore ,
648
647
Map <String , VariableExpr > classMemberVarExprs ,
649
- Map <String , VariableExpr > callableClassMemberVarExprs ,
650
648
Map <String , VariableExpr > protoMethodNameToDescriptorVarExprs ,
651
649
List <Statement > classStatements ) {
652
650
TypeNode stubSettingsType =
@@ -786,22 +784,33 @@ protected List<MethodDefinition> createConstructorMethods(
786
784
secondCtorStatements .add (EMPTY_LINE_STATEMENT );
787
785
788
786
// Initialize <method>Callable variables.
789
- secondCtorExprs .addAll (
790
- callableClassMemberVarExprs .entrySet ().stream ()
791
- .map (
792
- e ->
793
- createCallableInitExpr (
794
- context ,
795
- service ,
796
- e .getKey (),
797
- e .getValue (),
798
- callableFactoryVarExpr ,
799
- settingsVarExpr ,
800
- clientContextVarExpr ,
801
- operationsStubClassVarExpr ,
802
- thisExpr ,
803
- javaStyleMethodNameToTransportSettingsVarExprs ))
804
- .collect (Collectors .toList ()));
787
+ // The logic inside createCallableInitExprs() is very similar to createCallableClassMembers().
788
+ // It is mostly duplicated because `createCallableClassMembers` returns a heuristic to
789
+ // determine the RPC type. The RPCs are mapped by name and the types are determined by the
790
+ // generated name and was problematic for certain RPC names. For example, the GetApiOperation
791
+ // RPC name would have a mapping of GetApiOperationCallable, and the `createCallableInitExprs`
792
+ // method would attempt to generate LRO code because of the `OperationCallable` suffix.
793
+ // Instead, we now pass the method object which is the SoT for the type of the method and not
794
+ // based on heuristics/ suffix.
795
+ for (Method method : service .methods ()) {
796
+ // Do not generate callables for non supported RPCs (i.e. Bidi-Streaming and Client Streaming
797
+ // for HttpJson)
798
+ if (!method .isSupportedByTransport (getTransportContext ().transport ())) {
799
+ continue ;
800
+ }
801
+ secondCtorExprs .addAll (
802
+ createCallableInitExprs (
803
+ context ,
804
+ service ,
805
+ method ,
806
+ typeStore ,
807
+ callableFactoryVarExpr ,
808
+ settingsVarExpr ,
809
+ clientContextVarExpr ,
810
+ operationsStubClassVarExpr ,
811
+ thisExpr ,
812
+ javaStyleMethodNameToTransportSettingsVarExprs ));
813
+ }
805
814
secondCtorStatements .addAll (
806
815
secondCtorExprs .stream ().map (ExprStatement ::withExpr ).collect (Collectors .toList ()));
807
816
secondCtorExprs .clear ();
@@ -871,67 +880,116 @@ protected VariableExpr declareLongRunningClient() {
871
880
return null ;
872
881
}
873
882
874
- private Expr createCallableInitExpr (
883
+ // Can return multiple Exprs for a single RPC. Each of the Exprs will initialize a callable
884
+ // in the constructor. The possible combinations are Normal (Unary, Streaming, Batching) and
885
+ // either Operation or Paged (if needed). It is not possible to have three callable Exprs
886
+ // returned because LROs are not paged, so it will either be an additional LRO or paged callable.
887
+ private List <Expr > createCallableInitExprs (
875
888
GapicContext context ,
876
889
Service service ,
877
- String callableVarName ,
878
- VariableExpr callableVarExpr ,
890
+ Method method ,
891
+ TypeStore typeStore ,
879
892
VariableExpr callableFactoryVarExpr ,
880
893
VariableExpr settingsVarExpr ,
881
894
VariableExpr clientContextVarExpr ,
882
895
VariableExpr operationsStubClassVarExpr ,
883
896
Expr thisExpr ,
884
897
Map <String , VariableExpr > javaStyleMethodNameToTransportSettingsVarExprs ) {
885
- boolean isOperation = callableVarName .endsWith (OPERATION_CALLABLE_NAME );
886
- boolean isPaged = callableVarName .endsWith (PAGED_CALLABLE_NAME );
887
- int sublength ;
888
- if (isOperation ) {
889
- sublength = OPERATION_CALLABLE_NAME .length ();
890
- } else if (isPaged ) {
891
- sublength = PAGED_CALLABLE_NAME .length ();
892
- } else {
893
- sublength = CALLABLE_NAME .length ();
894
- }
895
- String javaStyleMethodName = callableVarName .substring (0 , callableVarName .length () - sublength );
896
- List <Expr > creatorMethodArgVarExprs ;
898
+ List <Expr > callableInitExprs = new ArrayList <>();
899
+ String javaStyleProtoMethodName = JavaStyle .toLowerCamelCase (method .name ());
900
+
897
901
Expr transportSettingsVarExpr =
898
- javaStyleMethodNameToTransportSettingsVarExprs .get (javaStyleMethodName );
899
- if (transportSettingsVarExpr == null && isOperation ) {
900
- // Try again, in case the name detection above was inaccurate.
901
- isOperation = false ;
902
- sublength = CALLABLE_NAME .length ();
903
- javaStyleMethodName = callableVarName .substring (0 , callableVarName .length () - sublength );
904
- transportSettingsVarExpr =
905
- javaStyleMethodNameToTransportSettingsVarExprs .get (javaStyleMethodName );
906
- }
902
+ javaStyleMethodNameToTransportSettingsVarExprs .get (javaStyleProtoMethodName );
907
903
Preconditions .checkNotNull (
908
904
transportSettingsVarExpr ,
909
905
String .format (
910
- "No transport settings variable found for method name %s" , javaStyleMethodName ));
911
- if (isOperation ) {
906
+ "No transport settings variable found for method name %s" , javaStyleProtoMethodName ));
907
+
908
+ // Build the normal callable which will be generated for every RPC
909
+ VariableExpr callableVarExpr =
910
+ getCallableExpr (
911
+ method , String .format (CALLABLE_CLASS_MEMBER_PATTERN , javaStyleProtoMethodName ));
912
+ List <Expr > creatorMethodArgVarExprs =
913
+ Arrays .asList (
914
+ transportSettingsVarExpr ,
915
+ MethodInvocationExpr .builder ()
916
+ .setExprReferenceExpr (settingsVarExpr )
917
+ .setMethodName (String .format ("%sSettings" , javaStyleProtoMethodName ))
918
+ .build (),
919
+ clientContextVarExpr );
920
+ AssignmentExpr callableExpr =
921
+ buildCallableTransportExpr (
922
+ context ,
923
+ service ,
924
+ callableFactoryVarExpr ,
925
+ thisExpr ,
926
+ javaStyleProtoMethodName ,
927
+ callableVarExpr ,
928
+ creatorMethodArgVarExprs );
929
+ callableInitExprs .add (callableExpr );
930
+
931
+ // Build an additional paged callable if the RPC is paged. The creatorMethodArgVarExprs is the
932
+ // same as the normal callable
933
+ if (method .isPaged ()) {
934
+ callableVarExpr =
935
+ getPagedCallableExpr (
936
+ typeStore ,
937
+ method ,
938
+ String .format (PAGED_CALLABLE_CLASS_MEMBER_PATTERN , javaStyleProtoMethodName ));
939
+ callableExpr =
940
+ buildCallableTransportExpr (
941
+ context ,
942
+ service ,
943
+ callableFactoryVarExpr ,
944
+ thisExpr ,
945
+ javaStyleProtoMethodName ,
946
+ callableVarExpr ,
947
+ creatorMethodArgVarExprs );
948
+ callableInitExprs .add (callableExpr );
949
+ }
950
+
951
+ // Build an additional operation callable if the RPC is an LRO. Rebuild the
952
+ // creatorMethodArgVarExprs as LROs have a special OperationSettings
953
+ if (method .hasLro ()) {
954
+ callableVarExpr =
955
+ getOperationCallableExpr (
956
+ method ,
957
+ String .format (OPERATION_CALLABLE_CLASS_MEMBER_PATTERN , javaStyleProtoMethodName ));
912
958
creatorMethodArgVarExprs =
913
959
Arrays .asList (
914
960
transportSettingsVarExpr ,
915
961
MethodInvocationExpr .builder ()
916
962
.setExprReferenceExpr (settingsVarExpr )
917
- .setMethodName (String .format ("%sOperationSettings" , javaStyleMethodName ))
963
+ .setMethodName (String .format ("%sOperationSettings" , javaStyleProtoMethodName ))
918
964
.build (),
919
965
clientContextVarExpr ,
920
966
operationsStubClassVarExpr );
921
- } else {
922
- creatorMethodArgVarExprs =
923
- Arrays .asList (
924
- transportSettingsVarExpr ,
925
- MethodInvocationExpr .builder ()
926
- .setExprReferenceExpr (settingsVarExpr )
927
- .setMethodName (String .format ("%sSettings" , javaStyleMethodName ))
928
- .build (),
929
- clientContextVarExpr );
967
+ callableExpr =
968
+ buildCallableTransportExpr (
969
+ context ,
970
+ service ,
971
+ callableFactoryVarExpr ,
972
+ thisExpr ,
973
+ javaStyleProtoMethodName ,
974
+ callableVarExpr ,
975
+ creatorMethodArgVarExprs );
976
+ callableInitExprs .add (callableExpr );
930
977
}
931
978
932
- String methodName = JavaStyle .toUpperCamelCase (javaStyleMethodName );
979
+ return callableInitExprs ;
980
+ }
981
+
982
+ private AssignmentExpr buildCallableTransportExpr (
983
+ GapicContext context ,
984
+ Service service ,
985
+ VariableExpr callableFactoryVarExpr ,
986
+ Expr thisExpr ,
987
+ String methodName ,
988
+ VariableExpr callableVarExpr ,
989
+ List <Expr > creatorMethodArgVarExprs ) {
933
990
Optional <String > callableCreatorMethodName =
934
- getCallableCreatorMethodName (context , service , callableVarExpr .type (), methodName );
991
+ getCallableCreatorMethodName (
992
+ context , service , callableVarExpr .type (), JavaStyle .toUpperCamelCase (methodName ));
935
993
936
994
Expr initExpr ;
937
995
if (callableCreatorMethodName .isPresent ()) {
0 commit comments