temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2 | // Copyright 2008 Google Inc. All rights reserved. |
Feng Xiao | e428862 | 2014-10-01 16:26:23 -0700 | [diff] [blame] | 3 | // https://developers.google.com/protocol-buffers/ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 4 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 8 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 18 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 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/java_extension.h> |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 36 | |
| 37 | #include <google/protobuf/compiler/java/java_context.h> |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 38 | #include <google/protobuf/compiler/java/java_doc_comment.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 39 | #include <google/protobuf/compiler/java/java_helpers.h> |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 40 | #include <google/protobuf/compiler/java/java_name_resolver.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 41 | #include <google/protobuf/io/printer.h> |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 42 | #include <google/protobuf/stubs/strutil.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 43 | |
| 44 | namespace google { |
| 45 | namespace protobuf { |
| 46 | namespace compiler { |
| 47 | namespace java { |
| 48 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 49 | ImmutableExtensionGenerator::ImmutableExtensionGenerator( |
| 50 | const FieldDescriptor* descriptor, Context* context) |
| 51 | : descriptor_(descriptor), context_(context), |
| 52 | name_resolver_(context->GetNameResolver()) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 53 | if (descriptor_->extension_scope() != NULL) { |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 54 | scope_ = name_resolver_->GetImmutableClassName( |
| 55 | descriptor_->extension_scope()); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 56 | } else { |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 57 | scope_ = name_resolver_->GetImmutableClassName(descriptor_->file()); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 60 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 61 | ImmutableExtensionGenerator::~ImmutableExtensionGenerator() {} |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 62 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 63 | // Initializes the vars referenced in the generated code templates. |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 64 | void ExtensionGenerator::InitTemplateVars(const FieldDescriptor* descriptor, |
| 65 | const string& scope, |
| 66 | bool immutable, |
| 67 | ClassNameResolver* name_resolver, |
| 68 | map<string, string>* vars_pointer) { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 69 | map<string, string> &vars = *vars_pointer; |
| 70 | vars["scope"] = scope; |
| 71 | vars["name"] = UnderscoresToCamelCase(descriptor); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 72 | vars["containing_type"] = |
| 73 | name_resolver->GetClassName(descriptor->containing_type(), immutable); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 74 | vars["number"] = SimpleItoa(descriptor->number()); |
| 75 | vars["constant_name"] = FieldConstantName(descriptor); |
| 76 | vars["index"] = SimpleItoa(descriptor->index()); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 77 | vars["default"] = descriptor->is_repeated() ? |
| 78 | "" : DefaultValue(descriptor, immutable, name_resolver); |
| 79 | vars["type_constant"] = FieldTypeName(GetType(descriptor)); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 80 | vars["packed"] = descriptor->options().packed() ? "true" : "false"; |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 81 | vars["enum_map"] = "null"; |
| 82 | vars["prototype"] = "null"; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 83 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 84 | JavaType java_type = GetJavaType(descriptor); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 85 | string singular_type; |
| 86 | switch (java_type) { |
| 87 | case JAVATYPE_MESSAGE: |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 88 | singular_type = name_resolver->GetClassName(descriptor->message_type(), |
| 89 | immutable); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 90 | vars["prototype"] = singular_type + ".getDefaultInstance()"; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 91 | break; |
| 92 | case JAVATYPE_ENUM: |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 93 | singular_type = name_resolver->GetClassName(descriptor->enum_type(), |
| 94 | immutable); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 95 | vars["enum_map"] = singular_type + ".internalGetValueMap()"; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 96 | break; |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 97 | case JAVATYPE_STRING: |
| 98 | singular_type = "java.lang.String"; |
| 99 | break; |
| 100 | case JAVATYPE_BYTES: |
| 101 | singular_type = immutable ? "com.google.protobuf.ByteString" : "byte[]"; |
| 102 | break; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 103 | default: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 104 | singular_type = BoxedPrimitiveTypeName(java_type); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 105 | break; |
| 106 | } |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 107 | vars["type"] = descriptor->is_repeated() ? |
| 108 | "java.util.List<" + singular_type + ">" : singular_type; |
| 109 | vars["singular_type"] = singular_type; |
| 110 | } |
| 111 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 112 | void ImmutableExtensionGenerator::Generate(io::Printer* printer) { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 113 | map<string, string> vars; |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 114 | const bool kUseImmutableNames = true; |
| 115 | InitTemplateVars(descriptor_, scope_, kUseImmutableNames, name_resolver_, |
| 116 | &vars); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 117 | printer->Print(vars, |
| 118 | "public static final int $constant_name$ = $number$;\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 119 | |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 120 | WriteFieldDocComment(printer, descriptor_); |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 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"); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 132 | } else { |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 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"); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Daniel Martin | e2416ca | 2014-11-25 10:37:57 -0500 | [diff] [blame] | 148 | int ImmutableExtensionGenerator::GenerateNonNestedInitializationCode( |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 149 | io::Printer* printer) { |
Daniel Martin | e2416ca | 2014-11-25 10:37:57 -0500 | [diff] [blame] | 150 | int bytecode_estimate = 0; |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 151 | if (descriptor_->extension_scope() == NULL) { |
| 152 | // Only applies to non-nested extensions. |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 153 | printer->Print( |
| 154 | "$name$.internalInit(descriptor.getExtensions().get($index$));\n", |
| 155 | "name", UnderscoresToCamelCase(descriptor_), |
| 156 | "index", SimpleItoa(descriptor_->index())); |
Daniel Martin | e2416ca | 2014-11-25 10:37:57 -0500 | [diff] [blame] | 157 | bytecode_estimate += 21; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 158 | } |
Daniel Martin | e2416ca | 2014-11-25 10:37:57 -0500 | [diff] [blame] | 159 | return bytecode_estimate; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Daniel Martin | e2416ca | 2014-11-25 10:37:57 -0500 | [diff] [blame] | 162 | int ImmutableExtensionGenerator::GenerateRegistrationCode( |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 163 | io::Printer* printer) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 164 | printer->Print( |
| 165 | "registry.add($scope$.$name$);\n", |
| 166 | "scope", scope_, |
| 167 | "name", UnderscoresToCamelCase(descriptor_)); |
Daniel Martin | e2416ca | 2014-11-25 10:37:57 -0500 | [diff] [blame] | 168 | return 7; |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 169 | } |
| 170 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 171 | } // namespace java |
| 172 | } // namespace compiler |
| 173 | } // namespace protobuf |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 174 | } // namespace google |