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