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: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34
35 #include <map>
36 #include <string>
37
38 #include <google/protobuf/compiler/java/java_context.h>
39 #include <google/protobuf/compiler/java/java_doc_comment.h>
40 #include <google/protobuf/compiler/java/java_helpers.h>
41 #include <google/protobuf/compiler/java/java_message_field_lite.h>
42 #include <google/protobuf/compiler/java/java_name_resolver.h>
43 #include <google/protobuf/io/printer.h>
44 #include <google/protobuf/wire_format.h>
45 #include <google/protobuf/stubs/strutil.h>
46
47
48 namespace google {
49 namespace protobuf {
50 namespace compiler {
51 namespace java {
52
53 namespace {
54
SetMessageVariables(const FieldDescriptor * descriptor,int messageBitIndex,int builderBitIndex,const FieldGeneratorInfo * info,ClassNameResolver * name_resolver,std::map<std::string,std::string> * variables)55 void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex,
56 int builderBitIndex, const FieldGeneratorInfo* info,
57 ClassNameResolver* name_resolver,
58 std::map<std::string, std::string>* variables) {
59 SetCommonFieldVariables(descriptor, info, variables);
60
61 (*variables)["type"] =
62 name_resolver->GetImmutableClassName(descriptor->message_type());
63 (*variables)["mutable_type"] =
64 name_resolver->GetMutableClassName(descriptor->message_type());
65 (*variables)["group_or_message"] =
66 (GetType(descriptor) == FieldDescriptor::TYPE_GROUP) ? "Group"
67 : "Message";
68 // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
69 // by the proto compiler
70 (*variables)["deprecation"] =
71 descriptor->options().deprecated() ? "@java.lang.Deprecated " : "";
72 (*variables)["required"] = descriptor->is_required() ? "true" : "false";
73
74 if (SupportFieldPresence(descriptor->file())) {
75 // For singular messages and builders, one bit is used for the hasField bit.
76 (*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex);
77
78 // Note that these have a trailing ";".
79 (*variables)["set_has_field_bit_message"] =
80 GenerateSetBit(messageBitIndex) + ";";
81 (*variables)["clear_has_field_bit_message"] =
82 GenerateClearBit(messageBitIndex) + ";";
83
84 (*variables)["is_field_present_message"] = GenerateGetBit(messageBitIndex);
85 } else {
86 (*variables)["set_has_field_bit_message"] = "";
87 (*variables)["clear_has_field_bit_message"] = "";
88
89 (*variables)["is_field_present_message"] =
90 (*variables)["name"] + "_ != null";
91 }
92
93 // For repeated builders, the underlying list tracks mutability state.
94 (*variables)["is_mutable"] = (*variables)["name"] + "_.isModifiable()";
95
96 (*variables)["get_has_field_bit_from_local"] =
97 GenerateGetBitFromLocal(builderBitIndex);
98 (*variables)["set_has_field_bit_to_local"] =
99 GenerateSetBitToLocal(messageBitIndex);
100 }
101
102 } // namespace
103
104 // ===================================================================
105
ImmutableMessageFieldLiteGenerator(const FieldDescriptor * descriptor,int messageBitIndex,Context * context)106 ImmutableMessageFieldLiteGenerator::ImmutableMessageFieldLiteGenerator(
107 const FieldDescriptor* descriptor, int messageBitIndex, Context* context)
108 : descriptor_(descriptor),
109 messageBitIndex_(messageBitIndex),
110 name_resolver_(context->GetNameResolver()) {
111 SetMessageVariables(descriptor, messageBitIndex, 0,
112 context->GetFieldGeneratorInfo(descriptor),
113 name_resolver_, &variables_);
114 }
115
~ImmutableMessageFieldLiteGenerator()116 ImmutableMessageFieldLiteGenerator::~ImmutableMessageFieldLiteGenerator() {}
117
GetNumBitsForMessage() const118 int ImmutableMessageFieldLiteGenerator::GetNumBitsForMessage() const {
119 return SupportFieldPresence(descriptor_->file()) ? 1 : 0;
120 }
121
GenerateInterfaceMembers(io::Printer * printer) const122 void ImmutableMessageFieldLiteGenerator::GenerateInterfaceMembers(
123 io::Printer* printer) const {
124 WriteFieldDocComment(printer, descriptor_);
125 printer->Print(variables_, "$deprecation$boolean has$capitalized_name$();\n");
126 WriteFieldDocComment(printer, descriptor_);
127 printer->Print(variables_, "$deprecation$$type$ get$capitalized_name$();\n");
128 }
129
GenerateMembers(io::Printer * printer) const130 void ImmutableMessageFieldLiteGenerator::GenerateMembers(
131 io::Printer* printer) const {
132
133 printer->Print(variables_, "private $type$ $name$_;\n");
134 PrintExtraFieldInfo(variables_, printer);
135
136 if (SupportFieldPresence(descriptor_->file())) {
137 WriteFieldDocComment(printer, descriptor_);
138 printer->Print(
139 variables_,
140 "@java.lang.Override\n"
141 "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
142 " return $get_has_field_bit_message$;\n"
143 "}\n");
144 printer->Annotate("{", "}", descriptor_);
145 WriteFieldDocComment(printer, descriptor_);
146 printer->Print(
147 variables_,
148 "@java.lang.Override\n"
149 "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n"
150 " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n"
151 "}\n");
152 printer->Annotate("{", "}", descriptor_);
153 } else {
154 WriteFieldDocComment(printer, descriptor_);
155 printer->Print(
156 variables_,
157 "@java.lang.Override\n"
158 "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
159 " return $name$_ != null;\n"
160 "}\n");
161 printer->Annotate("{", "}", descriptor_);
162 WriteFieldDocComment(printer, descriptor_);
163 printer->Print(
164 variables_,
165 "@java.lang.Override\n"
166 "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n"
167 " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n"
168 "}\n");
169 printer->Annotate("{", "}", descriptor_);
170 }
171
172 // Field.Builder setField(Field value)
173 WriteFieldDocComment(printer, descriptor_);
174 printer->Print(variables_,
175 "private void set$capitalized_name$($type$ value) {\n"
176 " if (value == null) {\n"
177 " throw new NullPointerException();\n"
178 " }\n"
179 " $name$_ = value;\n"
180 " $set_has_field_bit_message$\n"
181 " }\n");
182
183 // Field.Builder setField(Field.Builder builderForValue)
184 WriteFieldDocComment(printer, descriptor_);
185 printer->Print(variables_,
186 "private void set$capitalized_name$(\n"
187 " $type$.Builder builderForValue) {\n"
188 " $name$_ = builderForValue.build();\n"
189 " $set_has_field_bit_message$\n"
190 "}\n");
191
192 // Field.Builder mergeField(Field value)
193 WriteFieldDocComment(printer, descriptor_);
194 printer->Print(
195 variables_,
196 "@java.lang.SuppressWarnings({\"ReferenceEquality\"})\n"
197 "private void merge$capitalized_name$($type$ value) {\n"
198 " if (value == null) {\n"
199 " throw new NullPointerException();\n"
200 " }\n"
201 " if ($name$_ != null &&\n"
202 " $name$_ != $type$.getDefaultInstance()) {\n"
203 " $name$_ =\n"
204 " $type$.newBuilder($name$_).mergeFrom(value).buildPartial();\n"
205 " } else {\n"
206 " $name$_ = value;\n"
207 " }\n"
208 " $set_has_field_bit_message$\n"
209 "}\n");
210
211 // Field.Builder clearField()
212 WriteFieldDocComment(printer, descriptor_);
213 printer->Print(variables_,
214 "private void clear$capitalized_name$() {"
215 " $name$_ = null;\n"
216 " $clear_has_field_bit_message$\n"
217 "}\n");
218 }
219
GenerateBuilderMembers(io::Printer * printer) const220 void ImmutableMessageFieldLiteGenerator::GenerateBuilderMembers(
221 io::Printer* printer) const {
222 // The comments above the methods below are based on a hypothetical
223 // field of type "Field" called "Field".
224
225 // boolean hasField()
226 WriteFieldDocComment(printer, descriptor_);
227 printer->Print(variables_,
228 "@java.lang.Override\n"
229 "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
230 " return instance.has$capitalized_name$();\n"
231 "}\n");
232 printer->Annotate("{", "}", descriptor_);
233
234 // Field getField()
235 WriteFieldDocComment(printer, descriptor_);
236 printer->Print(variables_,
237 "@java.lang.Override\n"
238 "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n"
239 " return instance.get$capitalized_name$();\n"
240 "}\n");
241 printer->Annotate("{", "}", descriptor_);
242
243 // Field.Builder setField(Field value)
244 WriteFieldDocComment(printer, descriptor_);
245 printer->Print(variables_,
246 "$deprecation$public Builder "
247 "${$set$capitalized_name$$}$($type$ value) {\n"
248 " copyOnWrite();\n"
249 " instance.set$capitalized_name$(value);\n"
250 " return this;\n"
251 " }\n");
252 printer->Annotate("{", "}", descriptor_);
253
254 // Field.Builder setField(Field.Builder builderForValue)
255 WriteFieldDocComment(printer, descriptor_);
256 printer->Print(variables_,
257 "$deprecation$public Builder ${$set$capitalized_name$$}$(\n"
258 " $type$.Builder builderForValue) {\n"
259 " copyOnWrite();\n"
260 " instance.set$capitalized_name$(builderForValue);\n"
261 " return this;\n"
262 "}\n");
263 printer->Annotate("{", "}", descriptor_);
264
265 // Field.Builder mergeField(Field value)
266 WriteFieldDocComment(printer, descriptor_);
267 printer->Print(variables_,
268 "$deprecation$public Builder "
269 "${$merge$capitalized_name$$}$($type$ value) {\n"
270 " copyOnWrite();\n"
271 " instance.merge$capitalized_name$(value);\n"
272 " return this;\n"
273 "}\n");
274 printer->Annotate("{", "}", descriptor_);
275
276 // Field.Builder clearField()
277 WriteFieldDocComment(printer, descriptor_);
278 printer->Print(variables_,
279 "$deprecation$public Builder ${$clear$capitalized_name$$}$() {"
280 " copyOnWrite();\n"
281 " instance.clear$capitalized_name$();\n"
282 " return this;\n"
283 "}\n");
284 printer->Annotate("{", "}", descriptor_);
285 }
286
287
GenerateFieldInfo(io::Printer * printer,std::vector<uint16> * output) const288 void ImmutableMessageFieldLiteGenerator::GenerateFieldInfo(
289 io::Printer* printer, std::vector<uint16>* output) const {
290 WriteIntToUtf16CharSequence(descriptor_->number(), output);
291 WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
292 output);
293 if (SupportFieldPresence(descriptor_->file())) {
294 WriteIntToUtf16CharSequence(messageBitIndex_, output);
295 }
296 printer->Print(variables_, "\"$name$_\",\n");
297 }
298
GenerateInitializationCode(io::Printer * printer) const299 void ImmutableMessageFieldLiteGenerator::GenerateInitializationCode(
300 io::Printer* printer) const {}
301
GetBoxedType() const302 std::string ImmutableMessageFieldLiteGenerator::GetBoxedType() const {
303 return name_resolver_->GetImmutableClassName(descriptor_->message_type());
304 }
305
306 // ===================================================================
307
308 ImmutableMessageOneofFieldLiteGenerator::
ImmutableMessageOneofFieldLiteGenerator(const FieldDescriptor * descriptor,int messageBitIndex,Context * context)309 ImmutableMessageOneofFieldLiteGenerator(const FieldDescriptor* descriptor,
310 int messageBitIndex,
311 Context* context)
312 : ImmutableMessageFieldLiteGenerator(descriptor, messageBitIndex, context) {
313 const OneofGeneratorInfo* info =
314 context->GetOneofGeneratorInfo(descriptor->containing_oneof());
315 SetCommonOneofVariables(descriptor, info, &variables_);
316 }
317
318 ImmutableMessageOneofFieldLiteGenerator::
~ImmutableMessageOneofFieldLiteGenerator()319 ~ImmutableMessageOneofFieldLiteGenerator() {}
320
GenerateMembers(io::Printer * printer) const321 void ImmutableMessageOneofFieldLiteGenerator::GenerateMembers(
322 io::Printer* printer) const {
323 PrintExtraFieldInfo(variables_, printer);
324 WriteFieldDocComment(printer, descriptor_);
325 printer->Print(variables_,
326 "@java.lang.Override\n"
327 "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
328 " return $has_oneof_case_message$;\n"
329 "}\n");
330 printer->Annotate("{", "}", descriptor_);
331 WriteFieldDocComment(printer, descriptor_);
332 printer->Print(variables_,
333 "@java.lang.Override\n"
334 "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n"
335 " if ($has_oneof_case_message$) {\n"
336 " return ($type$) $oneof_name$_;\n"
337 " }\n"
338 " return $type$.getDefaultInstance();\n"
339 "}\n");
340 printer->Annotate("{", "}", descriptor_);
341
342 // Field.Builder setField(Field value)
343 WriteFieldDocComment(printer, descriptor_);
344 printer->Print(variables_,
345 "private void set$capitalized_name$($type$ value) {\n"
346 " if (value == null) {\n"
347 " throw new NullPointerException();\n"
348 " }\n"
349 " $oneof_name$_ = value;\n"
350 " $set_oneof_case_message$;\n"
351 "}\n");
352
353 // Field.Builder setField(Field.Builder builderForValue)
354 WriteFieldDocComment(printer, descriptor_);
355 printer->Print(variables_,
356 "private void set$capitalized_name$(\n"
357 " $type$.Builder builderForValue) {\n"
358 " $oneof_name$_ = builderForValue.build();\n"
359 " $set_oneof_case_message$;\n"
360 "}\n");
361
362 // Field.Builder mergeField(Field value)
363 WriteFieldDocComment(printer, descriptor_);
364 printer->Print(
365 variables_,
366 "private void merge$capitalized_name$($type$ value) {\n"
367 " if (value == null) {\n"
368 " throw new NullPointerException();\n"
369 " }\n"
370 " if ($has_oneof_case_message$ &&\n"
371 " $oneof_name$_ != $type$.getDefaultInstance()) {\n"
372 " $oneof_name$_ = $type$.newBuilder(($type$) $oneof_name$_)\n"
373 " .mergeFrom(value).buildPartial();\n"
374 " } else {\n"
375 " $oneof_name$_ = value;\n"
376 " }\n"
377 " $set_oneof_case_message$;\n"
378 "}\n");
379
380 // Field.Builder clearField()
381 WriteFieldDocComment(printer, descriptor_);
382 printer->Print(variables_,
383 "private void clear$capitalized_name$() {\n"
384 " if ($has_oneof_case_message$) {\n"
385 " $clear_oneof_case_message$;\n"
386 " $oneof_name$_ = null;\n"
387 " }\n"
388 "}\n");
389 }
390
GenerateFieldInfo(io::Printer * printer,std::vector<uint16> * output) const391 void ImmutableMessageOneofFieldLiteGenerator::GenerateFieldInfo(
392 io::Printer* printer, std::vector<uint16>* output) const {
393 WriteIntToUtf16CharSequence(descriptor_->number(), output);
394 WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
395 output);
396 WriteIntToUtf16CharSequence(descriptor_->containing_oneof()->index(), output);
397 printer->Print(variables_, "$oneof_stored_type$.class,\n");
398 }
399
GenerateBuilderMembers(io::Printer * printer) const400 void ImmutableMessageOneofFieldLiteGenerator::GenerateBuilderMembers(
401 io::Printer* printer) const {
402 // The comments above the methods below are based on a hypothetical
403 // field of type "Field" called "Field".
404
405 // boolean hasField()
406 WriteFieldDocComment(printer, descriptor_);
407 printer->Print(variables_,
408 "@java.lang.Override\n"
409 "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
410 " return instance.has$capitalized_name$();\n"
411 "}\n");
412 printer->Annotate("{", "}", descriptor_);
413
414 // Field getField()
415 WriteFieldDocComment(printer, descriptor_);
416 printer->Print(variables_,
417 "@java.lang.Override\n"
418 "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n"
419 " return instance.get$capitalized_name$();\n"
420 "}\n");
421 printer->Annotate("{", "}", descriptor_);
422
423 // Field.Builder setField(Field value)
424 WriteFieldDocComment(printer, descriptor_);
425 printer->Print(variables_,
426 "$deprecation$public Builder "
427 "${$set$capitalized_name$$}$($type$ value) {\n"
428 " copyOnWrite();\n"
429 " instance.set$capitalized_name$(value);\n"
430 " return this;\n"
431 "}\n");
432 printer->Annotate("{", "}", descriptor_);
433
434 // Field.Builder setField(Field.Builder builderForValue)
435 WriteFieldDocComment(printer, descriptor_);
436 printer->Print(variables_,
437 "$deprecation$public Builder ${$set$capitalized_name$$}$(\n"
438 " $type$.Builder builderForValue) {\n"
439 " copyOnWrite();\n"
440 " instance.set$capitalized_name$(builderForValue);\n"
441 " return this;\n"
442 "}\n");
443 printer->Annotate("{", "}", descriptor_);
444
445 // Field.Builder mergeField(Field value)
446 WriteFieldDocComment(printer, descriptor_);
447 printer->Print(variables_,
448 "$deprecation$public Builder "
449 "${$merge$capitalized_name$$}$($type$ value) {\n"
450 " copyOnWrite();\n"
451 " instance.merge$capitalized_name$(value);\n"
452 " return this;\n"
453 "}\n");
454 printer->Annotate("{", "}", descriptor_);
455
456 // Field.Builder clearField()
457 WriteFieldDocComment(printer, descriptor_);
458 printer->Print(
459 variables_,
460 "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n"
461 " copyOnWrite();\n"
462 " instance.clear$capitalized_name$();\n"
463 " return this;\n"
464 "}\n");
465 printer->Annotate("{", "}", descriptor_);
466 }
467
468 // ===================================================================
469
470 RepeatedImmutableMessageFieldLiteGenerator::
RepeatedImmutableMessageFieldLiteGenerator(const FieldDescriptor * descriptor,int messageBitIndex,Context * context)471 RepeatedImmutableMessageFieldLiteGenerator(
472 const FieldDescriptor* descriptor, int messageBitIndex,
473 Context* context)
474 : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
475 SetMessageVariables(descriptor, messageBitIndex, 0,
476 context->GetFieldGeneratorInfo(descriptor),
477 name_resolver_, &variables_);
478 }
479
480 RepeatedImmutableMessageFieldLiteGenerator::
~RepeatedImmutableMessageFieldLiteGenerator()481 ~RepeatedImmutableMessageFieldLiteGenerator() {}
482
GetNumBitsForMessage() const483 int RepeatedImmutableMessageFieldLiteGenerator::GetNumBitsForMessage() const {
484 return 0;
485 }
486
GenerateInterfaceMembers(io::Printer * printer) const487 void RepeatedImmutableMessageFieldLiteGenerator::GenerateInterfaceMembers(
488 io::Printer* printer) const {
489 // TODO(jonp): In the future, consider having methods specific to the
490 // interface so that builders can choose dynamically to either return a
491 // message or a nested builder, so that asking for the interface doesn't
492 // cause a message to ever be built.
493 WriteFieldDocComment(printer, descriptor_);
494 printer->Print(variables_,
495 "$deprecation$java.util.List<$type$> \n"
496 " get$capitalized_name$List();\n");
497 WriteFieldDocComment(printer, descriptor_);
498 printer->Print(variables_,
499 "$deprecation$$type$ get$capitalized_name$(int index);\n");
500 WriteFieldDocComment(printer, descriptor_);
501 printer->Print(variables_,
502 "$deprecation$int get$capitalized_name$Count();\n");
503 }
504
GenerateMembers(io::Printer * printer) const505 void RepeatedImmutableMessageFieldLiteGenerator::GenerateMembers(
506 io::Printer* printer) const {
507 printer->Print(
508 variables_,
509 "private com.google.protobuf.Internal.ProtobufList<$type$> $name$_;\n");
510 PrintExtraFieldInfo(variables_, printer);
511 WriteFieldDocComment(printer, descriptor_);
512 printer->Print(variables_,
513 "@java.lang.Override\n"
514 "$deprecation$public java.util.List<$type$> "
515 "${$get$capitalized_name$List$}$() {\n"
516 " return $name$_;\n" // note: unmodifiable list
517 "}\n");
518 printer->Annotate("{", "}", descriptor_);
519 WriteFieldDocComment(printer, descriptor_);
520 printer->Print(
521 variables_,
522 "$deprecation$public java.util.List<? extends $type$OrBuilder> \n"
523 " ${$get$capitalized_name$OrBuilderList$}$() {\n"
524 " return $name$_;\n"
525 "}\n");
526 printer->Annotate("{", "}", descriptor_);
527 WriteFieldDocComment(printer, descriptor_);
528 printer->Print(
529 variables_,
530 "@java.lang.Override\n"
531 "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n"
532 " return $name$_.size();\n"
533 "}\n");
534 printer->Annotate("{", "}", descriptor_);
535 WriteFieldDocComment(printer, descriptor_);
536 printer->Print(
537 variables_,
538 "@java.lang.Override\n"
539 "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n"
540 " return $name$_.get(index);\n"
541 "}\n");
542 printer->Annotate("{", "}", descriptor_);
543 WriteFieldDocComment(printer, descriptor_);
544 printer->Print(variables_,
545 "$deprecation$public $type$OrBuilder "
546 "${$get$capitalized_name$OrBuilder$}$(\n"
547 " int index) {\n"
548 " return $name$_.get(index);\n"
549 "}\n");
550 printer->Annotate("{", "}", descriptor_);
551
552 printer->Print(
553 variables_,
554 "private void ensure$capitalized_name$IsMutable() {\n"
555 " if (!$is_mutable$) {\n"
556 " $name$_ =\n"
557 " com.google.protobuf.GeneratedMessageLite.mutableCopy($name$_);\n"
558 " }\n"
559 "}\n"
560 "\n");
561
562 // Builder setRepeatedField(int index, Field value)
563 WriteFieldDocComment(printer, descriptor_);
564 printer->Print(variables_,
565 "private void set$capitalized_name$(\n"
566 " int index, $type$ value) {\n"
567 " if (value == null) {\n"
568 " throw new NullPointerException();\n"
569 " }\n"
570 " ensure$capitalized_name$IsMutable();\n"
571 " $name$_.set(index, value);\n"
572 "}\n");
573
574 // Builder setRepeatedField(int index, Field.Builder builderForValue)
575 WriteFieldDocComment(printer, descriptor_);
576 printer->Print(variables_,
577 "private void set$capitalized_name$(\n"
578 " int index, $type$.Builder builderForValue) {\n"
579 " ensure$capitalized_name$IsMutable();\n"
580 " $name$_.set(index, builderForValue.build());\n"
581 "}\n");
582
583 // Builder addRepeatedField(Field value)
584 WriteFieldDocComment(printer, descriptor_);
585 printer->Print(variables_,
586 "private void add$capitalized_name$($type$ value) {\n"
587 " if (value == null) {\n"
588 " throw new NullPointerException();\n"
589 " }\n"
590 " ensure$capitalized_name$IsMutable();\n"
591 " $name$_.add(value);\n"
592 "}\n");
593
594 // Builder addRepeatedField(int index, Field value)
595 WriteFieldDocComment(printer, descriptor_);
596 printer->Print(variables_,
597 "private void add$capitalized_name$(\n"
598 " int index, $type$ value) {\n"
599 " if (value == null) {\n"
600 " throw new NullPointerException();\n"
601 " }\n"
602 " ensure$capitalized_name$IsMutable();\n"
603 " $name$_.add(index, value);\n"
604 "}\n");
605 // Builder addRepeatedField(Field.Builder builderForValue)
606 WriteFieldDocComment(printer, descriptor_);
607 printer->Print(variables_,
608 "private void add$capitalized_name$(\n"
609 " $type$.Builder builderForValue) {\n"
610 " ensure$capitalized_name$IsMutable();\n"
611 " $name$_.add(builderForValue.build());\n"
612 "}\n");
613
614 // Builder addRepeatedField(int index, Field.Builder builderForValue)
615 WriteFieldDocComment(printer, descriptor_);
616 printer->Print(variables_,
617 "private void add$capitalized_name$(\n"
618 " int index, $type$.Builder builderForValue) {\n"
619 " ensure$capitalized_name$IsMutable();\n"
620 " $name$_.add(index, builderForValue.build());\n"
621 "}\n");
622
623 // Builder addAllRepeatedField(Iterable<Field> values)
624 WriteFieldDocComment(printer, descriptor_);
625 printer->Print(variables_,
626 "private void addAll$capitalized_name$(\n"
627 " java.lang.Iterable<? extends $type$> values) {\n"
628 " ensure$capitalized_name$IsMutable();\n"
629 " com.google.protobuf.AbstractMessageLite.addAll(\n"
630 " values, $name$_);\n"
631 "}\n");
632
633 // Builder clearAllRepeatedField()
634 WriteFieldDocComment(printer, descriptor_);
635 printer->Print(variables_,
636 "private void clear$capitalized_name$() {\n"
637 " $name$_ = emptyProtobufList();\n"
638 "}\n");
639
640 // Builder removeRepeatedField(int index)
641 WriteFieldDocComment(printer, descriptor_);
642 printer->Print(variables_,
643 "private void remove$capitalized_name$(int index) {\n"
644 " ensure$capitalized_name$IsMutable();\n"
645 " $name$_.remove(index);\n"
646 "}\n");
647 }
648
GenerateBuilderMembers(io::Printer * printer) const649 void RepeatedImmutableMessageFieldLiteGenerator::GenerateBuilderMembers(
650 io::Printer* printer) const {
651 // The comments above the methods below are based on a hypothetical
652 // repeated field of type "Field" called "RepeatedField".
653
654 // List<Field> getRepeatedFieldList()
655 WriteFieldDocComment(printer, descriptor_);
656 printer->Print(variables_,
657 "@java.lang.Override\n"
658 "$deprecation$public java.util.List<$type$> "
659 "${$get$capitalized_name$List$}$() {\n"
660 " return java.util.Collections.unmodifiableList(\n"
661 " instance.get$capitalized_name$List());\n"
662 "}\n");
663 printer->Annotate("{", "}", descriptor_);
664
665 // int getRepeatedFieldCount()
666 WriteFieldDocComment(printer, descriptor_);
667 printer->Print(
668 variables_,
669 "@java.lang.Override\n"
670 "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n"
671 " return instance.get$capitalized_name$Count();\n"
672 "}");
673 printer->Annotate("{", "}", descriptor_);
674
675 // Field getRepeatedField(int index)
676 WriteFieldDocComment(printer, descriptor_);
677 printer->Print(
678 variables_,
679 "@java.lang.Override\n"
680 "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n"
681 " return instance.get$capitalized_name$(index);\n"
682 "}\n");
683 printer->Annotate("{", "}", descriptor_);
684
685 // Builder setRepeatedField(int index, Field value)
686 WriteFieldDocComment(printer, descriptor_);
687 printer->Print(variables_,
688 "$deprecation$public Builder ${$set$capitalized_name$$}$(\n"
689 " int index, $type$ value) {\n"
690 " copyOnWrite();\n"
691 " instance.set$capitalized_name$(index, value);\n"
692 " return this;\n"
693 "}\n");
694 printer->Annotate("{", "}", descriptor_);
695
696 // Builder setRepeatedField(int index, Field.Builder builderForValue)
697 WriteFieldDocComment(printer, descriptor_);
698 printer->Print(variables_,
699 "$deprecation$public Builder ${$set$capitalized_name$$}$(\n"
700 " int index, $type$.Builder builderForValue) {\n"
701 " copyOnWrite();\n"
702 " instance.set$capitalized_name$(index, builderForValue);\n"
703 " return this;\n"
704 "}\n");
705 printer->Annotate("{", "}", descriptor_);
706
707 // Builder addRepeatedField(Field value)
708 WriteFieldDocComment(printer, descriptor_);
709 printer->Print(variables_,
710 "$deprecation$public Builder "
711 "${$add$capitalized_name$$}$($type$ value) {\n"
712 " copyOnWrite();\n"
713 " instance.add$capitalized_name$(value);\n"
714 " return this;\n"
715 "}\n");
716 printer->Annotate("{", "}", descriptor_);
717
718 // Builder addRepeatedField(int index, Field value)
719 WriteFieldDocComment(printer, descriptor_);
720 printer->Print(variables_,
721 "$deprecation$public Builder ${$add$capitalized_name$$}$(\n"
722 " int index, $type$ value) {\n"
723 " copyOnWrite();\n"
724 " instance.add$capitalized_name$(index, value);\n"
725 " return this;\n"
726 "}\n");
727 printer->Annotate("{", "}", descriptor_);
728 // Builder addRepeatedField(Field.Builder builderForValue)
729 WriteFieldDocComment(printer, descriptor_);
730 printer->Print(variables_,
731 "$deprecation$public Builder ${$add$capitalized_name$$}$(\n"
732 " $type$.Builder builderForValue) {\n"
733 " copyOnWrite();\n"
734 " instance.add$capitalized_name$(builderForValue);\n"
735 " return this;\n"
736 "}\n");
737 printer->Annotate("{", "}", descriptor_);
738
739 // Builder addRepeatedField(int index, Field.Builder builderForValue)
740 WriteFieldDocComment(printer, descriptor_);
741 printer->Print(variables_,
742 "$deprecation$public Builder ${$add$capitalized_name$$}$(\n"
743 " int index, $type$.Builder builderForValue) {\n"
744 " copyOnWrite();\n"
745 " instance.add$capitalized_name$(index, builderForValue);\n"
746 " return this;\n"
747 "}\n");
748 printer->Annotate("{", "}", descriptor_);
749
750 // Builder addAllRepeatedField(Iterable<Field> values)
751 WriteFieldDocComment(printer, descriptor_);
752 printer->Print(variables_,
753 "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n"
754 " java.lang.Iterable<? extends $type$> values) {\n"
755 " copyOnWrite();\n"
756 " instance.addAll$capitalized_name$(values);\n"
757 " return this;\n"
758 "}\n");
759 printer->Annotate("{", "}", descriptor_);
760
761 // Builder clearAllRepeatedField()
762 WriteFieldDocComment(printer, descriptor_);
763 printer->Print(
764 variables_,
765 "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n"
766 " copyOnWrite();\n"
767 " instance.clear$capitalized_name$();\n"
768 " return this;\n"
769 "}\n");
770 printer->Annotate("{", "}", descriptor_);
771
772 // Builder removeRepeatedField(int index)
773 WriteFieldDocComment(printer, descriptor_);
774 printer->Print(variables_,
775 "$deprecation$public Builder "
776 "${$remove$capitalized_name$$}$(int index) {\n"
777 " copyOnWrite();\n"
778 " instance.remove$capitalized_name$(index);\n"
779 " return this;\n"
780 "}\n");
781 printer->Annotate("{", "}", descriptor_);
782 }
783
784
GenerateFieldInfo(io::Printer * printer,std::vector<uint16> * output) const785 void RepeatedImmutableMessageFieldLiteGenerator::GenerateFieldInfo(
786 io::Printer* printer, std::vector<uint16>* output) const {
787 WriteIntToUtf16CharSequence(descriptor_->number(), output);
788 WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
789 output);
790 printer->Print(variables_,
791 "\"$name$_\",\n"
792 "$type$.class,\n");
793 }
794
GenerateInitializationCode(io::Printer * printer) const795 void RepeatedImmutableMessageFieldLiteGenerator::GenerateInitializationCode(
796 io::Printer* printer) const {
797 printer->Print(variables_, "$name$_ = emptyProtobufList();\n");
798 }
799
GetBoxedType() const800 std::string RepeatedImmutableMessageFieldLiteGenerator::GetBoxedType() const {
801 return name_resolver_->GetImmutableClassName(descriptor_->message_type());
802 }
803
804 } // namespace java
805 } // namespace compiler
806 } // namespace protobuf
807 } // namespace google
808