java_extension_lite.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #include <google/protobuf/compiler/java/java_extension_lite.h>
  31. #include <google/protobuf/compiler/java/java_context.h>
  32. #include <google/protobuf/compiler/java/java_doc_comment.h>
  33. #include <google/protobuf/compiler/java/java_helpers.h>
  34. #include <google/protobuf/compiler/java/java_name_resolver.h>
  35. #include <google/protobuf/io/printer.h>
  36. #include <google/protobuf/stubs/strutil.h>
  37. namespace google {
  38. namespace protobuf {
  39. namespace compiler {
  40. namespace java {
  41. ImmutableExtensionLiteGenerator::ImmutableExtensionLiteGenerator(
  42. const FieldDescriptor* descriptor, Context* context)
  43. : descriptor_(descriptor), context_(context),
  44. name_resolver_(context->GetNameResolver()) {
  45. if (descriptor_->extension_scope() != NULL) {
  46. scope_ = name_resolver_->GetImmutableClassName(
  47. descriptor_->extension_scope());
  48. } else {
  49. scope_ = name_resolver_->GetImmutableClassName(descriptor_->file());
  50. }
  51. }
  52. ImmutableExtensionLiteGenerator::~ImmutableExtensionLiteGenerator() {}
  53. void ImmutableExtensionLiteGenerator::Generate(io::Printer* printer) {
  54. std::map<string, string> vars;
  55. const bool kUseImmutableNames = true;
  56. InitTemplateVars(descriptor_, scope_, kUseImmutableNames, name_resolver_,
  57. &vars);
  58. printer->Print(vars,
  59. "public static final int $constant_name$ = $number$;\n");
  60. WriteFieldDocComment(printer, descriptor_);
  61. if (descriptor_->is_repeated()) {
  62. printer->Print(
  63. vars,
  64. "public static final\n"
  65. " com.google.protobuf.GeneratedMessageLite.GeneratedExtension<\n"
  66. " $containing_type$,\n"
  67. " $type$> $name$ = com.google.protobuf.GeneratedMessageLite\n"
  68. " .newRepeatedGeneratedExtension(\n"
  69. " $containing_type$.getDefaultInstance(),\n"
  70. " $prototype$,\n"
  71. " $enum_map$,\n"
  72. " $number$,\n"
  73. " com.google.protobuf.WireFormat.FieldType.$type_constant$,\n"
  74. " $packed$,\n"
  75. " $singular_type$.class);\n");
  76. } else {
  77. printer->Print(
  78. vars,
  79. "public static final\n"
  80. " com.google.protobuf.GeneratedMessageLite.GeneratedExtension<\n"
  81. " $containing_type$,\n"
  82. " $type$> $name$ = com.google.protobuf.GeneratedMessageLite\n"
  83. " .newSingularGeneratedExtension(\n"
  84. " $containing_type$.getDefaultInstance(),\n"
  85. " $default$,\n"
  86. " $prototype$,\n"
  87. " $enum_map$,\n"
  88. " $number$,\n"
  89. " com.google.protobuf.WireFormat.FieldType.$type_constant$,\n"
  90. " $singular_type$.class);\n");
  91. }
  92. printer->Annotate("name", descriptor_);
  93. }
  94. int ImmutableExtensionLiteGenerator::GenerateNonNestedInitializationCode(
  95. io::Printer* printer) {
  96. return 0;
  97. }
  98. int ImmutableExtensionLiteGenerator::GenerateRegistrationCode(
  99. io::Printer* printer) {
  100. printer->Print(
  101. "registry.add($scope$.$name$);\n",
  102. "scope", scope_,
  103. "name", UnderscoresToCamelCase(descriptor_));
  104. return 7;
  105. }
  106. } // namespace java
  107. } // namespace compiler
  108. } // namespace protobuf
  109. } // namespace google