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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3 | // http://code.google.com/p/protobuf/ |
| 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 | |
| 38 | #include <google/protobuf/compiler/java/java_primitive_field.h> |
| 39 | #include <google/protobuf/stubs/common.h> |
| 40 | #include <google/protobuf/compiler/java/java_helpers.h> |
| 41 | #include <google/protobuf/io/printer.h> |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 42 | #include <google/protobuf/wire_format.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 43 | #include <google/protobuf/stubs/strutil.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 44 | |
| 45 | namespace google { |
| 46 | namespace protobuf { |
| 47 | namespace compiler { |
| 48 | namespace java { |
| 49 | |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 50 | using internal::WireFormat; |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 51 | using internal::WireFormatLite; |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 52 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 53 | namespace { |
| 54 | |
| 55 | const char* PrimitiveTypeName(JavaType type) { |
| 56 | switch (type) { |
| 57 | case JAVATYPE_INT : return "int"; |
| 58 | case JAVATYPE_LONG : return "long"; |
| 59 | case JAVATYPE_FLOAT : return "float"; |
| 60 | case JAVATYPE_DOUBLE : return "double"; |
| 61 | case JAVATYPE_BOOLEAN: return "boolean"; |
| 62 | case JAVATYPE_STRING : return "java.lang.String"; |
| 63 | case JAVATYPE_BYTES : return "com.google.protobuf.ByteString"; |
| 64 | case JAVATYPE_ENUM : return NULL; |
| 65 | case JAVATYPE_MESSAGE: return NULL; |
| 66 | |
| 67 | // No default because we want the compiler to complain if any new |
| 68 | // JavaTypes are added. |
| 69 | } |
| 70 | |
| 71 | GOOGLE_LOG(FATAL) << "Can't get here."; |
| 72 | return NULL; |
| 73 | } |
| 74 | |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 75 | bool IsReferenceType(JavaType type) { |
| 76 | switch (type) { |
| 77 | case JAVATYPE_INT : return false; |
| 78 | case JAVATYPE_LONG : return false; |
| 79 | case JAVATYPE_FLOAT : return false; |
| 80 | case JAVATYPE_DOUBLE : return false; |
| 81 | case JAVATYPE_BOOLEAN: return false; |
| 82 | case JAVATYPE_STRING : return true; |
| 83 | case JAVATYPE_BYTES : return true; |
| 84 | case JAVATYPE_ENUM : return true; |
| 85 | case JAVATYPE_MESSAGE: return true; |
| 86 | |
| 87 | // No default because we want the compiler to complain if any new |
| 88 | // JavaTypes are added. |
| 89 | } |
| 90 | |
| 91 | GOOGLE_LOG(FATAL) << "Can't get here."; |
| 92 | return false; |
| 93 | } |
| 94 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 95 | const char* GetCapitalizedType(const FieldDescriptor* field) { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 96 | switch (GetType(field)) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 97 | case FieldDescriptor::TYPE_INT32 : return "Int32" ; |
| 98 | case FieldDescriptor::TYPE_UINT32 : return "UInt32" ; |
| 99 | case FieldDescriptor::TYPE_SINT32 : return "SInt32" ; |
| 100 | case FieldDescriptor::TYPE_FIXED32 : return "Fixed32" ; |
| 101 | case FieldDescriptor::TYPE_SFIXED32: return "SFixed32"; |
| 102 | case FieldDescriptor::TYPE_INT64 : return "Int64" ; |
| 103 | case FieldDescriptor::TYPE_UINT64 : return "UInt64" ; |
| 104 | case FieldDescriptor::TYPE_SINT64 : return "SInt64" ; |
| 105 | case FieldDescriptor::TYPE_FIXED64 : return "Fixed64" ; |
| 106 | case FieldDescriptor::TYPE_SFIXED64: return "SFixed64"; |
| 107 | case FieldDescriptor::TYPE_FLOAT : return "Float" ; |
| 108 | case FieldDescriptor::TYPE_DOUBLE : return "Double" ; |
| 109 | case FieldDescriptor::TYPE_BOOL : return "Bool" ; |
| 110 | case FieldDescriptor::TYPE_STRING : return "String" ; |
| 111 | case FieldDescriptor::TYPE_BYTES : return "Bytes" ; |
| 112 | case FieldDescriptor::TYPE_ENUM : return "Enum" ; |
| 113 | case FieldDescriptor::TYPE_GROUP : return "Group" ; |
| 114 | case FieldDescriptor::TYPE_MESSAGE : return "Message" ; |
| 115 | |
| 116 | // No default because we want the compiler to complain if any new |
| 117 | // types are added. |
| 118 | } |
| 119 | |
| 120 | GOOGLE_LOG(FATAL) << "Can't get here."; |
| 121 | return NULL; |
| 122 | } |
| 123 | |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 124 | // For encodings with fixed sizes, returns that size in bytes. Otherwise |
| 125 | // returns -1. |
| 126 | int FixedSize(FieldDescriptor::Type type) { |
| 127 | switch (type) { |
| 128 | case FieldDescriptor::TYPE_INT32 : return -1; |
| 129 | case FieldDescriptor::TYPE_INT64 : return -1; |
| 130 | case FieldDescriptor::TYPE_UINT32 : return -1; |
| 131 | case FieldDescriptor::TYPE_UINT64 : return -1; |
| 132 | case FieldDescriptor::TYPE_SINT32 : return -1; |
| 133 | case FieldDescriptor::TYPE_SINT64 : return -1; |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 134 | case FieldDescriptor::TYPE_FIXED32 : return WireFormatLite::kFixed32Size; |
| 135 | case FieldDescriptor::TYPE_FIXED64 : return WireFormatLite::kFixed64Size; |
| 136 | case FieldDescriptor::TYPE_SFIXED32: return WireFormatLite::kSFixed32Size; |
| 137 | case FieldDescriptor::TYPE_SFIXED64: return WireFormatLite::kSFixed64Size; |
| 138 | case FieldDescriptor::TYPE_FLOAT : return WireFormatLite::kFloatSize; |
| 139 | case FieldDescriptor::TYPE_DOUBLE : return WireFormatLite::kDoubleSize; |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 140 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 141 | case FieldDescriptor::TYPE_BOOL : return WireFormatLite::kBoolSize; |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 142 | case FieldDescriptor::TYPE_ENUM : return -1; |
| 143 | |
| 144 | case FieldDescriptor::TYPE_STRING : return -1; |
| 145 | case FieldDescriptor::TYPE_BYTES : return -1; |
| 146 | case FieldDescriptor::TYPE_GROUP : return -1; |
| 147 | case FieldDescriptor::TYPE_MESSAGE : return -1; |
| 148 | |
| 149 | // No default because we want the compiler to complain if any new |
| 150 | // types are added. |
| 151 | } |
| 152 | GOOGLE_LOG(FATAL) << "Can't get here."; |
| 153 | return -1; |
| 154 | } |
| 155 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 156 | void SetPrimitiveVariables(const FieldDescriptor* descriptor, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 157 | int messageBitIndex, |
| 158 | int builderBitIndex, |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 159 | map<string, string>* variables) { |
| 160 | (*variables)["name"] = |
| 161 | UnderscoresToCamelCase(descriptor); |
| 162 | (*variables)["capitalized_name"] = |
| 163 | UnderscoresToCapitalizedCamelCase(descriptor); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 164 | (*variables)["constant_name"] = FieldConstantName(descriptor); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 165 | (*variables)["number"] = SimpleItoa(descriptor->number()); |
| 166 | (*variables)["type"] = PrimitiveTypeName(GetJavaType(descriptor)); |
| 167 | (*variables)["boxed_type"] = BoxedPrimitiveTypeName(GetJavaType(descriptor)); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 168 | (*variables)["field_type"] = (*variables)["type"]; |
| 169 | (*variables)["field_list_type"] = "java.util.List<" + |
| 170 | (*variables)["boxed_type"] + ">"; |
| 171 | (*variables)["empty_list"] = "java.util.Collections.emptyList();"; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 172 | (*variables)["default"] = DefaultValue(descriptor); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 173 | (*variables)["default_init"] = IsDefaultValueJavaDefault(descriptor) ? |
| 174 | "" : ("= " + DefaultValue(descriptor)); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 175 | (*variables)["capitalized_type"] = GetCapitalizedType(descriptor); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 176 | (*variables)["tag"] = SimpleItoa(WireFormat::MakeTag(descriptor)); |
| 177 | (*variables)["tag_size"] = SimpleItoa( |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 178 | WireFormat::TagSize(descriptor->number(), GetType(descriptor))); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 179 | if (IsReferenceType(GetJavaType(descriptor))) { |
| 180 | (*variables)["null_check"] = |
| 181 | " if (value == null) {\n" |
| 182 | " throw new NullPointerException();\n" |
| 183 | " }\n"; |
| 184 | } else { |
| 185 | (*variables)["null_check"] = ""; |
| 186 | } |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 187 | // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported |
| 188 | // by the proto compiler |
| 189 | (*variables)["deprecation"] = descriptor->options().deprecated() |
| 190 | ? "@java.lang.Deprecated " : ""; |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 191 | int fixed_size = FixedSize(GetType(descriptor)); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 192 | if (fixed_size != -1) { |
| 193 | (*variables)["fixed_size"] = SimpleItoa(fixed_size); |
| 194 | } |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 195 | (*variables)["on_changed"] = |
| 196 | HasDescriptorMethods(descriptor->containing_type()) ? "onChanged();" : ""; |
| 197 | |
| 198 | // For singular messages and builders, one bit is used for the hasField bit. |
| 199 | (*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex); |
| 200 | |
| 201 | (*variables)["get_has_field_bit_builder"] = GenerateGetBit(builderBitIndex); |
| 202 | (*variables)["set_has_field_bit_builder"] = GenerateSetBit(builderBitIndex); |
| 203 | (*variables)["clear_has_field_bit_builder"] = |
| 204 | GenerateClearBit(builderBitIndex); |
| 205 | |
| 206 | // For repated builders, one bit is used for whether the array is immutable. |
| 207 | (*variables)["get_mutable_bit_builder"] = GenerateGetBit(builderBitIndex); |
| 208 | (*variables)["set_mutable_bit_builder"] = GenerateSetBit(builderBitIndex); |
| 209 | (*variables)["clear_mutable_bit_builder"] = GenerateClearBit(builderBitIndex); |
| 210 | |
| 211 | (*variables)["get_has_field_bit_from_local"] = |
| 212 | GenerateGetBitFromLocal(builderBitIndex); |
| 213 | (*variables)["set_has_field_bit_to_local"] = |
| 214 | GenerateSetBitToLocal(messageBitIndex); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 215 | } |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 216 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 217 | } // namespace |
| 218 | |
| 219 | // =================================================================== |
| 220 | |
| 221 | PrimitiveFieldGenerator:: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 222 | PrimitiveFieldGenerator(const FieldDescriptor* descriptor, |
| 223 | int messageBitIndex, |
| 224 | int builderBitIndex) |
| 225 | : descriptor_(descriptor), messageBitIndex_(messageBitIndex), |
| 226 | builderBitIndex_(builderBitIndex) { |
| 227 | SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex, |
| 228 | &variables_); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {} |
| 232 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 233 | int PrimitiveFieldGenerator::GetNumBitsForMessage() const { |
| 234 | return 1; |
| 235 | } |
| 236 | |
| 237 | int PrimitiveFieldGenerator::GetNumBitsForBuilder() const { |
| 238 | return 1; |
| 239 | } |
| 240 | |
| 241 | void PrimitiveFieldGenerator:: |
| 242 | GenerateInterfaceMembers(io::Printer* printer) const { |
| 243 | printer->Print(variables_, |
| 244 | "$deprecation$boolean has$capitalized_name$();\n" |
| 245 | "$deprecation$$type$ get$capitalized_name$();\n"); |
| 246 | } |
| 247 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 248 | void PrimitiveFieldGenerator:: |
| 249 | GenerateMembers(io::Printer* printer) const { |
| 250 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 251 | "private $field_type$ $name$_;\n" |
| 252 | "$deprecation$public boolean has$capitalized_name$() {\n" |
| 253 | " return $get_has_field_bit_message$;\n" |
| 254 | "}\n"); |
| 255 | |
| 256 | printer->Print(variables_, |
| 257 | "$deprecation$public $type$ get$capitalized_name$() {\n" |
| 258 | " return $name$_;\n" |
| 259 | "}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void PrimitiveFieldGenerator:: |
| 263 | GenerateBuilderMembers(io::Printer* printer) const { |
| 264 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 265 | "private $field_type$ $name$_ $default_init$;\n" |
| 266 | "$deprecation$public boolean has$capitalized_name$() {\n" |
| 267 | " return $get_has_field_bit_builder$;\n" |
| 268 | "}\n"); |
| 269 | |
| 270 | printer->Print(variables_, |
| 271 | "$deprecation$public $type$ get$capitalized_name$() {\n" |
| 272 | " return $name$_;\n" |
| 273 | "}\n"); |
| 274 | |
| 275 | printer->Print(variables_, |
| 276 | "$deprecation$public Builder set$capitalized_name$($type$ value) {\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 277 | "$null_check$" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 278 | " $set_has_field_bit_builder$;\n" |
| 279 | " $name$_ = value;\n" |
| 280 | " $on_changed$\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 281 | " return this;\n" |
| 282 | "}\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 283 | "$deprecation$public Builder clear$capitalized_name$() {\n" |
| 284 | " $clear_has_field_bit_builder$;\n"); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 285 | JavaType type = GetJavaType(descriptor_); |
| 286 | if (type == JAVATYPE_STRING || type == JAVATYPE_BYTES) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 287 | // The default value is not a simple literal so we want to avoid executing |
| 288 | // it multiple times. Instead, get the default out of the default instance. |
| 289 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 290 | " $name$_ = getDefaultInstance().get$capitalized_name$();\n"); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 291 | } else { |
| 292 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 293 | " $name$_ = $default$;\n"); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 294 | } |
| 295 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 296 | " $on_changed$\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 297 | " return this;\n" |
| 298 | "}\n"); |
| 299 | } |
| 300 | |
| 301 | void PrimitiveFieldGenerator:: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 302 | GenerateFieldBuilderInitializationCode(io::Printer* printer) const { |
| 303 | // noop for primitives |
| 304 | } |
| 305 | |
| 306 | void PrimitiveFieldGenerator:: |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 307 | GenerateInitializationCode(io::Printer* printer) const { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 308 | printer->Print(variables_, "$name$_ = $default$;\n"); |
| 309 | } |
| 310 | |
| 311 | void PrimitiveFieldGenerator:: |
| 312 | GenerateBuilderClearCode(io::Printer* printer) const { |
| 313 | printer->Print(variables_, |
| 314 | "$name$_ = $default$;\n" |
| 315 | "$clear_has_field_bit_builder$;\n"); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void PrimitiveFieldGenerator:: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 319 | GenerateMergingCode(io::Printer* printer) const { |
| 320 | printer->Print(variables_, |
| 321 | "if (other.has$capitalized_name$()) {\n" |
| 322 | " set$capitalized_name$(other.get$capitalized_name$());\n" |
| 323 | "}\n"); |
| 324 | } |
| 325 | |
| 326 | void PrimitiveFieldGenerator:: |
| 327 | GenerateBuildingCode(io::Printer* printer) const { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 328 | printer->Print(variables_, |
| 329 | "if ($get_has_field_bit_from_local$) {\n" |
| 330 | " $set_has_field_bit_to_local$;\n" |
| 331 | "}\n" |
| 332 | "result.$name$_ = $name$_;\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | void PrimitiveFieldGenerator:: |
| 336 | GenerateParsingCode(io::Printer* printer) const { |
| 337 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 338 | "$set_has_field_bit_builder$;\n" |
| 339 | "$name$_ = input.read$capitalized_type$();\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void PrimitiveFieldGenerator:: |
| 343 | GenerateSerializationCode(io::Printer* printer) const { |
kenton@google.com | a4a9ef8 | 2010-09-17 23:20:04 +0000 | [diff] [blame] | 344 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 345 | "if ($get_has_field_bit_message$) {\n" |
| 346 | " output.write$capitalized_type$($number$, $name$_);\n" |
kenton@google.com | a4a9ef8 | 2010-09-17 23:20:04 +0000 | [diff] [blame] | 347 | "}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void PrimitiveFieldGenerator:: |
| 351 | GenerateSerializedSizeCode(io::Printer* printer) const { |
kenton@google.com | a4a9ef8 | 2010-09-17 23:20:04 +0000 | [diff] [blame] | 352 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 353 | "if ($get_has_field_bit_message$) {\n" |
kenton@google.com | a4a9ef8 | 2010-09-17 23:20:04 +0000 | [diff] [blame] | 354 | " size += com.google.protobuf.CodedOutputStream\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 355 | " .compute$capitalized_type$Size($number$, $name$_);\n" |
kenton@google.com | a4a9ef8 | 2010-09-17 23:20:04 +0000 | [diff] [blame] | 356 | "}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 357 | } |
| 358 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 359 | void PrimitiveFieldGenerator:: |
| 360 | GenerateEqualsCode(io::Printer* printer) const { |
| 361 | switch (GetJavaType(descriptor_)) { |
| 362 | case JAVATYPE_INT: |
| 363 | case JAVATYPE_LONG: |
| 364 | case JAVATYPE_BOOLEAN: |
| 365 | printer->Print(variables_, |
| 366 | "result = result && (get$capitalized_name$()\n" |
| 367 | " == other.get$capitalized_name$());\n"); |
| 368 | break; |
| 369 | |
| 370 | case JAVATYPE_FLOAT: |
| 371 | printer->Print(variables_, |
| 372 | "result = result && (Float.floatToIntBits(get$capitalized_name$())" |
| 373 | " == Float.floatToIntBits(other.get$capitalized_name$()));\n"); |
| 374 | break; |
| 375 | |
| 376 | case JAVATYPE_DOUBLE: |
| 377 | printer->Print(variables_, |
| 378 | "result = result && (Double.doubleToLongBits(get$capitalized_name$())" |
| 379 | " == Double.doubleToLongBits(other.get$capitalized_name$()));\n"); |
| 380 | break; |
| 381 | |
| 382 | case JAVATYPE_STRING: |
| 383 | case JAVATYPE_BYTES: |
| 384 | printer->Print(variables_, |
| 385 | "result = result && get$capitalized_name$()\n" |
| 386 | " .equals(other.get$capitalized_name$());\n"); |
| 387 | break; |
| 388 | |
| 389 | case JAVATYPE_ENUM: |
| 390 | case JAVATYPE_MESSAGE: |
| 391 | default: |
| 392 | GOOGLE_LOG(FATAL) << "Can't get here."; |
| 393 | break; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | void PrimitiveFieldGenerator:: |
| 398 | GenerateHashCode(io::Printer* printer) const { |
| 399 | printer->Print(variables_, |
| 400 | "hash = (37 * hash) + $constant_name$;\n"); |
| 401 | switch (GetJavaType(descriptor_)) { |
| 402 | case JAVATYPE_INT: |
| 403 | printer->Print(variables_, |
| 404 | "hash = (53 * hash) + get$capitalized_name$();\n"); |
| 405 | break; |
| 406 | |
| 407 | case JAVATYPE_LONG: |
| 408 | printer->Print(variables_, |
| 409 | "hash = (53 * hash) + hashLong(get$capitalized_name$());\n"); |
| 410 | break; |
| 411 | |
| 412 | case JAVATYPE_BOOLEAN: |
| 413 | printer->Print(variables_, |
| 414 | "hash = (53 * hash) + hashBoolean(get$capitalized_name$());\n"); |
| 415 | break; |
| 416 | |
| 417 | case JAVATYPE_FLOAT: |
| 418 | printer->Print(variables_, |
| 419 | "hash = (53 * hash) + Float.floatToIntBits(\n" |
| 420 | " get$capitalized_name$());\n"); |
| 421 | break; |
| 422 | |
| 423 | case JAVATYPE_DOUBLE: |
| 424 | printer->Print(variables_, |
| 425 | "hash = (53 * hash) + hashLong(\n" |
| 426 | " Double.doubleToLongBits(get$capitalized_name$()));\n"); |
| 427 | break; |
| 428 | |
| 429 | case JAVATYPE_STRING: |
| 430 | case JAVATYPE_BYTES: |
| 431 | printer->Print(variables_, |
| 432 | "hash = (53 * hash) + get$capitalized_name$().hashCode();\n"); |
| 433 | break; |
| 434 | |
| 435 | case JAVATYPE_ENUM: |
| 436 | case JAVATYPE_MESSAGE: |
| 437 | default: |
| 438 | GOOGLE_LOG(FATAL) << "Can't get here."; |
| 439 | break; |
| 440 | } |
| 441 | } |
| 442 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 443 | string PrimitiveFieldGenerator::GetBoxedType() const { |
| 444 | return BoxedPrimitiveTypeName(GetJavaType(descriptor_)); |
| 445 | } |
| 446 | |
| 447 | // =================================================================== |
| 448 | |
| 449 | RepeatedPrimitiveFieldGenerator:: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 450 | RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor, |
| 451 | int messageBitIndex, |
| 452 | int builderBitIndex) |
| 453 | : descriptor_(descriptor), messageBitIndex_(messageBitIndex), |
| 454 | builderBitIndex_(builderBitIndex) { |
| 455 | SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex, |
| 456 | &variables_); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {} |
| 460 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 461 | int RepeatedPrimitiveFieldGenerator::GetNumBitsForMessage() const { |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | int RepeatedPrimitiveFieldGenerator::GetNumBitsForBuilder() const { |
| 466 | return 1; |
| 467 | } |
| 468 | |
| 469 | void RepeatedPrimitiveFieldGenerator:: |
| 470 | GenerateInterfaceMembers(io::Printer* printer) const { |
| 471 | printer->Print(variables_, |
| 472 | "$deprecation$java.util.List<$boxed_type$> get$capitalized_name$List();\n" |
| 473 | "$deprecation$int get$capitalized_name$Count();\n" |
| 474 | "$deprecation$$type$ get$capitalized_name$(int index);\n"); |
| 475 | } |
| 476 | |
| 477 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 478 | void RepeatedPrimitiveFieldGenerator:: |
| 479 | GenerateMembers(io::Printer* printer) const { |
| 480 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 481 | "private $field_list_type$ $name$_;\n" |
| 482 | "$deprecation$public java.util.List<$boxed_type$>\n" |
| 483 | " get$capitalized_name$List() {\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 484 | " return $name$_;\n" // note: unmodifiable list |
| 485 | "}\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 486 | "$deprecation$public int get$capitalized_name$Count() {\n" |
| 487 | " return $name$_.size();\n" |
| 488 | "}\n" |
| 489 | "$deprecation$public $type$ get$capitalized_name$(int index) {\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 490 | " return $name$_.get(index);\n" |
| 491 | "}\n"); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 492 | |
| 493 | if (descriptor_->options().packed() && |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 494 | HasGeneratedMethods(descriptor_->containing_type())) { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 495 | printer->Print(variables_, |
jasonh+personal@google.com | 9951233 | 2009-12-01 18:05:21 +0000 | [diff] [blame] | 496 | "private int $name$MemoizedSerializedSize = -1;\n"); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 497 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | void RepeatedPrimitiveFieldGenerator:: |
| 501 | GenerateBuilderMembers(io::Printer* printer) const { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 502 | // One field is the list and the bit field keeps track of whether the |
| 503 | // list is immutable. If it's immutable, the invariant is that it must |
| 504 | // either an instance of Collections.emptyList() or it's an ArrayList |
| 505 | // wrapped in a Collections.unmodifiableList() wrapper and nobody else has |
| 506 | // a refererence to the underlying ArrayList. This invariant allows us to |
| 507 | // share instances of lists between protocol buffers avoiding expensive |
| 508 | // memory allocations. Note, immutable is a strong guarantee here -- not |
| 509 | // just that the list cannot be modified via the reference but that the |
| 510 | // list can never be modified. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 511 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 512 | "private $field_list_type$ $name$_ = $empty_list$;\n"); |
| 513 | |
| 514 | printer->Print(variables_, |
| 515 | "private void ensure$capitalized_name$IsMutable() {\n" |
| 516 | " if (!$get_mutable_bit_builder$) {\n" |
| 517 | " $name$_ = new java.util.ArrayList<$boxed_type$>($name$_);\n" |
| 518 | " $set_mutable_bit_builder$;\n" |
| 519 | " }\n" |
| 520 | "}\n"); |
| 521 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 522 | // Note: We return an unmodifiable list because otherwise the caller |
| 523 | // could hold on to the returned list and modify it after the message |
| 524 | // has been built, thus mutating the message which is supposed to be |
| 525 | // immutable. |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 526 | printer->Print(variables_, |
| 527 | "$deprecation$public java.util.List<$boxed_type$>\n" |
| 528 | " get$capitalized_name$List() {\n" |
| 529 | " return java.util.Collections.unmodifiableList($name$_);\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 530 | "}\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 531 | "$deprecation$public int get$capitalized_name$Count() {\n" |
| 532 | " return $name$_.size();\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 533 | "}\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 534 | "$deprecation$public $type$ get$capitalized_name$(int index) {\n" |
| 535 | " return $name$_.get(index);\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 536 | "}\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 537 | "$deprecation$public Builder set$capitalized_name$(\n" |
| 538 | " int index, $type$ value) {\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 539 | "$null_check$" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 540 | " ensure$capitalized_name$IsMutable();\n" |
| 541 | " $name$_.set(index, value);\n" |
| 542 | " $on_changed$\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 543 | " return this;\n" |
| 544 | "}\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 545 | "$deprecation$public Builder add$capitalized_name$($type$ value) {\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 546 | "$null_check$" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 547 | " ensure$capitalized_name$IsMutable();\n" |
| 548 | " $name$_.add(value);\n" |
| 549 | " $on_changed$\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 550 | " return this;\n" |
| 551 | "}\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 552 | "$deprecation$public Builder addAll$capitalized_name$(\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 553 | " java.lang.Iterable<? extends $boxed_type$> values) {\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 554 | " ensure$capitalized_name$IsMutable();\n" |
| 555 | " super.addAll(values, $name$_);\n" |
| 556 | " $on_changed$\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 557 | " return this;\n" |
| 558 | "}\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 559 | "$deprecation$public Builder clear$capitalized_name$() {\n" |
| 560 | " $name$_ = $empty_list$;\n" |
| 561 | " $clear_mutable_bit_builder$;\n" |
| 562 | " $on_changed$\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 563 | " return this;\n" |
| 564 | "}\n"); |
| 565 | } |
| 566 | |
| 567 | void RepeatedPrimitiveFieldGenerator:: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 568 | GenerateFieldBuilderInitializationCode(io::Printer* printer) const { |
| 569 | // noop for primitives |
| 570 | } |
| 571 | |
| 572 | void RepeatedPrimitiveFieldGenerator:: |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 573 | GenerateInitializationCode(io::Printer* printer) const { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 574 | printer->Print(variables_, "$name$_ = $empty_list$;\n"); |
| 575 | } |
| 576 | |
| 577 | void RepeatedPrimitiveFieldGenerator:: |
| 578 | GenerateBuilderClearCode(io::Printer* printer) const { |
| 579 | printer->Print(variables_, |
| 580 | "$name$_ = $empty_list$;\n" |
| 581 | "$clear_mutable_bit_builder$;\n"); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | void RepeatedPrimitiveFieldGenerator:: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 585 | GenerateMergingCode(io::Printer* printer) const { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 586 | // The code below does two optimizations: |
| 587 | // 1. If the other list is empty, there's nothing to do. This ensures we |
| 588 | // don't allocate a new array if we already have an immutable one. |
| 589 | // 2. If the other list is non-empty and our current list is empty, we can |
| 590 | // reuse the other list which is guaranteed to be immutable. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 591 | printer->Print(variables_, |
| 592 | "if (!other.$name$_.isEmpty()) {\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 593 | " if ($name$_.isEmpty()) {\n" |
| 594 | " $name$_ = other.$name$_;\n" |
| 595 | " $clear_mutable_bit_builder$;\n" |
| 596 | " } else {\n" |
| 597 | " ensure$capitalized_name$IsMutable();\n" |
| 598 | " $name$_.addAll(other.$name$_);\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 599 | " }\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 600 | " $on_changed$\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 601 | "}\n"); |
| 602 | } |
| 603 | |
| 604 | void RepeatedPrimitiveFieldGenerator:: |
| 605 | GenerateBuildingCode(io::Printer* printer) const { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 606 | // The code below ensures that the result has an immutable list. If our |
| 607 | // 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] | 608 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 609 | "if ($get_mutable_bit_builder$) {\n" |
| 610 | " $name$_ = java.util.Collections.unmodifiableList($name$_);\n" |
| 611 | " $clear_mutable_bit_builder$;\n" |
| 612 | "}\n" |
| 613 | "result.$name$_ = $name$_;\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | void RepeatedPrimitiveFieldGenerator:: |
| 617 | GenerateParsingCode(io::Printer* printer) const { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 618 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 619 | "ensure$capitalized_name$IsMutable();\n" |
| 620 | "$name$_.add(input.read$capitalized_type$());\n"); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | void RepeatedPrimitiveFieldGenerator:: |
| 624 | GenerateParsingCodeFromPacked(io::Printer* printer) const { |
| 625 | printer->Print(variables_, |
| 626 | "int length = input.readRawVarint32();\n" |
| 627 | "int limit = input.pushLimit(length);\n" |
| 628 | "while (input.getBytesUntilLimit() > 0) {\n" |
| 629 | " add$capitalized_name$(input.read$capitalized_type$());\n" |
| 630 | "}\n" |
| 631 | "input.popLimit(limit);\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | void RepeatedPrimitiveFieldGenerator:: |
| 635 | GenerateSerializationCode(io::Printer* printer) const { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 636 | if (descriptor_->options().packed()) { |
| 637 | printer->Print(variables_, |
| 638 | "if (get$capitalized_name$List().size() > 0) {\n" |
| 639 | " output.writeRawVarint32($tag$);\n" |
| 640 | " output.writeRawVarint32($name$MemoizedSerializedSize);\n" |
| 641 | "}\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 642 | "for (int i = 0; i < $name$_.size(); i++) {\n" |
| 643 | " output.write$capitalized_type$NoTag($name$_.get(i));\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 644 | "}\n"); |
| 645 | } else { |
| 646 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 647 | "for (int i = 0; i < $name$_.size(); i++) {\n" |
| 648 | " output.write$capitalized_type$($number$, $name$_.get(i));\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 649 | "}\n"); |
| 650 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | void RepeatedPrimitiveFieldGenerator:: |
| 654 | GenerateSerializedSizeCode(io::Printer* printer) const { |
| 655 | printer->Print(variables_, |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 656 | "{\n" |
| 657 | " int dataSize = 0;\n"); |
| 658 | printer->Indent(); |
| 659 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 660 | if (FixedSize(GetType(descriptor_)) == -1) { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 661 | printer->Print(variables_, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 662 | "for (int i = 0; i < $name$_.size(); i++) {\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 663 | " dataSize += com.google.protobuf.CodedOutputStream\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 664 | " .compute$capitalized_type$SizeNoTag($name$_.get(i));\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 665 | "}\n"); |
| 666 | } else { |
| 667 | printer->Print(variables_, |
| 668 | "dataSize = $fixed_size$ * get$capitalized_name$List().size();\n"); |
| 669 | } |
| 670 | |
| 671 | printer->Print( |
| 672 | "size += dataSize;\n"); |
| 673 | |
| 674 | if (descriptor_->options().packed()) { |
| 675 | printer->Print(variables_, |
| 676 | "if (!get$capitalized_name$List().isEmpty()) {\n" |
| 677 | " size += $tag_size$;\n" |
| 678 | " size += com.google.protobuf.CodedOutputStream\n" |
| 679 | " .computeInt32SizeNoTag(dataSize);\n" |
| 680 | "}\n"); |
| 681 | } else { |
| 682 | printer->Print(variables_, |
| 683 | "size += $tag_size$ * get$capitalized_name$List().size();\n"); |
| 684 | } |
| 685 | |
| 686 | // cache the data size for packed fields. |
| 687 | if (descriptor_->options().packed()) { |
| 688 | printer->Print(variables_, |
| 689 | "$name$MemoizedSerializedSize = dataSize;\n"); |
| 690 | } |
| 691 | |
| 692 | printer->Outdent(); |
| 693 | printer->Print("}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 694 | } |
| 695 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 696 | void RepeatedPrimitiveFieldGenerator:: |
| 697 | GenerateEqualsCode(io::Printer* printer) const { |
| 698 | printer->Print(variables_, |
| 699 | "result = result && get$capitalized_name$List()\n" |
| 700 | " .equals(other.get$capitalized_name$List());\n"); |
| 701 | } |
| 702 | |
| 703 | void RepeatedPrimitiveFieldGenerator:: |
| 704 | GenerateHashCode(io::Printer* printer) const { |
| 705 | printer->Print(variables_, |
| 706 | "if (get$capitalized_name$Count() > 0) {\n" |
| 707 | " hash = (37 * hash) + $constant_name$;\n" |
| 708 | " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n" |
| 709 | "}\n"); |
| 710 | } |
| 711 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 712 | string RepeatedPrimitiveFieldGenerator::GetBoxedType() const { |
| 713 | return BoxedPrimitiveTypeName(GetJavaType(descriptor_)); |
| 714 | } |
| 715 | |
| 716 | } // namespace java |
| 717 | } // namespace compiler |
| 718 | } // namespace protobuf |
| 719 | } // namespace google |