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