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 <map> |
| 36 | #include <string> |
| 37 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 38 | #include <google/protobuf/stubs/common.h> |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 39 | #include <google/protobuf/compiler/java/java_context.h> |
| 40 | #include <google/protobuf/compiler/java/java_doc_comment.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 41 | #include <google/protobuf/compiler/java/java_helpers.h> |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 42 | #include <google/protobuf/compiler/java/java_name_resolver.h> |
| 43 | #include <google/protobuf/compiler/java/java_primitive_field.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 44 | #include <google/protobuf/io/printer.h> |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 45 | #include <google/protobuf/wire_format.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 46 | #include <google/protobuf/stubs/strutil.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 47 | |
| 48 | namespace google { |
| 49 | namespace protobuf { |
| 50 | namespace compiler { |
| 51 | namespace java { |
| 52 | |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 53 | using internal::WireFormat; |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 54 | using internal::WireFormatLite; |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 55 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 56 | namespace { |
| 57 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 58 | void SetPrimitiveVariables(const FieldDescriptor* descriptor, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 59 | int messageBitIndex, |
| 60 | int builderBitIndex, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 61 | const FieldGeneratorInfo* info, |
| 62 | ClassNameResolver* name_resolver, |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 63 | map<string, string>* variables) { |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 64 | SetCommonFieldVariables(descriptor, info, variables); |
| 65 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 66 | (*variables)["type"] = PrimitiveTypeName(GetJavaType(descriptor)); |
| 67 | (*variables)["boxed_type"] = BoxedPrimitiveTypeName(GetJavaType(descriptor)); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 68 | (*variables)["field_type"] = (*variables)["type"]; |
| 69 | (*variables)["field_list_type"] = "java.util.List<" + |
| 70 | (*variables)["boxed_type"] + ">"; |
liujisi@google.com | 295a096 | 2011-07-05 06:16:40 +0000 | [diff] [blame] | 71 | (*variables)["empty_list"] = "java.util.Collections.emptyList()"; |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 72 | (*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 73 | (*variables)["default_init"] = IsDefaultValueJavaDefault(descriptor) ? |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 74 | "" : ("= " + ImmutableDefaultValue(descriptor, name_resolver)); |
| 75 | (*variables)["capitalized_type"] = |
| 76 | GetCapitalizedType(descriptor, /* immutable = */ true); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 77 | (*variables)["tag"] = SimpleItoa(WireFormat::MakeTag(descriptor)); |
| 78 | (*variables)["tag_size"] = SimpleItoa( |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 79 | WireFormat::TagSize(descriptor->number(), GetType(descriptor))); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 80 | if (IsReferenceType(GetJavaType(descriptor))) { |
| 81 | (*variables)["null_check"] = |
| 82 | " if (value == null) {\n" |
| 83 | " throw new NullPointerException();\n" |
| 84 | " }\n"; |
| 85 | } else { |
| 86 | (*variables)["null_check"] = ""; |
| 87 | } |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 88 | // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported |
| 89 | // by the proto compiler |
| 90 | (*variables)["deprecation"] = descriptor->options().deprecated() |
| 91 | ? "@java.lang.Deprecated " : ""; |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 92 | int fixed_size = FixedSize(GetType(descriptor)); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 93 | if (fixed_size != -1) { |
| 94 | (*variables)["fixed_size"] = SimpleItoa(fixed_size); |
| 95 | } |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 96 | (*variables)["on_changed"] = |
| 97 | HasDescriptorMethods(descriptor->containing_type()) ? "onChanged();" : ""; |
| 98 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 99 | if (SupportFieldPresence(descriptor->file())) { |
| 100 | // For singular messages and builders, one bit is used for the hasField bit. |
| 101 | (*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex); |
| 102 | (*variables)["get_has_field_bit_builder"] = GenerateGetBit(builderBitIndex); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 103 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 104 | // Note that these have a trailing ";". |
| 105 | (*variables)["set_has_field_bit_message"] = |
| 106 | GenerateSetBit(messageBitIndex) + ";"; |
| 107 | (*variables)["set_has_field_bit_builder"] = |
| 108 | GenerateSetBit(builderBitIndex) + ";"; |
| 109 | (*variables)["clear_has_field_bit_builder"] = |
| 110 | GenerateClearBit(builderBitIndex) + ";"; |
| 111 | |
| 112 | (*variables)["is_field_present_message"] = GenerateGetBit(messageBitIndex); |
| 113 | } else { |
| 114 | (*variables)["set_has_field_bit_message"] = ""; |
| 115 | (*variables)["set_has_field_bit_builder"] = ""; |
| 116 | (*variables)["clear_has_field_bit_builder"] = ""; |
| 117 | |
| 118 | if (descriptor->type() == FieldDescriptor::TYPE_BYTES) { |
| 119 | (*variables)["is_field_present_message"] = |
| 120 | "!" + (*variables)["name"] + "_.isEmpty()"; |
| 121 | } else { |
| 122 | (*variables)["is_field_present_message"] = |
| 123 | (*variables)["name"] + "_ != " + (*variables)["default"]; |
| 124 | } |
| 125 | } |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 126 | |
| 127 | // For repated builders, one bit is used for whether the array is immutable. |
| 128 | (*variables)["get_mutable_bit_builder"] = GenerateGetBit(builderBitIndex); |
| 129 | (*variables)["set_mutable_bit_builder"] = GenerateSetBit(builderBitIndex); |
| 130 | (*variables)["clear_mutable_bit_builder"] = GenerateClearBit(builderBitIndex); |
| 131 | |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 132 | // For repeated fields, one bit is used for whether the array is immutable |
| 133 | // in the parsing constructor. |
| 134 | (*variables)["get_mutable_bit_parser"] = |
| 135 | GenerateGetBitMutableLocal(builderBitIndex); |
| 136 | (*variables)["set_mutable_bit_parser"] = |
| 137 | GenerateSetBitMutableLocal(builderBitIndex); |
| 138 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 139 | (*variables)["get_has_field_bit_from_local"] = |
| 140 | GenerateGetBitFromLocal(builderBitIndex); |
| 141 | (*variables)["set_has_field_bit_to_local"] = |
| 142 | GenerateSetBitToLocal(messageBitIndex); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 143 | } |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 144 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 145 | } // namespace |
| 146 | |
| 147 | // =================================================================== |
| 148 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 149 | ImmutablePrimitiveFieldGenerator:: |
| 150 | ImmutablePrimitiveFieldGenerator(const FieldDescriptor* descriptor, |
| 151 | int messageBitIndex, |
| 152 | int builderBitIndex, |
| 153 | Context* context) |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 154 | : descriptor_(descriptor), messageBitIndex_(messageBitIndex), |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 155 | builderBitIndex_(builderBitIndex), context_(context), |
| 156 | name_resolver_(context->GetNameResolver()) { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 157 | SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 158 | context->GetFieldGeneratorInfo(descriptor), |
| 159 | name_resolver_, &variables_); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 160 | } |
| 161 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 162 | ImmutablePrimitiveFieldGenerator::~ImmutablePrimitiveFieldGenerator() {} |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 163 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 164 | int ImmutablePrimitiveFieldGenerator::GetNumBitsForMessage() const { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 165 | return 1; |
| 166 | } |
| 167 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 168 | int ImmutablePrimitiveFieldGenerator::GetNumBitsForBuilder() const { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 169 | return 1; |
| 170 | } |
| 171 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 172 | void ImmutablePrimitiveFieldGenerator:: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 173 | GenerateInterfaceMembers(io::Printer* printer) const { |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 174 | if (SupportFieldPresence(descriptor_->file())) { |
| 175 | WriteFieldDocComment(printer, descriptor_); |
| 176 | printer->Print(variables_, |
| 177 | "$deprecation$boolean has$capitalized_name$();\n"); |
| 178 | } |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 179 | WriteFieldDocComment(printer, descriptor_); |
| 180 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 181 | "$deprecation$$type$ get$capitalized_name$();\n"); |
| 182 | } |
| 183 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 184 | void ImmutablePrimitiveFieldGenerator:: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 185 | GenerateMembers(io::Printer* printer) const { |
| 186 | printer->Print(variables_, |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 187 | "private $field_type$ $name$_;\n"); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 188 | PrintExtraFieldInfo(variables_, printer); |
| 189 | if (SupportFieldPresence(descriptor_->file())) { |
| 190 | WriteFieldDocComment(printer, descriptor_); |
| 191 | printer->Print(variables_, |
| 192 | "$deprecation$public boolean has$capitalized_name$() {\n" |
| 193 | " return $get_has_field_bit_message$;\n" |
| 194 | "}\n"); |
| 195 | } |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 196 | |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 197 | WriteFieldDocComment(printer, descriptor_); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 198 | printer->Print(variables_, |
| 199 | "$deprecation$public $type$ get$capitalized_name$() {\n" |
| 200 | " return $name$_;\n" |
| 201 | "}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 202 | } |
| 203 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 204 | void ImmutablePrimitiveFieldGenerator:: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 205 | GenerateBuilderMembers(io::Printer* printer) const { |
| 206 | printer->Print(variables_, |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 207 | "private $field_type$ $name$_ $default_init$;\n"); |
| 208 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 209 | if (SupportFieldPresence(descriptor_->file())) { |
| 210 | WriteFieldDocComment(printer, descriptor_); |
| 211 | printer->Print(variables_, |
| 212 | "$deprecation$public boolean has$capitalized_name$() {\n" |
| 213 | " return $get_has_field_bit_builder$;\n" |
| 214 | "}\n"); |
| 215 | } |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 216 | |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 217 | WriteFieldDocComment(printer, descriptor_); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 218 | printer->Print(variables_, |
| 219 | "$deprecation$public $type$ get$capitalized_name$() {\n" |
| 220 | " return $name$_;\n" |
| 221 | "}\n"); |
| 222 | |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 223 | WriteFieldDocComment(printer, descriptor_); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 224 | printer->Print(variables_, |
| 225 | "$deprecation$public Builder set$capitalized_name$($type$ value) {\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 226 | "$null_check$" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 227 | " $set_has_field_bit_builder$\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 228 | " $name$_ = value;\n" |
| 229 | " $on_changed$\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 230 | " return this;\n" |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 231 | "}\n"); |
| 232 | |
| 233 | WriteFieldDocComment(printer, descriptor_); |
| 234 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 235 | "$deprecation$public Builder clear$capitalized_name$() {\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 236 | " $clear_has_field_bit_builder$\n"); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 237 | JavaType type = GetJavaType(descriptor_); |
| 238 | if (type == JAVATYPE_STRING || type == JAVATYPE_BYTES) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 239 | // The default value is not a simple literal so we want to avoid executing |
| 240 | // it multiple times. Instead, get the default out of the default instance. |
| 241 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 242 | " $name$_ = getDefaultInstance().get$capitalized_name$();\n"); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 243 | } else { |
| 244 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 245 | " $name$_ = $default$;\n"); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 246 | } |
| 247 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 248 | " $on_changed$\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 249 | " return this;\n" |
| 250 | "}\n"); |
| 251 | } |
| 252 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 253 | void ImmutablePrimitiveFieldGenerator:: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 254 | GenerateFieldBuilderInitializationCode(io::Printer* printer) const { |
| 255 | // noop for primitives |
| 256 | } |
| 257 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 258 | void ImmutablePrimitiveFieldGenerator:: |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 259 | GenerateInitializationCode(io::Printer* printer) const { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 260 | printer->Print(variables_, "$name$_ = $default$;\n"); |
| 261 | } |
| 262 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 263 | void ImmutablePrimitiveFieldGenerator:: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 264 | GenerateBuilderClearCode(io::Printer* printer) const { |
| 265 | printer->Print(variables_, |
| 266 | "$name$_ = $default$;\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 267 | "$clear_has_field_bit_builder$\n"); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 268 | } |
| 269 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 270 | void ImmutablePrimitiveFieldGenerator:: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 271 | GenerateMergingCode(io::Printer* printer) const { |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 272 | if (SupportFieldPresence(descriptor_->file())) { |
| 273 | printer->Print(variables_, |
| 274 | "if (other.has$capitalized_name$()) {\n" |
| 275 | " set$capitalized_name$(other.get$capitalized_name$());\n" |
| 276 | "}\n"); |
| 277 | } else { |
| 278 | printer->Print(variables_, |
| 279 | "if (other.get$capitalized_name$() != $default$) {\n" |
| 280 | " set$capitalized_name$(other.get$capitalized_name$());\n" |
| 281 | "}\n"); |
| 282 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 283 | } |
| 284 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 285 | void ImmutablePrimitiveFieldGenerator:: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 286 | GenerateBuildingCode(io::Printer* printer) const { |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 287 | if (SupportFieldPresence(descriptor_->file())) { |
| 288 | printer->Print(variables_, |
| 289 | "if ($get_has_field_bit_from_local$) {\n" |
| 290 | " $set_has_field_bit_to_local$;\n" |
| 291 | "}\n"); |
| 292 | } |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 293 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 294 | "result.$name$_ = $name$_;\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 295 | } |
| 296 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 297 | void ImmutablePrimitiveFieldGenerator:: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 298 | GenerateParsingCode(io::Printer* printer) const { |
| 299 | printer->Print(variables_, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 300 | "$set_has_field_bit_message$\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 301 | "$name$_ = input.read$capitalized_type$();\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 302 | } |
| 303 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 304 | void ImmutablePrimitiveFieldGenerator:: |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 305 | GenerateParsingDoneCode(io::Printer* printer) const { |
| 306 | // noop for primitives. |
| 307 | } |
| 308 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 309 | void ImmutablePrimitiveFieldGenerator:: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 310 | GenerateSerializationCode(io::Printer* printer) const { |
kenton@google.com | a4a9ef8 | 2010-09-17 23:20:04 +0000 | [diff] [blame] | 311 | printer->Print(variables_, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 312 | "if ($is_field_present_message$) {\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 313 | " output.write$capitalized_type$($number$, $name$_);\n" |
kenton@google.com | a4a9ef8 | 2010-09-17 23:20:04 +0000 | [diff] [blame] | 314 | "}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 315 | } |
| 316 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 317 | void ImmutablePrimitiveFieldGenerator:: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 318 | GenerateSerializedSizeCode(io::Printer* printer) const { |
kenton@google.com | a4a9ef8 | 2010-09-17 23:20:04 +0000 | [diff] [blame] | 319 | printer->Print(variables_, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 320 | "if ($is_field_present_message$) {\n" |
kenton@google.com | a4a9ef8 | 2010-09-17 23:20:04 +0000 | [diff] [blame] | 321 | " size += com.google.protobuf.CodedOutputStream\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 322 | " .compute$capitalized_type$Size($number$, $name$_);\n" |
kenton@google.com | a4a9ef8 | 2010-09-17 23:20:04 +0000 | [diff] [blame] | 323 | "}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 324 | } |
| 325 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 326 | void ImmutablePrimitiveFieldGenerator:: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 327 | GenerateEqualsCode(io::Printer* printer) const { |
| 328 | switch (GetJavaType(descriptor_)) { |
| 329 | case JAVATYPE_INT: |
| 330 | case JAVATYPE_LONG: |
| 331 | case JAVATYPE_BOOLEAN: |
| 332 | printer->Print(variables_, |
| 333 | "result = result && (get$capitalized_name$()\n" |
| 334 | " == other.get$capitalized_name$());\n"); |
| 335 | break; |
| 336 | |
| 337 | case JAVATYPE_FLOAT: |
| 338 | printer->Print(variables_, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 339 | "result = result && (\n" |
| 340 | " java.lang.Float.floatToIntBits(get$capitalized_name$())\n" |
| 341 | " == java.lang.Float.floatToIntBits(\n" |
| 342 | " other.get$capitalized_name$()));\n"); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 343 | break; |
| 344 | |
| 345 | case JAVATYPE_DOUBLE: |
| 346 | printer->Print(variables_, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 347 | "result = result && (\n" |
| 348 | " java.lang.Double.doubleToLongBits(get$capitalized_name$())\n" |
| 349 | " == java.lang.Double.doubleToLongBits(\n" |
| 350 | " other.get$capitalized_name$()));\n"); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 351 | break; |
| 352 | |
| 353 | case JAVATYPE_STRING: |
| 354 | case JAVATYPE_BYTES: |
| 355 | printer->Print(variables_, |
| 356 | "result = result && get$capitalized_name$()\n" |
| 357 | " .equals(other.get$capitalized_name$());\n"); |
| 358 | break; |
| 359 | |
| 360 | case JAVATYPE_ENUM: |
| 361 | case JAVATYPE_MESSAGE: |
| 362 | default: |
| 363 | GOOGLE_LOG(FATAL) << "Can't get here."; |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 368 | void ImmutablePrimitiveFieldGenerator:: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 369 | GenerateHashCode(io::Printer* printer) const { |
| 370 | printer->Print(variables_, |
| 371 | "hash = (37 * hash) + $constant_name$;\n"); |
| 372 | switch (GetJavaType(descriptor_)) { |
| 373 | case JAVATYPE_INT: |
| 374 | printer->Print(variables_, |
| 375 | "hash = (53 * hash) + get$capitalized_name$();\n"); |
| 376 | break; |
| 377 | |
| 378 | case JAVATYPE_LONG: |
| 379 | printer->Print(variables_, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 380 | "hash = (53 * hash) + com.google.protobuf.Internal.hashLong(\n" |
| 381 | " get$capitalized_name$());\n"); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 382 | break; |
| 383 | |
| 384 | case JAVATYPE_BOOLEAN: |
| 385 | printer->Print(variables_, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 386 | "hash = (53 * hash) + com.google.protobuf.Internal.hashBoolean(\n" |
| 387 | " get$capitalized_name$());\n"); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 388 | break; |
| 389 | |
| 390 | case JAVATYPE_FLOAT: |
| 391 | printer->Print(variables_, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 392 | "hash = (53 * hash) + java.lang.Float.floatToIntBits(\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 393 | " get$capitalized_name$());\n"); |
| 394 | break; |
| 395 | |
| 396 | case JAVATYPE_DOUBLE: |
| 397 | printer->Print(variables_, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 398 | "hash = (53 * hash) + com.google.protobuf.Internal.hashLong(\n" |
| 399 | " java.lang.Double.doubleToLongBits(get$capitalized_name$()));\n"); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 400 | break; |
| 401 | |
| 402 | case JAVATYPE_STRING: |
| 403 | case JAVATYPE_BYTES: |
| 404 | printer->Print(variables_, |
| 405 | "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); |
| 406 | break; |
| 407 | |
| 408 | case JAVATYPE_ENUM: |
| 409 | case JAVATYPE_MESSAGE: |
| 410 | default: |
| 411 | GOOGLE_LOG(FATAL) << "Can't get here."; |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 416 | string ImmutablePrimitiveFieldGenerator::GetBoxedType() const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 417 | return BoxedPrimitiveTypeName(GetJavaType(descriptor_)); |
| 418 | } |
| 419 | |
| 420 | // =================================================================== |
| 421 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 422 | ImmutablePrimitiveOneofFieldGenerator:: |
| 423 | ImmutablePrimitiveOneofFieldGenerator(const FieldDescriptor* descriptor, |
| 424 | int messageBitIndex, |
| 425 | int builderBitIndex, |
| 426 | Context* context) |
| 427 | : ImmutablePrimitiveFieldGenerator( |
| 428 | descriptor, messageBitIndex, builderBitIndex, context) { |
| 429 | const OneofGeneratorInfo* info = |
| 430 | context->GetOneofGeneratorInfo(descriptor->containing_oneof()); |
| 431 | SetCommonOneofVariables(descriptor, info, &variables_); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 432 | } |
| 433 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 434 | ImmutablePrimitiveOneofFieldGenerator:: |
| 435 | ~ImmutablePrimitiveOneofFieldGenerator() {} |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 436 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 437 | void ImmutablePrimitiveOneofFieldGenerator:: |
| 438 | GenerateMembers(io::Printer* printer) const { |
| 439 | PrintExtraFieldInfo(variables_, printer); |
| 440 | if (SupportFieldPresence(descriptor_->file())) { |
| 441 | WriteFieldDocComment(printer, descriptor_); |
| 442 | printer->Print(variables_, |
| 443 | "$deprecation$public boolean has$capitalized_name$() {\n" |
| 444 | " return $has_oneof_case_message$;\n" |
| 445 | "}\n"); |
| 446 | } |
| 447 | |
| 448 | WriteFieldDocComment(printer, descriptor_); |
| 449 | printer->Print(variables_, |
| 450 | "$deprecation$public $type$ get$capitalized_name$() {\n" |
| 451 | " if ($has_oneof_case_message$) {\n" |
| 452 | " return ($boxed_type$) $oneof_name$_;\n" |
| 453 | " }\n" |
| 454 | " return $default$;\n" |
| 455 | "}\n"); |
| 456 | } |
| 457 | |
| 458 | |
| 459 | void ImmutablePrimitiveOneofFieldGenerator:: |
| 460 | GenerateBuilderMembers(io::Printer* printer) const { |
| 461 | if (SupportFieldPresence(descriptor_->file())) { |
| 462 | WriteFieldDocComment(printer, descriptor_); |
| 463 | printer->Print(variables_, |
| 464 | "$deprecation$public boolean has$capitalized_name$() {\n" |
| 465 | " return $has_oneof_case_message$;\n" |
| 466 | "}\n"); |
| 467 | } |
| 468 | |
| 469 | WriteFieldDocComment(printer, descriptor_); |
| 470 | printer->Print(variables_, |
| 471 | "$deprecation$public $type$ get$capitalized_name$() {\n" |
| 472 | " if ($has_oneof_case_message$) {\n" |
| 473 | " return ($boxed_type$) $oneof_name$_;\n" |
| 474 | " }\n" |
| 475 | " return $default$;\n" |
| 476 | "}\n"); |
| 477 | |
| 478 | WriteFieldDocComment(printer, descriptor_); |
| 479 | printer->Print(variables_, |
| 480 | "$deprecation$public Builder set$capitalized_name$($type$ value) {\n" |
| 481 | "$null_check$" |
| 482 | " $set_oneof_case_message$;\n" |
| 483 | " $oneof_name$_ = value;\n" |
| 484 | " $on_changed$\n" |
| 485 | " return this;\n" |
| 486 | "}\n"); |
| 487 | |
| 488 | WriteFieldDocComment(printer, descriptor_); |
| 489 | printer->Print(variables_, |
| 490 | "$deprecation$public Builder clear$capitalized_name$() {\n" |
| 491 | " if ($has_oneof_case_message$) {\n" |
| 492 | " $clear_oneof_case_message$;\n" |
| 493 | " $oneof_name$_ = null;\n" |
| 494 | " $on_changed$\n" |
| 495 | " }\n" |
| 496 | " return this;\n" |
| 497 | "}\n"); |
| 498 | } |
| 499 | |
| 500 | void ImmutablePrimitiveOneofFieldGenerator:: |
| 501 | GenerateBuildingCode(io::Printer* printer) const { |
| 502 | printer->Print(variables_, |
| 503 | "if ($has_oneof_case_message$) {\n" |
| 504 | " result.$oneof_name$_ = $oneof_name$_;\n" |
| 505 | "}\n"); |
| 506 | } |
| 507 | |
| 508 | void ImmutablePrimitiveOneofFieldGenerator:: |
| 509 | GenerateMergingCode(io::Printer* printer) const { |
| 510 | printer->Print(variables_, |
| 511 | "set$capitalized_name$(other.get$capitalized_name$());\n"); |
| 512 | } |
| 513 | |
| 514 | void ImmutablePrimitiveOneofFieldGenerator:: |
| 515 | GenerateParsingCode(io::Printer* printer) const { |
| 516 | printer->Print(variables_, |
| 517 | "$set_oneof_case_message$;\n" |
| 518 | "$oneof_name$_ = input.read$capitalized_type$();\n"); |
| 519 | } |
| 520 | |
| 521 | void ImmutablePrimitiveOneofFieldGenerator:: |
| 522 | GenerateSerializationCode(io::Printer* printer) const { |
| 523 | printer->Print(variables_, |
| 524 | "if ($has_oneof_case_message$) {\n" |
| 525 | " output.write$capitalized_type$(\n" |
| 526 | " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n" |
| 527 | "}\n"); |
| 528 | } |
| 529 | |
| 530 | void ImmutablePrimitiveOneofFieldGenerator:: |
| 531 | GenerateSerializedSizeCode(io::Printer* printer) const { |
| 532 | printer->Print(variables_, |
| 533 | "if ($has_oneof_case_message$) {\n" |
| 534 | " size += com.google.protobuf.CodedOutputStream\n" |
| 535 | " .compute$capitalized_type$Size(\n" |
| 536 | " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n" |
| 537 | "}\n"); |
| 538 | } |
| 539 | |
| 540 | // =================================================================== |
| 541 | |
| 542 | RepeatedImmutablePrimitiveFieldGenerator:: |
| 543 | RepeatedImmutablePrimitiveFieldGenerator(const FieldDescriptor* descriptor, |
| 544 | int messageBitIndex, |
| 545 | int builderBitIndex, |
| 546 | Context* context) |
| 547 | : descriptor_(descriptor), messageBitIndex_(messageBitIndex), |
| 548 | builderBitIndex_(builderBitIndex), context_(context), |
| 549 | name_resolver_(context->GetNameResolver()) { |
| 550 | SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex, |
| 551 | context->GetFieldGeneratorInfo(descriptor), |
| 552 | name_resolver_, &variables_); |
| 553 | } |
| 554 | |
| 555 | RepeatedImmutablePrimitiveFieldGenerator:: |
| 556 | ~RepeatedImmutablePrimitiveFieldGenerator() {} |
| 557 | |
| 558 | int RepeatedImmutablePrimitiveFieldGenerator::GetNumBitsForMessage() const { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 559 | return 0; |
| 560 | } |
| 561 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 562 | int RepeatedImmutablePrimitiveFieldGenerator::GetNumBitsForBuilder() const { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 563 | return 1; |
| 564 | } |
| 565 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 566 | void RepeatedImmutablePrimitiveFieldGenerator:: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 567 | GenerateInterfaceMembers(io::Printer* printer) const { |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 568 | WriteFieldDocComment(printer, descriptor_); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 569 | printer->Print(variables_, |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 570 | "$deprecation$java.util.List<$boxed_type$> get$capitalized_name$List();\n"); |
| 571 | WriteFieldDocComment(printer, descriptor_); |
| 572 | printer->Print(variables_, |
| 573 | "$deprecation$int get$capitalized_name$Count();\n"); |
| 574 | WriteFieldDocComment(printer, descriptor_); |
| 575 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 576 | "$deprecation$$type$ get$capitalized_name$(int index);\n"); |
| 577 | } |
| 578 | |
| 579 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 580 | void RepeatedImmutablePrimitiveFieldGenerator:: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 581 | GenerateMembers(io::Printer* printer) const { |
| 582 | printer->Print(variables_, |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 583 | "private $field_list_type$ $name$_;\n"); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 584 | PrintExtraFieldInfo(variables_, printer); |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 585 | WriteFieldDocComment(printer, descriptor_); |
| 586 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 587 | "$deprecation$public java.util.List<$boxed_type$>\n" |
| 588 | " get$capitalized_name$List() {\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 589 | " return $name$_;\n" // note: unmodifiable list |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 590 | "}\n"); |
| 591 | WriteFieldDocComment(printer, descriptor_); |
| 592 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 593 | "$deprecation$public int get$capitalized_name$Count() {\n" |
| 594 | " return $name$_.size();\n" |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 595 | "}\n"); |
| 596 | WriteFieldDocComment(printer, descriptor_); |
| 597 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 598 | "$deprecation$public $type$ get$capitalized_name$(int index) {\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 599 | " return $name$_.get(index);\n" |
| 600 | "}\n"); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 601 | |
| 602 | if (descriptor_->options().packed() && |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 603 | HasGeneratedMethods(descriptor_->containing_type())) { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 604 | printer->Print(variables_, |
jasonh+personal@google.com | 9951233 | 2009-12-01 18:05:21 +0000 | [diff] [blame] | 605 | "private int $name$MemoizedSerializedSize = -1;\n"); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 606 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 607 | } |
| 608 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 609 | void RepeatedImmutablePrimitiveFieldGenerator:: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 610 | GenerateBuilderMembers(io::Printer* printer) const { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 611 | // One field is the list and the bit field keeps track of whether the |
| 612 | // list is immutable. If it's immutable, the invariant is that it must |
| 613 | // either an instance of Collections.emptyList() or it's an ArrayList |
| 614 | // wrapped in a Collections.unmodifiableList() wrapper and nobody else has |
| 615 | // a refererence to the underlying ArrayList. This invariant allows us to |
| 616 | // share instances of lists between protocol buffers avoiding expensive |
| 617 | // memory allocations. Note, immutable is a strong guarantee here -- not |
| 618 | // just that the list cannot be modified via the reference but that the |
| 619 | // list can never be modified. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 620 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 621 | "private $field_list_type$ $name$_ = $empty_list$;\n"); |
| 622 | |
| 623 | printer->Print(variables_, |
| 624 | "private void ensure$capitalized_name$IsMutable() {\n" |
| 625 | " if (!$get_mutable_bit_builder$) {\n" |
| 626 | " $name$_ = new java.util.ArrayList<$boxed_type$>($name$_);\n" |
| 627 | " $set_mutable_bit_builder$;\n" |
| 628 | " }\n" |
| 629 | "}\n"); |
| 630 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 631 | // Note: We return an unmodifiable list because otherwise the caller |
| 632 | // could hold on to the returned list and modify it after the message |
| 633 | // has been built, thus mutating the message which is supposed to be |
| 634 | // immutable. |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 635 | WriteFieldDocComment(printer, descriptor_); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 636 | printer->Print(variables_, |
| 637 | "$deprecation$public java.util.List<$boxed_type$>\n" |
| 638 | " get$capitalized_name$List() {\n" |
| 639 | " return java.util.Collections.unmodifiableList($name$_);\n" |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 640 | "}\n"); |
| 641 | WriteFieldDocComment(printer, descriptor_); |
| 642 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 643 | "$deprecation$public int get$capitalized_name$Count() {\n" |
| 644 | " return $name$_.size();\n" |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 645 | "}\n"); |
| 646 | WriteFieldDocComment(printer, descriptor_); |
| 647 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 648 | "$deprecation$public $type$ get$capitalized_name$(int index) {\n" |
| 649 | " return $name$_.get(index);\n" |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 650 | "}\n"); |
| 651 | WriteFieldDocComment(printer, descriptor_); |
| 652 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 653 | "$deprecation$public Builder set$capitalized_name$(\n" |
| 654 | " int index, $type$ value) {\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 655 | "$null_check$" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 656 | " ensure$capitalized_name$IsMutable();\n" |
| 657 | " $name$_.set(index, value);\n" |
| 658 | " $on_changed$\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 659 | " return this;\n" |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 660 | "}\n"); |
| 661 | WriteFieldDocComment(printer, descriptor_); |
| 662 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 663 | "$deprecation$public Builder add$capitalized_name$($type$ value) {\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 664 | "$null_check$" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 665 | " ensure$capitalized_name$IsMutable();\n" |
| 666 | " $name$_.add(value);\n" |
| 667 | " $on_changed$\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 668 | " return this;\n" |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 669 | "}\n"); |
| 670 | WriteFieldDocComment(printer, descriptor_); |
| 671 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 672 | "$deprecation$public Builder addAll$capitalized_name$(\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 673 | " java.lang.Iterable<? extends $boxed_type$> values) {\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 674 | " ensure$capitalized_name$IsMutable();\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 675 | " com.google.protobuf.AbstractMessageLite.Builder.addAll(\n" |
| 676 | " values, $name$_);\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 677 | " $on_changed$\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 678 | " return this;\n" |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 679 | "}\n"); |
| 680 | WriteFieldDocComment(printer, descriptor_); |
| 681 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 682 | "$deprecation$public Builder clear$capitalized_name$() {\n" |
| 683 | " $name$_ = $empty_list$;\n" |
| 684 | " $clear_mutable_bit_builder$;\n" |
| 685 | " $on_changed$\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 686 | " return this;\n" |
| 687 | "}\n"); |
| 688 | } |
| 689 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 690 | void RepeatedImmutablePrimitiveFieldGenerator:: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 691 | GenerateFieldBuilderInitializationCode(io::Printer* printer) const { |
| 692 | // noop for primitives |
| 693 | } |
| 694 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 695 | void RepeatedImmutablePrimitiveFieldGenerator:: |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 696 | GenerateInitializationCode(io::Printer* printer) const { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 697 | printer->Print(variables_, "$name$_ = $empty_list$;\n"); |
| 698 | } |
| 699 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 700 | void RepeatedImmutablePrimitiveFieldGenerator:: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 701 | GenerateBuilderClearCode(io::Printer* printer) const { |
| 702 | printer->Print(variables_, |
| 703 | "$name$_ = $empty_list$;\n" |
| 704 | "$clear_mutable_bit_builder$;\n"); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 705 | } |
| 706 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 707 | void RepeatedImmutablePrimitiveFieldGenerator:: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 708 | GenerateMergingCode(io::Printer* printer) const { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 709 | // The code below does two optimizations: |
| 710 | // 1. If the other list is empty, there's nothing to do. This ensures we |
| 711 | // don't allocate a new array if we already have an immutable one. |
| 712 | // 2. If the other list is non-empty and our current list is empty, we can |
| 713 | // reuse the other list which is guaranteed to be immutable. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 714 | printer->Print(variables_, |
| 715 | "if (!other.$name$_.isEmpty()) {\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 716 | " if ($name$_.isEmpty()) {\n" |
| 717 | " $name$_ = other.$name$_;\n" |
| 718 | " $clear_mutable_bit_builder$;\n" |
| 719 | " } else {\n" |
| 720 | " ensure$capitalized_name$IsMutable();\n" |
| 721 | " $name$_.addAll(other.$name$_);\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 722 | " }\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 723 | " $on_changed$\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 724 | "}\n"); |
| 725 | } |
| 726 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 727 | void RepeatedImmutablePrimitiveFieldGenerator:: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 728 | GenerateBuildingCode(io::Printer* printer) const { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 729 | // The code below ensures that the result has an immutable list. If our |
| 730 | // list is immutable, we can just reuse it. If not, we make it immutable. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 731 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 732 | "if ($get_mutable_bit_builder$) {\n" |
| 733 | " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" |
| 734 | " $clear_mutable_bit_builder$;\n" |
| 735 | "}\n" |
| 736 | "result.$name$_ = $name$_;\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 737 | } |
| 738 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 739 | void RepeatedImmutablePrimitiveFieldGenerator:: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 740 | GenerateParsingCode(io::Printer* printer) const { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 741 | printer->Print(variables_, |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 742 | "if (!$get_mutable_bit_parser$) {\n" |
| 743 | " $name$_ = new java.util.ArrayList<$boxed_type$>();\n" |
| 744 | " $set_mutable_bit_parser$;\n" |
| 745 | "}\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 746 | "$name$_.add(input.read$capitalized_type$());\n"); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 747 | } |
| 748 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 749 | void RepeatedImmutablePrimitiveFieldGenerator:: |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 750 | GenerateParsingCodeFromPacked(io::Printer* printer) const { |
| 751 | printer->Print(variables_, |
| 752 | "int length = input.readRawVarint32();\n" |
| 753 | "int limit = input.pushLimit(length);\n" |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 754 | "if (!$get_mutable_bit_parser$ && input.getBytesUntilLimit() > 0) {\n" |
| 755 | " $name$_ = new java.util.ArrayList<$boxed_type$>();\n" |
| 756 | " $set_mutable_bit_parser$;\n" |
| 757 | "}\n" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 758 | "while (input.getBytesUntilLimit() > 0) {\n" |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 759 | " $name$_.add(input.read$capitalized_type$());\n" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 760 | "}\n" |
| 761 | "input.popLimit(limit);\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 762 | } |
| 763 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 764 | void RepeatedImmutablePrimitiveFieldGenerator:: |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 765 | GenerateParsingDoneCode(io::Printer* printer) const { |
| 766 | printer->Print(variables_, |
| 767 | "if ($get_mutable_bit_parser$) {\n" |
| 768 | " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" |
| 769 | "}\n"); |
| 770 | } |
| 771 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 772 | void RepeatedImmutablePrimitiveFieldGenerator:: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 773 | GenerateSerializationCode(io::Printer* printer) const { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 774 | if (descriptor_->options().packed()) { |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 775 | // We invoke getSerializedSize in writeTo for messages that have packed |
| 776 | // fields in ImmutableMessageGenerator::GenerateMessageSerializationMethods. |
| 777 | // That makes it safe to rely on the memoized size here. |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 778 | printer->Print(variables_, |
| 779 | "if (get$capitalized_name$List().size() > 0) {\n" |
| 780 | " output.writeRawVarint32($tag$);\n" |
| 781 | " output.writeRawVarint32($name$MemoizedSerializedSize);\n" |
| 782 | "}\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 783 | "for (int i = 0; i < $name$_.size(); i++) {\n" |
| 784 | " output.write$capitalized_type$NoTag($name$_.get(i));\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 785 | "}\n"); |
| 786 | } else { |
| 787 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 788 | "for (int i = 0; i < $name$_.size(); i++) {\n" |
| 789 | " output.write$capitalized_type$($number$, $name$_.get(i));\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 790 | "}\n"); |
| 791 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 792 | } |
| 793 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 794 | void RepeatedImmutablePrimitiveFieldGenerator:: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 795 | GenerateSerializedSizeCode(io::Printer* printer) const { |
| 796 | printer->Print(variables_, |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 797 | "{\n" |
| 798 | " int dataSize = 0;\n"); |
| 799 | printer->Indent(); |
| 800 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 801 | if (FixedSize(GetType(descriptor_)) == -1) { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 802 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 803 | "for (int i = 0; i < $name$_.size(); i++) {\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 804 | " dataSize += com.google.protobuf.CodedOutputStream\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 805 | " .compute$capitalized_type$SizeNoTag($name$_.get(i));\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 806 | "}\n"); |
| 807 | } else { |
| 808 | printer->Print(variables_, |
| 809 | "dataSize = $fixed_size$ * get$capitalized_name$List().size();\n"); |
| 810 | } |
| 811 | |
| 812 | printer->Print( |
| 813 | "size += dataSize;\n"); |
| 814 | |
| 815 | if (descriptor_->options().packed()) { |
| 816 | printer->Print(variables_, |
| 817 | "if (!get$capitalized_name$List().isEmpty()) {\n" |
| 818 | " size += $tag_size$;\n" |
| 819 | " size += com.google.protobuf.CodedOutputStream\n" |
| 820 | " .computeInt32SizeNoTag(dataSize);\n" |
| 821 | "}\n"); |
| 822 | } else { |
| 823 | printer->Print(variables_, |
| 824 | "size += $tag_size$ * get$capitalized_name$List().size();\n"); |
| 825 | } |
| 826 | |
| 827 | // cache the data size for packed fields. |
| 828 | if (descriptor_->options().packed()) { |
| 829 | printer->Print(variables_, |
| 830 | "$name$MemoizedSerializedSize = dataSize;\n"); |
| 831 | } |
| 832 | |
| 833 | printer->Outdent(); |
| 834 | printer->Print("}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 835 | } |
| 836 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 837 | void RepeatedImmutablePrimitiveFieldGenerator:: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 838 | GenerateEqualsCode(io::Printer* printer) const { |
| 839 | printer->Print(variables_, |
| 840 | "result = result && get$capitalized_name$List()\n" |
| 841 | " .equals(other.get$capitalized_name$List());\n"); |
| 842 | } |
| 843 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 844 | void RepeatedImmutablePrimitiveFieldGenerator:: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 845 | GenerateHashCode(io::Printer* printer) const { |
| 846 | printer->Print(variables_, |
| 847 | "if (get$capitalized_name$Count() > 0) {\n" |
| 848 | " hash = (37 * hash) + $constant_name$;\n" |
| 849 | " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" |
| 850 | "}\n"); |
| 851 | } |
| 852 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 853 | string RepeatedImmutablePrimitiveFieldGenerator::GetBoxedType() const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 854 | return BoxedPrimitiveTypeName(GetJavaType(descriptor_)); |
| 855 | } |
| 856 | |
| 857 | } // namespace java |
| 858 | } // namespace compiler |
| 859 | } // namespace protobuf |
| 860 | } // namespace google |