@@ -60,7 +60,8 @@ static inline string MessageFullJavaName(const Descriptor* desc) {
6060}
6161
6262static void PrintMethodFields (
63- const ServiceDescriptor* service, map<string, string>* vars, Printer* p) {
63+ const ServiceDescriptor* service, map<string, string>* vars, Printer* p,
64+ bool generate_nano) {
6465 for (int i = 0 ; i < service->method_count (); ++i) {
6566 const MethodDescriptor* method = service->method (i);
6667 (*vars)[" method_name" ] = method->name ();
@@ -82,14 +83,43 @@ static void PrintMethodFields(
8283 (*vars)[" method_type" ] = " UNARY" ;
8384 }
8485 }
85- p->Print (
86- *vars,
87- " private static final $Method$<$input_type$,\n "
88- " $output_type$> $method_field_name$ =\n "
89- " $Method$.create(\n "
90- " $MethodType$.$method_type$, \" $method_name$\" ,\n "
91- " $ProtoUtils$.marshaller($input_type$.PARSER),\n "
92- " $ProtoUtils$.marshaller($output_type$.PARSER));\n " );
86+
87+ if (generate_nano) {
88+ // TODO(zsurocking): we're creating two Parsers for each method right now.
89+ // We could instead create static Parsers and reuse them if some methods
90+ // share the same request or response messages.
91+ p->Print (
92+ *vars,
93+ " private static final $Method$<$input_type$,\n "
94+ " $output_type$> $method_field_name$ =\n "
95+ " $Method$.create(\n "
96+ " $MethodType$.$method_type$, \" $method_name$\" ,\n "
97+ " $NanoUtils$.<$input_type$>marshaller(\n "
98+ " new io.grpc.nano.Parser<$input_type$>() {\n "
99+ " @Override\n "
100+ " public $input_type$ parse("
101+ " $CodedInputByteBufferNano$ input) throws IOException {\n "
102+ " return $input_type$.parseFrom(input);\n "
103+ " }\n "
104+ " }),\n "
105+ " $NanoUtils$.<$output_type$>marshaller(\n "
106+ " new io.grpc.nano.Parser<$output_type$>() {\n "
107+ " @Override\n "
108+ " public $output_type$ parse("
109+ " $CodedInputByteBufferNano$ input) throws IOException {\n "
110+ " return $output_type$.parseFrom(input);\n "
111+ " }\n "
112+ " }));\n " );
113+ } else {
114+ p->Print (
115+ *vars,
116+ " private static final $Method$<$input_type$,\n "
117+ " $output_type$> $method_field_name$ =\n "
118+ " $Method$.create(\n "
119+ " $MethodType$.$method_type$, \" $method_name$\" ,\n "
120+ " $ProtoUtils$.marshaller($input_type$.PARSER),\n "
121+ " $ProtoUtils$.marshaller($output_type$.PARSER));\n " );
122+ }
93123 }
94124 p->Print (" \n " );
95125}
@@ -526,7 +556,8 @@ static void PrintBindServiceMethod(const ServiceDescriptor* service,
526556
527557static void PrintService (const ServiceDescriptor* service,
528558 map<string, string>* vars,
529- Printer* p) {
559+ Printer* p,
560+ bool generate_nano) {
530561 (*vars)[" service_name" ] = service->name ();
531562 (*vars)[" service_class_name" ] = ServiceClassName (service);
532563 p->Print (
@@ -535,7 +566,7 @@ static void PrintService(const ServiceDescriptor* service,
535566 " public class $service_class_name$ {\n\n " );
536567 p->Indent ();
537568
538- PrintMethodFields (service, vars, p);
569+ PrintMethodFields (service, vars, p, generate_nano );
539570
540571 p->Print (
541572 *vars,
@@ -583,7 +614,7 @@ static void PrintService(const ServiceDescriptor* service,
583614 p->Print (" }\n " );
584615}
585616
586- void PrintImports (Printer* p) {
617+ void PrintImports (Printer* p, bool generate_nano ) {
587618 p->Print (
588619 " import static "
589620 " io.grpc.stub.Calls.createMethodDescriptor;\n "
@@ -607,10 +638,14 @@ void PrintImports(Printer* p) {
607638 " io.grpc.stub.ServerCalls.asyncUnaryRequestCall;\n "
608639 " import static "
609640 " io.grpc.stub.ServerCalls.asyncStreamingRequestCall;\n\n " );
641+ if (generate_nano) {
642+ p->Print (" import java.io.IOException;\n\n " );
643+ }
610644}
611645
612646void GenerateService (const ServiceDescriptor* service,
613- google::protobuf::io::ZeroCopyOutputStream* out) {
647+ google::protobuf::io::ZeroCopyOutputStream* out,
648+ bool generate_nano) {
614649 // All non-generated classes must be referred by fully qualified names to
615650 // avoid collision with generated classes.
616651 map<string, string> vars;
@@ -627,6 +662,7 @@ void GenerateService(const ServiceDescriptor* service,
627662 vars[" ImmutableList" ] = " com.google.common.collect.ImmutableList" ;
628663 vars[" MethodDescriptor" ] = " io.grpc.MethodDescriptor" ;
629664 vars[" ProtoUtils" ] = " io.grpc.proto.ProtoUtils" ;
665+ vars[" NanoUtils" ] = " io.grpc.nano.NanoUtils" ;
630666 vars[" StreamObserver" ] = " io.grpc.stub.StreamObserver" ;
631667 vars[" Iterator" ] = " java.util.Iterator" ;
632668 vars[" Map" ] = " java.util.Map" ;
@@ -635,20 +671,22 @@ void GenerateService(const ServiceDescriptor* service,
635671 vars[" Immutable" ] = " javax.annotation.concurrent.Immutable" ;
636672 vars[" ListenableFuture" ] =
637673 " com.google.common.util.concurrent.ListenableFuture" ;
674+ vars[" CodedInputByteBufferNano" ] =
675+ " com.google.protobuf.nano.CodedInputByteBufferNano" ;
638676
639677 Printer printer (out, ' $' );
640678 string package_name = ServiceJavaPackage (service->file ());
641679 printer.Print (
642680 " package $package_name$;\n\n " ,
643681 " package_name" , package_name);
644- PrintImports (&printer);
682+ PrintImports (&printer, generate_nano );
645683
646684 // Package string is used to fully qualify method names.
647685 vars[" Package" ] = service->file ()->package ();
648686 if (!vars[" Package" ].empty ()) {
649687 vars[" Package" ].append (" ." );
650688 }
651- PrintService (service, &vars, &printer);
689+ PrintService (service, &vars, &printer, generate_nano );
652690}
653691
654692string ServiceJavaPackage (const FileDescriptor* file) {
0 commit comments