7979public class GrpcServiceStubClassComposer implements ClassComposer {
8080 private static final Statement EMPTY_LINE_STATEMENT = EmptyLineStatement .create ();
8181
82- private static final String CLASS_NAME_PATTERN = "Grpc%sStub" ;
8382 private static final String GRPC_SERVICE_CALLABLE_FACTORY_PATTERN = "Grpc%sCallableFactory" ;
8483 private static final String METHOD_DESCRIPTOR_NAME_PATTERN = "%sMethodDescriptor" ;
8584 private static final String PAGED_RESPONSE_TYPE_NAME_PATTERN = "%sPagedResponse" ;
@@ -117,7 +116,7 @@ public static GrpcServiceStubClassComposer instance() {
117116 public GapicClass generate (Service service , Map <String , Message > ignore ) {
118117 String pakkage = service .pakkage () + ".stub" ;
119118 Map <String , TypeNode > types = createDynamicTypes (service , pakkage );
120- String className = getThisClassName ( service . name () );
119+ String className = ClassNames . getGrpcServiceStubClassName ( service );
121120 GapicClass .Kind kind = Kind .STUB ;
122121
123122 Map <String , VariableExpr > protoMethodNameToDescriptorVarExprs =
@@ -400,7 +399,7 @@ private static List<MethodDefinition> createClassMethods(
400399 Map <String , VariableExpr > callableClassMemberVarExprs ,
401400 Map <String , VariableExpr > protoMethodNameToDescriptorVarExprs ) {
402401 List <MethodDefinition > javaMethods = new ArrayList <>();
403- javaMethods .addAll (createStaticCreatorMethods (service . name () , types ));
402+ javaMethods .addAll (createStaticCreatorMethods (service , types ));
404403 javaMethods .addAll (
405404 createConstructorMethods (
406405 service ,
@@ -417,8 +416,8 @@ private static List<MethodDefinition> createClassMethods(
417416 }
418417
419418 private static List <MethodDefinition > createStaticCreatorMethods (
420- String serviceName , Map <String , TypeNode > types ) {
421- TypeNode creatorMethodReturnType = types .get (getThisClassName ( serviceName ));
419+ Service service , Map <String , TypeNode > types ) {
420+ TypeNode creatorMethodReturnType = types .get (ClassNames . getGrpcServiceStubClassName ( service ));
422421 Function <List <VariableExpr >, MethodDefinition .Builder > creatorMethodStarterFn =
423422 argList ->
424423 MethodDefinition .builder ()
@@ -439,7 +438,7 @@ private static List<MethodDefinition> createStaticCreatorMethods(
439438 argList ->
440439 NewObjectExpr .builder ().setType (creatorMethodReturnType ).setArguments (argList ).build ();
441440
442- TypeNode stubSettingsType = types .get (String . format ( STUB_SETTINGS_PATTERN , serviceName ));
441+ TypeNode stubSettingsType = types .get (ClassNames . getServiceStubSettingsClassName ( service ));
443442 VariableExpr settingsVarExpr =
444443 VariableExpr .withVariable (
445444 Variable .builder ().setName ("settings" ).setType (stubSettingsType ).build ());
@@ -518,7 +517,7 @@ private static List<MethodDefinition> createConstructorMethods(
518517 .setType (STATIC_TYPES .get ("GrpcStubCallableFactory" ))
519518 .build ());
520519
521- TypeNode thisClassType = types .get (getThisClassName ( service . name () ));
520+ TypeNode thisClassType = types .get (ClassNames . getGrpcServiceStubClassName ( service ));
522521 TypeNode ioExceptionType =
523522 TypeNode .withReference (ConcreteReference .withClazz (IOException .class ));
524523
@@ -527,8 +526,7 @@ private static List<MethodDefinition> createConstructorMethods(
527526 MethodDefinition .constructorBuilder ()
528527 .setScope (ScopeNode .PROTECTED )
529528 .setReturnType (thisClassType )
530- .setHeaderCommentStatements (
531- Arrays .asList (createProtectedCtorComment (service .name ())))
529+ .setHeaderCommentStatements (Arrays .asList (createProtectedCtorComment (service )))
532530 .setArguments (
533531 args .stream ()
534532 .map (v -> v .toBuilder ().setIsDecl (true ).build ())
@@ -557,7 +555,8 @@ private static List<MethodDefinition> createConstructorMethods(
557555 .build ())));
558556
559557 Expr thisExpr =
560- ValueExpr .withValue (ThisObjectValue .withType (types .get (getThisClassName (service .name ()))));
558+ ValueExpr .withValue (
559+ ThisObjectValue .withType (types .get (ClassNames .getGrpcServiceStubClassName (service ))));
561560 // Body of the second constructor method.
562561 List <Statement > secondCtorStatements = new ArrayList <>();
563562 List <Expr > secondCtorExprs = new ArrayList <>();
@@ -1049,20 +1048,17 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String
10491048 Map <String , TypeNode > types = new HashMap <>();
10501049 types .putAll (
10511050 Arrays .asList (
1052- CLASS_NAME_PATTERN ,
1053- STUB_SETTINGS_PATTERN ,
1054- STUB_PATTERN ,
1055- GRPC_SERVICE_CALLABLE_FACTORY_PATTERN )
1051+ ClassNames . getGrpcServiceStubClassName ( service ) ,
1052+ ClassNames . getServiceStubSettingsClassName ( service ) ,
1053+ ClassNames . getServiceStubClassName ( service ) ,
1054+ ClassNames . getGrpcServiceCallableFactoryClassName ( service ) )
10561055 .stream ()
10571056 .collect (
10581057 Collectors .toMap (
1059- p -> String . format ( p , service . name ()) ,
1060- p ->
1058+ n -> n ,
1059+ n ->
10611060 TypeNode .withReference (
1062- VaporReference .builder ()
1063- .setName (String .format (p , service .name ()))
1064- .setPakkage (stubPakkage )
1065- .build ()))));
1061+ VaporReference .builder ().setName (n ).setPakkage (stubPakkage ).build ()))));
10661062 // Pagination types.
10671063 types .putAll (
10681064 service .methods ().stream ()
@@ -1076,7 +1072,7 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String
10761072 .setName (String .format (PAGED_RESPONSE_TYPE_NAME_PATTERN , m .name ()))
10771073 .setPakkage (service .pakkage ())
10781074 .setEnclosingClassNames (
1079- String . format ( "%sClient" , service . overriddenName () ))
1075+ ClassNames . getServiceClientClassName ( service ))
10801076 .setIsStaticImport (true )
10811077 .build ()))));
10821078 return types ;
@@ -1144,17 +1140,13 @@ private static String getProtoRpcFullMethodName(Service protoService, Method pro
11441140 return String .format ("google.iam.v1.IAMPolicy/%s" , protoMethod .name ());
11451141 }
11461142
1147- private static String getThisClassName (String serviceName ) {
1148- return String .format (CLASS_NAME_PATTERN , serviceName );
1149- }
1150-
1151- private static CommentStatement createProtectedCtorComment (String serviceName ) {
1143+ private static CommentStatement createProtectedCtorComment (Service service ) {
11521144 return CommentStatement .withComment (
11531145 JavaDocComment .withComment (
11541146 String .format (
11551147 "Constructs an instance of %s, using the given settings. This is protected so that"
11561148 + " it is easy to make a subclass, but otherwise, the static factory methods"
11571149 + " should be preferred." ,
1158- getThisClassName ( serviceName ))));
1150+ ClassNames . getGrpcServiceStubClassName ( service ))));
11591151 }
11601152}
0 commit comments