Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. All rights reserved. |
| 3 | // https://developers.google.com/protocol-buffers/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | // Author: niwasaki@google.com (Naoki Iwasaki) |
| 32 | // Based on original Protocol Buffers design by |
| 33 | // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | |
| 35 | #include <google/protobuf/compiler/java/java_context.h> |
| 36 | #include <google/protobuf/compiler/java/java_lazy_message_field_lite.h> |
| 37 | #include <google/protobuf/compiler/java/java_doc_comment.h> |
| 38 | #include <google/protobuf/compiler/java/java_helpers.h> |
| 39 | #include <google/protobuf/io/printer.h> |
| 40 | |
| 41 | namespace google { |
| 42 | namespace protobuf { |
| 43 | namespace compiler { |
| 44 | namespace java { |
| 45 | |
| 46 | ImmutableLazyMessageFieldLiteGenerator:: |
| 47 | ImmutableLazyMessageFieldLiteGenerator( |
| 48 | const FieldDescriptor* descriptor, |
| 49 | int messageBitIndex, |
| 50 | int builderBitIndex, |
| 51 | Context* context) |
| 52 | : ImmutableMessageFieldLiteGenerator( |
| 53 | descriptor, messageBitIndex, builderBitIndex, context) { |
| 54 | } |
| 55 | |
| 56 | ImmutableLazyMessageFieldLiteGenerator:: |
| 57 | ~ImmutableLazyMessageFieldLiteGenerator() {} |
| 58 | |
| 59 | void ImmutableLazyMessageFieldLiteGenerator:: |
| 60 | GenerateMembers(io::Printer* printer) const { |
| 61 | printer->Print(variables_, |
| 62 | "private com.google.protobuf.LazyFieldLite $name$_ =\n" |
| 63 | " new com.google.protobuf.LazyFieldLite();\n"); |
| 64 | |
| 65 | PrintExtraFieldInfo(variables_, printer); |
| 66 | WriteFieldDocComment(printer, descriptor_); |
| 67 | printer->Print(variables_, |
| 68 | "$deprecation$public boolean has$capitalized_name$() {\n" |
| 69 | " return $get_has_field_bit_message$;\n" |
| 70 | "}\n"); |
| 71 | |
| 72 | WriteFieldDocComment(printer, descriptor_); |
| 73 | printer->Print(variables_, |
| 74 | "$deprecation$public $type$ get$capitalized_name$() {\n" |
| 75 | " return ($type$) $name$_.getValue($type$.getDefaultInstance());\n" |
| 76 | "}\n"); |
| 77 | |
| 78 | // Field.Builder setField(Field value) |
| 79 | WriteFieldDocComment(printer, descriptor_); |
| 80 | printer->Print(variables_, |
| 81 | "private void set$capitalized_name$($type$ value) {\n" |
| 82 | " if (value == null) {\n" |
| 83 | " throw new NullPointerException();\n" |
| 84 | " }\n" |
| 85 | " $name$_.setValue(value);\n" |
| 86 | " $set_has_field_bit_message$;\n" |
| 87 | "}\n"); |
| 88 | |
| 89 | // Field.Builder setField(Field.Builder builderForValue) |
| 90 | WriteFieldDocComment(printer, descriptor_); |
| 91 | printer->Print(variables_, |
| 92 | "private void set$capitalized_name$(\n" |
| 93 | " $type$.Builder builderForValue) {\n" |
| 94 | " $name$_.setValue(builderForValue.build());\n" |
| 95 | " $set_has_field_bit_message$;\n" |
| 96 | "}\n"); |
| 97 | |
| 98 | // Field.Builder mergeField(Field value) |
| 99 | WriteFieldDocComment(printer, descriptor_); |
| 100 | printer->Print(variables_, |
| 101 | "private void merge$capitalized_name$($type$ value) {\n" |
| 102 | " if ($get_has_field_bit_message$ &&\n" |
| 103 | " !$name$_.containsDefaultInstance()) {\n" |
| 104 | " $name$_.setValue(\n" |
| 105 | " $type$.newBuilder(\n" |
| 106 | " get$capitalized_name$()).mergeFrom(value).buildPartial());\n" |
| 107 | " } else {\n" |
| 108 | " $name$_.setValue(value);\n" |
| 109 | " }\n" |
| 110 | " $set_has_field_bit_message$;\n" |
| 111 | "}\n"); |
| 112 | |
| 113 | // Field.Builder clearField() |
| 114 | WriteFieldDocComment(printer, descriptor_); |
| 115 | printer->Print(variables_, |
| 116 | "private void clear$capitalized_name$() {\n" |
| 117 | " $name$_.clear();\n" |
| 118 | " $clear_has_field_bit_message$;\n" |
| 119 | "}\n"); |
| 120 | } |
| 121 | |
| 122 | void ImmutableLazyMessageFieldLiteGenerator:: |
| 123 | GenerateBuilderMembers(io::Printer* printer) const { |
| 124 | // The comments above the methods below are based on a hypothetical |
| 125 | // field of type "Field" called "Field". |
| 126 | |
| 127 | // boolean hasField() |
| 128 | WriteFieldDocComment(printer, descriptor_); |
| 129 | printer->Print(variables_, |
| 130 | "$deprecation$public boolean has$capitalized_name$() {\n" |
| 131 | " return instance.has$capitalized_name$();\n" |
| 132 | "}\n"); |
| 133 | |
| 134 | WriteFieldDocComment(printer, descriptor_); |
| 135 | printer->Print(variables_, |
| 136 | "$deprecation$public $type$ get$capitalized_name$() {\n" |
| 137 | " return instance.get$capitalized_name$();\n" |
| 138 | "}\n"); |
| 139 | |
| 140 | // Field.Builder setField(Field value) |
| 141 | WriteFieldDocComment(printer, descriptor_); |
| 142 | printer->Print(variables_, |
| 143 | "$deprecation$public Builder set$capitalized_name$($type$ value) {\n" |
| 144 | " copyOnWrite();\n" |
| 145 | " instance.set$capitalized_name$(value);\n" |
| 146 | " return this;\n" |
| 147 | "}\n"); |
| 148 | |
| 149 | // Field.Builder setField(Field.Builder builderForValue) |
| 150 | WriteFieldDocComment(printer, descriptor_); |
| 151 | printer->Print(variables_, |
| 152 | "$deprecation$public Builder set$capitalized_name$(\n" |
| 153 | " $type$.Builder builderForValue) {\n" |
| 154 | " copyOnWrite();\n" |
| 155 | " instance.set$capitalized_name$(builderForValue);\n" |
| 156 | " return this;\n" |
| 157 | "}\n"); |
| 158 | |
| 159 | // Field.Builder mergeField(Field value) |
| 160 | WriteFieldDocComment(printer, descriptor_); |
| 161 | printer->Print(variables_, |
| 162 | "$deprecation$public Builder merge$capitalized_name$($type$ value) {\n" |
| 163 | " copyOnWrite();\n" |
| 164 | " instance.merge$capitalized_name$(value);\n" |
| 165 | " return this;\n" |
| 166 | "}\n"); |
| 167 | |
| 168 | // Field.Builder clearField() |
| 169 | WriteFieldDocComment(printer, descriptor_); |
| 170 | printer->Print(variables_, |
| 171 | "$deprecation$public Builder clear$capitalized_name$() {\n" |
| 172 | " copyOnWrite();\n" |
| 173 | " instance.clear$capitalized_name$();\n" |
| 174 | " return this;\n" |
| 175 | "}\n"); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | void ImmutableLazyMessageFieldLiteGenerator:: |
| 180 | GenerateInitializationCode(io::Printer* printer) const { |
| 181 | printer->Print(variables_, "$name$_.clear();\n"); |
| 182 | } |
| 183 | |
| 184 | void ImmutableLazyMessageFieldLiteGenerator:: |
| 185 | GenerateMergingCode(io::Printer* printer) const { |
| 186 | printer->Print(variables_, |
| 187 | "if (other.has$capitalized_name$()) {\n" |
| 188 | " $name$_.merge(other.$name$_);\n" |
| 189 | " $set_has_field_bit_message$;\n" |
| 190 | "}\n"); |
| 191 | } |
| 192 | |
| 193 | void ImmutableLazyMessageFieldLiteGenerator:: |
| 194 | GenerateParsingCode(io::Printer* printer) const { |
| 195 | printer->Print(variables_, |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 196 | "$name$_.mergeFrom(input, extensionRegistry);\n"); |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 197 | printer->Print(variables_, |
| 198 | "$set_has_field_bit_message$;\n"); |
| 199 | } |
| 200 | |
| 201 | void ImmutableLazyMessageFieldLiteGenerator:: |
| 202 | GenerateSerializationCode(io::Printer* printer) const { |
| 203 | // Do not de-serialize lazy fields. |
| 204 | printer->Print(variables_, |
| 205 | "if ($get_has_field_bit_message$) {\n" |
| 206 | " output.writeBytes($number$, $name$_.toByteString());\n" |
| 207 | "}\n"); |
| 208 | } |
| 209 | |
| 210 | void ImmutableLazyMessageFieldLiteGenerator:: |
| 211 | GenerateSerializedSizeCode(io::Printer* printer) const { |
| 212 | printer->Print(variables_, |
| 213 | "if ($get_has_field_bit_message$) {\n" |
| 214 | " size += com.google.protobuf.CodedOutputStream\n" |
| 215 | " .computeLazyFieldSize($number$, $name$_);\n" |
| 216 | "}\n"); |
| 217 | } |
| 218 | |
| 219 | // =================================================================== |
| 220 | |
| 221 | ImmutableLazyMessageOneofFieldLiteGenerator:: |
| 222 | ImmutableLazyMessageOneofFieldLiteGenerator(const FieldDescriptor* descriptor, |
| 223 | int messageBitIndex, |
| 224 | int builderBitIndex, |
| 225 | Context* context) |
| 226 | : ImmutableLazyMessageFieldLiteGenerator( |
| 227 | descriptor, messageBitIndex, builderBitIndex, context) { |
| 228 | const OneofGeneratorInfo* info = |
| 229 | context->GetOneofGeneratorInfo(descriptor->containing_oneof()); |
| 230 | SetCommonOneofVariables(descriptor, info, &variables_); |
| 231 | variables_["lazy_type"] = "com.google.protobuf.LazyFieldLite"; |
| 232 | } |
| 233 | |
| 234 | ImmutableLazyMessageOneofFieldLiteGenerator:: |
| 235 | ~ImmutableLazyMessageOneofFieldLiteGenerator() {} |
| 236 | |
| 237 | void ImmutableLazyMessageOneofFieldLiteGenerator:: |
| 238 | GenerateMembers(io::Printer* printer) const { |
| 239 | PrintExtraFieldInfo(variables_, printer); |
| 240 | WriteFieldDocComment(printer, descriptor_); |
| 241 | |
| 242 | printer->Print(variables_, |
| 243 | "$deprecation$public boolean has$capitalized_name$() {\n" |
| 244 | " return $has_oneof_case_message$;\n" |
| 245 | "}\n"); |
| 246 | WriteFieldDocComment(printer, descriptor_); |
| 247 | |
| 248 | printer->Print(variables_, |
| 249 | "$deprecation$public $type$ get$capitalized_name$() {\n" |
| 250 | " if ($has_oneof_case_message$) {\n" |
| 251 | " return ($type$) (($lazy_type$) $oneof_name$_).getValue(\n" |
| 252 | " $type$.getDefaultInstance());\n" |
| 253 | " }\n" |
| 254 | " return $type$.getDefaultInstance();\n" |
| 255 | "}\n"); |
| 256 | |
| 257 | // Field.Builder setField(Field value) |
| 258 | WriteFieldDocComment(printer, descriptor_); |
| 259 | printer->Print(variables_, |
| 260 | "private void set$capitalized_name$($type$ value) {\n" |
| 261 | " if (value == null) {\n" |
| 262 | " throw new NullPointerException();\n" |
| 263 | " }\n" |
| 264 | " if (!($has_oneof_case_message$)) {\n" |
| 265 | " $oneof_name$_ = new $lazy_type$();\n" |
| 266 | " $set_oneof_case_message$;\n" |
| 267 | " }\n" |
| 268 | " (($lazy_type$) $oneof_name$_).setValue(value);\n" |
| 269 | "}\n"); |
| 270 | |
| 271 | // Field.Builder setField(Field.Builder builderForValue) |
| 272 | WriteFieldDocComment(printer, descriptor_); |
| 273 | printer->Print(variables_, |
| 274 | "private void set$capitalized_name$(\n" |
| 275 | " $type$.Builder builderForValue) {\n" |
| 276 | " if (!($has_oneof_case_message$)) {\n" |
| 277 | " $oneof_name$_ = new $lazy_type$();\n" |
| 278 | " $set_oneof_case_message$;\n" |
| 279 | " }\n" |
| 280 | " (($lazy_type$) $oneof_name$_).setValue(builderForValue.build());\n" |
| 281 | "}\n"); |
| 282 | |
| 283 | // Field.Builder mergeField(Field value) |
| 284 | WriteFieldDocComment(printer, descriptor_); |
| 285 | printer->Print(variables_, |
| 286 | "private void merge$capitalized_name$($type$ value) {\n" |
| 287 | " if ($has_oneof_case_message$ &&\n" |
| 288 | " !(($lazy_type$) $oneof_name$_).containsDefaultInstance()) {\n" |
| 289 | " (($lazy_type$) $oneof_name$_).setValue(\n" |
| 290 | " $type$.newBuilder(\n" |
| 291 | " get$capitalized_name$()).mergeFrom(value).buildPartial());\n" |
| 292 | " } else {\n" |
| 293 | " if (!($has_oneof_case_message$)) {\n" |
| 294 | " $oneof_name$_ = new $lazy_type$();\n" |
| 295 | " $set_oneof_case_message$;\n" |
| 296 | " }\n" |
| 297 | " (($lazy_type$) $oneof_name$_).setValue(value);\n" |
| 298 | " }\n" |
| 299 | "}\n"); |
| 300 | |
| 301 | // Field.Builder clearField() |
| 302 | WriteFieldDocComment(printer, descriptor_); |
| 303 | printer->Print(variables_, |
| 304 | "private void clear$capitalized_name$() {\n" |
| 305 | " if ($has_oneof_case_message$) {\n" |
| 306 | " $clear_oneof_case_message$;\n" |
| 307 | " $oneof_name$_ = null;\n" |
| 308 | " }\n" |
| 309 | "}\n"); |
| 310 | } |
| 311 | |
| 312 | void ImmutableLazyMessageOneofFieldLiteGenerator:: |
| 313 | GenerateBuilderMembers(io::Printer* printer) const { |
| 314 | // boolean hasField() |
| 315 | WriteFieldDocComment(printer, descriptor_); |
| 316 | printer->Print(variables_, |
| 317 | "$deprecation$public boolean has$capitalized_name$() {\n" |
| 318 | " return instance.has$capitalized_name$();\n" |
| 319 | "}\n"); |
| 320 | |
| 321 | printer->Print(variables_, |
| 322 | "$deprecation$public $type$ get$capitalized_name$() {\n" |
| 323 | " return instance.get$capitalized_name$();\n" |
| 324 | "}\n"); |
| 325 | |
| 326 | // Field.Builder setField(Field value) |
| 327 | WriteFieldDocComment(printer, descriptor_); |
| 328 | printer->Print(variables_, |
| 329 | "$deprecation$public Builder set$capitalized_name$($type$ value) {\n" |
| 330 | " copyOnWrite();\n" |
| 331 | " instance.set$capitalized_name$(value);\n" |
| 332 | " return this;\n" |
| 333 | "}\n"); |
| 334 | |
| 335 | // Field.Builder setField(Field.Builder builderForValue) |
| 336 | WriteFieldDocComment(printer, descriptor_); |
| 337 | printer->Print(variables_, |
| 338 | "$deprecation$public Builder set$capitalized_name$(\n" |
| 339 | " $type$.Builder builderForValue) {\n" |
| 340 | " copyOnWrite();\n" |
| 341 | " instance.set$capitalized_name$(builderForValue);\n" |
| 342 | " return this;\n" |
| 343 | "}\n"); |
| 344 | |
| 345 | // Field.Builder mergeField(Field value) |
| 346 | WriteFieldDocComment(printer, descriptor_); |
| 347 | printer->Print(variables_, |
| 348 | "$deprecation$public Builder merge$capitalized_name$($type$ value) {\n" |
| 349 | " copyOnWrite();\n" |
| 350 | " instance.merge$capitalized_name$(value);\n" |
| 351 | " return this;\n" |
| 352 | "}\n"); |
| 353 | |
| 354 | // Field.Builder clearField() |
| 355 | WriteFieldDocComment(printer, descriptor_); |
| 356 | printer->Print(variables_, |
| 357 | "$deprecation$public Builder clear$capitalized_name$() {\n" |
| 358 | " copyOnWrite();\n" |
| 359 | " instance.clear$capitalized_name$();\n" |
| 360 | " return this;\n" |
| 361 | "}\n"); |
| 362 | } |
| 363 | |
| 364 | void ImmutableLazyMessageOneofFieldLiteGenerator:: |
| 365 | GenerateMergingCode(io::Printer* printer) const { |
| 366 | printer->Print(variables_, |
| 367 | "if (!($has_oneof_case_message$)) {\n" |
| 368 | " $oneof_name$_ = new $lazy_type$();\n" |
| 369 | "}\n" |
| 370 | "(($lazy_type$) $oneof_name$_).merge(\n" |
| 371 | " ($lazy_type$) other.$oneof_name$_);\n" |
| 372 | "$set_oneof_case_message$;\n"); |
| 373 | } |
| 374 | |
| 375 | void ImmutableLazyMessageOneofFieldLiteGenerator:: |
| 376 | GenerateParsingCode(io::Printer* printer) const { |
| 377 | printer->Print(variables_, |
| 378 | "if (!($has_oneof_case_message$)) {\n" |
| 379 | " $oneof_name$_ = new $lazy_type$();\n" |
| 380 | "}\n" |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 381 | "(($lazy_type$) $oneof_name$_).mergeFrom(input, extensionRegistry);\n" |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 382 | "$set_oneof_case_message$;\n"); |
| 383 | } |
| 384 | |
| 385 | void ImmutableLazyMessageOneofFieldLiteGenerator:: |
| 386 | GenerateSerializationCode(io::Printer* printer) const { |
| 387 | // Do not de-serialize lazy fields. |
| 388 | printer->Print(variables_, |
| 389 | "if ($has_oneof_case_message$) {\n" |
| 390 | " output.writeBytes(\n" |
| 391 | " $number$, (($lazy_type$) $oneof_name$_).toByteString());\n" |
| 392 | "}\n"); |
| 393 | } |
| 394 | |
| 395 | void ImmutableLazyMessageOneofFieldLiteGenerator:: |
| 396 | GenerateSerializedSizeCode(io::Printer* printer) const { |
| 397 | printer->Print(variables_, |
| 398 | "if ($has_oneof_case_message$) {\n" |
| 399 | " size += com.google.protobuf.CodedOutputStream\n" |
| 400 | " .computeLazyFieldSize($number$, ($lazy_type$) $oneof_name$_);\n" |
| 401 | "}\n"); |
| 402 | } |
| 403 | |
| 404 | // =================================================================== |
| 405 | |
| 406 | RepeatedImmutableLazyMessageFieldLiteGenerator:: |
| 407 | RepeatedImmutableLazyMessageFieldLiteGenerator( |
| 408 | const FieldDescriptor* descriptor, |
| 409 | int messageBitIndex, |
| 410 | int builderBitIndex, |
| 411 | Context* context) |
| 412 | : RepeatedImmutableMessageFieldLiteGenerator( |
| 413 | descriptor, messageBitIndex, builderBitIndex, context) { |
| 414 | } |
| 415 | |
| 416 | |
| 417 | RepeatedImmutableLazyMessageFieldLiteGenerator:: |
| 418 | ~RepeatedImmutableLazyMessageFieldLiteGenerator() {} |
| 419 | |
| 420 | void RepeatedImmutableLazyMessageFieldLiteGenerator:: |
| 421 | GenerateMembers(io::Printer* printer) const { |
| 422 | printer->Print(variables_, |
| 423 | "private com.google.protobuf.Internal.ProtobufList<\n" |
| 424 | " com.google.protobuf.LazyFieldLite> $name$_;\n"); |
| 425 | PrintExtraFieldInfo(variables_, printer); |
| 426 | WriteFieldDocComment(printer, descriptor_); |
| 427 | printer->Print(variables_, |
| 428 | "$deprecation$public java.util.List<$type$>\n" |
| 429 | " get$capitalized_name$List() {\n" |
| 430 | " java.util.List<$type$> list =\n" |
| 431 | " new java.util.ArrayList<$type$>($name$_.size());\n" |
| 432 | " for (com.google.protobuf.LazyFieldLite lf : $name$_) {\n" |
| 433 | " list.add(($type$) lf.getValue($type$.getDefaultInstance()));\n" |
| 434 | " }\n" |
| 435 | // TODO(dweis): Make this list immutable? |
| 436 | " return list;\n" |
| 437 | "}\n"); |
| 438 | WriteFieldDocComment(printer, descriptor_); |
| 439 | printer->Print(variables_, |
| 440 | "$deprecation$public java.util.List<? extends $type$OrBuilder>\n" |
| 441 | " get$capitalized_name$OrBuilderList() {\n" |
| 442 | " return get$capitalized_name$List();\n" |
| 443 | "}\n"); |
| 444 | WriteFieldDocComment(printer, descriptor_); |
| 445 | printer->Print(variables_, |
| 446 | "$deprecation$public int get$capitalized_name$Count() {\n" |
| 447 | " return $name$_.size();\n" |
| 448 | "}\n"); |
| 449 | WriteFieldDocComment(printer, descriptor_); |
| 450 | printer->Print(variables_, |
| 451 | "$deprecation$public $type$ get$capitalized_name$(int index) {\n" |
| 452 | " return ($type$)\n" |
| 453 | " $name$_.get(index).getValue($type$.getDefaultInstance());\n" |
| 454 | "}\n"); |
| 455 | WriteFieldDocComment(printer, descriptor_); |
| 456 | printer->Print(variables_, |
| 457 | "$deprecation$public $type$OrBuilder get$capitalized_name$OrBuilder(\n" |
| 458 | " int index) {\n" |
| 459 | " return ($type$OrBuilder)\n" |
| 460 | " $name$_.get(index).getValue($type$.getDefaultInstance());\n" |
| 461 | "}\n"); |
| 462 | |
| 463 | printer->Print(variables_, |
| 464 | "private void ensure$capitalized_name$IsMutable() {\n" |
| 465 | " if (!$is_mutable$) {\n" |
| 466 | " $name$_ = newProtobufList($name$_);\n" |
| 467 | " }\n" |
| 468 | "}\n" |
| 469 | "\n"); |
| 470 | |
| 471 | // Builder setRepeatedField(int index, Field value) |
| 472 | WriteFieldDocComment(printer, descriptor_); |
| 473 | printer->Print(variables_, |
| 474 | "private void set$capitalized_name$(\n" |
| 475 | " int index, $type$ value) {\n" |
| 476 | " if (value == null) {\n" |
| 477 | " throw new NullPointerException();\n" |
| 478 | " }\n" |
| 479 | " ensure$capitalized_name$IsMutable();\n" |
| 480 | " $name$_.set(\n" |
| 481 | " index, com.google.protobuf.LazyFieldLite.fromValue(value));\n" |
| 482 | "}\n"); |
| 483 | |
| 484 | // Builder setRepeatedField(int index, Field.Builder builderForValue) |
| 485 | WriteFieldDocComment(printer, descriptor_); |
| 486 | printer->Print(variables_, |
| 487 | "private void set$capitalized_name$(\n" |
| 488 | " int index, $type$.Builder builderForValue) {\n" |
| 489 | " ensure$capitalized_name$IsMutable();\n" |
| 490 | " $name$_.set(index, com.google.protobuf.LazyFieldLite.fromValue(\n" |
| 491 | " builderForValue.build()));\n" |
| 492 | "}\n"); |
| 493 | |
| 494 | // Builder addRepeatedField(Field value) |
| 495 | WriteFieldDocComment(printer, descriptor_); |
| 496 | printer->Print(variables_, |
| 497 | "private void add$capitalized_name$($type$ value) {\n" |
| 498 | " if (value == null) {\n" |
| 499 | " throw new NullPointerException();\n" |
| 500 | " }\n" |
| 501 | " ensure$capitalized_name$IsMutable();\n" |
| 502 | " $name$_.add(com.google.protobuf.LazyFieldLite.fromValue(value));\n" |
| 503 | "}\n"); |
| 504 | |
| 505 | // Builder addRepeatedField(int index, Field value) |
| 506 | WriteFieldDocComment(printer, descriptor_); |
| 507 | printer->Print(variables_, |
| 508 | "private void add$capitalized_name$(\n" |
| 509 | " int index, $type$ value) {\n" |
| 510 | " if (value == null) {\n" |
| 511 | " throw new NullPointerException();\n" |
| 512 | " }\n" |
| 513 | " ensure$capitalized_name$IsMutable();\n" |
| 514 | " $name$_.add(\n" |
| 515 | " index, com.google.protobuf.LazyFieldLite.fromValue(value));\n" |
| 516 | "}\n"); |
| 517 | |
| 518 | // Builder addRepeatedField(Field.Builder builderForValue) |
| 519 | WriteFieldDocComment(printer, descriptor_); |
| 520 | printer->Print(variables_, |
| 521 | "private void add$capitalized_name$(\n" |
| 522 | " $type$.Builder builderForValue) {\n" |
| 523 | " ensure$capitalized_name$IsMutable();\n" |
| 524 | " $name$_.add(com.google.protobuf.LazyFieldLite.fromValue(\n" |
| 525 | " builderForValue.build()));\n" |
| 526 | "}\n"); |
| 527 | |
| 528 | // Builder addRepeatedField(int index, Field.Builder builderForValue) |
| 529 | WriteFieldDocComment(printer, descriptor_); |
| 530 | printer->Print(variables_, |
| 531 | "private void add$capitalized_name$(\n" |
| 532 | " int index, $type$.Builder builderForValue) {\n" |
| 533 | " ensure$capitalized_name$IsMutable();\n" |
| 534 | " $name$_.add(index, com.google.protobuf.LazyFieldLite.fromValue(\n" |
| 535 | " builderForValue.build()));\n" |
| 536 | "}\n"); |
| 537 | |
| 538 | // Builder addAllRepeatedField(Iterable<Field> values) |
| 539 | WriteFieldDocComment(printer, descriptor_); |
| 540 | printer->Print(variables_, |
| 541 | "private void addAll$capitalized_name$(\n" |
| 542 | " java.lang.Iterable<? extends $type$> values) {\n" |
| 543 | " ensure$capitalized_name$IsMutable();\n" |
| 544 | " for (com.google.protobuf.MessageLite v : values) {\n" |
| 545 | " $name$_.add(com.google.protobuf.LazyFieldLite.fromValue(v));\n" |
| 546 | " }\n" |
| 547 | "}\n"); |
| 548 | |
| 549 | // Builder clearAllRepeatedField() |
| 550 | WriteFieldDocComment(printer, descriptor_); |
| 551 | printer->Print(variables_, |
| 552 | "private void clear$capitalized_name$() {\n" |
| 553 | " $name$_ = emptyProtobufList();\n" |
| 554 | "}\n"); |
| 555 | |
| 556 | // Builder removeRepeatedField(int index) |
| 557 | WriteFieldDocComment(printer, descriptor_); |
| 558 | printer->Print(variables_, |
| 559 | "private void remove$capitalized_name$(int index) {\n" |
| 560 | " ensure$capitalized_name$IsMutable();\n" |
| 561 | " $name$_.remove(index);\n" |
| 562 | "}\n"); |
| 563 | } |
| 564 | |
| 565 | void RepeatedImmutableLazyMessageFieldLiteGenerator:: |
| 566 | GenerateBuilderMembers(io::Printer* printer) const { |
| 567 | // List<Field> getRepeatedFieldList() |
| 568 | WriteFieldDocComment(printer, descriptor_); |
| 569 | printer->Print(variables_, |
| 570 | "$deprecation$public java.util.List<$type$> get$capitalized_name$List() {\n" |
| 571 | " return java.util.Collections.unmodifiableList(\n" |
| 572 | " instance.get$capitalized_name$List());\n" |
| 573 | "}\n"); |
| 574 | |
| 575 | // int getRepeatedFieldCount() |
| 576 | WriteFieldDocComment(printer, descriptor_); |
| 577 | printer->Print(variables_, |
| 578 | "$deprecation$public int get$capitalized_name$Count() {\n" |
| 579 | " return instance.get$capitalized_name$Count();\n" |
| 580 | "}\n"); |
| 581 | |
| 582 | // Field getRepeatedField(int index) |
| 583 | WriteFieldDocComment(printer, descriptor_); |
| 584 | printer->Print(variables_, |
| 585 | "$deprecation$public $type$ get$capitalized_name$(int index) {\n" |
| 586 | " return instance.get$capitalized_name$(index);\n" |
| 587 | "}\n"); |
| 588 | |
| 589 | // Builder setRepeatedField(int index, Field value) |
| 590 | WriteFieldDocComment(printer, descriptor_); |
| 591 | printer->Print(variables_, |
| 592 | "$deprecation$public Builder set$capitalized_name$(\n" |
| 593 | " int index, $type$ value) {\n" |
| 594 | " copyOnWrite();\n" |
| 595 | " instance.set$capitalized_name$(index, value);\n" |
| 596 | " return this;\n" |
| 597 | "}\n"); |
| 598 | |
| 599 | // Builder setRepeatedField(int index, Field.Builder builderForValue) |
| 600 | WriteFieldDocComment(printer, descriptor_); |
| 601 | printer->Print(variables_, |
| 602 | "$deprecation$public Builder set$capitalized_name$(\n" |
| 603 | " int index, $type$.Builder builderForValue) {\n" |
| 604 | " copyOnWrite();\n" |
| 605 | " instance.set$capitalized_name$(index, builderForValue);\n" |
| 606 | " return this;\n" |
| 607 | "}\n"); |
| 608 | |
| 609 | // Builder addRepeatedField(Field value) |
| 610 | WriteFieldDocComment(printer, descriptor_); |
| 611 | printer->Print(variables_, |
| 612 | "$deprecation$public Builder add$capitalized_name$($type$ value) {\n" |
| 613 | " copyOnWrite();\n" |
| 614 | " instance.add$capitalized_name$(value);\n" |
| 615 | " return this;\n" |
| 616 | "}\n"); |
| 617 | |
| 618 | // Builder addRepeatedField(int index, Field value) |
| 619 | WriteFieldDocComment(printer, descriptor_); |
| 620 | printer->Print(variables_, |
| 621 | "$deprecation$public Builder add$capitalized_name$(\n" |
| 622 | " int index, $type$ value) {\n" |
| 623 | " copyOnWrite();\n" |
| 624 | " instance.add$capitalized_name$(index, value);\n" |
| 625 | " return this;\n" |
| 626 | "}\n"); |
| 627 | |
| 628 | // Builder addRepeatedField(Field.Builder builderForValue) |
| 629 | WriteFieldDocComment(printer, descriptor_); |
| 630 | printer->Print(variables_, |
| 631 | "$deprecation$public Builder add$capitalized_name$(\n" |
| 632 | " $type$.Builder builderForValue) {\n" |
| 633 | " copyOnWrite();\n" |
| 634 | " instance.add$capitalized_name$(builderForValue);\n" |
| 635 | " return this;\n" |
| 636 | "}\n"); |
| 637 | |
| 638 | // Builder addRepeatedField(int index, Field.Builder builderForValue) |
| 639 | WriteFieldDocComment(printer, descriptor_); |
| 640 | printer->Print(variables_, |
| 641 | "$deprecation$public Builder add$capitalized_name$(\n" |
| 642 | " int index, $type$.Builder builderForValue) {\n" |
| 643 | " copyOnWrite();\n" |
| 644 | " instance.add$capitalized_name$(index, builderForValue);\n" |
| 645 | " return this;\n" |
| 646 | "}\n"); |
| 647 | |
| 648 | // Builder addAllRepeatedField(Iterable<Field> values) |
| 649 | WriteFieldDocComment(printer, descriptor_); |
| 650 | printer->Print(variables_, |
| 651 | "$deprecation$public Builder addAll$capitalized_name$(\n" |
| 652 | " java.lang.Iterable<? extends $type$> values) {\n" |
| 653 | " copyOnWrite();\n" |
| 654 | " instance.addAll$capitalized_name$(values);\n" |
| 655 | " return this;\n" |
| 656 | "}\n"); |
| 657 | |
| 658 | // Builder clearAllRepeatedField() |
| 659 | WriteFieldDocComment(printer, descriptor_); |
| 660 | printer->Print(variables_, |
| 661 | "$deprecation$public Builder clear$capitalized_name$() {\n" |
| 662 | " copyOnWrite();\n" |
| 663 | " instance.clear$capitalized_name$();\n" |
| 664 | " return this;\n" |
| 665 | "}\n"); |
| 666 | |
| 667 | // Builder removeRepeatedField(int index) |
| 668 | WriteFieldDocComment(printer, descriptor_); |
| 669 | printer->Print(variables_, |
| 670 | "$deprecation$public Builder remove$capitalized_name$(int index) {\n" |
| 671 | " copyOnWrite();\n" |
| 672 | " instance.remove$capitalized_name$(index);\n" |
| 673 | " return this;\n" |
| 674 | "}\n"); |
| 675 | } |
| 676 | |
| 677 | void RepeatedImmutableLazyMessageFieldLiteGenerator:: |
| 678 | GenerateParsingCode(io::Printer* printer) const { |
| 679 | printer->Print(variables_, |
| 680 | "if (!$is_mutable$) {\n" |
| 681 | " $name$_ = newProtobufList();\n" |
| 682 | "}\n" |
| 683 | "$name$_.add(new com.google.protobuf.LazyFieldLite(\n" |
| 684 | " extensionRegistry, input.readBytes()));\n"); |
| 685 | } |
| 686 | |
| 687 | void RepeatedImmutableLazyMessageFieldLiteGenerator:: |
| 688 | GenerateSerializationCode(io::Printer* printer) const { |
| 689 | printer->Print(variables_, |
| 690 | "for (int i = 0; i < $name$_.size(); i++) {\n" |
| 691 | " output.writeBytes($number$, $name$_.get(i).toByteString());\n" |
| 692 | "}\n"); |
| 693 | } |
| 694 | |
| 695 | void RepeatedImmutableLazyMessageFieldLiteGenerator:: |
| 696 | GenerateSerializedSizeCode(io::Printer* printer) const { |
| 697 | printer->Print(variables_, |
| 698 | "for (int i = 0; i < $name$_.size(); i++) {\n" |
| 699 | " size += com.google.protobuf.CodedOutputStream\n" |
| 700 | " .computeLazyFieldSize($number$, $name$_.get(i));\n" |
| 701 | "}\n"); |
| 702 | } |
| 703 | |
| 704 | } // namespace java |
| 705 | } // namespace compiler |
| 706 | } // namespace protobuf |
| 707 | } // namespace google |