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

http://github.com/tomahawk-player/tomahawk · C++ · 216 lines · 159 code · 17 blank · 40 comment · 14 complexity · 5af5902c72d6c0b18066ec4eb95c6014 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_extension.h>
  34. #include <google/protobuf/compiler/java/java_helpers.h>
  35. #include <google/protobuf/stubs/strutil.h>
  36. #include <google/protobuf/io/printer.h>
  37. namespace google {
  38. namespace protobuf {
  39. namespace compiler {
  40. namespace java {
  41. namespace {
  42. const char* TypeName(FieldDescriptor::Type field_type) {
  43. switch (field_type) {
  44. case FieldDescriptor::TYPE_INT32 : return "INT32";
  45. case FieldDescriptor::TYPE_UINT32 : return "UINT32";
  46. case FieldDescriptor::TYPE_SINT32 : return "SINT32";
  47. case FieldDescriptor::TYPE_FIXED32 : return "FIXED32";
  48. case FieldDescriptor::TYPE_SFIXED32: return "SFIXED32";
  49. case FieldDescriptor::TYPE_INT64 : return "INT64";
  50. case FieldDescriptor::TYPE_UINT64 : return "UINT64";
  51. case FieldDescriptor::TYPE_SINT64 : return "SINT64";
  52. case FieldDescriptor::TYPE_FIXED64 : return "FIXED64";
  53. case FieldDescriptor::TYPE_SFIXED64: return "SFIXED64";
  54. case FieldDescriptor::TYPE_FLOAT : return "FLOAT";
  55. case FieldDescriptor::TYPE_DOUBLE : return "DOUBLE";
  56. case FieldDescriptor::TYPE_BOOL : return "BOOL";
  57. case FieldDescriptor::TYPE_STRING : return "STRING";
  58. case FieldDescriptor::TYPE_BYTES : return "BYTES";
  59. case FieldDescriptor::TYPE_ENUM : return "ENUM";
  60. case FieldDescriptor::TYPE_GROUP : return "GROUP";
  61. case FieldDescriptor::TYPE_MESSAGE : return "MESSAGE";
  62. // No default because we want the compiler to complain if any new
  63. // types are added.
  64. }
  65. GOOGLE_LOG(FATAL) << "Can't get here.";
  66. return NULL;
  67. }
  68. }
  69. ExtensionGenerator::ExtensionGenerator(const FieldDescriptor* descriptor)
  70. : descriptor_(descriptor) {
  71. if (descriptor_->extension_scope() != NULL) {
  72. scope_ = ClassName(descriptor_->extension_scope());
  73. } else {
  74. scope_ = ClassName(descriptor_->file());
  75. }
  76. }
  77. ExtensionGenerator::~ExtensionGenerator() {}
  78. // Initializes the vars referenced in the generated code templates.
  79. void InitTemplateVars(const FieldDescriptor* descriptor,
  80. const string& scope,
  81. map<string, string>* vars_pointer) {
  82. map<string, string> &vars = *vars_pointer;
  83. vars["scope"] = scope;
  84. vars["name"] = UnderscoresToCamelCase(descriptor);
  85. vars["containing_type"] = ClassName(descriptor->containing_type());
  86. vars["number"] = SimpleItoa(descriptor->number());
  87. vars["constant_name"] = FieldConstantName(descriptor);
  88. vars["index"] = SimpleItoa(descriptor->index());
  89. vars["default"] =
  90. descriptor->is_repeated() ? "" : DefaultValue(descriptor);
  91. vars["type_constant"] = TypeName(GetType(descriptor));
  92. vars["packed"] = descriptor->options().packed() ? "true" : "false";
  93. vars["enum_map"] = "null";
  94. vars["prototype"] = "null";
  95. JavaType java_type = GetJavaType(descriptor);
  96. string singular_type;
  97. switch (java_type) {
  98. case JAVATYPE_MESSAGE:
  99. singular_type = ClassName(descriptor->message_type());
  100. vars["prototype"] = singular_type + ".getDefaultInstance()";
  101. break;
  102. case JAVATYPE_ENUM:
  103. singular_type = ClassName(descriptor->enum_type());
  104. vars["enum_map"] = singular_type + ".internalGetValueMap()";
  105. break;
  106. default:
  107. singular_type = BoxedPrimitiveTypeName(java_type);
  108. break;
  109. }
  110. vars["type"] = descriptor->is_repeated() ?
  111. "java.util.List<" + singular_type + ">" : singular_type;
  112. vars["singular_type"] = singular_type;
  113. }
  114. void ExtensionGenerator::Generate(io::Printer* printer) {
  115. map<string, string> vars;
  116. InitTemplateVars(descriptor_, scope_, &vars);
  117. printer->Print(vars,
  118. "public static final int $constant_name$ = $number$;\n");
  119. if (HasDescriptorMethods(descriptor_->file())) {
  120. // Non-lite extensions
  121. if (descriptor_->extension_scope() == NULL) {
  122. // Non-nested
  123. printer->Print(
  124. vars,
  125. "public static final\n"
  126. " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
  127. " $containing_type$,\n"
  128. " $type$> $name$ = com.google.protobuf.GeneratedMessage\n"
  129. " .newFileScopedGeneratedExtension(\n"
  130. " $singular_type$.class,\n"
  131. " $prototype$);\n");
  132. } else {
  133. // Nested
  134. printer->Print(
  135. vars,
  136. "public static final\n"
  137. " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
  138. " $containing_type$,\n"
  139. " $type$> $name$ = com.google.protobuf.GeneratedMessage\n"
  140. " .newMessageScopedGeneratedExtension(\n"
  141. " $scope$.getDefaultInstance(),\n"
  142. " $index$,\n"
  143. " $singular_type$.class,\n"
  144. " $prototype$);\n");
  145. }
  146. } else {
  147. // Lite extensions
  148. if (descriptor_->is_repeated()) {
  149. printer->Print(
  150. vars,
  151. "public static final\n"
  152. " com.google.protobuf.GeneratedMessageLite.GeneratedExtension<\n"
  153. " $containing_type$,\n"
  154. " $type$> $name$ = com.google.protobuf.GeneratedMessageLite\n"
  155. " .newRepeatedGeneratedExtension(\n"
  156. " $containing_type$.getDefaultInstance(),\n"
  157. " $prototype$,\n"
  158. " $enum_map$,\n"
  159. " $number$,\n"
  160. " com.google.protobuf.WireFormat.FieldType.$type_constant$,\n"
  161. " $packed$);\n");
  162. } else {
  163. printer->Print(
  164. vars,
  165. "public static final\n"
  166. " com.google.protobuf.GeneratedMessageLite.GeneratedExtension<\n"
  167. " $containing_type$,\n"
  168. " $type$> $name$ = com.google.protobuf.GeneratedMessageLite\n"
  169. " .newSingularGeneratedExtension(\n"
  170. " $containing_type$.getDefaultInstance(),\n"
  171. " $default$,\n"
  172. " $prototype$,\n"
  173. " $enum_map$,\n"
  174. " $number$,\n"
  175. " com.google.protobuf.WireFormat.FieldType.$type_constant$);\n");
  176. }
  177. }
  178. }
  179. void ExtensionGenerator::GenerateNonNestedInitializationCode(
  180. io::Printer* printer) {
  181. if (descriptor_->extension_scope() == NULL &&
  182. HasDescriptorMethods(descriptor_->file())) {
  183. // Only applies to non-nested, non-lite extensions.
  184. printer->Print(
  185. "$name$.internalInit(descriptor.getExtensions().get($index$));\n",
  186. "name", UnderscoresToCamelCase(descriptor_),
  187. "index", SimpleItoa(descriptor_->index()));
  188. }
  189. }
  190. void ExtensionGenerator::GenerateRegistrationCode(io::Printer* printer) {
  191. printer->Print(
  192. "registry.add($scope$.$name$);\n",
  193. "scope", scope_,
  194. "name", UnderscoresToCamelCase(descriptor_));
  195. }
  196. } // namespace java
  197. } // namespace compiler
  198. } // namespace protobuf
  199. } // namespace google