java_service.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 
36 
43 
44 
45 namespace google {
46 namespace protobuf {
47 namespace compiler {
48 namespace java {
49 
52 
54 
55 // ===================================================================
57  const ServiceDescriptor* descriptor, Context* context)
59  context_(context),
60  name_resolver_(context->GetNameResolver()) {}
61 
63 
65  bool is_own_file = IsOwnFile(descriptor_, /* immutable = */ true);
68  /* immutable = */ true);
69  printer->Print(
70  "public $static$ abstract class $classname$\n"
71  " implements com.google.protobuf.Service {\n",
72  "static", is_own_file ? "" : "static", "classname", descriptor_->name());
73  printer->Indent();
74 
75  printer->Print("protected $classname$() {}\n\n", "classname",
76  descriptor_->name());
77 
78  GenerateInterface(printer);
79 
82 
83  GenerateAbstractMethods(printer);
84 
85  // Generate getDescriptor() and getDescriptorForType().
86  printer->Print(
87  "public static final\n"
88  " com.google.protobuf.Descriptors.ServiceDescriptor\n"
89  " getDescriptor() {\n"
90  " return $file$.getDescriptor().getServices().get($index$);\n"
91  "}\n",
93  "index", StrCat(descriptor_->index()));
95 
96  // Generate more stuff.
97  GenerateCallMethod(printer);
98  GenerateGetPrototype(REQUEST, printer);
100  GenerateStub(printer);
101  GenerateBlockingStub(printer);
102 
103  // Add an insertion point.
104  printer->Print(
105  "\n"
106  "// @@protoc_insertion_point(class_scope:$full_name$)\n",
107  "full_name", descriptor_->full_name());
108 
109  printer->Outdent();
110  printer->Print("}\n\n");
111 }
112 
114  io::Printer* printer) {
115  printer->Print(
116  "public final com.google.protobuf.Descriptors.ServiceDescriptor\n"
117  " getDescriptorForType() {\n"
118  " return getDescriptor();\n"
119  "}\n");
120 }
121 
123  printer->Print("public interface Interface {\n");
124  printer->Indent();
125  GenerateAbstractMethods(printer);
126  printer->Outdent();
127  printer->Print("}\n\n");
128 }
129 
131  io::Printer* printer) {
132  printer->Print(
133  "public static com.google.protobuf.Service newReflectiveService(\n"
134  " final Interface impl) {\n"
135  " return new $classname$() {\n",
136  "classname", descriptor_->name());
137  printer->Indent();
138  printer->Indent();
139 
140  for (int i = 0; i < descriptor_->method_count(); i++) {
142  printer->Print("@java.lang.Override\n");
144  printer->Print(
145  " {\n"
146  " impl.$method$(controller, request, done);\n"
147  "}\n\n",
148  "method", UnderscoresToCamelCase(method));
149  }
150 
151  printer->Outdent();
152  printer->Print("};\n");
153  printer->Outdent();
154  printer->Print("}\n\n");
155 }
156 
158  io::Printer* printer) {
159  printer->Print(
160  "public static com.google.protobuf.BlockingService\n"
161  " newReflectiveBlockingService(final BlockingInterface impl) {\n"
162  " return new com.google.protobuf.BlockingService() {\n");
163  printer->Indent();
164  printer->Indent();
165 
167 
169  GenerateGetPrototype(REQUEST, printer);
170  GenerateGetPrototype(RESPONSE, printer);
171 
172  printer->Outdent();
173  printer->Print("};\n");
174  printer->Outdent();
175  printer->Print("}\n\n");
176 }
177 
179  for (int i = 0; i < descriptor_->method_count(); i++) {
181  WriteMethodDocComment(printer, method);
183  printer->Print(";\n\n");
184  }
185 }
186 
188  const MethodDescriptor* method) {
189  return name_resolver_->GetImmutableClassName(method->output_type());
190 }
191 
193  printer->Print(
194  "\n"
195  "public final void callMethod(\n"
196  " com.google.protobuf.Descriptors.MethodDescriptor method,\n"
197  " com.google.protobuf.RpcController controller,\n"
198  " com.google.protobuf.Message request,\n"
199  " com.google.protobuf.RpcCallback<\n"
200  " com.google.protobuf.Message> done) {\n"
201  " if (method.getService() != getDescriptor()) {\n"
202  " throw new java.lang.IllegalArgumentException(\n"
203  " \"Service.callMethod() given method descriptor for wrong \" +\n"
204  " \"service type.\");\n"
205  " }\n"
206  " switch(method.getIndex()) {\n");
207  printer->Indent();
208  printer->Indent();
209 
210  for (int i = 0; i < descriptor_->method_count(); i++) {
212  std::map<std::string, std::string> vars;
213  vars["index"] = StrCat(i);
214  vars["method"] = UnderscoresToCamelCase(method);
215  vars["input"] = name_resolver_->GetImmutableClassName(method->input_type());
216  vars["output"] = GetOutput(method);
217  printer->Print(
218  vars,
219  "case $index$:\n"
220  " this.$method$(controller, ($input$)request,\n"
221  " com.google.protobuf.RpcUtil.<$output$>specializeCallback(\n"
222  " done));\n"
223  " return;\n");
224  }
225 
226  printer->Print(
227  "default:\n"
228  " throw new java.lang.AssertionError(\"Can't get here.\");\n");
229 
230  printer->Outdent();
231  printer->Outdent();
232 
233  printer->Print(
234  " }\n"
235  "}\n"
236  "\n");
237 }
238 
240  io::Printer* printer) {
241  printer->Print(
242  "\n"
243  "public final com.google.protobuf.Message callBlockingMethod(\n"
244  " com.google.protobuf.Descriptors.MethodDescriptor method,\n"
245  " com.google.protobuf.RpcController controller,\n"
246  " com.google.protobuf.Message request)\n"
247  " throws com.google.protobuf.ServiceException {\n"
248  " if (method.getService() != getDescriptor()) {\n"
249  " throw new java.lang.IllegalArgumentException(\n"
250  " \"Service.callBlockingMethod() given method descriptor for \" +\n"
251  " \"wrong service type.\");\n"
252  " }\n"
253  " switch(method.getIndex()) {\n");
254  printer->Indent();
255  printer->Indent();
256 
257  for (int i = 0; i < descriptor_->method_count(); i++) {
259  std::map<std::string, std::string> vars;
260  vars["index"] = StrCat(i);
261  vars["method"] = UnderscoresToCamelCase(method);
262  vars["input"] = name_resolver_->GetImmutableClassName(method->input_type());
263  vars["output"] = GetOutput(method);
264  printer->Print(vars,
265  "case $index$:\n"
266  " return impl.$method$(controller, ($input$)request);\n");
267  }
268 
269  printer->Print(
270  "default:\n"
271  " throw new java.lang.AssertionError(\"Can't get here.\");\n");
272 
273  printer->Outdent();
274  printer->Outdent();
275 
276  printer->Print(
277  " }\n"
278  "}\n"
279  "\n");
280 }
281 
283  io::Printer* printer) {
284  /*
285  * TODO(cpovirk): The exception message says "Service.foo" when it may be
286  * "BlockingService.foo." Consider fixing.
287  */
288  printer->Print(
289  "public final com.google.protobuf.Message\n"
290  " get$request_or_response$Prototype(\n"
291  " com.google.protobuf.Descriptors.MethodDescriptor method) {\n"
292  " if (method.getService() != getDescriptor()) {\n"
293  " throw new java.lang.IllegalArgumentException(\n"
294  " \"Service.get$request_or_response$Prototype() given method \" +\n"
295  " \"descriptor for wrong service type.\");\n"
296  " }\n"
297  " switch(method.getIndex()) {\n",
298  "request_or_response", (which == REQUEST) ? "Request" : "Response");
299  printer->Indent();
300  printer->Indent();
301 
302  for (int i = 0; i < descriptor_->method_count(); i++) {
304  std::map<std::string, std::string> vars;
305  vars["index"] = StrCat(i);
306  vars["type"] =
307  (which == REQUEST)
309  : GetOutput(method);
310  printer->Print(vars,
311  "case $index$:\n"
312  " return $type$.getDefaultInstance();\n");
313  }
314 
315  printer->Print(
316  "default:\n"
317  " throw new java.lang.AssertionError(\"Can't get here.\");\n");
318 
319  printer->Outdent();
320  printer->Outdent();
321 
322  printer->Print(
323  " }\n"
324  "}\n"
325  "\n");
326 }
327 
329  printer->Print(
330  "public static Stub newStub(\n"
331  " com.google.protobuf.RpcChannel channel) {\n"
332  " return new Stub(channel);\n"
333  "}\n"
334  "\n"
335  "public static final class Stub extends $classname$ implements Interface "
336  "{"
337  "\n",
339  printer->Indent();
340 
341  printer->Print(
342  "private Stub(com.google.protobuf.RpcChannel channel) {\n"
343  " this.channel = channel;\n"
344  "}\n"
345  "\n"
346  "private final com.google.protobuf.RpcChannel channel;\n"
347  "\n"
348  "public com.google.protobuf.RpcChannel getChannel() {\n"
349  " return channel;\n"
350  "}\n");
351 
352  for (int i = 0; i < descriptor_->method_count(); i++) {
354  printer->Print("\n");
356  printer->Print(" {\n");
357  printer->Indent();
358 
359  std::map<std::string, std::string> vars;
360  vars["index"] = StrCat(i);
361  vars["output"] = GetOutput(method);
362  printer->Print(vars,
363  "channel.callMethod(\n"
364  " getDescriptor().getMethods().get($index$),\n"
365  " controller,\n"
366  " request,\n"
367  " $output$.getDefaultInstance(),\n"
368  " com.google.protobuf.RpcUtil.generalizeCallback(\n"
369  " done,\n"
370  " $output$.class,\n"
371  " $output$.getDefaultInstance()));\n");
372 
373  printer->Outdent();
374  printer->Print("}\n");
375  }
376 
377  printer->Outdent();
378  printer->Print(
379  "}\n"
380  "\n");
381 }
382 
384  printer->Print(
385  "public static BlockingInterface newBlockingStub(\n"
386  " com.google.protobuf.BlockingRpcChannel channel) {\n"
387  " return new BlockingStub(channel);\n"
388  "}\n"
389  "\n");
390 
391  printer->Print("public interface BlockingInterface {");
392  printer->Indent();
393 
394  for (int i = 0; i < descriptor_->method_count(); i++) {
397  printer->Print(";\n");
398  }
399 
400  printer->Outdent();
401  printer->Print(
402  "}\n"
403  "\n");
404 
405  printer->Print(
406  "private static final class BlockingStub implements BlockingInterface "
407  "{\n");
408  printer->Indent();
409 
410  printer->Print(
411  "private BlockingStub(com.google.protobuf.BlockingRpcChannel channel) {\n"
412  " this.channel = channel;\n"
413  "}\n"
414  "\n"
415  "private final com.google.protobuf.BlockingRpcChannel channel;\n");
416 
417  for (int i = 0; i < descriptor_->method_count(); i++) {
420  printer->Print(" {\n");
421  printer->Indent();
422 
423  std::map<std::string, std::string> vars;
424  vars["index"] = StrCat(i);
425  vars["output"] = GetOutput(method);
426  printer->Print(vars,
427  "return ($output$) channel.callBlockingMethod(\n"
428  " getDescriptor().getMethods().get($index$),\n"
429  " controller,\n"
430  " request,\n"
431  " $output$.getDefaultInstance());\n");
432 
433  printer->Outdent();
434  printer->Print(
435  "}\n"
436  "\n");
437  }
438 
439  printer->Outdent();
440  printer->Print("}\n");
441 }
442 
444  io::Printer* printer, const MethodDescriptor* method,
445  IsAbstract is_abstract) {
446  std::map<std::string, std::string> vars;
447  vars["name"] = UnderscoresToCamelCase(method);
448  vars["input"] = name_resolver_->GetImmutableClassName(method->input_type());
449  vars["output"] = GetOutput(method);
450  vars["abstract"] = (is_abstract == IS_ABSTRACT) ? "abstract" : "";
451  printer->Print(vars,
452  "public $abstract$ void $name$(\n"
453  " com.google.protobuf.RpcController controller,\n"
454  " $input$ request,\n"
455  " com.google.protobuf.RpcCallback<$output$> done)");
456 }
457 
459  io::Printer* printer, const MethodDescriptor* method) {
460  std::map<std::string, std::string> vars;
461  vars["method"] = UnderscoresToCamelCase(method);
462  vars["input"] = name_resolver_->GetImmutableClassName(method->input_type());
463  vars["output"] = GetOutput(method);
464  printer->Print(vars,
465  "\n"
466  "public $output$ $method$(\n"
467  " com.google.protobuf.RpcController controller,\n"
468  " $input$ request)\n"
469  " throws com.google.protobuf.ServiceException");
470 }
471 
472 } // namespace java
473 } // namespace compiler
474 } // namespace protobuf
475 } // namespace google
google::protobuf::compiler::java::UnderscoresToCamelCase
std::string UnderscoresToCamelCase(const std::string &input, bool cap_next_letter)
Definition: java_helpers.cc:159
google::protobuf::io::Printer::Print
void Print(const std::map< std::string, std::string > &variables, const char *text)
Definition: printer.cc:112
context_
MockGeneratorContext context_
Definition: csharp_bootstrap_unittest.cc:125
google::protobuf::compiler::java::ImmutableServiceGenerator::GenerateMethodSignature
void GenerateMethodSignature(io::Printer *printer, const MethodDescriptor *method, IsAbstract is_abstract)
Definition: java_service.cc:443
google::protobuf::compiler::java::MaybePrintGeneratedAnnotation
void MaybePrintGeneratedAnnotation(Context *context, io::Printer *printer, Descriptor *descriptor, bool immutable, const std::string &suffix="")
Definition: java_helpers.h:187
google::protobuf::compiler::java::ServiceGenerator
Definition: java_service.h:60
google::protobuf::compiler::java::ServiceGenerator::IS_ABSTRACT
@ IS_ABSTRACT
Definition: java_service.h:68
java_name_resolver.h
java_doc_comment.h
google::protobuf::compiler::java::ImmutableServiceGenerator::GetOutput
std::string GetOutput(const MethodDescriptor *method)
Definition: java_service.cc:187
java_helpers.h
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: strutil.cc:1480
java_service.h
google::protobuf::ServiceDescriptor::index
int index() const
Definition: src/google/protobuf/descriptor.h:2123
google::protobuf::compiler::java::ImmutableServiceGenerator::name_resolver_
ClassNameResolver * name_resolver_
Definition: java_service.h:130
google::protobuf::ServiceDescriptor::full_name
const std::string & full_name() const
google::protobuf::compiler::java::Context
Definition: java_context.h:65
google::protobuf::compiler::java::ServiceGenerator::ServiceGenerator
ServiceGenerator(const ServiceDescriptor *descriptor)
Definition: java_service.cc:50
google::protobuf::compiler::java::ImmutableServiceGenerator::context_
Context * context_
Definition: java_service.h:129
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
google::protobuf::compiler::java::ClassNameResolver::GetImmutableClassName
std::string GetImmutableClassName(const DescriptorType *descriptor)
Definition: java_name_resolver.h:88
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::ServiceDescriptor::name
const std::string & name() const
google::protobuf::compiler::java::ServiceGenerator::~ServiceGenerator
virtual ~ServiceGenerator()
Definition: java_service.cc:53
google::protobuf::compiler::java::ImmutableServiceGenerator::GenerateNewReflectiveBlockingServiceMethod
void GenerateNewReflectiveBlockingServiceMethod(io::Printer *printer)
Definition: java_service.cc:157
google::protobuf::ServiceDescriptor
Definition: src/google/protobuf/descriptor.h:1152
google::protobuf::io::Printer::Indent
void Indent()
Definition: printer.cc:185
strutil.h
google::protobuf::compiler::java::ImmutableServiceGenerator::GenerateGetPrototype
void GenerateGetPrototype(RequestOrResponse which, io::Printer *printer)
Definition: java_service.cc:282
google::protobuf::compiler::java::ImmutableServiceGenerator::GenerateCallBlockingMethod
void GenerateCallBlockingMethod(io::Printer *printer)
Definition: java_service.cc:239
google::protobuf::compiler::java::ServiceGenerator::RESPONSE
@ RESPONSE
Definition: java_service.h:67
printer.h
google::protobuf::compiler::java::ServiceGenerator::descriptor_
const ServiceDescriptor * descriptor_
Definition: java_service.h:71
google::protobuf::compiler::java::ImmutableServiceGenerator::GenerateInterface
void GenerateInterface(io::Printer *printer)
Definition: java_service.cc:122
google::protobuf::compiler::java::ImmutableServiceGenerator::GenerateNewReflectiveServiceMethod
void GenerateNewReflectiveServiceMethod(io::Printer *printer)
Definition: java_service.cc:130
google::protobuf::compiler::java::ImmutableServiceGenerator::GenerateBlockingMethodSignature
void GenerateBlockingMethodSignature(io::Printer *printer, const MethodDescriptor *method)
Definition: java_service.cc:458
google::protobuf::compiler::java::ImmutableServiceGenerator::GenerateBlockingStub
void GenerateBlockingStub(io::Printer *printer)
Definition: java_service.cc:383
google::protobuf::ServiceDescriptor::file
const FileDescriptor * file() const
google::protobuf::MethodDescriptor
Definition: src/google/protobuf/descriptor.h:1234
google::protobuf::compiler::java::WriteServiceDocComment
void WriteServiceDocComment(io::Printer *printer, const ServiceDescriptor *service)
Definition: java_doc_comment.cc:210
google::protobuf::io::Printer
Definition: printer.h:181
i
int i
Definition: gmock-matchers_test.cc:764
java
google::protobuf::compiler::java::ServiceGenerator::RequestOrResponse
RequestOrResponse
Definition: java_service.h:67
which
int which
Definition: cJSON.h:253
google::protobuf::compiler::java::ImmutableServiceGenerator::Generate
virtual void Generate(io::Printer *printer)
Definition: java_service.cc:64
google::protobuf::compiler::java::ImmutableServiceGenerator::GenerateCallMethod
void GenerateCallMethod(io::Printer *printer)
Definition: java_service.cc:192
google::protobuf::compiler::java::ImmutableServiceGenerator::ImmutableServiceGenerator
ImmutableServiceGenerator(const ServiceDescriptor *descriptor, Context *context)
Definition: java_service.cc:56
google::protobuf::ServiceDescriptor::method_count
int method_count() const
google::protobuf::compiler::java::ServiceGenerator::REQUEST
@ REQUEST
Definition: java_service.h:67
google::protobuf::compiler::java::WriteMethodDocComment
void WriteMethodDocComment(io::Printer *printer, const MethodDescriptor *method)
Definition: java_doc_comment.cc:220
google::protobuf::compiler::java::ServiceGenerator::IS_CONCRETE
@ IS_CONCRETE
Definition: java_service.h:68
java_context.h
google::protobuf::compiler::java::ImmutableServiceGenerator::GenerateGetDescriptorForType
void GenerateGetDescriptorForType(io::Printer *printer)
Definition: java_service.cc:113
google::protobuf::ServiceDescriptor::method
const MethodDescriptor * method(int index) const
descriptor_
const Descriptor * descriptor_
Definition: field_comparator_test.cc:56
google::protobuf::compiler::java::ImmutableServiceGenerator::GenerateAbstractMethods
void GenerateAbstractMethods(io::Printer *printer)
Definition: java_service.cc:178
google::protobuf::compiler::java::ServiceGenerator::IsAbstract
IsAbstract
Definition: java_service.h:68
google::protobuf::compiler::java::IsOwnFile
bool IsOwnFile(const Descriptor *descriptor, bool immutable)
Definition: java_helpers.h:166
google::protobuf::io::Printer::Outdent
void Outdent()
Definition: printer.cc:187
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::compiler::java::ImmutableServiceGenerator::~ImmutableServiceGenerator
virtual ~ImmutableServiceGenerator()
Definition: java_service.cc:62
google::protobuf::method
const Descriptor::ReservedRange const EnumValueDescriptor method
Definition: src/google/protobuf/descriptor.h:1973
google::protobuf::compiler::java::ImmutableServiceGenerator::GenerateStub
void GenerateStub(io::Printer *printer)
Definition: java_service.cc:328


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:55