OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 | 53 |
54 // =================================================================== | 54 // =================================================================== |
55 ImmutableServiceGenerator::ImmutableServiceGenerator( | 55 ImmutableServiceGenerator::ImmutableServiceGenerator( |
56 const ServiceDescriptor* descriptor, Context* context) | 56 const ServiceDescriptor* descriptor, Context* context) |
57 : ServiceGenerator(descriptor), context_(context), | 57 : ServiceGenerator(descriptor), context_(context), |
58 name_resolver_(context->GetNameResolver()) {} | 58 name_resolver_(context->GetNameResolver()) {} |
59 | 59 |
60 ImmutableServiceGenerator::~ImmutableServiceGenerator() {} | 60 ImmutableServiceGenerator::~ImmutableServiceGenerator() {} |
61 | 61 |
62 void ImmutableServiceGenerator::Generate(io::Printer* printer) { | 62 void ImmutableServiceGenerator::Generate(io::Printer* printer) { |
63 bool is_own_file = IsOwnFile(descriptor_, /* immutable = */ true); | 63 bool is_own_file = |
| 64 MultipleJavaFiles(descriptor_->file(), /* immutable = */ true); |
64 WriteServiceDocComment(printer, descriptor_); | 65 WriteServiceDocComment(printer, descriptor_); |
65 MaybePrintGeneratedAnnotation(context_, printer, descriptor_, | |
66 /* immutable = */ true); | |
67 printer->Print( | 66 printer->Print( |
68 "public $static$ abstract class $classname$\n" | 67 "public $static$ abstract class $classname$\n" |
69 " implements com.google.protobuf.Service {\n", | 68 " implements com.google.protobuf.Service {\n", |
70 "static", is_own_file ? "" : "static", | 69 "static", is_own_file ? "" : "static", |
71 "classname", descriptor_->name()); | 70 "classname", descriptor_->name()); |
72 printer->Indent(); | 71 printer->Indent(); |
73 | 72 |
74 printer->Print( | 73 printer->Print( |
75 "protected $classname$() {}\n\n", | 74 "protected $classname$() {}\n\n", |
76 "classname", descriptor_->name()); | 75 "classname", descriptor_->name()); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 | 176 |
178 void ImmutableServiceGenerator::GenerateAbstractMethods(io::Printer* printer) { | 177 void ImmutableServiceGenerator::GenerateAbstractMethods(io::Printer* printer) { |
179 for (int i = 0; i < descriptor_->method_count(); i++) { | 178 for (int i = 0; i < descriptor_->method_count(); i++) { |
180 const MethodDescriptor* method = descriptor_->method(i); | 179 const MethodDescriptor* method = descriptor_->method(i); |
181 WriteMethodDocComment(printer, method); | 180 WriteMethodDocComment(printer, method); |
182 GenerateMethodSignature(printer, method, IS_ABSTRACT); | 181 GenerateMethodSignature(printer, method, IS_ABSTRACT); |
183 printer->Print(";\n\n"); | 182 printer->Print(";\n\n"); |
184 } | 183 } |
185 } | 184 } |
186 | 185 |
187 string ImmutableServiceGenerator::GetOutput(const MethodDescriptor* method) { | |
188 return name_resolver_->GetImmutableClassName(method->output_type()); | |
189 } | |
190 | |
191 void ImmutableServiceGenerator::GenerateCallMethod(io::Printer* printer) { | 186 void ImmutableServiceGenerator::GenerateCallMethod(io::Printer* printer) { |
192 printer->Print( | 187 printer->Print( |
193 "\n" | 188 "\n" |
194 "public final void callMethod(\n" | 189 "public final void callMethod(\n" |
195 " com.google.protobuf.Descriptors.MethodDescriptor method,\n" | 190 " com.google.protobuf.Descriptors.MethodDescriptor method,\n" |
196 " com.google.protobuf.RpcController controller,\n" | 191 " com.google.protobuf.RpcController controller,\n" |
197 " com.google.protobuf.Message request,\n" | 192 " com.google.protobuf.Message request,\n" |
198 " com.google.protobuf.RpcCallback<\n" | 193 " com.google.protobuf.RpcCallback<\n" |
199 " com.google.protobuf.Message> done) {\n" | 194 " com.google.protobuf.Message> done) {\n" |
200 " if (method.getService() != getDescriptor()) {\n" | 195 " if (method.getService() != getDescriptor()) {\n" |
201 " throw new java.lang.IllegalArgumentException(\n" | 196 " throw new java.lang.IllegalArgumentException(\n" |
202 " \"Service.callMethod() given method descriptor for wrong \" +\n" | 197 " \"Service.callMethod() given method descriptor for wrong \" +\n" |
203 " \"service type.\");\n" | 198 " \"service type.\");\n" |
204 " }\n" | 199 " }\n" |
205 " switch(method.getIndex()) {\n"); | 200 " switch(method.getIndex()) {\n"); |
206 printer->Indent(); | 201 printer->Indent(); |
207 printer->Indent(); | 202 printer->Indent(); |
208 | 203 |
209 for (int i = 0; i < descriptor_->method_count(); i++) { | 204 for (int i = 0; i < descriptor_->method_count(); i++) { |
210 const MethodDescriptor* method = descriptor_->method(i); | 205 const MethodDescriptor* method = descriptor_->method(i); |
211 std::map<string, string> vars; | 206 map<string, string> vars; |
212 vars["index"] = SimpleItoa(i); | 207 vars["index"] = SimpleItoa(i); |
213 vars["method"] = UnderscoresToCamelCase(method); | 208 vars["method"] = UnderscoresToCamelCase(method); |
214 vars["input"] = name_resolver_->GetImmutableClassName( | 209 vars["input"] = name_resolver_->GetImmutableClassName( |
215 method->input_type()); | 210 method->input_type()); |
216 vars["output"] = GetOutput(method); | 211 vars["output"] = name_resolver_->GetImmutableClassName( |
| 212 method->output_type()); |
217 printer->Print(vars, | 213 printer->Print(vars, |
218 "case $index$:\n" | 214 "case $index$:\n" |
219 " this.$method$(controller, ($input$)request,\n" | 215 " this.$method$(controller, ($input$)request,\n" |
220 " com.google.protobuf.RpcUtil.<$output$>specializeCallback(\n" | 216 " com.google.protobuf.RpcUtil.<$output$>specializeCallback(\n" |
221 " done));\n" | 217 " done));\n" |
222 " return;\n"); | 218 " return;\n"); |
223 } | 219 } |
224 | 220 |
225 printer->Print( | 221 printer->Print( |
226 "default:\n" | 222 "default:\n" |
(...skipping 21 matching lines...) Expand all Loading... |
248 " throw new java.lang.IllegalArgumentException(\n" | 244 " throw new java.lang.IllegalArgumentException(\n" |
249 " \"Service.callBlockingMethod() given method descriptor for \" +\n" | 245 " \"Service.callBlockingMethod() given method descriptor for \" +\n" |
250 " \"wrong service type.\");\n" | 246 " \"wrong service type.\");\n" |
251 " }\n" | 247 " }\n" |
252 " switch(method.getIndex()) {\n"); | 248 " switch(method.getIndex()) {\n"); |
253 printer->Indent(); | 249 printer->Indent(); |
254 printer->Indent(); | 250 printer->Indent(); |
255 | 251 |
256 for (int i = 0; i < descriptor_->method_count(); i++) { | 252 for (int i = 0; i < descriptor_->method_count(); i++) { |
257 const MethodDescriptor* method = descriptor_->method(i); | 253 const MethodDescriptor* method = descriptor_->method(i); |
258 std::map<string, string> vars; | 254 map<string, string> vars; |
259 vars["index"] = SimpleItoa(i); | 255 vars["index"] = SimpleItoa(i); |
260 vars["method"] = UnderscoresToCamelCase(method); | 256 vars["method"] = UnderscoresToCamelCase(method); |
261 vars["input"] = name_resolver_->GetImmutableClassName( | 257 vars["input"] = name_resolver_->GetImmutableClassName( |
262 method->input_type()); | 258 method->input_type()); |
263 vars["output"] = GetOutput(method); | 259 vars["output"] = name_resolver_->GetImmutableClassName( |
| 260 method->output_type()); |
264 printer->Print(vars, | 261 printer->Print(vars, |
265 "case $index$:\n" | 262 "case $index$:\n" |
266 " return impl.$method$(controller, ($input$)request);\n"); | 263 " return impl.$method$(controller, ($input$)request);\n"); |
267 } | 264 } |
268 | 265 |
269 printer->Print( | 266 printer->Print( |
270 "default:\n" | 267 "default:\n" |
271 " throw new java.lang.AssertionError(\"Can't get here.\");\n"); | 268 " throw new java.lang.AssertionError(\"Can't get here.\");\n"); |
272 | 269 |
273 printer->Outdent(); | 270 printer->Outdent(); |
(...skipping 20 matching lines...) Expand all Loading... |
294 " \"Service.get$request_or_response$Prototype() given method \" +\n" | 291 " \"Service.get$request_or_response$Prototype() given method \" +\n" |
295 " \"descriptor for wrong service type.\");\n" | 292 " \"descriptor for wrong service type.\");\n" |
296 " }\n" | 293 " }\n" |
297 " switch(method.getIndex()) {\n", | 294 " switch(method.getIndex()) {\n", |
298 "request_or_response", (which == REQUEST) ? "Request" : "Response"); | 295 "request_or_response", (which == REQUEST) ? "Request" : "Response"); |
299 printer->Indent(); | 296 printer->Indent(); |
300 printer->Indent(); | 297 printer->Indent(); |
301 | 298 |
302 for (int i = 0; i < descriptor_->method_count(); i++) { | 299 for (int i = 0; i < descriptor_->method_count(); i++) { |
303 const MethodDescriptor* method = descriptor_->method(i); | 300 const MethodDescriptor* method = descriptor_->method(i); |
304 std::map<string, string> vars; | 301 map<string, string> vars; |
305 vars["index"] = SimpleItoa(i); | 302 vars["index"] = SimpleItoa(i); |
306 vars["type"] = name_resolver_->GetImmutableClassName( | 303 vars["type"] = name_resolver_->GetImmutableClassName( |
307 (which == REQUEST) ? method->input_type() : method->output_type()); | 304 (which == REQUEST) ? method->input_type() : method->output_type()); |
308 printer->Print(vars, | 305 printer->Print(vars, |
309 "case $index$:\n" | 306 "case $index$:\n" |
310 " return $type$.getDefaultInstance();\n"); | 307 " return $type$.getDefaultInstance();\n"); |
311 } | 308 } |
312 | 309 |
313 printer->Print( | 310 printer->Print( |
314 "default:\n" | 311 "default:\n" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 " return channel;\n" | 343 " return channel;\n" |
347 "}\n"); | 344 "}\n"); |
348 | 345 |
349 for (int i = 0; i < descriptor_->method_count(); i++) { | 346 for (int i = 0; i < descriptor_->method_count(); i++) { |
350 const MethodDescriptor* method = descriptor_->method(i); | 347 const MethodDescriptor* method = descriptor_->method(i); |
351 printer->Print("\n"); | 348 printer->Print("\n"); |
352 GenerateMethodSignature(printer, method, IS_CONCRETE); | 349 GenerateMethodSignature(printer, method, IS_CONCRETE); |
353 printer->Print(" {\n"); | 350 printer->Print(" {\n"); |
354 printer->Indent(); | 351 printer->Indent(); |
355 | 352 |
356 std::map<string, string> vars; | 353 map<string, string> vars; |
357 vars["index"] = SimpleItoa(i); | 354 vars["index"] = SimpleItoa(i); |
358 vars["output"] = GetOutput(method); | 355 vars["output"] = name_resolver_->GetImmutableClassName( |
| 356 method->output_type()); |
359 printer->Print(vars, | 357 printer->Print(vars, |
360 "channel.callMethod(\n" | 358 "channel.callMethod(\n" |
361 " getDescriptor().getMethods().get($index$),\n" | 359 " getDescriptor().getMethods().get($index$),\n" |
362 " controller,\n" | 360 " controller,\n" |
363 " request,\n" | 361 " request,\n" |
364 " $output$.getDefaultInstance(),\n" | 362 " $output$.getDefaultInstance(),\n" |
365 " com.google.protobuf.RpcUtil.generalizeCallback(\n" | 363 " com.google.protobuf.RpcUtil.generalizeCallback(\n" |
366 " done,\n" | 364 " done,\n" |
367 " $output$.class,\n" | 365 " $output$.class,\n" |
368 " $output$.getDefaultInstance()));\n"); | 366 " $output$.getDefaultInstance()));\n"); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 "}\n" | 408 "}\n" |
411 "\n" | 409 "\n" |
412 "private final com.google.protobuf.BlockingRpcChannel channel;\n"); | 410 "private final com.google.protobuf.BlockingRpcChannel channel;\n"); |
413 | 411 |
414 for (int i = 0; i < descriptor_->method_count(); i++) { | 412 for (int i = 0; i < descriptor_->method_count(); i++) { |
415 const MethodDescriptor* method = descriptor_->method(i); | 413 const MethodDescriptor* method = descriptor_->method(i); |
416 GenerateBlockingMethodSignature(printer, method); | 414 GenerateBlockingMethodSignature(printer, method); |
417 printer->Print(" {\n"); | 415 printer->Print(" {\n"); |
418 printer->Indent(); | 416 printer->Indent(); |
419 | 417 |
420 std::map<string, string> vars; | 418 map<string, string> vars; |
421 vars["index"] = SimpleItoa(i); | 419 vars["index"] = SimpleItoa(i); |
422 vars["output"] = GetOutput(method); | 420 vars["output"] = name_resolver_->GetImmutableClassName( |
| 421 method->output_type()); |
423 printer->Print(vars, | 422 printer->Print(vars, |
424 "return ($output$) channel.callBlockingMethod(\n" | 423 "return ($output$) channel.callBlockingMethod(\n" |
425 " getDescriptor().getMethods().get($index$),\n" | 424 " getDescriptor().getMethods().get($index$),\n" |
426 " controller,\n" | 425 " controller,\n" |
427 " request,\n" | 426 " request,\n" |
428 " $output$.getDefaultInstance());\n"); | 427 " $output$.getDefaultInstance());\n"); |
429 | 428 |
430 printer->Outdent(); | 429 printer->Outdent(); |
431 printer->Print( | 430 printer->Print( |
432 "}\n" | 431 "}\n" |
433 "\n"); | 432 "\n"); |
434 } | 433 } |
435 | 434 |
436 printer->Outdent(); | 435 printer->Outdent(); |
437 printer->Print("}\n"); | 436 printer->Print("}\n"); |
438 } | 437 } |
439 | 438 |
440 void ImmutableServiceGenerator::GenerateMethodSignature(io::Printer* printer, | 439 void ImmutableServiceGenerator::GenerateMethodSignature(io::Printer* printer, |
441 const MethodDescriptor* method, | 440 const MethodDescriptor* method, |
442 IsAbstract is_abstract) { | 441 IsAbstract is_abstract) { |
443 std::map<string, string> vars; | 442 map<string, string> vars; |
444 vars["name"] = UnderscoresToCamelCase(method); | 443 vars["name"] = UnderscoresToCamelCase(method); |
445 vars["input"] = name_resolver_->GetImmutableClassName(method->input_type()); | 444 vars["input"] = name_resolver_->GetImmutableClassName(method->input_type()); |
446 vars["output"] = GetOutput(method); | 445 vars["output"] = name_resolver_->GetImmutableClassName(method->output_type()); |
447 vars["abstract"] = (is_abstract == IS_ABSTRACT) ? "abstract" : ""; | 446 vars["abstract"] = (is_abstract == IS_ABSTRACT) ? "abstract" : ""; |
448 printer->Print(vars, | 447 printer->Print(vars, |
449 "public $abstract$ void $name$(\n" | 448 "public $abstract$ void $name$(\n" |
450 " com.google.protobuf.RpcController controller,\n" | 449 " com.google.protobuf.RpcController controller,\n" |
451 " $input$ request,\n" | 450 " $input$ request,\n" |
452 " com.google.protobuf.RpcCallback<$output$> done)"); | 451 " com.google.protobuf.RpcCallback<$output$> done)"); |
453 } | 452 } |
454 | 453 |
455 void ImmutableServiceGenerator::GenerateBlockingMethodSignature( | 454 void ImmutableServiceGenerator::GenerateBlockingMethodSignature( |
456 io::Printer* printer, | 455 io::Printer* printer, |
457 const MethodDescriptor* method) { | 456 const MethodDescriptor* method) { |
458 std::map<string, string> vars; | 457 map<string, string> vars; |
459 vars["method"] = UnderscoresToCamelCase(method); | 458 vars["method"] = UnderscoresToCamelCase(method); |
460 vars["input"] = name_resolver_->GetImmutableClassName(method->input_type()); | 459 vars["input"] = name_resolver_->GetImmutableClassName(method->input_type()); |
461 vars["output"] = GetOutput(method); | 460 vars["output"] = name_resolver_->GetImmutableClassName(method->output_type()); |
462 printer->Print(vars, | 461 printer->Print(vars, |
463 "\n" | 462 "\n" |
464 "public $output$ $method$(\n" | 463 "public $output$ $method$(\n" |
465 " com.google.protobuf.RpcController controller,\n" | 464 " com.google.protobuf.RpcController controller,\n" |
466 " $input$ request)\n" | 465 " $input$ request)\n" |
467 " throws com.google.protobuf.ServiceException"); | 466 " throws com.google.protobuf.ServiceException"); |
468 } | 467 } |
469 | 468 |
470 } // namespace java | 469 } // namespace java |
471 } // namespace compiler | 470 } // namespace compiler |
472 } // namespace protobuf | 471 } // namespace protobuf |
473 } // namespace google | 472 } // namespace google |
OLD | NEW |