• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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 ImmutableLazyMessageFieldGenerator::
ImmutableLazyMessageFieldGenerator(const FieldDescriptor * descriptor,int messageBitIndex,int builderBitIndex,Context * context)47 ImmutableLazyMessageFieldGenerator(
48     const FieldDescriptor* descriptor,
49     int messageBitIndex,
50     int builderBitIndex,
51     Context* context)
52     : ImmutableMessageFieldGenerator(
53           descriptor, messageBitIndex, builderBitIndex, context) {
54 }
55 
~ImmutableLazyMessageFieldGenerator()56 ImmutableLazyMessageFieldGenerator::~ImmutableLazyMessageFieldGenerator() {}
57 
58 void ImmutableLazyMessageFieldGenerator::
GenerateMembers(io::Printer * printer) const59 GenerateMembers(io::Printer* printer) const {
60   printer->Print(variables_,
61     "private com.google.protobuf.LazyFieldLite $name$_ =\n"
62     "    new com.google.protobuf.LazyFieldLite();\n");
63 
64   PrintExtraFieldInfo(variables_, printer);
65   WriteFieldDocComment(printer, descriptor_);
66   printer->Print(variables_,
67     "$deprecation$public boolean has$capitalized_name$() {\n"
68     "  return $get_has_field_bit_message$;\n"
69     "}\n");
70   WriteFieldDocComment(printer, descriptor_);
71 
72   printer->Print(variables_,
73     "$deprecation$public $type$ get$capitalized_name$() {\n"
74     "  return ($type$) $name$_.getValue($type$.getDefaultInstance());\n"
75     "}\n");
76   WriteFieldDocComment(printer, descriptor_);
77   printer->Print(variables_,
78     "$deprecation$public $type$OrBuilder get$capitalized_name$OrBuilder() {\n"
79     "  return $name$_;\n"
80     "}\n");
81 }
82 
83 void ImmutableLazyMessageFieldGenerator::
GenerateBuilderMembers(io::Printer * printer) const84 GenerateBuilderMembers(io::Printer* printer) const {
85   // When using nested-builders, the code initially works just like the
86   // non-nested builder case. It only creates a nested builder lazily on
87   // demand and then forever delegates to it after creation.
88 
89   printer->Print(variables_,
90     "private com.google.protobuf.LazyFieldLite $name$_ =\n"
91     "    new com.google.protobuf.LazyFieldLite();\n");
92 
93   printer->Print(variables_,
94     // If this builder is non-null, it is used and the other fields are
95     // ignored.
96     "private com.google.protobuf.SingleFieldBuilder<\n"
97     "    $type$, $type$.Builder, $type$OrBuilder> $name$Builder_;"
98     "\n");
99 
100   // The comments above the methods below are based on a hypothetical
101   // field of type "Field" called "Field".
102 
103   // boolean hasField()
104   WriteFieldDocComment(printer, descriptor_);
105   printer->Print(variables_,
106     "$deprecation$public boolean has$capitalized_name$() {\n"
107     "  return $get_has_field_bit_builder$;\n"
108     "}\n");
109 
110     printer->Print(variables_,
111       "$deprecation$public $type$ get$capitalized_name$() {\n"
112       "  return ($type$) $name$_.getValue($type$.getDefaultInstance());\n"
113       "}\n");
114 
115   // Field.Builder setField(Field value)
116   WriteFieldDocComment(printer, descriptor_);
117   PrintNestedBuilderFunction(printer,
118     "$deprecation$public Builder set$capitalized_name$($type$ value)",
119 
120     "if (value == null) {\n"
121     "  throw new NullPointerException();\n"
122     "}\n"
123     "$name$_.setValue(value);\n"
124     "$on_changed$\n",
125 
126      NULL,  // Lazy fields are supported only for lite-runtime.
127 
128     "$set_has_field_bit_builder$;\n"
129     "return this;\n");
130 
131   // Field.Builder setField(Field.Builder builderForValue)
132   WriteFieldDocComment(printer, descriptor_);
133   PrintNestedBuilderFunction(printer,
134     "$deprecation$public Builder set$capitalized_name$(\n"
135     "    $type$.Builder builderForValue)",
136 
137     "$name$_.setValue(builderForValue.build());\n"
138     "$on_changed$\n",
139 
140     NULL,
141 
142     "$set_has_field_bit_builder$;\n"
143     "return this;\n");
144 
145   // Field.Builder mergeField(Field value)
146   WriteFieldDocComment(printer, descriptor_);
147   PrintNestedBuilderFunction(printer,
148     "$deprecation$public Builder merge$capitalized_name$($type$ value)",
149 
150     "if ($get_has_field_bit_builder$ &&\n"
151     "    !$name$_.containsDefaultInstance()) {\n"
152     "  $name$_.setValue(\n"
153     "    $type$.newBuilder(\n"
154     "        get$capitalized_name$()).mergeFrom(value).buildPartial());\n"
155     "} else {\n"
156     "  $name$_.setValue(value);\n"
157     "}\n"
158     "$on_changed$\n",
159 
160     NULL,
161 
162     "$set_has_field_bit_builder$;\n"
163     "return this;\n");
164 
165   // Field.Builder clearField()
166   WriteFieldDocComment(printer, descriptor_);
167   PrintNestedBuilderFunction(printer,
168     "$deprecation$public Builder clear$capitalized_name$()",
169 
170     "$name$_.clear();\n"
171     "$on_changed$\n",
172 
173     NULL,
174 
175     "$clear_has_field_bit_builder$;\n"
176     "return this;\n");
177 
178   WriteFieldDocComment(printer, descriptor_);
179   printer->Print(variables_,
180     "$deprecation$public $type$.Builder get$capitalized_name$Builder() {\n"
181     "  $set_has_field_bit_builder$;\n"
182     "  $on_changed$\n"
183     "  return get$capitalized_name$FieldBuilder().getBuilder();\n"
184     "}\n");
185   WriteFieldDocComment(printer, descriptor_);
186   printer->Print(variables_,
187     "$deprecation$public $type$OrBuilder get$capitalized_name$OrBuilder() {\n"
188     "  if ($name$Builder_ != null) {\n"
189     "    return $name$Builder_.getMessageOrBuilder();\n"
190     "  } else {\n"
191     "    return $name$_;\n"
192     "  }\n"
193     "}\n");
194   WriteFieldDocComment(printer, descriptor_);
195   printer->Print(variables_,
196     "private com.google.protobuf.SingleFieldBuilder<\n"
197     "    $type$, $type$.Builder, $type$OrBuilder> \n"
198     "    get$capitalized_name$FieldBuilder() {\n"
199     "  if ($name$Builder_ == null) {\n"
200     "    $name$Builder_ = new com.google.protobuf.SingleFieldBuilder<\n"
201     "        $type$, $type$.Builder, $type$OrBuilder>(\n"
202     "            $name$_,\n"
203     "            getParentForChildren(),\n"
204     "            isClean());\n"
205     "    $name$_ = null;\n"
206     "  }\n"
207     "  return $name$Builder_;\n"
208     "}\n");
209 }
210 
211 
212 void ImmutableLazyMessageFieldGenerator::
GenerateInitializationCode(io::Printer * printer) const213 GenerateInitializationCode(io::Printer* printer) const {
214   printer->Print(variables_, "$name$_.clear();\n");
215 }
216 
217 void ImmutableLazyMessageFieldGenerator::
GenerateBuilderClearCode(io::Printer * printer) const218 GenerateBuilderClearCode(io::Printer* printer) const {
219   printer->Print(variables_, "$name$_.clear();\n");
220   printer->Print(variables_, "$clear_has_field_bit_builder$;\n");
221 }
222 
223 void ImmutableLazyMessageFieldGenerator::
GenerateMergingCode(io::Printer * printer) const224 GenerateMergingCode(io::Printer* printer) const {
225   printer->Print(variables_,
226     "if (other.has$capitalized_name$()) {\n"
227     "  $name$_.merge(other.$name$_);\n"
228     "  $set_has_field_bit_builder$;\n"
229     "}\n");
230 }
231 
232 void ImmutableLazyMessageFieldGenerator::
GenerateBuildingCode(io::Printer * printer) const233 GenerateBuildingCode(io::Printer* printer) const {
234   printer->Print(variables_,
235       "if ($get_has_field_bit_from_local$) {\n"
236       "  $set_has_field_bit_to_local$;\n"
237       "}\n");
238 
239   printer->Print(variables_,
240       "result.$name$_.set(\n"
241       "    $name$_);\n");
242 }
243 
244 void ImmutableLazyMessageFieldGenerator::
GenerateParsingCode(io::Printer * printer) const245 GenerateParsingCode(io::Printer* printer) const {
246   printer->Print(variables_,
247     "$name$_.setByteString(input.readBytes(), extensionRegistry);\n");
248   printer->Print(variables_,
249     "$set_has_field_bit_message$;\n");
250 }
251 
252 void ImmutableLazyMessageFieldGenerator::
GenerateSerializationCode(io::Printer * printer) const253 GenerateSerializationCode(io::Printer* printer) const {
254   // Do not de-serialize lazy fields.
255   printer->Print(variables_,
256     "if ($get_has_field_bit_message$) {\n"
257     "  output.writeBytes($number$, $name$_.toByteString());\n"
258     "}\n");
259 }
260 
261 void ImmutableLazyMessageFieldGenerator::
GenerateSerializedSizeCode(io::Printer * printer) const262 GenerateSerializedSizeCode(io::Printer* printer) const {
263   printer->Print(variables_,
264     "if ($get_has_field_bit_message$) {\n"
265     "  size += com.google.protobuf.CodedOutputStream\n"
266     "    .computeLazyFieldSize($number$, $name$_);\n"
267     "}\n");
268 }
269 
270 // ===================================================================
271 
272 ImmutableLazyMessageOneofFieldGenerator::
ImmutableLazyMessageOneofFieldGenerator(const FieldDescriptor * descriptor,int messageBitIndex,int builderBitIndex,Context * context)273 ImmutableLazyMessageOneofFieldGenerator(const FieldDescriptor* descriptor,
274                                         int messageBitIndex,
275                                         int builderBitIndex,
276                                         Context* context)
277     : ImmutableLazyMessageFieldGenerator(
278           descriptor, messageBitIndex, builderBitIndex, context) {
279   const OneofGeneratorInfo* info =
280       context->GetOneofGeneratorInfo(descriptor->containing_oneof());
281   SetCommonOneofVariables(descriptor, info, &variables_);
282   variables_["lazy_type"] = "com.google.protobuf.LazyFieldLite";
283 }
284 
285 ImmutableLazyMessageOneofFieldGenerator::
~ImmutableLazyMessageOneofFieldGenerator()286 ~ImmutableLazyMessageOneofFieldGenerator() {}
287 
288 void ImmutableLazyMessageOneofFieldGenerator::
GenerateMembers(io::Printer * printer) const289 GenerateMembers(io::Printer* printer) const {
290   PrintExtraFieldInfo(variables_, printer);
291   WriteFieldDocComment(printer, descriptor_);
292 
293   printer->Print(variables_,
294     "$deprecation$public boolean has$capitalized_name$() {\n"
295     "  return $has_oneof_case_message$;\n"
296     "}\n");
297   WriteFieldDocComment(printer, descriptor_);
298 
299   printer->Print(variables_,
300     "$deprecation$public $type$ get$capitalized_name$() {\n"
301     "  if ($has_oneof_case_message$) {\n"
302     "    return ($type$) (($lazy_type$) $oneof_name$_).getValue(\n"
303     "        $type$.getDefaultInstance());\n"
304     "  }\n"
305     "  return $type$.getDefaultInstance();\n"
306     "}\n");
307 }
308 
309 void ImmutableLazyMessageOneofFieldGenerator::
GenerateBuilderMembers(io::Printer * printer) const310 GenerateBuilderMembers(io::Printer* printer) const {
311   // boolean hasField()
312   WriteFieldDocComment(printer, descriptor_);
313   printer->Print(variables_,
314     "$deprecation$public boolean has$capitalized_name$() {\n"
315     "  return $has_oneof_case_message$;\n"
316     "}\n");
317 
318   printer->Print(variables_,
319     "$deprecation$public $type$ get$capitalized_name$() {\n"
320     "  if ($has_oneof_case_message$) {\n"
321     "    return ($type$) (($lazy_type$) $oneof_name$_).getValue(\n"
322     "        $type$.getDefaultInstance());\n"
323     "  }\n"
324     "  return $type$.getDefaultInstance();\n"
325     "}\n");
326 
327   // Field.Builder setField(Field value)
328   WriteFieldDocComment(printer, descriptor_);
329   PrintNestedBuilderFunction(printer,
330     "$deprecation$public Builder set$capitalized_name$($type$ value)",
331 
332     "if (value == null) {\n"
333     "  throw new NullPointerException();\n"
334     "}\n"
335     "if (!($has_oneof_case_message$)) {\n"
336     "  $oneof_name$_ = new $lazy_type$();\n"
337     "  $set_oneof_case_message$;\n"
338     "}\n"
339     "(($lazy_type$) $oneof_name$_).setValue(value);\n"
340     "$on_changed$\n",
341 
342      NULL,  // Lazy fields are supported only for lite-runtime.
343 
344     "return this;\n");
345 
346   // Field.Builder setField(Field.Builder builderForValue)
347   WriteFieldDocComment(printer, descriptor_);
348   PrintNestedBuilderFunction(printer,
349     "$deprecation$public Builder set$capitalized_name$(\n"
350     "    $type$.Builder builderForValue)",
351 
352     "if (!($has_oneof_case_message$)) {\n"
353     "  $oneof_name$_ = new $lazy_type$();\n"
354     "  $set_oneof_case_message$;\n"
355     "}\n"
356     "(($lazy_type$) $oneof_name$_).setValue(builderForValue.build());\n"
357     "$on_changed$\n",
358 
359     NULL,
360 
361     "return this;\n");
362 
363   // Field.Builder mergeField(Field value)
364   WriteFieldDocComment(printer, descriptor_);
365   PrintNestedBuilderFunction(printer,
366     "$deprecation$public Builder merge$capitalized_name$($type$ value)",
367 
368     "if ($has_oneof_case_message$ &&\n"
369     "    !(($lazy_type$) $oneof_name$_).containsDefaultInstance()) {\n"
370     "  (($lazy_type$) $oneof_name$_).setValue(\n"
371     "    $type$.newBuilder(\n"
372     "        get$capitalized_name$()).mergeFrom(value).buildPartial());\n"
373     "} else {\n"
374     "  if (!($has_oneof_case_message$)) {\n"
375     "    $oneof_name$_ = new $lazy_type$();\n"
376     "    $set_oneof_case_message$;\n"
377     "  }\n"
378     "  (($lazy_type$) $oneof_name$_).setValue(value);\n"
379     "}\n"
380     "$on_changed$\n",
381 
382     NULL,
383 
384     "return this;\n");
385 
386   // Field.Builder clearField()
387   WriteFieldDocComment(printer, descriptor_);
388   PrintNestedBuilderFunction(printer,
389     "$deprecation$public Builder clear$capitalized_name$()",
390 
391     "if ($has_oneof_case_message$) {\n"
392     "  $clear_oneof_case_message$;\n"
393     "  $oneof_name$_ = null;\n"
394     "  $on_changed$\n"
395     "}\n",
396 
397     NULL,
398 
399     "return this;\n");
400 }
401 
402 void ImmutableLazyMessageOneofFieldGenerator::
GenerateMergingCode(io::Printer * printer) const403 GenerateMergingCode(io::Printer* printer) const {
404   printer->Print(variables_,
405     "if (!($has_oneof_case_message$)) {\n"
406     "  $oneof_name$_ = new $lazy_type$();\n"
407     "}\n"
408     "(($lazy_type$) $oneof_name$_).merge(\n"
409     "    ($lazy_type$) other.$oneof_name$_);\n"
410     "$set_oneof_case_message$;\n");
411 }
412 
413 void ImmutableLazyMessageOneofFieldGenerator::
GenerateBuildingCode(io::Printer * printer) const414 GenerateBuildingCode(io::Printer* printer) const {
415   printer->Print(variables_,
416                  "if ($has_oneof_case_message$) {\n");
417   printer->Indent();
418 
419   printer->Print(variables_,
420       "result.$oneof_name$_ = new $lazy_type$();\n"
421       "(($lazy_type$) result.$oneof_name$_).set(\n"
422       "    (($lazy_type$) $oneof_name$_));\n");
423   printer->Outdent();
424   printer->Print("}\n");
425 }
426 
427 void ImmutableLazyMessageOneofFieldGenerator::
GenerateParsingCode(io::Printer * printer) const428 GenerateParsingCode(io::Printer* printer) const {
429   printer->Print(variables_,
430     "if (!($has_oneof_case_message$)) {\n"
431     "  $oneof_name$_ = new $lazy_type$();\n"
432     "}\n"
433     "(($lazy_type$) $oneof_name$_).setByteString(\n"
434     "    input.readBytes(), extensionRegistry);\n"
435     "$set_oneof_case_message$;\n");
436 }
437 
438 void ImmutableLazyMessageOneofFieldGenerator::
GenerateSerializationCode(io::Printer * printer) const439 GenerateSerializationCode(io::Printer* printer) const {
440   // Do not de-serialize lazy fields.
441   printer->Print(variables_,
442     "if ($has_oneof_case_message$) {\n"
443     "  output.writeBytes(\n"
444     "      $number$, (($lazy_type$) $oneof_name$_).toByteString());\n"
445     "}\n");
446 }
447 
448 void ImmutableLazyMessageOneofFieldGenerator::
GenerateSerializedSizeCode(io::Printer * printer) const449 GenerateSerializedSizeCode(io::Printer* printer) const {
450   printer->Print(variables_,
451     "if ($has_oneof_case_message$) {\n"
452     "  size += com.google.protobuf.CodedOutputStream\n"
453     "    .computeLazyFieldSize($number$, ($lazy_type$) $oneof_name$_);\n"
454     "}\n");
455 }
456 
457 // ===================================================================
458 
459 RepeatedImmutableLazyMessageFieldGenerator::
RepeatedImmutableLazyMessageFieldGenerator(const FieldDescriptor * descriptor,int messageBitIndex,int builderBitIndex,Context * context)460 RepeatedImmutableLazyMessageFieldGenerator(
461     const FieldDescriptor* descriptor,
462     int messageBitIndex,
463     int builderBitIndex,
464     Context* context)
465     : RepeatedImmutableMessageFieldGenerator(
466           descriptor, messageBitIndex, builderBitIndex, context) {
467 }
468 
469 
470 RepeatedImmutableLazyMessageFieldGenerator::
~RepeatedImmutableLazyMessageFieldGenerator()471 ~RepeatedImmutableLazyMessageFieldGenerator() {}
472 
473 void RepeatedImmutableLazyMessageFieldGenerator::
GenerateMembers(io::Printer * printer) const474 GenerateMembers(io::Printer* printer) const {
475   printer->Print(variables_,
476     "private java.util.List<com.google.protobuf.LazyFieldLite> $name$_;\n");
477   PrintExtraFieldInfo(variables_, printer);
478   WriteFieldDocComment(printer, descriptor_);
479   printer->Print(variables_,
480     "$deprecation$public java.util.List<$type$>\n"
481     "    get$capitalized_name$List() {\n"
482     "  java.util.List<$type$> list =\n"
483     "      new java.util.ArrayList<$type$>($name$_.size());\n"
484     "  for (com.google.protobuf.LazyFieldLite lf : $name$_) {\n"
485     "    list.add(($type$) lf.getValue($type$.getDefaultInstance()));\n"
486     "  }\n"
487     "  return list;\n"
488     "}\n");
489   WriteFieldDocComment(printer, descriptor_);
490   printer->Print(variables_,
491     "$deprecation$public java.util.List<? extends $type$OrBuilder>\n"
492     "    get$capitalized_name$OrBuilderList() {\n"
493     "  return get$capitalized_name$List();\n"
494     "}\n");
495   WriteFieldDocComment(printer, descriptor_);
496   printer->Print(variables_,
497     "$deprecation$public int get$capitalized_name$Count() {\n"
498     "  return $name$_.size();\n"
499     "}\n");
500   WriteFieldDocComment(printer, descriptor_);
501   printer->Print(variables_,
502     "$deprecation$public $type$ get$capitalized_name$(int index) {\n"
503     "  return ($type$)\n"
504     "      $name$_.get(index).getValue($type$.getDefaultInstance());\n"
505     "}\n");
506   WriteFieldDocComment(printer, descriptor_);
507   printer->Print(variables_,
508     "$deprecation$public $type$OrBuilder get$capitalized_name$OrBuilder(\n"
509     "    int index) {\n"
510     "  return ($type$OrBuilder)\n"
511     "      $name$_.get(index).getValue($type$.getDefaultInstance());\n"
512     "}\n");
513 }
514 
515 void RepeatedImmutableLazyMessageFieldGenerator::
GenerateBuilderMembers(io::Printer * printer) const516 GenerateBuilderMembers(io::Printer* printer) const {
517   // When using nested-builders, the code initially works just like the
518   // non-nested builder case. It only creates a nested builder lazily on
519   // demand and then forever delegates to it after creation.
520 
521   printer->Print(variables_,
522     "private java.util.List<com.google.protobuf.LazyFieldLite> $name$_ =\n"
523     "  java.util.Collections.emptyList();\n"
524 
525     "private void ensure$capitalized_name$IsMutable() {\n"
526     "  if (!$get_mutable_bit_builder$) {\n"
527     "    $name$_ =\n"
528     "        new java.util.ArrayList<com.google.protobuf.LazyFieldLite>(\n"
529     "            $name$_);\n"
530     "    $set_mutable_bit_builder$;\n"
531     "   }\n"
532     "}\n"
533     "\n");
534 
535   printer->Print(variables_,
536     // If this builder is non-null, it is used and the other fields are
537     // ignored.
538     "private com.google.protobuf.RepeatedFieldBuilder<\n"
539     "    $type$, $type$.Builder, $type$OrBuilder> $name$Builder_;\n"
540     "\n");
541 
542   // The comments above the methods below are based on a hypothetical
543   // repeated field of type "Field" called "RepeatedField".
544 
545   // List<Field> getRepeatedFieldList()
546   WriteFieldDocComment(printer, descriptor_);
547   PrintNestedBuilderFunction(printer,
548     "$deprecation$public java.util.List<$type$> get$capitalized_name$List()",
549 
550     "java.util.List<$type$> list =\n"
551     "    new java.util.ArrayList<$type$>($name$_.size());\n"
552     "for (com.google.protobuf.LazyFieldLite lf : $name$_) {\n"
553     "  list.add(($type$) lf.getValue($type$.getDefaultInstance()));\n"
554     "}\n"
555     "return java.util.Collections.unmodifiableList(list);\n",
556 
557     "return $name$Builder_.getMessageList();\n",
558 
559     NULL);
560 
561   // int getRepeatedFieldCount()
562   WriteFieldDocComment(printer, descriptor_);
563   PrintNestedBuilderFunction(printer,
564     "$deprecation$public int get$capitalized_name$Count()",
565 
566     "return $name$_.size();\n",
567     "return $name$Builder_.getCount();\n",
568 
569     NULL);
570 
571   // Field getRepeatedField(int index)
572   WriteFieldDocComment(printer, descriptor_);
573   PrintNestedBuilderFunction(printer,
574     "$deprecation$public $type$ get$capitalized_name$(int index)",
575 
576     "return ($type$) $name$_.get(index).getValue(\n"
577     "    $type$.getDefaultInstance());\n",
578 
579     "return $name$Builder_.getMessage(index);\n",
580 
581     NULL);
582 
583   // Builder setRepeatedField(int index, Field value)
584   WriteFieldDocComment(printer, descriptor_);
585   PrintNestedBuilderFunction(printer,
586     "$deprecation$public Builder set$capitalized_name$(\n"
587     "    int index, $type$ value)",
588     "if (value == null) {\n"
589     "  throw new NullPointerException();\n"
590     "}\n"
591     "ensure$capitalized_name$IsMutable();\n"
592     "$name$_.set(index, com.google.protobuf.LazyFieldLite.fromValue(value));\n"
593     "$on_changed$\n",
594     "$name$Builder_.setMessage(index, value);\n",
595     "return this;\n");
596 
597   // Builder setRepeatedField(int index, Field.Builder builderForValue)
598   WriteFieldDocComment(printer, descriptor_);
599   PrintNestedBuilderFunction(printer,
600     "$deprecation$public Builder set$capitalized_name$(\n"
601     "    int index, $type$.Builder builderForValue)",
602 
603     "ensure$capitalized_name$IsMutable();\n"
604     "$name$_.set(index, com.google.protobuf.LazyFieldLite.fromValue(\n"
605     "    builderForValue.build()));\n"
606     "$on_changed$\n",
607 
608     "$name$Builder_.setMessage(index, builderForValue.build());\n",
609 
610     "return this;\n");
611 
612   // Builder addRepeatedField(Field value)
613   WriteFieldDocComment(printer, descriptor_);
614   PrintNestedBuilderFunction(printer,
615     "$deprecation$public Builder add$capitalized_name$($type$ value)",
616 
617     "if (value == null) {\n"
618     "  throw new NullPointerException();\n"
619     "}\n"
620     "ensure$capitalized_name$IsMutable();\n"
621     "$name$_.add(com.google.protobuf.LazyFieldLite.fromValue(value));\n"
622 
623     "$on_changed$\n",
624 
625     "$name$Builder_.addMessage(value);\n",
626 
627     "return this;\n");
628 
629   // Builder addRepeatedField(int index, Field value)
630   WriteFieldDocComment(printer, descriptor_);
631   PrintNestedBuilderFunction(printer,
632     "$deprecation$public Builder add$capitalized_name$(\n"
633     "    int index, $type$ value)",
634 
635     "if (value == null) {\n"
636     "  throw new NullPointerException();\n"
637     "}\n"
638     "ensure$capitalized_name$IsMutable();\n"
639     "$name$_.add(index, com.google.protobuf.LazyFieldLite.fromValue(value));\n"
640     "$on_changed$\n",
641 
642     "$name$Builder_.addMessage(index, value);\n",
643 
644     "return this;\n");
645 
646   // Builder addRepeatedField(Field.Builder builderForValue)
647   WriteFieldDocComment(printer, descriptor_);
648   PrintNestedBuilderFunction(printer,
649     "$deprecation$public Builder add$capitalized_name$(\n"
650     "    $type$.Builder builderForValue)",
651 
652     "ensure$capitalized_name$IsMutable();\n"
653     "$name$_.add(com.google.protobuf.LazyFieldLite.fromValue(\n"
654     "    builderForValue.build()));\n"
655     "$on_changed$\n",
656 
657     "$name$Builder_.addMessage(builderForValue.build());\n",
658 
659     "return this;\n");
660 
661   // Builder addRepeatedField(int index, Field.Builder builderForValue)
662   WriteFieldDocComment(printer, descriptor_);
663   PrintNestedBuilderFunction(printer,
664     "$deprecation$public Builder add$capitalized_name$(\n"
665     "    int index, $type$.Builder builderForValue)",
666 
667     "ensure$capitalized_name$IsMutable();\n"
668     "$name$_.add(index, com.google.protobuf.LazyFieldLite.fromValue(\n"
669     "    builderForValue.build()));\n"
670     "$on_changed$\n",
671 
672     "$name$Builder_.addMessage(index, builderForValue.build());\n",
673 
674     "return this;\n");
675 
676   // Builder addAllRepeatedField(Iterable<Field> values)
677   WriteFieldDocComment(printer, descriptor_);
678   PrintNestedBuilderFunction(printer,
679     "$deprecation$public Builder addAll$capitalized_name$(\n"
680     "    java.lang.Iterable<? extends $type$> values)",
681 
682     "ensure$capitalized_name$IsMutable();\n"
683     "for (com.google.protobuf.MessageLite v : values) {\n"
684     "  $name$_.add(com.google.protobuf.LazyFieldLite.fromValue(v));\n"
685     "}\n"
686     "$on_changed$\n",
687 
688     "$name$Builder_.addAllMessages(values);\n",
689 
690     "return this;\n");
691 
692   // Builder clearAllRepeatedField()
693   WriteFieldDocComment(printer, descriptor_);
694   PrintNestedBuilderFunction(printer,
695     "$deprecation$public Builder clear$capitalized_name$()",
696 
697     "$name$_ = java.util.Collections.emptyList();\n"
698     "$clear_mutable_bit_builder$;\n"
699     "$on_changed$\n",
700 
701     "$name$Builder_.clear();\n",
702 
703     "return this;\n");
704 
705   // Builder removeRepeatedField(int index)
706   WriteFieldDocComment(printer, descriptor_);
707   PrintNestedBuilderFunction(printer,
708     "$deprecation$public Builder remove$capitalized_name$(int index)",
709 
710     "ensure$capitalized_name$IsMutable();\n"
711     "$name$_.remove(index);\n"
712     "$on_changed$\n",
713 
714     "$name$Builder_.remove(index);\n",
715 
716     "return this;\n");
717 
718   WriteFieldDocComment(printer, descriptor_);
719   printer->Print(variables_,
720     "$deprecation$public $type$.Builder get$capitalized_name$Builder(\n"
721     "    int index) {\n"
722     "  return get$capitalized_name$FieldBuilder().getBuilder(index);\n"
723     "}\n");
724 
725   WriteFieldDocComment(printer, descriptor_);
726       printer->Print(variables_,
727     "$deprecation$public $type$OrBuilder get$capitalized_name$OrBuilder(\n"
728     "    int index) {\n"
729     "  if ($name$Builder_ == null) {\n"
730     "    return $name$_.get(index);"
731     "  } else {\n"
732     "    return $name$Builder_.getMessageOrBuilder(index);\n"
733     "  }\n"
734     "}\n");
735 
736   WriteFieldDocComment(printer, descriptor_);
737       printer->Print(variables_,
738     "$deprecation$public java.util.List<? extends $type$OrBuilder> \n"
739     "     get$capitalized_name$OrBuilderList() {\n"
740     "  if ($name$Builder_ != null) {\n"
741     "    return $name$Builder_.getMessageOrBuilderList();\n"
742     "  } else {\n"
743     "    return java.util.Collections.unmodifiableList($name$_);\n"
744     "  }\n"
745     "}\n");
746 
747   WriteFieldDocComment(printer, descriptor_);
748       printer->Print(variables_,
749     "$deprecation$public $type$.Builder add$capitalized_name$Builder() {\n"
750     "  return get$capitalized_name$FieldBuilder().addBuilder(\n"
751     "      $type$.getDefaultInstance());\n"
752     "}\n");
753   WriteFieldDocComment(printer, descriptor_);
754       printer->Print(variables_,
755     "$deprecation$public $type$.Builder add$capitalized_name$Builder(\n"
756     "    int index) {\n"
757     "  return get$capitalized_name$FieldBuilder().addBuilder(\n"
758     "      index, $type$.getDefaultInstance());\n"
759     "}\n");
760   WriteFieldDocComment(printer, descriptor_);
761       printer->Print(variables_,
762     "$deprecation$public java.util.List<$type$.Builder> \n"
763     "     get$capitalized_name$BuilderList() {\n"
764     "  return get$capitalized_name$FieldBuilder().getBuilderList();\n"
765     "}\n"
766     "private com.google.protobuf.RepeatedFieldBuilder<\n"
767     "    $type$, $type$.Builder, $type$OrBuilder> \n"
768     "    get$capitalized_name$FieldBuilder() {\n"
769     "  if ($name$Builder_ == null) {\n"
770     "    $name$Builder_ = new com.google.protobuf.RepeatedFieldBuilder<\n"
771     "        $type$, $type$.Builder, $type$OrBuilder>(\n"
772     "            $name$_,\n"
773     "            $get_mutable_bit_builder$,\n"
774     "            getParentForChildren(),\n"
775     "            isClean());\n"
776     "    $name$_ = null;\n"
777     "  }\n"
778     "  return $name$Builder_;\n"
779     "}\n");
780 }
781 
782 void RepeatedImmutableLazyMessageFieldGenerator::
GenerateParsingCode(io::Printer * printer) const783 GenerateParsingCode(io::Printer* printer) const {
784   printer->Print(variables_,
785     "if (!$get_mutable_bit_parser$) {\n"
786     "  $name$_ =\n"
787     "      new java.util.ArrayList<com.google.protobuf.LazyFieldLite>();\n"
788     "  $set_mutable_bit_parser$;\n"
789     "}\n"
790     "$name$_.add(new com.google.protobuf.LazyFieldLite(\n"
791     "    extensionRegistry, input.readBytes()));\n");
792 }
793 
794 void RepeatedImmutableLazyMessageFieldGenerator::
GenerateSerializationCode(io::Printer * printer) const795 GenerateSerializationCode(io::Printer* printer) const {
796   printer->Print(variables_,
797     "for (int i = 0; i < $name$_.size(); i++) {\n"
798     "  output.writeBytes($number$, $name$_.get(i).toByteString());\n"
799     "}\n");
800 }
801 
802 void RepeatedImmutableLazyMessageFieldGenerator::
GenerateSerializedSizeCode(io::Printer * printer) const803 GenerateSerializedSizeCode(io::Printer* printer) const {
804   printer->Print(variables_,
805     "for (int i = 0; i < $name$_.size(); i++) {\n"
806     "  size += com.google.protobuf.CodedOutputStream\n"
807     "    .computeLazyFieldSize($number$, $name$_.get(i));\n"
808     "}\n");
809 }
810 
811 }  // namespace java
812 }  // namespace compiler
813 }  // namespace protobuf
814 }  // namespace google
815