• 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_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::
ImmutableLazyMessageFieldLiteGenerator(const FieldDescriptor * descriptor,int messageBitIndex,int builderBitIndex,Context * context)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::
~ImmutableLazyMessageFieldLiteGenerator()57 ~ImmutableLazyMessageFieldLiteGenerator() {}
58 
59 void ImmutableLazyMessageFieldLiteGenerator::
GenerateMembers(io::Printer * printer) const60 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::
GenerateBuilderMembers(io::Printer * printer) const123 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::
GenerateInitializationCode(io::Printer * printer) const180 GenerateInitializationCode(io::Printer* printer) const {
181   printer->Print(variables_, "$name$_.clear();\n");
182 }
183 
184 void ImmutableLazyMessageFieldLiteGenerator::
GenerateVisitCode(io::Printer * printer) const185 GenerateVisitCode(io::Printer* printer) const {
186   printer->Print(variables_,
187     "$name$_ = visitor.visitLazyMessage(\n"
188     "    has$capitalized_name$(), $name$_,\n"
189     "    other.has$capitalized_name$(), other.$name$_);\n");
190 }
191 
192 void ImmutableLazyMessageFieldLiteGenerator::
GenerateParsingCode(io::Printer * printer) const193 GenerateParsingCode(io::Printer* printer) const {
194   printer->Print(variables_,
195     "$name$_.mergeFrom(input, extensionRegistry);\n");
196   printer->Print(variables_,
197     "$set_has_field_bit_message$;\n");
198 }
199 
200 void ImmutableLazyMessageFieldLiteGenerator::
GenerateSerializationCode(io::Printer * printer) const201 GenerateSerializationCode(io::Printer* printer) const {
202   // Do not de-serialize lazy fields.
203   printer->Print(variables_,
204     "if ($get_has_field_bit_message$) {\n"
205     "  output.writeBytes($number$, $name$_.toByteString());\n"
206     "}\n");
207 }
208 
209 void ImmutableLazyMessageFieldLiteGenerator::
GenerateSerializedSizeCode(io::Printer * printer) const210 GenerateSerializedSizeCode(io::Printer* printer) const {
211   printer->Print(variables_,
212     "if ($get_has_field_bit_message$) {\n"
213     "  size += com.google.protobuf.CodedOutputStream\n"
214     "    .computeLazyFieldSize($number$, $name$_);\n"
215     "}\n");
216 }
217 
218 // ===================================================================
219 
220 ImmutableLazyMessageOneofFieldLiteGenerator::
ImmutableLazyMessageOneofFieldLiteGenerator(const FieldDescriptor * descriptor,int messageBitIndex,int builderBitIndex,Context * context)221 ImmutableLazyMessageOneofFieldLiteGenerator(const FieldDescriptor* descriptor,
222                                             int messageBitIndex,
223                                             int builderBitIndex,
224                                             Context* context)
225     : ImmutableLazyMessageFieldLiteGenerator(
226           descriptor, messageBitIndex, builderBitIndex, context) {
227   const OneofGeneratorInfo* info =
228       context->GetOneofGeneratorInfo(descriptor->containing_oneof());
229   SetCommonOneofVariables(descriptor, info, &variables_);
230   variables_["lazy_type"] = "com.google.protobuf.LazyFieldLite";
231 }
232 
233 ImmutableLazyMessageOneofFieldLiteGenerator::
~ImmutableLazyMessageOneofFieldLiteGenerator()234 ~ImmutableLazyMessageOneofFieldLiteGenerator() {}
235 
236 void ImmutableLazyMessageOneofFieldLiteGenerator::
GenerateMembers(io::Printer * printer) const237 GenerateMembers(io::Printer* printer) const {
238   PrintExtraFieldInfo(variables_, printer);
239   WriteFieldDocComment(printer, descriptor_);
240 
241   printer->Print(variables_,
242     "$deprecation$public boolean has$capitalized_name$() {\n"
243     "  return $has_oneof_case_message$;\n"
244     "}\n");
245   WriteFieldDocComment(printer, descriptor_);
246 
247   printer->Print(variables_,
248     "$deprecation$public $type$ get$capitalized_name$() {\n"
249     "  if ($has_oneof_case_message$) {\n"
250     "    return ($type$) (($lazy_type$) $oneof_name$_).getValue(\n"
251     "        $type$.getDefaultInstance());\n"
252     "  }\n"
253     "  return $type$.getDefaultInstance();\n"
254     "}\n");
255 
256   // Field.Builder setField(Field value)
257   WriteFieldDocComment(printer, descriptor_);
258   printer->Print(variables_,
259     "private void set$capitalized_name$($type$ value) {\n"
260     "  if (value == null) {\n"
261     "    throw new NullPointerException();\n"
262     "  }\n"
263     "  if (!($has_oneof_case_message$)) {\n"
264     "    $oneof_name$_ = new $lazy_type$();\n"
265     "    $set_oneof_case_message$;\n"
266     "  }\n"
267     "  (($lazy_type$) $oneof_name$_).setValue(value);\n"
268     "}\n");
269 
270   // Field.Builder setField(Field.Builder builderForValue)
271   WriteFieldDocComment(printer, descriptor_);
272   printer->Print(variables_,
273     "private void set$capitalized_name$(\n"
274     "    $type$.Builder builderForValue) {\n"
275     "  if (!($has_oneof_case_message$)) {\n"
276     "    $oneof_name$_ = new $lazy_type$();\n"
277     "    $set_oneof_case_message$;\n"
278     "  }\n"
279     "  (($lazy_type$) $oneof_name$_).setValue(builderForValue.build());\n"
280     "}\n");
281 
282   // Field.Builder mergeField(Field value)
283   WriteFieldDocComment(printer, descriptor_);
284   printer->Print(variables_,
285     "private void merge$capitalized_name$($type$ value) {\n"
286     "  if ($has_oneof_case_message$ &&\n"
287     "      !(($lazy_type$) $oneof_name$_).containsDefaultInstance()) {\n"
288     "    (($lazy_type$) $oneof_name$_).setValue(\n"
289     "       $type$.newBuilder(\n"
290     "          get$capitalized_name$()).mergeFrom(value).buildPartial());\n"
291     "  } else {\n"
292     "    if (!($has_oneof_case_message$)) {\n"
293     "      $oneof_name$_ = new $lazy_type$();\n"
294     "      $set_oneof_case_message$;\n"
295     "    }\n"
296     "    (($lazy_type$) $oneof_name$_).setValue(value);\n"
297     "  }\n"
298     "}\n");
299 
300   // Field.Builder clearField()
301   WriteFieldDocComment(printer, descriptor_);
302   printer->Print(variables_,
303     "private void clear$capitalized_name$() {\n"
304     "  if ($has_oneof_case_message$) {\n"
305     "    $clear_oneof_case_message$;\n"
306     "    $oneof_name$_ = null;\n"
307     "  }\n"
308     "}\n");
309 }
310 
311 void ImmutableLazyMessageOneofFieldLiteGenerator::
GenerateBuilderMembers(io::Printer * printer) const312 GenerateBuilderMembers(io::Printer* printer) const {
313   // boolean hasField()
314   WriteFieldDocComment(printer, descriptor_);
315   printer->Print(variables_,
316     "$deprecation$public boolean has$capitalized_name$() {\n"
317     "  return instance.has$capitalized_name$();\n"
318     "}\n");
319 
320   printer->Print(variables_,
321     "$deprecation$public $type$ get$capitalized_name$() {\n"
322     "  return instance.get$capitalized_name$();\n"
323     "}\n");
324 
325   // Field.Builder setField(Field value)
326   WriteFieldDocComment(printer, descriptor_);
327   printer->Print(variables_,
328     "$deprecation$public Builder set$capitalized_name$($type$ value) {\n"
329     "  copyOnWrite();\n"
330     "  instance.set$capitalized_name$(value);\n"
331     "  return this;\n"
332     "}\n");
333 
334   // Field.Builder setField(Field.Builder builderForValue)
335   WriteFieldDocComment(printer, descriptor_);
336   printer->Print(variables_,
337     "$deprecation$public Builder set$capitalized_name$(\n"
338     "    $type$.Builder builderForValue) {\n"
339     "  copyOnWrite();\n"
340     "  instance.set$capitalized_name$(builderForValue);\n"
341     "  return this;\n"
342     "}\n");
343 
344   // Field.Builder mergeField(Field value)
345   WriteFieldDocComment(printer, descriptor_);
346   printer->Print(variables_,
347     "$deprecation$public Builder merge$capitalized_name$($type$ value) {\n"
348     "  copyOnWrite();\n"
349     "  instance.merge$capitalized_name$(value);\n"
350     "  return this;\n"
351     "}\n");
352 
353   // Field.Builder clearField()
354   WriteFieldDocComment(printer, descriptor_);
355   printer->Print(variables_,
356     "$deprecation$public Builder clear$capitalized_name$() {\n"
357     "  copyOnWrite();\n"
358     "  instance.clear$capitalized_name$();\n"
359     "  return this;\n"
360     "}\n");
361 }
362 
363 void ImmutableLazyMessageOneofFieldLiteGenerator::
GenerateVisitCode(io::Printer * printer) const364 GenerateVisitCode(io::Printer* printer) const {
365   printer->Print(variables_,
366     "$oneof_name$_ = visitor.visitOneofLazyMessage(\n"
367     "    $has_oneof_case_message$,\n"
368     "    ($lazy_type$) $oneof_name$_,\n"
369     "    ($lazy_type$) other.$oneof_name$_);\n");
370 }
371 
372 void ImmutableLazyMessageOneofFieldLiteGenerator::
GenerateParsingCode(io::Printer * printer) const373 GenerateParsingCode(io::Printer* printer) const {
374   printer->Print(variables_,
375     "if (!($has_oneof_case_message$)) {\n"
376     "  $oneof_name$_ = new $lazy_type$();\n"
377     "}\n"
378     "(($lazy_type$) $oneof_name$_).mergeFrom(input, extensionRegistry);\n"
379     "$set_oneof_case_message$;\n");
380 }
381 
382 void ImmutableLazyMessageOneofFieldLiteGenerator::
GenerateSerializationCode(io::Printer * printer) const383 GenerateSerializationCode(io::Printer* printer) const {
384   // Do not de-serialize lazy fields.
385   printer->Print(variables_,
386     "if ($has_oneof_case_message$) {\n"
387     "  output.writeBytes(\n"
388     "      $number$, (($lazy_type$) $oneof_name$_).toByteString());\n"
389     "}\n");
390 }
391 
392 void ImmutableLazyMessageOneofFieldLiteGenerator::
GenerateSerializedSizeCode(io::Printer * printer) const393 GenerateSerializedSizeCode(io::Printer* printer) const {
394   printer->Print(variables_,
395     "if ($has_oneof_case_message$) {\n"
396     "  size += com.google.protobuf.CodedOutputStream\n"
397     "    .computeLazyFieldSize($number$, ($lazy_type$) $oneof_name$_);\n"
398     "}\n");
399 }
400 
401 // ===================================================================
402 
403 RepeatedImmutableLazyMessageFieldLiteGenerator::
RepeatedImmutableLazyMessageFieldLiteGenerator(const FieldDescriptor * descriptor,int messageBitIndex,int builderBitIndex,Context * context)404 RepeatedImmutableLazyMessageFieldLiteGenerator(
405     const FieldDescriptor* descriptor,
406     int messageBitIndex,
407     int builderBitIndex,
408     Context* context)
409     : RepeatedImmutableMessageFieldLiteGenerator(
410           descriptor, messageBitIndex, builderBitIndex, context) {
411 }
412 
413 
414 RepeatedImmutableLazyMessageFieldLiteGenerator::
~RepeatedImmutableLazyMessageFieldLiteGenerator()415 ~RepeatedImmutableLazyMessageFieldLiteGenerator() {}
416 
417 void RepeatedImmutableLazyMessageFieldLiteGenerator::
GenerateMembers(io::Printer * printer) const418 GenerateMembers(io::Printer* printer) const {
419   printer->Print(variables_,
420     "private com.google.protobuf.Internal.ProtobufList<\n"
421     "    com.google.protobuf.LazyFieldLite> $name$_;\n");
422   PrintExtraFieldInfo(variables_, printer);
423   WriteFieldDocComment(printer, descriptor_);
424   printer->Print(variables_,
425     "$deprecation$public java.util.List<$type$>\n"
426     "    get$capitalized_name$List() {\n"
427     "  java.util.List<$type$> list =\n"
428     "      new java.util.ArrayList<$type$>($name$_.size());\n"
429     "  for (com.google.protobuf.LazyFieldLite lf : $name$_) {\n"
430     "    list.add(($type$) lf.getValue($type$.getDefaultInstance()));\n"
431     "  }\n"
432     // TODO(dweis): Make this list immutable?
433     "  return list;\n"
434     "}\n");
435   WriteFieldDocComment(printer, descriptor_);
436   printer->Print(variables_,
437     "$deprecation$public java.util.List<? extends $type$OrBuilder>\n"
438     "    get$capitalized_name$OrBuilderList() {\n"
439     "  return get$capitalized_name$List();\n"
440     "}\n");
441   WriteFieldDocComment(printer, descriptor_);
442   printer->Print(variables_,
443     "$deprecation$public int get$capitalized_name$Count() {\n"
444     "  return $name$_.size();\n"
445     "}\n");
446   WriteFieldDocComment(printer, descriptor_);
447   printer->Print(variables_,
448     "$deprecation$public $type$ get$capitalized_name$(int index) {\n"
449     "  return ($type$)\n"
450     "      $name$_.get(index).getValue($type$.getDefaultInstance());\n"
451     "}\n");
452   WriteFieldDocComment(printer, descriptor_);
453   printer->Print(variables_,
454     "$deprecation$public $type$OrBuilder get$capitalized_name$OrBuilder(\n"
455     "    int index) {\n"
456     "  return ($type$OrBuilder)\n"
457     "      $name$_.get(index).getValue($type$.getDefaultInstance());\n"
458     "}\n");
459 
460   printer->Print(variables_,
461     "private void ensure$capitalized_name$IsMutable() {\n"
462     "  if (!$is_mutable$) {\n"
463     "    $name$_ =\n"
464     "        com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n"
465     "   }\n"
466     "}\n"
467     "\n");
468 
469   // Builder setRepeatedField(int index, Field value)
470   WriteFieldDocComment(printer, descriptor_);
471   printer->Print(variables_,
472     "private void set$capitalized_name$(\n"
473     "    int index, $type$ value) {\n"
474     "  if (value == null) {\n"
475     "    throw new NullPointerException();\n"
476     "  }\n"
477     "  ensure$capitalized_name$IsMutable();\n"
478     "  $name$_.set(\n"
479     "      index, com.google.protobuf.LazyFieldLite.fromValue(value));\n"
480     "}\n");
481 
482   // Builder setRepeatedField(int index, Field.Builder builderForValue)
483   WriteFieldDocComment(printer, descriptor_);
484   printer->Print(variables_,
485     "private void set$capitalized_name$(\n"
486     "    int index, $type$.Builder builderForValue) {\n"
487     "  ensure$capitalized_name$IsMutable();\n"
488     "  $name$_.set(index, com.google.protobuf.LazyFieldLite.fromValue(\n"
489     "      builderForValue.build()));\n"
490     "}\n");
491 
492   // Builder addRepeatedField(Field value)
493   WriteFieldDocComment(printer, descriptor_);
494   printer->Print(variables_,
495     "private void add$capitalized_name$($type$ value) {\n"
496     "  if (value == null) {\n"
497     "    throw new NullPointerException();\n"
498     "  }\n"
499     "  ensure$capitalized_name$IsMutable();\n"
500     "  $name$_.add(com.google.protobuf.LazyFieldLite.fromValue(value));\n"
501     "}\n");
502 
503   // Builder addRepeatedField(int index, Field value)
504   WriteFieldDocComment(printer, descriptor_);
505   printer->Print(variables_,
506     "private void add$capitalized_name$(\n"
507     "    int index, $type$ value) {\n"
508     "  if (value == null) {\n"
509     "    throw new NullPointerException();\n"
510     "  }\n"
511     "  ensure$capitalized_name$IsMutable();\n"
512     "  $name$_.add(\n"
513     "      index, com.google.protobuf.LazyFieldLite.fromValue(value));\n"
514     "}\n");
515 
516   // Builder addRepeatedField(Field.Builder builderForValue)
517   WriteFieldDocComment(printer, descriptor_);
518   printer->Print(variables_,
519     "private void add$capitalized_name$(\n"
520     "    $type$.Builder builderForValue) {\n"
521     "  ensure$capitalized_name$IsMutable();\n"
522     "  $name$_.add(com.google.protobuf.LazyFieldLite.fromValue(\n"
523     "      builderForValue.build()));\n"
524     "}\n");
525 
526   // Builder addRepeatedField(int index, Field.Builder builderForValue)
527   WriteFieldDocComment(printer, descriptor_);
528   printer->Print(variables_,
529     "private void add$capitalized_name$(\n"
530     "    int index, $type$.Builder builderForValue) {\n"
531     "  ensure$capitalized_name$IsMutable();\n"
532     "  $name$_.add(index, com.google.protobuf.LazyFieldLite.fromValue(\n"
533     "      builderForValue.build()));\n"
534     "}\n");
535 
536   // Builder addAllRepeatedField(Iterable<Field> values)
537   WriteFieldDocComment(printer, descriptor_);
538   printer->Print(variables_,
539     "private void addAll$capitalized_name$(\n"
540     "    java.lang.Iterable<? extends $type$> values) {\n"
541     "  ensure$capitalized_name$IsMutable();\n"
542     "  for (com.google.protobuf.MessageLite v : values) {\n"
543     "    $name$_.add(com.google.protobuf.LazyFieldLite.fromValue(v));\n"
544     "  }\n"
545     "}\n");
546 
547   // Builder clearAllRepeatedField()
548   WriteFieldDocComment(printer, descriptor_);
549   printer->Print(variables_,
550     "private void clear$capitalized_name$() {\n"
551     "  $name$_ = emptyProtobufList();\n"
552     "}\n");
553 
554   // Builder removeRepeatedField(int index)
555   WriteFieldDocComment(printer, descriptor_);
556   printer->Print(variables_,
557     "private void remove$capitalized_name$(int index) {\n"
558     "  ensure$capitalized_name$IsMutable();\n"
559     "  $name$_.remove(index);\n"
560     "}\n");
561 }
562 
563 void RepeatedImmutableLazyMessageFieldLiteGenerator::
GenerateBuilderMembers(io::Printer * printer) const564 GenerateBuilderMembers(io::Printer* printer) const {
565   // List<Field> getRepeatedFieldList()
566   WriteFieldDocComment(printer, descriptor_);
567   printer->Print(variables_,
568     "$deprecation$public java.util.List<$type$> get$capitalized_name$List() {\n"
569     "  return java.util.Collections.unmodifiableList(\n"
570     "      instance.get$capitalized_name$List());\n"
571     "}\n");
572 
573   // int getRepeatedFieldCount()
574   WriteFieldDocComment(printer, descriptor_);
575   printer->Print(variables_,
576     "$deprecation$public int get$capitalized_name$Count() {\n"
577     "  return instance.get$capitalized_name$Count();\n"
578     "}\n");
579 
580   // Field getRepeatedField(int index)
581   WriteFieldDocComment(printer, descriptor_);
582   printer->Print(variables_,
583     "$deprecation$public $type$ get$capitalized_name$(int index) {\n"
584     "  return instance.get$capitalized_name$(index);\n"
585     "}\n");
586 
587   // Builder setRepeatedField(int index, Field value)
588   WriteFieldDocComment(printer, descriptor_);
589   printer->Print(variables_,
590     "$deprecation$public Builder set$capitalized_name$(\n"
591     "    int index, $type$ value) {\n"
592     "  copyOnWrite();\n"
593     "  instance.set$capitalized_name$(index, value);\n"
594     "  return this;\n"
595     "}\n");
596 
597   // Builder setRepeatedField(int index, Field.Builder builderForValue)
598   WriteFieldDocComment(printer, descriptor_);
599   printer->Print(variables_,
600     "$deprecation$public Builder set$capitalized_name$(\n"
601     "    int index, $type$.Builder builderForValue) {\n"
602     "  copyOnWrite();\n"
603     "  instance.set$capitalized_name$(index, builderForValue);\n"
604     "  return this;\n"
605     "}\n");
606 
607   // Builder addRepeatedField(Field value)
608   WriteFieldDocComment(printer, descriptor_);
609   printer->Print(variables_,
610     "$deprecation$public Builder add$capitalized_name$($type$ value) {\n"
611     "  copyOnWrite();\n"
612     "  instance.add$capitalized_name$(value);\n"
613     "  return this;\n"
614     "}\n");
615 
616   // Builder addRepeatedField(int index, Field value)
617   WriteFieldDocComment(printer, descriptor_);
618   printer->Print(variables_,
619     "$deprecation$public Builder add$capitalized_name$(\n"
620     "    int index, $type$ value) {\n"
621     "  copyOnWrite();\n"
622     "  instance.add$capitalized_name$(index, value);\n"
623     "  return this;\n"
624     "}\n");
625 
626   // Builder addRepeatedField(Field.Builder builderForValue)
627   WriteFieldDocComment(printer, descriptor_);
628   printer->Print(variables_,
629     "$deprecation$public Builder add$capitalized_name$(\n"
630     "    $type$.Builder builderForValue) {\n"
631     "  copyOnWrite();\n"
632     "  instance.add$capitalized_name$(builderForValue);\n"
633     "  return this;\n"
634     "}\n");
635 
636   // Builder addRepeatedField(int index, Field.Builder builderForValue)
637   WriteFieldDocComment(printer, descriptor_);
638   printer->Print(variables_,
639     "$deprecation$public Builder add$capitalized_name$(\n"
640     "    int index, $type$.Builder builderForValue) {\n"
641     "  copyOnWrite();\n"
642     "  instance.add$capitalized_name$(index, builderForValue);\n"
643     "  return this;\n"
644     "}\n");
645 
646   // Builder addAllRepeatedField(Iterable<Field> values)
647   WriteFieldDocComment(printer, descriptor_);
648   printer->Print(variables_,
649     "$deprecation$public Builder addAll$capitalized_name$(\n"
650     "    java.lang.Iterable<? extends $type$> values) {\n"
651     "  copyOnWrite();\n"
652     "  instance.addAll$capitalized_name$(values);\n"
653     "  return this;\n"
654     "}\n");
655 
656   // Builder clearAllRepeatedField()
657   WriteFieldDocComment(printer, descriptor_);
658   printer->Print(variables_,
659     "$deprecation$public Builder clear$capitalized_name$() {\n"
660     "  copyOnWrite();\n"
661     "  instance.clear$capitalized_name$();\n"
662     "  return this;\n"
663     "}\n");
664 
665   // Builder removeRepeatedField(int index)
666   WriteFieldDocComment(printer, descriptor_);
667   printer->Print(variables_,
668     "$deprecation$public Builder remove$capitalized_name$(int index) {\n"
669     "  copyOnWrite();\n"
670     "  instance.remove$capitalized_name$(index);\n"
671     "  return this;\n"
672     "}\n");
673 }
674 
675 void RepeatedImmutableLazyMessageFieldLiteGenerator::
GenerateParsingCode(io::Printer * printer) const676 GenerateParsingCode(io::Printer* printer) const {
677   printer->Print(variables_,
678     "if (!$is_mutable$) {\n"
679     "  $name$_ =\n"
680     "      com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n"
681     "}\n"
682     "$name$_.add(new com.google.protobuf.LazyFieldLite(\n"
683     "    extensionRegistry, input.readBytes()));\n");
684 }
685 
686 void RepeatedImmutableLazyMessageFieldLiteGenerator::
GenerateSerializationCode(io::Printer * printer) const687 GenerateSerializationCode(io::Printer* printer) const {
688   printer->Print(variables_,
689     "for (int i = 0; i < $name$_.size(); i++) {\n"
690     "  output.writeBytes($number$, $name$_.get(i).toByteString());\n"
691     "}\n");
692 }
693 
694 void RepeatedImmutableLazyMessageFieldLiteGenerator::
GenerateSerializedSizeCode(io::Printer * printer) const695 GenerateSerializedSizeCode(io::Printer* printer) const {
696   printer->Print(variables_,
697     "for (int i = 0; i < $name$_.size(); i++) {\n"
698     "  size += com.google.protobuf.CodedOutputStream\n"
699     "    .computeLazyFieldSize($number$, $name$_.get(i));\n"
700     "}\n");
701 }
702 
703 }  // namespace java
704 }  // namespace compiler
705 }  // namespace protobuf
706 }  // namespace google
707