/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/compiler/java/java_service.cc

http://github.com/tomahawk-player/tomahawk · C++ · 444 lines · 349 code · 57 blank · 38 comment · 11 complexity · 34f6d8971049661d25b888b88a360ced MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <google/protobuf/compiler/java/java_service.h>
  34. #include <google/protobuf/compiler/java/java_helpers.h>
  35. #include <google/protobuf/io/printer.h>
  36. #include <google/protobuf/descriptor.pb.h>
  37. #include <google/protobuf/stubs/strutil.h>
  38. namespace google {
  39. namespace protobuf {
  40. namespace compiler {
  41. namespace java {
  42. ServiceGenerator::ServiceGenerator(const ServiceDescriptor* descriptor)
  43. : descriptor_(descriptor) {}
  44. ServiceGenerator::~ServiceGenerator() {}
  45. void ServiceGenerator::Generate(io::Printer* printer) {
  46. bool is_own_file = descriptor_->file()->options().java_multiple_files();
  47. printer->Print(
  48. "public $static$ abstract class $classname$\n"
  49. " implements com.google.protobuf.Service {\n",
  50. "static", is_own_file ? "" : "static",
  51. "classname", descriptor_->name());
  52. printer->Indent();
  53. printer->Print(
  54. "protected $classname$() {}\n\n",
  55. "classname", descriptor_->name());
  56. GenerateInterface(printer);
  57. GenerateNewReflectiveServiceMethod(printer);
  58. GenerateNewReflectiveBlockingServiceMethod(printer);
  59. GenerateAbstractMethods(printer);
  60. // Generate getDescriptor() and getDescriptorForType().
  61. printer->Print(
  62. "public static final\n"
  63. " com.google.protobuf.Descriptors.ServiceDescriptor\n"
  64. " getDescriptor() {\n"
  65. " return $file$.getDescriptor().getServices().get($index$);\n"
  66. "}\n",
  67. "file", ClassName(descriptor_->file()),
  68. "index", SimpleItoa(descriptor_->index()));
  69. GenerateGetDescriptorForType(printer);
  70. // Generate more stuff.
  71. GenerateCallMethod(printer);
  72. GenerateGetPrototype(REQUEST, printer);
  73. GenerateGetPrototype(RESPONSE, printer);
  74. GenerateStub(printer);
  75. GenerateBlockingStub(printer);
  76. printer->Outdent();
  77. printer->Print("}\n\n");
  78. }
  79. void ServiceGenerator::GenerateGetDescriptorForType(io::Printer* printer) {
  80. printer->Print(
  81. "public final com.google.protobuf.Descriptors.ServiceDescriptor\n"
  82. " getDescriptorForType() {\n"
  83. " return getDescriptor();\n"
  84. "}\n");
  85. }
  86. void ServiceGenerator::GenerateInterface(io::Printer* printer) {
  87. printer->Print("public interface Interface {\n");
  88. printer->Indent();
  89. GenerateAbstractMethods(printer);
  90. printer->Outdent();
  91. printer->Print("}\n\n");
  92. }
  93. void ServiceGenerator::GenerateNewReflectiveServiceMethod(
  94. io::Printer* printer) {
  95. printer->Print(
  96. "public static com.google.protobuf.Service newReflectiveService(\n"
  97. " final Interface impl) {\n"
  98. " return new $classname$() {\n",
  99. "classname", descriptor_->name());
  100. printer->Indent();
  101. printer->Indent();
  102. for (int i = 0; i < descriptor_->method_count(); i++) {
  103. const MethodDescriptor* method = descriptor_->method(i);
  104. printer->Print("@java.lang.Override\n");
  105. GenerateMethodSignature(printer, method, IS_CONCRETE);
  106. printer->Print(
  107. " {\n"
  108. " impl.$method$(controller, request, done);\n"
  109. "}\n\n",
  110. "method", UnderscoresToCamelCase(method));
  111. }
  112. printer->Outdent();
  113. printer->Print("};\n");
  114. printer->Outdent();
  115. printer->Print("}\n\n");
  116. }
  117. void ServiceGenerator::GenerateNewReflectiveBlockingServiceMethod(
  118. io::Printer* printer) {
  119. printer->Print(
  120. "public static com.google.protobuf.BlockingService\n"
  121. " newReflectiveBlockingService(final BlockingInterface impl) {\n"
  122. " return new com.google.protobuf.BlockingService() {\n");
  123. printer->Indent();
  124. printer->Indent();
  125. GenerateGetDescriptorForType(printer);
  126. GenerateCallBlockingMethod(printer);
  127. GenerateGetPrototype(REQUEST, printer);
  128. GenerateGetPrototype(RESPONSE, printer);
  129. printer->Outdent();
  130. printer->Print("};\n");
  131. printer->Outdent();
  132. printer->Print("}\n\n");
  133. }
  134. void ServiceGenerator::GenerateAbstractMethods(io::Printer* printer) {
  135. for (int i = 0; i < descriptor_->method_count(); i++) {
  136. const MethodDescriptor* method = descriptor_->method(i);
  137. GenerateMethodSignature(printer, method, IS_ABSTRACT);
  138. printer->Print(";\n\n");
  139. }
  140. }
  141. void ServiceGenerator::GenerateCallMethod(io::Printer* printer) {
  142. printer->Print(
  143. "\n"
  144. "public final void callMethod(\n"
  145. " com.google.protobuf.Descriptors.MethodDescriptor method,\n"
  146. " com.google.protobuf.RpcController controller,\n"
  147. " com.google.protobuf.Message request,\n"
  148. " com.google.protobuf.RpcCallback<\n"
  149. " com.google.protobuf.Message> done) {\n"
  150. " if (method.getService() != getDescriptor()) {\n"
  151. " throw new java.lang.IllegalArgumentException(\n"
  152. " \"Service.callMethod() given method descriptor for wrong \" +\n"
  153. " \"service type.\");\n"
  154. " }\n"
  155. " switch(method.getIndex()) {\n");
  156. printer->Indent();
  157. printer->Indent();
  158. for (int i = 0; i < descriptor_->method_count(); i++) {
  159. const MethodDescriptor* method = descriptor_->method(i);
  160. map<string, string> vars;
  161. vars["index"] = SimpleItoa(i);
  162. vars["method"] = UnderscoresToCamelCase(method);
  163. vars["input"] = ClassName(method->input_type());
  164. vars["output"] = ClassName(method->output_type());
  165. printer->Print(vars,
  166. "case $index$:\n"
  167. " this.$method$(controller, ($input$)request,\n"
  168. " com.google.protobuf.RpcUtil.<$output$>specializeCallback(\n"
  169. " done));\n"
  170. " return;\n");
  171. }
  172. printer->Print(
  173. "default:\n"
  174. " throw new java.lang.AssertionError(\"Can't get here.\");\n");
  175. printer->Outdent();
  176. printer->Outdent();
  177. printer->Print(
  178. " }\n"
  179. "}\n"
  180. "\n");
  181. }
  182. void ServiceGenerator::GenerateCallBlockingMethod(io::Printer* printer) {
  183. printer->Print(
  184. "\n"
  185. "public final com.google.protobuf.Message callBlockingMethod(\n"
  186. " com.google.protobuf.Descriptors.MethodDescriptor method,\n"
  187. " com.google.protobuf.RpcController controller,\n"
  188. " com.google.protobuf.Message request)\n"
  189. " throws com.google.protobuf.ServiceException {\n"
  190. " if (method.getService() != getDescriptor()) {\n"
  191. " throw new java.lang.IllegalArgumentException(\n"
  192. " \"Service.callBlockingMethod() given method descriptor for \" +\n"
  193. " \"wrong service type.\");\n"
  194. " }\n"
  195. " switch(method.getIndex()) {\n");
  196. printer->Indent();
  197. printer->Indent();
  198. for (int i = 0; i < descriptor_->method_count(); i++) {
  199. const MethodDescriptor* method = descriptor_->method(i);
  200. map<string, string> vars;
  201. vars["index"] = SimpleItoa(i);
  202. vars["method"] = UnderscoresToCamelCase(method);
  203. vars["input"] = ClassName(method->input_type());
  204. vars["output"] = ClassName(method->output_type());
  205. printer->Print(vars,
  206. "case $index$:\n"
  207. " return impl.$method$(controller, ($input$)request);\n");
  208. }
  209. printer->Print(
  210. "default:\n"
  211. " throw new java.lang.AssertionError(\"Can't get here.\");\n");
  212. printer->Outdent();
  213. printer->Outdent();
  214. printer->Print(
  215. " }\n"
  216. "}\n"
  217. "\n");
  218. }
  219. void ServiceGenerator::GenerateGetPrototype(RequestOrResponse which,
  220. io::Printer* printer) {
  221. /*
  222. * TODO(cpovirk): The exception message says "Service.foo" when it may be
  223. * "BlockingService.foo." Consider fixing.
  224. */
  225. printer->Print(
  226. "public final com.google.protobuf.Message\n"
  227. " get$request_or_response$Prototype(\n"
  228. " com.google.protobuf.Descriptors.MethodDescriptor method) {\n"
  229. " if (method.getService() != getDescriptor()) {\n"
  230. " throw new java.lang.IllegalArgumentException(\n"
  231. " \"Service.get$request_or_response$Prototype() given method \" +\n"
  232. " \"descriptor for wrong service type.\");\n"
  233. " }\n"
  234. " switch(method.getIndex()) {\n",
  235. "request_or_response", (which == REQUEST) ? "Request" : "Response");
  236. printer->Indent();
  237. printer->Indent();
  238. for (int i = 0; i < descriptor_->method_count(); i++) {
  239. const MethodDescriptor* method = descriptor_->method(i);
  240. map<string, string> vars;
  241. vars["index"] = SimpleItoa(i);
  242. vars["type"] = ClassName(
  243. (which == REQUEST) ? method->input_type() : method->output_type());
  244. printer->Print(vars,
  245. "case $index$:\n"
  246. " return $type$.getDefaultInstance();\n");
  247. }
  248. printer->Print(
  249. "default:\n"
  250. " throw new java.lang.AssertionError(\"Can't get here.\");\n");
  251. printer->Outdent();
  252. printer->Outdent();
  253. printer->Print(
  254. " }\n"
  255. "}\n"
  256. "\n");
  257. }
  258. void ServiceGenerator::GenerateStub(io::Printer* printer) {
  259. printer->Print(
  260. "public static Stub newStub(\n"
  261. " com.google.protobuf.RpcChannel channel) {\n"
  262. " return new Stub(channel);\n"
  263. "}\n"
  264. "\n"
  265. "public static final class Stub extends $classname$ implements Interface {"
  266. "\n",
  267. "classname", ClassName(descriptor_));
  268. printer->Indent();
  269. printer->Print(
  270. "private Stub(com.google.protobuf.RpcChannel channel) {\n"
  271. " this.channel = channel;\n"
  272. "}\n"
  273. "\n"
  274. "private final com.google.protobuf.RpcChannel channel;\n"
  275. "\n"
  276. "public com.google.protobuf.RpcChannel getChannel() {\n"
  277. " return channel;\n"
  278. "}\n");
  279. for (int i = 0; i < descriptor_->method_count(); i++) {
  280. const MethodDescriptor* method = descriptor_->method(i);
  281. printer->Print("\n");
  282. GenerateMethodSignature(printer, method, IS_CONCRETE);
  283. printer->Print(" {\n");
  284. printer->Indent();
  285. map<string, string> vars;
  286. vars["index"] = SimpleItoa(i);
  287. vars["output"] = ClassName(method->output_type());
  288. printer->Print(vars,
  289. "channel.callMethod(\n"
  290. " getDescriptor().getMethods().get($index$),\n"
  291. " controller,\n"
  292. " request,\n"
  293. " $output$.getDefaultInstance(),\n"
  294. " com.google.protobuf.RpcUtil.generalizeCallback(\n"
  295. " done,\n"
  296. " $output$.class,\n"
  297. " $output$.getDefaultInstance()));\n");
  298. printer->Outdent();
  299. printer->Print("}\n");
  300. }
  301. printer->Outdent();
  302. printer->Print(
  303. "}\n"
  304. "\n");
  305. }
  306. void ServiceGenerator::GenerateBlockingStub(io::Printer* printer) {
  307. printer->Print(
  308. "public static BlockingInterface newBlockingStub(\n"
  309. " com.google.protobuf.BlockingRpcChannel channel) {\n"
  310. " return new BlockingStub(channel);\n"
  311. "}\n"
  312. "\n");
  313. printer->Print(
  314. "public interface BlockingInterface {");
  315. printer->Indent();
  316. for (int i = 0; i < descriptor_->method_count(); i++) {
  317. const MethodDescriptor* method = descriptor_->method(i);
  318. GenerateBlockingMethodSignature(printer, method);
  319. printer->Print(";\n");
  320. }
  321. printer->Outdent();
  322. printer->Print(
  323. "}\n"
  324. "\n");
  325. printer->Print(
  326. "private static final class BlockingStub implements BlockingInterface {\n");
  327. printer->Indent();
  328. printer->Print(
  329. "private BlockingStub(com.google.protobuf.BlockingRpcChannel channel) {\n"
  330. " this.channel = channel;\n"
  331. "}\n"
  332. "\n"
  333. "private final com.google.protobuf.BlockingRpcChannel channel;\n");
  334. for (int i = 0; i < descriptor_->method_count(); i++) {
  335. const MethodDescriptor* method = descriptor_->method(i);
  336. GenerateBlockingMethodSignature(printer, method);
  337. printer->Print(" {\n");
  338. printer->Indent();
  339. map<string, string> vars;
  340. vars["index"] = SimpleItoa(i);
  341. vars["output"] = ClassName(method->output_type());
  342. printer->Print(vars,
  343. "return ($output$) channel.callBlockingMethod(\n"
  344. " getDescriptor().getMethods().get($index$),\n"
  345. " controller,\n"
  346. " request,\n"
  347. " $output$.getDefaultInstance());\n");
  348. printer->Outdent();
  349. printer->Print(
  350. "}\n"
  351. "\n");
  352. }
  353. printer->Outdent();
  354. printer->Print("}\n");
  355. }
  356. void ServiceGenerator::GenerateMethodSignature(io::Printer* printer,
  357. const MethodDescriptor* method,
  358. IsAbstract is_abstract) {
  359. map<string, string> vars;
  360. vars["name"] = UnderscoresToCamelCase(method);
  361. vars["input"] = ClassName(method->input_type());
  362. vars["output"] = ClassName(method->output_type());
  363. vars["abstract"] = (is_abstract == IS_ABSTRACT) ? "abstract" : "";
  364. printer->Print(vars,
  365. "public $abstract$ void $name$(\n"
  366. " com.google.protobuf.RpcController controller,\n"
  367. " $input$ request,\n"
  368. " com.google.protobuf.RpcCallback<$output$> done)");
  369. }
  370. void ServiceGenerator::GenerateBlockingMethodSignature(
  371. io::Printer* printer,
  372. const MethodDescriptor* method) {
  373. map<string, string> vars;
  374. vars["method"] = UnderscoresToCamelCase(method);
  375. vars["input"] = ClassName(method->input_type());
  376. vars["output"] = ClassName(method->output_type());
  377. printer->Print(vars,
  378. "\n"
  379. "public $output$ $method$(\n"
  380. " com.google.protobuf.RpcController controller,\n"
  381. " $input$ request)\n"
  382. " throws com.google.protobuf.ServiceException");
  383. }
  384. } // namespace java
  385. } // namespace compiler
  386. } // namespace protobuf
  387. } // namespace google