java_extension.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. // 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_context.h>
  35. #include <google/protobuf/compiler/java/java_doc_comment.h>
  36. #include <google/protobuf/compiler/java/java_helpers.h>
  37. #include <google/protobuf/compiler/java/java_name_resolver.h>
  38. #include <google/protobuf/io/printer.h>
  39. #include <google/protobuf/stubs/strutil.h>
  40. namespace google {
  41. namespace protobuf {
  42. namespace compiler {
  43. namespace java {
  44. ImmutableExtensionGenerator::ImmutableExtensionGenerator(
  45. const FieldDescriptor* descriptor, Context* context)
  46. : descriptor_(descriptor), context_(context),
  47. name_resolver_(context->GetNameResolver()) {
  48. if (descriptor_->extension_scope() != NULL) {
  49. scope_ = name_resolver_->GetImmutableClassName(
  50. descriptor_->extension_scope());
  51. } else {
  52. scope_ = name_resolver_->GetImmutableClassName(descriptor_->file());
  53. }
  54. }
  55. ImmutableExtensionGenerator::~ImmutableExtensionGenerator() {}
  56. // Initializes the vars referenced in the generated code templates.
  57. void ExtensionGenerator::InitTemplateVars(
  58. const FieldDescriptor* descriptor, const string& scope, bool immutable,
  59. ClassNameResolver* name_resolver, std::map<string, string>* vars_pointer) {
  60. std::map<string, string> &vars = *vars_pointer;
  61. vars["scope"] = scope;
  62. vars["name"] = UnderscoresToCamelCase(descriptor);
  63. vars["containing_type"] =
  64. name_resolver->GetClassName(descriptor->containing_type(), immutable);
  65. vars["number"] = SimpleItoa(descriptor->number());
  66. vars["constant_name"] = FieldConstantName(descriptor);
  67. vars["index"] = SimpleItoa(descriptor->index());
  68. vars["default"] = descriptor->is_repeated() ?
  69. "" : DefaultValue(descriptor, immutable, name_resolver);
  70. vars["type_constant"] = FieldTypeName(GetType(descriptor));
  71. vars["packed"] = descriptor->is_packed() ? "true" : "false";
  72. vars["enum_map"] = "null";
  73. vars["prototype"] = "null";
  74. JavaType java_type = GetJavaType(descriptor);
  75. string singular_type;
  76. switch (java_type) {
  77. case JAVATYPE_MESSAGE:
  78. singular_type = name_resolver->GetClassName(descriptor->message_type(),
  79. immutable);
  80. vars["prototype"] = singular_type + ".getDefaultInstance()";
  81. break;
  82. case JAVATYPE_ENUM:
  83. singular_type = name_resolver->GetClassName(descriptor->enum_type(),
  84. immutable);
  85. vars["enum_map"] = singular_type + ".internalGetValueMap()";
  86. break;
  87. case JAVATYPE_STRING:
  88. singular_type = "java.lang.String";
  89. break;
  90. case JAVATYPE_BYTES:
  91. singular_type = immutable ? "com.google.protobuf.ByteString" : "byte[]";
  92. break;
  93. default:
  94. singular_type = BoxedPrimitiveTypeName(java_type);
  95. break;
  96. }
  97. vars["type"] = descriptor->is_repeated() ?
  98. "java.util.List<" + singular_type + ">" : singular_type;
  99. vars["singular_type"] = singular_type;
  100. }
  101. void ImmutableExtensionGenerator::Generate(io::Printer* printer) {
  102. std::map<string, string> vars;
  103. const bool kUseImmutableNames = true;
  104. InitTemplateVars(descriptor_, scope_, kUseImmutableNames, name_resolver_,
  105. &vars);
  106. printer->Print(vars,
  107. "public static final int $constant_name$ = $number$;\n");
  108. WriteFieldDocComment(printer, descriptor_);
  109. if (descriptor_->extension_scope() == NULL) {
  110. // Non-nested
  111. printer->Print(
  112. vars,
  113. "public static final\n"
  114. " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
  115. " $containing_type$,\n"
  116. " $type$> $name$ = com.google.protobuf.GeneratedMessage\n"
  117. " .newFileScopedGeneratedExtension(\n"
  118. " $singular_type$.class,\n"
  119. " $prototype$);\n");
  120. } else {
  121. // Nested
  122. printer->Print(
  123. vars,
  124. "public static final\n"
  125. " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n"
  126. " $containing_type$,\n"
  127. " $type$> $name$ = com.google.protobuf.GeneratedMessage\n"
  128. " .newMessageScopedGeneratedExtension(\n"
  129. " $scope$.getDefaultInstance(),\n"
  130. " $index$,\n"
  131. " $singular_type$.class,\n"
  132. " $prototype$);\n");
  133. }
  134. printer->Annotate("name", descriptor_);
  135. }
  136. int ImmutableExtensionGenerator::GenerateNonNestedInitializationCode(
  137. io::Printer* printer) {
  138. int bytecode_estimate = 0;
  139. if (descriptor_->extension_scope() == NULL) {
  140. // Only applies to non-nested extensions.
  141. printer->Print(
  142. "$name$.internalInit(descriptor.getExtensions().get($index$));\n",
  143. "name", UnderscoresToCamelCase(descriptor_),
  144. "index", SimpleItoa(descriptor_->index()));
  145. bytecode_estimate += 21;
  146. }
  147. return bytecode_estimate;
  148. }
  149. int ImmutableExtensionGenerator::GenerateRegistrationCode(
  150. io::Printer* printer) {
  151. printer->Print(
  152. "registry.add($scope$.$name$);\n",
  153. "scope", scope_,
  154. "name", UnderscoresToCamelCase(descriptor_));
  155. return 7;
  156. }
  157. } // namespace java
  158. } // namespace compiler
  159. } // namespace protobuf
  160. } // namespace google