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$_;");
63 
64   PrintExtraFieldInfo(variables_, printer);
65   WriteFieldDocComment(printer, descriptor_);
66   if (SupportFieldPresence(descriptor_->file())) {
67     printer->Print(variables_,
68       "$deprecation$public boolean has$capitalized_name$() {\n"
69       "  return $get_has_field_bit_message$;\n"
70       "}\n");
71   } else {
72     printer->Print(variables_,
73       "$deprecation$public boolean has$capitalized_name$() {\n"
74       "  return $name$_ != null;\n"
75       "}\n");
76   }
77 
78   WriteFieldDocComment(printer, descriptor_);
79   printer->Print(variables_,
80     "$deprecation$public $type$ get$capitalized_name$() {\n"
81     "  if ($name$_ == null) {\n"
82     "    return $type$.getDefaultInstance();\n"
83     "  }\n"
84     "  return ($type$) $name$_.getValue($type$.getDefaultInstance());\n"
85    "}\n");
86 
87   // Field.Builder setField(Field value)
88   WriteFieldDocComment(printer, descriptor_);
89   printer->Print(variables_,
90     "private void set$capitalized_name$($type$ value) {\n"
91     "  if (value == null) {\n"
92     "    throw new NullPointerException();\n"
93     "  }\n"
94     "  if ($name$_ == null) {\n"
95     "    $name$_ = new com.google.protobuf.LazyFieldLite();\n"
96     "  }\n"
97     "  $name$_.setValue(value);\n"
98     "  $set_has_field_bit_message$\n"
99     "}\n");
100 
101   // Field.Builder setField(Field.Builder builderForValue)
102   WriteFieldDocComment(printer, descriptor_);
103   printer->Print(variables_,
104     "private void set$capitalized_name$(\n"
105     "    $type$.Builder builderForValue) {\n"
106     "  if ($name$_ == null) {\n"
107     "    $name$_ = new com.google.protobuf.LazyFieldLite();\n"
108     "  }\n"
109     "  $name$_.setValue(builderForValue.build());\n"
110     "  $set_has_field_bit_message$\n"
111     "}\n");
112 
113   // Field.Builder mergeField(Field value)
114   WriteFieldDocComment(printer, descriptor_);
115   printer->Print(variables_,
116     "private void merge$capitalized_name$($type$ value) {\n"
117     "  if (has$capitalized_name$() &&\n"
118     "      !$name$_.containsDefaultInstance()) {\n"
119     "    $name$_.setValue(\n"
120     "      $type$.newBuilder(\n"
121     "          get$capitalized_name$()).mergeFrom(value).buildPartial());\n"
122     "  } else {\n"
123     "    if ($name$_ == null) {\n"
124     "      $name$_ = new com.google.protobuf.LazyFieldLite();\n"
125     "    }\n"
126     "    $name$_.setValue(value);\n"
127     "    $set_has_field_bit_message$\n"
128     "  }\n"
129     "}\n");
130 
131   // Field.Builder clearField()
132   WriteFieldDocComment(printer, descriptor_);
133   printer->Print(variables_,
134     "private void clear$capitalized_name$() {\n"
135     "  $name$_ = null;\n"
136     "  $clear_has_field_bit_message$;\n"
137     "}\n");
138 }
139 
140 void ImmutableLazyMessageFieldLiteGenerator::
GenerateBuilderMembers(io::Printer * printer) const141 GenerateBuilderMembers(io::Printer* printer) const {
142   // The comments above the methods below are based on a hypothetical
143   // field of type "Field" called "Field".
144 
145   // boolean hasField()
146   WriteFieldDocComment(printer, descriptor_);
147   printer->Print(variables_,
148     "$deprecation$public boolean has$capitalized_name$() {\n"
149     "  return instance.has$capitalized_name$();\n"
150     "}\n");
151 
152   WriteFieldDocComment(printer, descriptor_);
153   printer->Print(variables_,
154     "$deprecation$public $type$ get$capitalized_name$() {\n"
155     "  return instance.get$capitalized_name$();\n"
156     "}\n");
157 
158   // Field.Builder setField(Field value)
159   WriteFieldDocComment(printer, descriptor_);
160   printer->Print(variables_,
161     "$deprecation$public Builder set$capitalized_name$($type$ value) {\n"
162     "  copyOnWrite();\n"
163     "  instance.set$capitalized_name$(value);\n"
164     "  return this;\n"
165     "}\n");
166 
167   // Field.Builder setField(Field.Builder builderForValue)
168   WriteFieldDocComment(printer, descriptor_);
169   printer->Print(variables_,
170     "$deprecation$public Builder set$capitalized_name$(\n"
171     "    $type$.Builder builderForValue) {\n"
172     "  copyOnWrite();\n"
173     "  instance.set$capitalized_name$(builderForValue);\n"
174     "  return this;\n"
175     "}\n");
176 
177   // Field.Builder mergeField(Field value)
178   WriteFieldDocComment(printer, descriptor_);
179   printer->Print(variables_,
180     "$deprecation$public Builder merge$capitalized_name$($type$ value) {\n"
181     "  copyOnWrite();\n"
182     "  instance.merge$capitalized_name$(value);\n"
183     "  return this;\n"
184     "}\n");
185 
186   // Field.Builder clearField()
187   WriteFieldDocComment(printer, descriptor_);
188   printer->Print(variables_,
189     "$deprecation$public Builder clear$capitalized_name$() {\n"
190     "  copyOnWrite();\n"
191     "  instance.clear$capitalized_name$();\n"
192     "  return this;\n"
193     "}\n");
194 }
195 
196 
197 void ImmutableLazyMessageFieldLiteGenerator::
GenerateInitializationCode(io::Printer * printer) const198 GenerateInitializationCode(io::Printer* printer) const {}
199 
200 void ImmutableLazyMessageFieldLiteGenerator::
GenerateVisitCode(io::Printer * printer) const201 GenerateVisitCode(io::Printer* printer) const {
202   printer->Print(variables_,
203     "$name$_ = visitor.visitLazyMessage($name$_, other.$name$_);\n");
204 }
205 
206 void ImmutableLazyMessageFieldLiteGenerator::
GenerateParsingCode(io::Printer * printer) const207 GenerateParsingCode(io::Printer* printer) const {
208   printer->Print(variables_,
209     "if ($name$_ == null) {\n"
210     "  $name$_ = new com.google.protobuf.LazyFieldLite();\n"
211     "}\n"
212     "$name$_.mergeFrom(input, extensionRegistry);\n");
213   printer->Print(variables_,
214     "$set_has_field_bit_message$\n");
215 }
216 
217 void ImmutableLazyMessageFieldLiteGenerator::
GenerateSerializationCode(io::Printer * printer) const218 GenerateSerializationCode(io::Printer* printer) const {
219   // Do not de-serialize lazy fields.
220   printer->Print(variables_,
221     "if (has$capitalized_name$()) {\n"
222     "  output.writeBytes($number$, $name$_.toByteString());\n"
223     "}\n");
224 }
225 
226 void ImmutableLazyMessageFieldLiteGenerator::
GenerateSerializedSizeCode(io::Printer * printer) const227 GenerateSerializedSizeCode(io::Printer* printer) const {
228   printer->Print(variables_,
229     "if (has$capitalized_name$()) {\n"
230     "  size += com.google.protobuf.CodedOutputStream\n"
231     "    .computeLazyFieldSize($number$, $name$_);\n"
232     "}\n");
233 }
234 
235 // ===================================================================
236 
237 ImmutableLazyMessageOneofFieldLiteGenerator::
ImmutableLazyMessageOneofFieldLiteGenerator(const FieldDescriptor * descriptor,int messageBitIndex,int builderBitIndex,Context * context)238 ImmutableLazyMessageOneofFieldLiteGenerator(const FieldDescriptor* descriptor,
239                                             int messageBitIndex,
240                                             int builderBitIndex,
241                                             Context* context)
242     : ImmutableLazyMessageFieldLiteGenerator(
243           descriptor, messageBitIndex, builderBitIndex, context) {
244   const OneofGeneratorInfo* info =
245       context->GetOneofGeneratorInfo(descriptor->containing_oneof());
246   SetCommonOneofVariables(descriptor, info, &variables_);
247   variables_["lazy_type"] = "com.google.protobuf.LazyFieldLite";
248 }
249 
250 ImmutableLazyMessageOneofFieldLiteGenerator::
~ImmutableLazyMessageOneofFieldLiteGenerator()251 ~ImmutableLazyMessageOneofFieldLiteGenerator() {}
252 
253 void ImmutableLazyMessageOneofFieldLiteGenerator::
GenerateMembers(io::Printer * printer) const254 GenerateMembers(io::Printer* printer) const {
255   PrintExtraFieldInfo(variables_, printer);
256   WriteFieldDocComment(printer, descriptor_);
257 
258   printer->Print(variables_,
259     "$deprecation$public boolean has$capitalized_name$() {\n"
260     "  return $has_oneof_case_message$;\n"
261     "}\n");
262   WriteFieldDocComment(printer, descriptor_);
263 
264   printer->Print(variables_,
265     "$deprecation$public $type$ get$capitalized_name$() {\n"
266     "  if ($has_oneof_case_message$) {\n"
267     "    return ($type$) (($lazy_type$) $oneof_name$_).getValue(\n"
268     "        $type$.getDefaultInstance());\n"
269     "  }\n"
270     "  return $type$.getDefaultInstance();\n"
271     "}\n");
272 
273   // Field.Builder setField(Field value)
274   WriteFieldDocComment(printer, descriptor_);
275   printer->Print(variables_,
276     "private void set$capitalized_name$($type$ value) {\n"
277     "  if (value == null) {\n"
278     "    throw new NullPointerException();\n"
279     "  }\n"
280     "  if (!($has_oneof_case_message$)) {\n"
281     "    $oneof_name$_ = new $lazy_type$();\n"
282     "    $set_oneof_case_message$;\n"
283     "  }\n"
284     "  (($lazy_type$) $oneof_name$_).setValue(value);\n"
285     "}\n");
286 
287   // Field.Builder setField(Field.Builder builderForValue)
288   WriteFieldDocComment(printer, descriptor_);
289   printer->Print(variables_,
290     "private void set$capitalized_name$(\n"
291     "    $type$.Builder builderForValue) {\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(builderForValue.build());\n"
297     "}\n");
298 
299   // Field.Builder mergeField(Field value)
300   WriteFieldDocComment(printer, descriptor_);
301   printer->Print(variables_,
302     "private void merge$capitalized_name$($type$ value) {\n"
303     "  if ($has_oneof_case_message$ &&\n"
304     "      !(($lazy_type$) $oneof_name$_).containsDefaultInstance()) {\n"
305     "    (($lazy_type$) $oneof_name$_).setValue(\n"
306     "       $type$.newBuilder(\n"
307     "          get$capitalized_name$()).mergeFrom(value).buildPartial());\n"
308     "  } else {\n"
309     "    if (!($has_oneof_case_message$)) {\n"
310     "      $oneof_name$_ = new $lazy_type$();\n"
311     "      $set_oneof_case_message$;\n"
312     "    }\n"
313     "    (($lazy_type$) $oneof_name$_).setValue(value);\n"
314     "  }\n"
315     "}\n");
316 
317   // Field.Builder clearField()
318   WriteFieldDocComment(printer, descriptor_);
319   printer->Print(variables_,
320     "private void clear$capitalized_name$() {\n"
321     "  if ($has_oneof_case_message$) {\n"
322     "    $clear_oneof_case_message$;\n"
323     "    $oneof_name$_ = null;\n"
324     "  }\n"
325     "}\n");
326 }
327 
328 void ImmutableLazyMessageOneofFieldLiteGenerator::
GenerateBuilderMembers(io::Printer * printer) const329 GenerateBuilderMembers(io::Printer* printer) const {
330   // boolean hasField()
331   WriteFieldDocComment(printer, descriptor_);
332   printer->Print(variables_,
333     "$deprecation$public boolean has$capitalized_name$() {\n"
334     "  return instance.has$capitalized_name$();\n"
335     "}\n");
336 
337   printer->Print(variables_,
338     "$deprecation$public $type$ get$capitalized_name$() {\n"
339     "  return instance.get$capitalized_name$();\n"
340     "}\n");
341 
342   // Field.Builder setField(Field value)
343   WriteFieldDocComment(printer, descriptor_);
344   printer->Print(variables_,
345     "$deprecation$public Builder set$capitalized_name$($type$ value) {\n"
346     "  copyOnWrite();\n"
347     "  instance.set$capitalized_name$(value);\n"
348     "  return this;\n"
349     "}\n");
350 
351   // Field.Builder setField(Field.Builder builderForValue)
352   WriteFieldDocComment(printer, descriptor_);
353   printer->Print(variables_,
354     "$deprecation$public Builder set$capitalized_name$(\n"
355     "    $type$.Builder builderForValue) {\n"
356     "  copyOnWrite();\n"
357     "  instance.set$capitalized_name$(builderForValue);\n"
358     "  return this;\n"
359     "}\n");
360 
361   // Field.Builder mergeField(Field value)
362   WriteFieldDocComment(printer, descriptor_);
363   printer->Print(variables_,
364     "$deprecation$public Builder merge$capitalized_name$($type$ value) {\n"
365     "  copyOnWrite();\n"
366     "  instance.merge$capitalized_name$(value);\n"
367     "  return this;\n"
368     "}\n");
369 
370   // Field.Builder clearField()
371   WriteFieldDocComment(printer, descriptor_);
372   printer->Print(variables_,
373     "$deprecation$public Builder clear$capitalized_name$() {\n"
374     "  copyOnWrite();\n"
375     "  instance.clear$capitalized_name$();\n"
376     "  return this;\n"
377     "}\n");
378 }
379 
380 void ImmutableLazyMessageOneofFieldLiteGenerator::
GenerateVisitCode(io::Printer * printer) const381 GenerateVisitCode(io::Printer* printer) const {
382   printer->Print(variables_,
383     "$oneof_name$_ = visitor.visitOneofLazyMessage(\n"
384     "    $has_oneof_case_message$,\n"
385     "    ($lazy_type$) $oneof_name$_,\n"
386     "    ($lazy_type$) other.$oneof_name$_);\n");
387 }
388 
389 void ImmutableLazyMessageOneofFieldLiteGenerator::
GenerateParsingCode(io::Printer * printer) const390 GenerateParsingCode(io::Printer* printer) const {
391   printer->Print(variables_,
392     "if (!($has_oneof_case_message$)) {\n"
393     "  $oneof_name$_ = new $lazy_type$();\n"
394     "}\n"
395     "(($lazy_type$) $oneof_name$_).mergeFrom(input, extensionRegistry);\n"
396     "$set_oneof_case_message$;\n");
397 }
398 
399 void ImmutableLazyMessageOneofFieldLiteGenerator::
GenerateSerializationCode(io::Printer * printer) const400 GenerateSerializationCode(io::Printer* printer) const {
401   // Do not de-serialize lazy fields.
402   printer->Print(variables_,
403     "if ($has_oneof_case_message$) {\n"
404     "  output.writeBytes(\n"
405     "      $number$, (($lazy_type$) $oneof_name$_).toByteString());\n"
406     "}\n");
407 }
408 
409 void ImmutableLazyMessageOneofFieldLiteGenerator::
GenerateSerializedSizeCode(io::Printer * printer) const410 GenerateSerializedSizeCode(io::Printer* printer) const {
411   printer->Print(variables_,
412     "if ($has_oneof_case_message$) {\n"
413     "  size += com.google.protobuf.CodedOutputStream\n"
414     "    .computeLazyFieldSize($number$, ($lazy_type$) $oneof_name$_);\n"
415     "}\n");
416 }
417 
418 // ===================================================================
419 
420 RepeatedImmutableLazyMessageFieldLiteGenerator::
RepeatedImmutableLazyMessageFieldLiteGenerator(const FieldDescriptor * descriptor,int messageBitIndex,int builderBitIndex,Context * context)421 RepeatedImmutableLazyMessageFieldLiteGenerator(
422     const FieldDescriptor* descriptor,
423     int messageBitIndex,
424     int builderBitIndex,
425     Context* context)
426     : RepeatedImmutableMessageFieldLiteGenerator(
427           descriptor, messageBitIndex, builderBitIndex, context) {
428 }
429 
430 
431 RepeatedImmutableLazyMessageFieldLiteGenerator::
~RepeatedImmutableLazyMessageFieldLiteGenerator()432 ~RepeatedImmutableLazyMessageFieldLiteGenerator() {}
433 
434 void RepeatedImmutableLazyMessageFieldLiteGenerator::
GenerateMembers(io::Printer * printer) const435 GenerateMembers(io::Printer* printer) const {
436   printer->Print(variables_,
437     "private com.google.protobuf.Internal.ProtobufList<\n"
438     "    com.google.protobuf.LazyFieldLite> $name$_;\n");
439   PrintExtraFieldInfo(variables_, printer);
440   WriteFieldDocComment(printer, descriptor_);
441   printer->Print(variables_,
442     "$deprecation$public java.util.List<$type$>\n"
443     "    get$capitalized_name$List() {\n"
444     "  java.util.List<$type$> list =\n"
445     "      new java.util.ArrayList<$type$>($name$_.size());\n"
446     "  for (com.google.protobuf.LazyFieldLite lf : $name$_) {\n"
447     "    list.add(($type$) lf.getValue($type$.getDefaultInstance()));\n"
448     "  }\n"
449     // TODO(dweis): Make this list immutable?
450     "  return list;\n"
451     "}\n");
452   WriteFieldDocComment(printer, descriptor_);
453   printer->Print(variables_,
454     "$deprecation$public java.util.List<? extends $type$OrBuilder>\n"
455     "    get$capitalized_name$OrBuilderList() {\n"
456     "  return get$capitalized_name$List();\n"
457     "}\n");
458   WriteFieldDocComment(printer, descriptor_);
459   printer->Print(variables_,
460     "$deprecation$public int get$capitalized_name$Count() {\n"
461     "  return $name$_.size();\n"
462     "}\n");
463   WriteFieldDocComment(printer, descriptor_);
464   printer->Print(variables_,
465     "$deprecation$public $type$ get$capitalized_name$(int index) {\n"
466     "  return ($type$)\n"
467     "      $name$_.get(index).getValue($type$.getDefaultInstance());\n"
468     "}\n");
469   WriteFieldDocComment(printer, descriptor_);
470   printer->Print(variables_,
471     "$deprecation$public $type$OrBuilder get$capitalized_name$OrBuilder(\n"
472     "    int index) {\n"
473     "  return ($type$OrBuilder)\n"
474     "      $name$_.get(index).getValue($type$.getDefaultInstance());\n"
475     "}\n");
476 
477   printer->Print(variables_,
478     "private void ensure$capitalized_name$IsMutable() {\n"
479     "  if (!$is_mutable$) {\n"
480     "    $name$_ =\n"
481     "        com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n"
482     "   }\n"
483     "}\n"
484     "\n");
485 
486   // Builder setRepeatedField(int index, Field value)
487   WriteFieldDocComment(printer, descriptor_);
488   printer->Print(variables_,
489     "private void set$capitalized_name$(\n"
490     "    int index, $type$ value) {\n"
491     "  if (value == null) {\n"
492     "    throw new NullPointerException();\n"
493     "  }\n"
494     "  ensure$capitalized_name$IsMutable();\n"
495     "  $name$_.set(\n"
496     "      index, com.google.protobuf.LazyFieldLite.fromValue(value));\n"
497     "}\n");
498 
499   // Builder setRepeatedField(int index, Field.Builder builderForValue)
500   WriteFieldDocComment(printer, descriptor_);
501   printer->Print(variables_,
502     "private void set$capitalized_name$(\n"
503     "    int index, $type$.Builder builderForValue) {\n"
504     "  ensure$capitalized_name$IsMutable();\n"
505     "  $name$_.set(index, com.google.protobuf.LazyFieldLite.fromValue(\n"
506     "      builderForValue.build()));\n"
507     "}\n");
508 
509   // Builder addRepeatedField(Field value)
510   WriteFieldDocComment(printer, descriptor_);
511   printer->Print(variables_,
512     "private void add$capitalized_name$($type$ value) {\n"
513     "  if (value == null) {\n"
514     "    throw new NullPointerException();\n"
515     "  }\n"
516     "  ensure$capitalized_name$IsMutable();\n"
517     "  $name$_.add(com.google.protobuf.LazyFieldLite.fromValue(value));\n"
518     "}\n");
519 
520   // Builder addRepeatedField(int index, Field value)
521   WriteFieldDocComment(printer, descriptor_);
522   printer->Print(variables_,
523     "private void add$capitalized_name$(\n"
524     "    int index, $type$ value) {\n"
525     "  if (value == null) {\n"
526     "    throw new NullPointerException();\n"
527     "  }\n"
528     "  ensure$capitalized_name$IsMutable();\n"
529     "  $name$_.add(\n"
530     "      index, com.google.protobuf.LazyFieldLite.fromValue(value));\n"
531     "}\n");
532 
533   // Builder addRepeatedField(Field.Builder builderForValue)
534   WriteFieldDocComment(printer, descriptor_);
535   printer->Print(variables_,
536     "private void add$capitalized_name$(\n"
537     "    $type$.Builder builderForValue) {\n"
538     "  ensure$capitalized_name$IsMutable();\n"
539     "  $name$_.add(com.google.protobuf.LazyFieldLite.fromValue(\n"
540     "      builderForValue.build()));\n"
541     "}\n");
542 
543   // Builder addRepeatedField(int index, Field.Builder builderForValue)
544   WriteFieldDocComment(printer, descriptor_);
545   printer->Print(variables_,
546     "private void add$capitalized_name$(\n"
547     "    int index, $type$.Builder builderForValue) {\n"
548     "  ensure$capitalized_name$IsMutable();\n"
549     "  $name$_.add(index, com.google.protobuf.LazyFieldLite.fromValue(\n"
550     "      builderForValue.build()));\n"
551     "}\n");
552 
553   // Builder addAllRepeatedField(Iterable<Field> values)
554   WriteFieldDocComment(printer, descriptor_);
555   printer->Print(variables_,
556     "private void addAll$capitalized_name$(\n"
557     "    java.lang.Iterable<? extends $type$> values) {\n"
558     "  ensure$capitalized_name$IsMutable();\n"
559     "  for (com.google.protobuf.MessageLite v : values) {\n"
560     "    $name$_.add(com.google.protobuf.LazyFieldLite.fromValue(v));\n"
561     "  }\n"
562     "}\n");
563 
564   // Builder clearAllRepeatedField()
565   WriteFieldDocComment(printer, descriptor_);
566   printer->Print(variables_,
567     "private void clear$capitalized_name$() {\n"
568     "  $name$_ = emptyProtobufList();\n"
569     "}\n");
570 
571   // Builder removeRepeatedField(int index)
572   WriteFieldDocComment(printer, descriptor_);
573   printer->Print(variables_,
574     "private void remove$capitalized_name$(int index) {\n"
575     "  ensure$capitalized_name$IsMutable();\n"
576     "  $name$_.remove(index);\n"
577     "}\n");
578 }
579 
580 void RepeatedImmutableLazyMessageFieldLiteGenerator::
GenerateBuilderMembers(io::Printer * printer) const581 GenerateBuilderMembers(io::Printer* printer) const {
582   // List<Field> getRepeatedFieldList()
583   WriteFieldDocComment(printer, descriptor_);
584   printer->Print(variables_,
585     "$deprecation$public java.util.List<$type$> get$capitalized_name$List() {\n"
586     "  return java.util.Collections.unmodifiableList(\n"
587     "      instance.get$capitalized_name$List());\n"
588     "}\n");
589 
590   // int getRepeatedFieldCount()
591   WriteFieldDocComment(printer, descriptor_);
592   printer->Print(variables_,
593     "$deprecation$public int get$capitalized_name$Count() {\n"
594     "  return instance.get$capitalized_name$Count();\n"
595     "}\n");
596 
597   // Field getRepeatedField(int index)
598   WriteFieldDocComment(printer, descriptor_);
599   printer->Print(variables_,
600     "$deprecation$public $type$ get$capitalized_name$(int index) {\n"
601     "  return instance.get$capitalized_name$(index);\n"
602     "}\n");
603 
604   // Builder setRepeatedField(int index, Field value)
605   WriteFieldDocComment(printer, descriptor_);
606   printer->Print(variables_,
607     "$deprecation$public Builder set$capitalized_name$(\n"
608     "    int index, $type$ value) {\n"
609     "  copyOnWrite();\n"
610     "  instance.set$capitalized_name$(index, value);\n"
611     "  return this;\n"
612     "}\n");
613 
614   // Builder setRepeatedField(int index, Field.Builder builderForValue)
615   WriteFieldDocComment(printer, descriptor_);
616   printer->Print(variables_,
617     "$deprecation$public Builder set$capitalized_name$(\n"
618     "    int index, $type$.Builder builderForValue) {\n"
619     "  copyOnWrite();\n"
620     "  instance.set$capitalized_name$(index, builderForValue);\n"
621     "  return this;\n"
622     "}\n");
623 
624   // Builder addRepeatedField(Field value)
625   WriteFieldDocComment(printer, descriptor_);
626   printer->Print(variables_,
627     "$deprecation$public Builder add$capitalized_name$($type$ value) {\n"
628     "  copyOnWrite();\n"
629     "  instance.add$capitalized_name$(value);\n"
630     "  return this;\n"
631     "}\n");
632 
633   // Builder addRepeatedField(int index, Field value)
634   WriteFieldDocComment(printer, descriptor_);
635   printer->Print(variables_,
636     "$deprecation$public Builder add$capitalized_name$(\n"
637     "    int index, $type$ value) {\n"
638     "  copyOnWrite();\n"
639     "  instance.add$capitalized_name$(index, value);\n"
640     "  return this;\n"
641     "}\n");
642 
643   // Builder addRepeatedField(Field.Builder builderForValue)
644   WriteFieldDocComment(printer, descriptor_);
645   printer->Print(variables_,
646     "$deprecation$public Builder add$capitalized_name$(\n"
647     "    $type$.Builder builderForValue) {\n"
648     "  copyOnWrite();\n"
649     "  instance.add$capitalized_name$(builderForValue);\n"
650     "  return this;\n"
651     "}\n");
652 
653   // Builder addRepeatedField(int index, Field.Builder builderForValue)
654   WriteFieldDocComment(printer, descriptor_);
655   printer->Print(variables_,
656     "$deprecation$public Builder add$capitalized_name$(\n"
657     "    int index, $type$.Builder builderForValue) {\n"
658     "  copyOnWrite();\n"
659     "  instance.add$capitalized_name$(index, builderForValue);\n"
660     "  return this;\n"
661     "}\n");
662 
663   // Builder addAllRepeatedField(Iterable<Field> values)
664   WriteFieldDocComment(printer, descriptor_);
665   printer->Print(variables_,
666     "$deprecation$public Builder addAll$capitalized_name$(\n"
667     "    java.lang.Iterable<? extends $type$> values) {\n"
668     "  copyOnWrite();\n"
669     "  instance.addAll$capitalized_name$(values);\n"
670     "  return this;\n"
671     "}\n");
672 
673   // Builder clearAllRepeatedField()
674   WriteFieldDocComment(printer, descriptor_);
675   printer->Print(variables_,
676     "$deprecation$public Builder clear$capitalized_name$() {\n"
677     "  copyOnWrite();\n"
678     "  instance.clear$capitalized_name$();\n"
679     "  return this;\n"
680     "}\n");
681 
682   // Builder removeRepeatedField(int index)
683   WriteFieldDocComment(printer, descriptor_);
684   printer->Print(variables_,
685     "$deprecation$public Builder remove$capitalized_name$(int index) {\n"
686     "  copyOnWrite();\n"
687     "  instance.remove$capitalized_name$(index);\n"
688     "  return this;\n"
689     "}\n");
690 }
691 
692 void RepeatedImmutableLazyMessageFieldLiteGenerator::
GenerateParsingCode(io::Printer * printer) const693 GenerateParsingCode(io::Printer* printer) const {
694   printer->Print(variables_,
695     "if (!$is_mutable$) {\n"
696     "  $name$_ =\n"
697     "      com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n"
698     "}\n"
699     "$name$_.add(new com.google.protobuf.LazyFieldLite(\n"
700     "    extensionRegistry, input.readBytes()));\n");
701 }
702 
703 void RepeatedImmutableLazyMessageFieldLiteGenerator::
GenerateSerializationCode(io::Printer * printer) const704 GenerateSerializationCode(io::Printer* printer) const {
705   printer->Print(variables_,
706     "for (int i = 0; i < $name$_.size(); i++) {\n"
707     "  output.writeBytes($number$, $name$_.get(i).toByteString());\n"
708     "}\n");
709 }
710 
711 void RepeatedImmutableLazyMessageFieldLiteGenerator::
GenerateSerializedSizeCode(io::Printer * printer) const712 GenerateSerializedSizeCode(io::Printer* printer) const {
713   printer->Print(variables_,
714     "for (int i = 0; i < $name$_.size(); i++) {\n"
715     "  size += com.google.protobuf.CodedOutputStream\n"
716     "    .computeLazyFieldSize($number$, $name$_.get(i));\n"
717     "}\n");
718 }
719 
720 }  // namespace java
721 }  // namespace compiler
722 }  // namespace protobuf
723 }  // namespace google
724