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/stubs/logging.h>
39 #include <google/protobuf/stubs/common.h>
40 #include <google/protobuf/compiler/java/java_context.h>
41 #include <google/protobuf/compiler/java/java_doc_comment.h>
42 #include <google/protobuf/compiler/java/java_enum_field.h>
43 #include <google/protobuf/compiler/java/java_helpers.h>
44 #include <google/protobuf/compiler/java/java_name_resolver.h>
45 #include <google/protobuf/io/printer.h>
46 #include <google/protobuf/wire_format.h>
47 #include <google/protobuf/stubs/strutil.h>
48
49 namespace google {
50 namespace protobuf {
51 namespace compiler {
52 namespace java {
53
54 namespace {
55
SetEnumVariables(const FieldDescriptor * descriptor,int messageBitIndex,int builderBitIndex,const FieldGeneratorInfo * info,ClassNameResolver * name_resolver,map<string,string> * variables)56 void SetEnumVariables(const FieldDescriptor* descriptor,
57 int messageBitIndex,
58 int builderBitIndex,
59 const FieldGeneratorInfo* info,
60 ClassNameResolver* name_resolver,
61 map<string, string>* variables) {
62 SetCommonFieldVariables(descriptor, info, variables);
63
64 (*variables)["type"] =
65 name_resolver->GetImmutableClassName(descriptor->enum_type());
66 (*variables)["mutable_type"] =
67 name_resolver->GetMutableClassName(descriptor->enum_type());
68 (*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver);
69 (*variables)["default_number"] = SimpleItoa(
70 descriptor->default_value_enum()->number());
71 (*variables)["tag"] = SimpleItoa(internal::WireFormat::MakeTag(descriptor));
72 (*variables)["tag_size"] = SimpleItoa(
73 internal::WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
74 // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
75 // by the proto compiler
76 (*variables)["deprecation"] = descriptor->options().deprecated()
77 ? "@java.lang.Deprecated " : "";
78 (*variables)["on_changed"] = "onChanged();";
79 // Use deprecated valueOf() method to be compatible with old generated code
80 // for v2.5.0/v2.6.1.
81 // TODO(xiaofeng): Use "forNumber" when we no longer support compatibility
82 // with v2.5.0/v2.6.1.
83 (*variables)["for_number"] = "valueOf";
84
85 if (SupportFieldPresence(descriptor->file())) {
86 // For singular messages and builders, one bit is used for the hasField bit.
87 (*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex);
88 (*variables)["get_has_field_bit_builder"] = GenerateGetBit(builderBitIndex);
89
90 // Note that these have a trailing ";".
91 (*variables)["set_has_field_bit_message"] =
92 GenerateSetBit(messageBitIndex) + ";";
93 (*variables)["set_has_field_bit_builder"] =
94 GenerateSetBit(builderBitIndex) + ";";
95 (*variables)["clear_has_field_bit_builder"] =
96 GenerateClearBit(builderBitIndex) + ";";
97
98 (*variables)["is_field_present_message"] = GenerateGetBit(messageBitIndex);
99 } else {
100 (*variables)["set_has_field_bit_message"] = "";
101 (*variables)["set_has_field_bit_builder"] = "";
102 (*variables)["clear_has_field_bit_builder"] = "";
103
104 (*variables)["is_field_present_message"] =
105 (*variables)["name"] + "_ != " +
106 (*variables)["default"] + ".getNumber()";
107 }
108
109 // For repated builders, one bit is used for whether the array is immutable.
110 (*variables)["get_mutable_bit_builder"] = GenerateGetBit(builderBitIndex);
111 (*variables)["set_mutable_bit_builder"] = GenerateSetBit(builderBitIndex);
112 (*variables)["clear_mutable_bit_builder"] = GenerateClearBit(builderBitIndex);
113
114 // For repeated fields, one bit is used for whether the array is immutable
115 // in the parsing constructor.
116 (*variables)["get_mutable_bit_parser"] =
117 GenerateGetBitMutableLocal(builderBitIndex);
118 (*variables)["set_mutable_bit_parser"] =
119 GenerateSetBitMutableLocal(builderBitIndex);
120
121 (*variables)["get_has_field_bit_from_local"] =
122 GenerateGetBitFromLocal(builderBitIndex);
123 (*variables)["set_has_field_bit_to_local"] =
124 GenerateSetBitToLocal(messageBitIndex);
125
126 if (SupportUnknownEnumValue(descriptor->file())) {
127 (*variables)["unknown"] = (*variables)["type"] + ".UNRECOGNIZED";
128 } else {
129 (*variables)["unknown"] = (*variables)["default"];
130 }
131 }
132
133 } // namespace
134
135 // ===================================================================
136
137 ImmutableEnumFieldGenerator::
ImmutableEnumFieldGenerator(const FieldDescriptor * descriptor,int messageBitIndex,int builderBitIndex,Context * context)138 ImmutableEnumFieldGenerator(const FieldDescriptor* descriptor,
139 int messageBitIndex,
140 int builderBitIndex,
141 Context* context)
142 : descriptor_(descriptor), messageBitIndex_(messageBitIndex),
143 builderBitIndex_(builderBitIndex),
144 name_resolver_(context->GetNameResolver()) {
145 SetEnumVariables(descriptor, messageBitIndex, builderBitIndex,
146 context->GetFieldGeneratorInfo(descriptor),
147 name_resolver_, &variables_);
148 }
149
~ImmutableEnumFieldGenerator()150 ImmutableEnumFieldGenerator::~ImmutableEnumFieldGenerator() {}
151
GetNumBitsForMessage() const152 int ImmutableEnumFieldGenerator::GetNumBitsForMessage() const {
153 return 1;
154 }
155
GetNumBitsForBuilder() const156 int ImmutableEnumFieldGenerator::GetNumBitsForBuilder() const {
157 return 1;
158 }
159
160 void ImmutableEnumFieldGenerator::
GenerateInterfaceMembers(io::Printer * printer) const161 GenerateInterfaceMembers(io::Printer* printer) const {
162 if (SupportFieldPresence(descriptor_->file())) {
163 WriteFieldDocComment(printer, descriptor_);
164 printer->Print(variables_,
165 "$deprecation$boolean has$capitalized_name$();\n");
166 }
167 if (SupportUnknownEnumValue(descriptor_->file())) {
168 WriteFieldDocComment(printer, descriptor_);
169 printer->Print(variables_,
170 "$deprecation$int get$capitalized_name$Value();\n");
171 }
172 WriteFieldDocComment(printer, descriptor_);
173 printer->Print(variables_,
174 "$deprecation$$type$ get$capitalized_name$();\n");
175 }
176
177 void ImmutableEnumFieldGenerator::
GenerateMembers(io::Printer * printer) const178 GenerateMembers(io::Printer* printer) const {
179 printer->Print(variables_,
180 "private int $name$_;\n");
181 PrintExtraFieldInfo(variables_, printer);
182 if (SupportFieldPresence(descriptor_->file())) {
183 WriteFieldDocComment(printer, descriptor_);
184 printer->Print(variables_,
185 "$deprecation$public boolean has$capitalized_name$() {\n"
186 " return $get_has_field_bit_message$;\n"
187 "}\n");
188 }
189 if (SupportUnknownEnumValue(descriptor_->file())) {
190 WriteFieldDocComment(printer, descriptor_);
191 printer->Print(variables_,
192 "$deprecation$public int get$capitalized_name$Value() {\n"
193 " return $name$_;\n"
194 "}\n");
195 }
196 WriteFieldDocComment(printer, descriptor_);
197 printer->Print(variables_,
198 "$deprecation$public $type$ get$capitalized_name$() {\n"
199 " $type$ result = $type$.$for_number$($name$_);\n"
200 " return result == null ? $unknown$ : result;\n"
201 "}\n");
202 }
203
204 void ImmutableEnumFieldGenerator::
GenerateBuilderMembers(io::Printer * printer) const205 GenerateBuilderMembers(io::Printer* printer) const {
206 printer->Print(variables_,
207 "private int $name$_ = $default_number$;\n");
208 if (SupportFieldPresence(descriptor_->file())) {
209 WriteFieldDocComment(printer, descriptor_);
210 printer->Print(variables_,
211 "$deprecation$public boolean has$capitalized_name$() {\n"
212 " return $get_has_field_bit_builder$;\n"
213 "}\n");
214 }
215 if (SupportUnknownEnumValue(descriptor_->file())) {
216 WriteFieldDocComment(printer, descriptor_);
217 printer->Print(variables_,
218 "$deprecation$public int get$capitalized_name$Value() {\n"
219 " return $name$_;\n"
220 "}\n");
221 WriteFieldDocComment(printer, descriptor_);
222 printer->Print(variables_,
223 "$deprecation$public Builder set$capitalized_name$Value(int value) {\n"
224 " $name$_ = value;\n"
225 " $on_changed$\n"
226 " return this;\n"
227 "}\n");
228 }
229 WriteFieldDocComment(printer, descriptor_);
230 printer->Print(variables_,
231 "$deprecation$public $type$ get$capitalized_name$() {\n"
232 " $type$ result = $type$.$for_number$($name$_);\n"
233 " return result == null ? $unknown$ : result;\n"
234 "}\n");
235 WriteFieldDocComment(printer, descriptor_);
236 printer->Print(variables_,
237 "$deprecation$public Builder set$capitalized_name$($type$ value) {\n"
238 " if (value == null) {\n"
239 " throw new NullPointerException();\n"
240 " }\n"
241 " $set_has_field_bit_builder$\n"
242 " $name$_ = value.getNumber();\n"
243 " $on_changed$\n"
244 " return this;\n"
245 "}\n");
246 WriteFieldDocComment(printer, descriptor_);
247 printer->Print(variables_,
248 "$deprecation$public Builder clear$capitalized_name$() {\n"
249 " $clear_has_field_bit_builder$\n"
250 " $name$_ = $default_number$;\n"
251 " $on_changed$\n"
252 " return this;\n"
253 "}\n");
254 }
255
256 void ImmutableEnumFieldGenerator::
GenerateFieldBuilderInitializationCode(io::Printer * printer) const257 GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
258 // noop for enums
259 }
260
261 void ImmutableEnumFieldGenerator::
GenerateInitializationCode(io::Printer * printer) const262 GenerateInitializationCode(io::Printer* printer) const {
263 printer->Print(variables_, "$name$_ = $default_number$;\n");
264 }
265
266 void ImmutableEnumFieldGenerator::
GenerateBuilderClearCode(io::Printer * printer) const267 GenerateBuilderClearCode(io::Printer* printer) const {
268 printer->Print(variables_,
269 "$name$_ = $default_number$;\n"
270 "$clear_has_field_bit_builder$\n");
271 }
272
273 void ImmutableEnumFieldGenerator::
GenerateMergingCode(io::Printer * printer) const274 GenerateMergingCode(io::Printer* printer) const {
275 if (SupportFieldPresence(descriptor_->file())) {
276 printer->Print(variables_,
277 "if (other.has$capitalized_name$()) {\n"
278 " set$capitalized_name$(other.get$capitalized_name$());\n"
279 "}\n");
280 } else if (SupportUnknownEnumValue(descriptor_->file())) {
281 printer->Print(variables_,
282 "if (other.$name$_ != $default_number$) {\n"
283 " set$capitalized_name$Value(other.get$capitalized_name$Value());\n"
284 "}\n");
285 } else {
286 GOOGLE_LOG(FATAL) << "Can't reach here.";
287 }
288 }
289
290 void ImmutableEnumFieldGenerator::
GenerateBuildingCode(io::Printer * printer) const291 GenerateBuildingCode(io::Printer* printer) const {
292 if (SupportFieldPresence(descriptor_->file())) {
293 printer->Print(variables_,
294 "if ($get_has_field_bit_from_local$) {\n"
295 " $set_has_field_bit_to_local$;\n"
296 "}\n");
297 }
298 printer->Print(variables_,
299 "result.$name$_ = $name$_;\n");
300 }
301
302 void ImmutableEnumFieldGenerator::
GenerateParsingCode(io::Printer * printer) const303 GenerateParsingCode(io::Printer* printer) const {
304 if (SupportUnknownEnumValue(descriptor_->file())) {
305 printer->Print(variables_,
306 "int rawValue = input.readEnum();\n"
307 "$set_has_field_bit_message$\n"
308 "$name$_ = rawValue;\n");
309 } else {
310 printer->Print(variables_,
311 "int rawValue = input.readEnum();\n"
312 "$type$ value = $type$.$for_number$(rawValue);\n"
313 "if (value == null) {\n");
314 if (PreserveUnknownFields(descriptor_->containing_type())) {
315 printer->Print(variables_,
316 " unknownFields.mergeVarintField($number$, rawValue);\n");
317 }
318 printer->Print(variables_,
319 "} else {\n"
320 " $set_has_field_bit_message$\n"
321 " $name$_ = rawValue;\n"
322 "}\n");
323 }
324 }
325
326 void ImmutableEnumFieldGenerator::
GenerateParsingDoneCode(io::Printer * printer) const327 GenerateParsingDoneCode(io::Printer* printer) const {
328 // noop for enums
329 }
330
331 void ImmutableEnumFieldGenerator::
GenerateSerializationCode(io::Printer * printer) const332 GenerateSerializationCode(io::Printer* printer) const {
333 printer->Print(variables_,
334 "if ($is_field_present_message$) {\n"
335 " output.writeEnum($number$, $name$_);\n"
336 "}\n");
337 }
338
339 void ImmutableEnumFieldGenerator::
GenerateSerializedSizeCode(io::Printer * printer) const340 GenerateSerializedSizeCode(io::Printer* printer) const {
341 printer->Print(variables_,
342 "if ($is_field_present_message$) {\n"
343 " size += com.google.protobuf.CodedOutputStream\n"
344 " .computeEnumSize($number$, $name$_);\n"
345 "}\n");
346 }
347
348 void ImmutableEnumFieldGenerator::
GenerateEqualsCode(io::Printer * printer) const349 GenerateEqualsCode(io::Printer* printer) const {
350 printer->Print(variables_,
351 "result = result && $name$_ == other.$name$_;\n");
352 }
353
354 void ImmutableEnumFieldGenerator::
GenerateHashCode(io::Printer * printer) const355 GenerateHashCode(io::Printer* printer) const {
356 printer->Print(variables_,
357 "hash = (37 * hash) + $constant_name$;\n"
358 "hash = (53 * hash) + $name$_;\n");
359 }
360
GetBoxedType() const361 string ImmutableEnumFieldGenerator::GetBoxedType() const {
362 return name_resolver_->GetImmutableClassName(descriptor_->enum_type());
363 }
364
365 // ===================================================================
366
367 ImmutableEnumOneofFieldGenerator::
ImmutableEnumOneofFieldGenerator(const FieldDescriptor * descriptor,int messageBitIndex,int builderBitIndex,Context * context)368 ImmutableEnumOneofFieldGenerator(const FieldDescriptor* descriptor,
369 int messageBitIndex,
370 int builderBitIndex,
371 Context* context)
372 : ImmutableEnumFieldGenerator(
373 descriptor, messageBitIndex, builderBitIndex, context) {
374 const OneofGeneratorInfo* info =
375 context->GetOneofGeneratorInfo(descriptor->containing_oneof());
376 SetCommonOneofVariables(descriptor, info, &variables_);
377 }
378
379 ImmutableEnumOneofFieldGenerator::
~ImmutableEnumOneofFieldGenerator()380 ~ImmutableEnumOneofFieldGenerator() {}
381
382 void ImmutableEnumOneofFieldGenerator::
GenerateMembers(io::Printer * printer) const383 GenerateMembers(io::Printer* printer) const {
384 PrintExtraFieldInfo(variables_, printer);
385 if (SupportFieldPresence(descriptor_->file())) {
386 WriteFieldDocComment(printer, descriptor_);
387 printer->Print(variables_,
388 "$deprecation$public boolean has$capitalized_name$() {\n"
389 " return $has_oneof_case_message$;\n"
390 "}\n");
391 }
392 if (SupportUnknownEnumValue(descriptor_->file())) {
393 WriteFieldDocComment(printer, descriptor_);
394 printer->Print(variables_,
395 "$deprecation$public int get$capitalized_name$Value() {\n"
396 " if ($has_oneof_case_message$) {\n"
397 " return (java.lang.Integer) $oneof_name$_;\n"
398 " }\n"
399 " return $default_number$;\n"
400 "}\n");
401 }
402 WriteFieldDocComment(printer, descriptor_);
403 printer->Print(variables_,
404 "$deprecation$public $type$ get$capitalized_name$() {\n"
405 " if ($has_oneof_case_message$) {\n"
406 " $type$ result = $type$.$for_number$(\n"
407 " (java.lang.Integer) $oneof_name$_);\n"
408 " return result == null ? $unknown$ : result;\n"
409 " }\n"
410 " return $default$;\n"
411 "}\n");
412 }
413
414 void ImmutableEnumOneofFieldGenerator::
GenerateBuilderMembers(io::Printer * printer) const415 GenerateBuilderMembers(io::Printer* printer) const {
416 if (SupportFieldPresence(descriptor_->file())) {
417 WriteFieldDocComment(printer, descriptor_);
418 printer->Print(variables_,
419 "$deprecation$public boolean has$capitalized_name$() {\n"
420 " return $has_oneof_case_message$;\n"
421 "}\n");
422 }
423 if (SupportUnknownEnumValue(descriptor_->file())) {
424 WriteFieldDocComment(printer, descriptor_);
425 printer->Print(variables_,
426 "$deprecation$public int get$capitalized_name$Value() {\n"
427 " if ($has_oneof_case_message$) {\n"
428 " return ((java.lang.Integer) $oneof_name$_).intValue();\n"
429 " }\n"
430 " return $default_number$;\n"
431 "}\n");
432 WriteFieldDocComment(printer, descriptor_);
433 printer->Print(variables_,
434 "$deprecation$public Builder set$capitalized_name$Value(int value) {\n"
435 " $set_oneof_case_message$;\n"
436 " $oneof_name$_ = value;\n"
437 " $on_changed$\n"
438 " return this;\n"
439 "}\n");
440 }
441 WriteFieldDocComment(printer, descriptor_);
442 printer->Print(variables_,
443 "$deprecation$public $type$ get$capitalized_name$() {\n"
444 " if ($has_oneof_case_message$) {\n"
445 " $type$ result = $type$.$for_number$(\n"
446 " (java.lang.Integer) $oneof_name$_);\n"
447 " return result == null ? $unknown$ : result;\n"
448 " }\n"
449 " return $default$;\n"
450 "}\n");
451 WriteFieldDocComment(printer, descriptor_);
452 printer->Print(variables_,
453 "$deprecation$public Builder set$capitalized_name$($type$ value) {\n"
454 " if (value == null) {\n"
455 " throw new NullPointerException();\n"
456 " }\n"
457 " $set_oneof_case_message$;\n"
458 " $oneof_name$_ = value.getNumber();\n"
459 " $on_changed$\n"
460 " return this;\n"
461 "}\n");
462 WriteFieldDocComment(printer, descriptor_);
463 printer->Print(variables_,
464 "$deprecation$public Builder clear$capitalized_name$() {\n"
465 " if ($has_oneof_case_message$) {\n"
466 " $clear_oneof_case_message$;\n"
467 " $oneof_name$_ = null;\n"
468 " $on_changed$\n"
469 " }\n"
470 " return this;\n"
471 "}\n");
472 }
473
474 void ImmutableEnumOneofFieldGenerator::
GenerateBuildingCode(io::Printer * printer) const475 GenerateBuildingCode(io::Printer* printer) const {
476 printer->Print(variables_,
477 "if ($has_oneof_case_message$) {\n"
478 " result.$oneof_name$_ = $oneof_name$_;\n"
479 "}\n");
480 }
481
482 void ImmutableEnumOneofFieldGenerator::
GenerateMergingCode(io::Printer * printer) const483 GenerateMergingCode(io::Printer* printer) const {
484 if (SupportUnknownEnumValue(descriptor_->file())) {
485 printer->Print(variables_,
486 "set$capitalized_name$Value(other.get$capitalized_name$Value());\n");
487 } else {
488 printer->Print(variables_,
489 "set$capitalized_name$(other.get$capitalized_name$());\n");
490 }
491 }
492
493 void ImmutableEnumOneofFieldGenerator::
GenerateParsingCode(io::Printer * printer) const494 GenerateParsingCode(io::Printer* printer) const {
495 if (SupportUnknownEnumValue(descriptor_->file())) {
496 printer->Print(variables_,
497 "int rawValue = input.readEnum();\n"
498 "$set_oneof_case_message$;\n"
499 "$oneof_name$_ = rawValue;\n");
500 } else {
501 printer->Print(variables_,
502 "int rawValue = input.readEnum();\n"
503 "$type$ value = $type$.$for_number$(rawValue);\n"
504 "if (value == null) {\n");
505 if (PreserveUnknownFields(descriptor_->containing_type())) {
506 printer->Print(variables_,
507 " unknownFields.mergeVarintField($number$, rawValue);\n");
508 }
509 printer->Print(variables_,
510 "} else {\n"
511 " $set_oneof_case_message$;\n"
512 " $oneof_name$_ = rawValue;\n"
513 "}\n");
514 }
515 }
516
517 void ImmutableEnumOneofFieldGenerator::
GenerateSerializationCode(io::Printer * printer) const518 GenerateSerializationCode(io::Printer* printer) const {
519 printer->Print(variables_,
520 "if ($has_oneof_case_message$) {\n"
521 " output.writeEnum($number$, ((java.lang.Integer) $oneof_name$_));\n"
522 "}\n");
523 }
524
525 void ImmutableEnumOneofFieldGenerator::
GenerateSerializedSizeCode(io::Printer * printer) const526 GenerateSerializedSizeCode(io::Printer* printer) const {
527 printer->Print(variables_,
528 "if ($has_oneof_case_message$) {\n"
529 " size += com.google.protobuf.CodedOutputStream\n"
530 " .computeEnumSize($number$, ((java.lang.Integer) $oneof_name$_));\n"
531 "}\n");
532 }
533
534 void ImmutableEnumOneofFieldGenerator::
GenerateEqualsCode(io::Printer * printer) const535 GenerateEqualsCode(io::Printer* printer) const {
536 if (SupportUnknownEnumValue(descriptor_->file())) {
537 printer->Print(variables_,
538 "result = result && get$capitalized_name$Value()\n"
539 " == other.get$capitalized_name$Value();\n");
540 } else {
541 printer->Print(variables_,
542 "result = result && get$capitalized_name$()\n"
543 " .equals(other.get$capitalized_name$());\n");
544 }
545 }
546
547 void ImmutableEnumOneofFieldGenerator::
GenerateHashCode(io::Printer * printer) const548 GenerateHashCode(io::Printer* printer) const {
549 if (SupportUnknownEnumValue(descriptor_->file())) {
550 printer->Print(variables_,
551 "hash = (37 * hash) + $constant_name$;\n"
552 "hash = (53 * hash) + get$capitalized_name$Value();\n");
553 } else {
554 printer->Print(variables_,
555 "hash = (37 * hash) + $constant_name$;\n"
556 "hash = (53 * hash) + get$capitalized_name$().getNumber();\n");
557 }
558 }
559
560 // ===================================================================
561
562 RepeatedImmutableEnumFieldGenerator::
RepeatedImmutableEnumFieldGenerator(const FieldDescriptor * descriptor,int messageBitIndex,int builderBitIndex,Context * context)563 RepeatedImmutableEnumFieldGenerator(const FieldDescriptor* descriptor,
564 int messageBitIndex,
565 int builderBitIndex,
566 Context* context)
567 : descriptor_(descriptor), messageBitIndex_(messageBitIndex),
568 builderBitIndex_(builderBitIndex), context_(context),
569 name_resolver_(context->GetNameResolver()) {
570 SetEnumVariables(descriptor, messageBitIndex, builderBitIndex,
571 context->GetFieldGeneratorInfo(descriptor),
572 name_resolver_, &variables_);
573 }
574
~RepeatedImmutableEnumFieldGenerator()575 RepeatedImmutableEnumFieldGenerator::~RepeatedImmutableEnumFieldGenerator() {}
576
GetNumBitsForMessage() const577 int RepeatedImmutableEnumFieldGenerator::GetNumBitsForMessage() const {
578 return 0;
579 }
580
GetNumBitsForBuilder() const581 int RepeatedImmutableEnumFieldGenerator::GetNumBitsForBuilder() const {
582 return 1;
583 }
584
585 void RepeatedImmutableEnumFieldGenerator::
GenerateInterfaceMembers(io::Printer * printer) const586 GenerateInterfaceMembers(io::Printer* printer) const {
587 WriteFieldDocComment(printer, descriptor_);
588 printer->Print(variables_,
589 "$deprecation$java.util.List<$type$> get$capitalized_name$List();\n");
590 WriteFieldDocComment(printer, descriptor_);
591 printer->Print(variables_,
592 "$deprecation$int get$capitalized_name$Count();\n");
593 WriteFieldDocComment(printer, descriptor_);
594 printer->Print(variables_,
595 "$deprecation$$type$ get$capitalized_name$(int index);\n");
596 if (SupportUnknownEnumValue(descriptor_->file())) {
597 WriteFieldDocComment(printer, descriptor_);
598 printer->Print(variables_,
599 "$deprecation$java.util.List<java.lang.Integer>\n"
600 "get$capitalized_name$ValueList();\n");
601 WriteFieldDocComment(printer, descriptor_);
602 printer->Print(variables_,
603 "$deprecation$int get$capitalized_name$Value(int index);\n");
604 }
605 }
606
607 void RepeatedImmutableEnumFieldGenerator::
GenerateMembers(io::Printer * printer) const608 GenerateMembers(io::Printer* printer) const {
609 printer->Print(variables_,
610 "private java.util.List<java.lang.Integer> $name$_;\n"
611 "private static final com.google.protobuf.Internal.ListAdapter.Converter<\n"
612 " java.lang.Integer, $type$> $name$_converter_ =\n"
613 " new com.google.protobuf.Internal.ListAdapter.Converter<\n"
614 " java.lang.Integer, $type$>() {\n"
615 " public $type$ convert(java.lang.Integer from) {\n"
616 " $type$ result = $type$.$for_number$(from);\n"
617 " return result == null ? $unknown$ : result;\n"
618 " }\n"
619 " };\n");
620 PrintExtraFieldInfo(variables_, printer);
621 WriteFieldDocComment(printer, descriptor_);
622 printer->Print(variables_,
623 "$deprecation$public java.util.List<$type$> get$capitalized_name$List() {\n"
624 " return new com.google.protobuf.Internal.ListAdapter<\n"
625 " java.lang.Integer, $type$>($name$_, $name$_converter_);\n"
626 "}\n");
627 WriteFieldDocComment(printer, descriptor_);
628 printer->Print(variables_,
629 "$deprecation$public int get$capitalized_name$Count() {\n"
630 " return $name$_.size();\n"
631 "}\n");
632 WriteFieldDocComment(printer, descriptor_);
633 printer->Print(variables_,
634 "$deprecation$public $type$ get$capitalized_name$(int index) {\n"
635 " return $name$_converter_.convert($name$_.get(index));\n"
636 "}\n");
637 if (SupportUnknownEnumValue(descriptor_->file())) {
638 WriteFieldDocComment(printer, descriptor_);
639 printer->Print(variables_,
640 "$deprecation$public java.util.List<java.lang.Integer>\n"
641 "get$capitalized_name$ValueList() {\n"
642 " return $name$_;\n"
643 "}\n");
644 WriteFieldDocComment(printer, descriptor_);
645 printer->Print(variables_,
646 "$deprecation$public int get$capitalized_name$Value(int index) {\n"
647 " return $name$_.get(index);\n"
648 "}\n");
649 }
650
651 if (descriptor_->is_packed() &&
652 context_->HasGeneratedMethods(descriptor_->containing_type())) {
653 printer->Print(variables_,
654 "private int $name$MemoizedSerializedSize;\n");
655 }
656 }
657
658 void RepeatedImmutableEnumFieldGenerator::
GenerateBuilderMembers(io::Printer * printer) const659 GenerateBuilderMembers(io::Printer* printer) const {
660 printer->Print(variables_,
661 // One field is the list and the other field keeps track of whether the
662 // list is immutable. If it's immutable, the invariant is that it must
663 // either an instance of Collections.emptyList() or it's an ArrayList
664 // wrapped in a Collections.unmodifiableList() wrapper and nobody else has
665 // a refererence to the underlying ArrayList. This invariant allows us to
666 // share instances of lists between protocol buffers avoiding expensive
667 // memory allocations. Note, immutable is a strong guarantee here -- not
668 // just that the list cannot be modified via the reference but that the
669 // list can never be modified.
670 "private java.util.List<java.lang.Integer> $name$_ =\n"
671 " java.util.Collections.emptyList();\n"
672
673 "private void ensure$capitalized_name$IsMutable() {\n"
674 " if (!$get_mutable_bit_builder$) {\n"
675 " $name$_ = new java.util.ArrayList<java.lang.Integer>($name$_);\n"
676 " $set_mutable_bit_builder$;\n"
677 " }\n"
678 "}\n");
679
680 WriteFieldDocComment(printer, descriptor_);
681 printer->Print(variables_,
682 // Note: We return an unmodifiable list because otherwise the caller
683 // could hold on to the returned list and modify it after the message
684 // has been built, thus mutating the message which is supposed to be
685 // immutable.
686 "$deprecation$public java.util.List<$type$> get$capitalized_name$List() {\n"
687 " return new com.google.protobuf.Internal.ListAdapter<\n"
688 " java.lang.Integer, $type$>($name$_, $name$_converter_);\n"
689 "}\n");
690 WriteFieldDocComment(printer, descriptor_);
691 printer->Print(variables_,
692 "$deprecation$public int get$capitalized_name$Count() {\n"
693 " return $name$_.size();\n"
694 "}\n");
695 WriteFieldDocComment(printer, descriptor_);
696 printer->Print(variables_,
697 "$deprecation$public $type$ get$capitalized_name$(int index) {\n"
698 " return $name$_converter_.convert($name$_.get(index));\n"
699 "}\n");
700 WriteFieldDocComment(printer, descriptor_);
701 printer->Print(variables_,
702 "$deprecation$public Builder set$capitalized_name$(\n"
703 " int index, $type$ value) {\n"
704 " if (value == null) {\n"
705 " throw new NullPointerException();\n"
706 " }\n"
707 " ensure$capitalized_name$IsMutable();\n"
708 " $name$_.set(index, value.getNumber());\n"
709 " $on_changed$\n"
710 " return this;\n"
711 "}\n");
712 WriteFieldDocComment(printer, descriptor_);
713 printer->Print(variables_,
714 "$deprecation$public Builder add$capitalized_name$($type$ value) {\n"
715 " if (value == null) {\n"
716 " throw new NullPointerException();\n"
717 " }\n"
718 " ensure$capitalized_name$IsMutable();\n"
719 " $name$_.add(value.getNumber());\n"
720 " $on_changed$\n"
721 " return this;\n"
722 "}\n");
723 WriteFieldDocComment(printer, descriptor_);
724 printer->Print(variables_,
725 "$deprecation$public Builder addAll$capitalized_name$(\n"
726 " java.lang.Iterable<? extends $type$> values) {\n"
727 " ensure$capitalized_name$IsMutable();\n"
728 " for ($type$ value : values) {\n"
729 " $name$_.add(value.getNumber());\n"
730 " }\n"
731 " $on_changed$\n"
732 " return this;\n"
733 "}\n");
734 WriteFieldDocComment(printer, descriptor_);
735 printer->Print(variables_,
736 "$deprecation$public Builder clear$capitalized_name$() {\n"
737 " $name$_ = java.util.Collections.emptyList();\n"
738 " $clear_mutable_bit_builder$;\n"
739 " $on_changed$\n"
740 " return this;\n"
741 "}\n");
742
743 if (SupportUnknownEnumValue(descriptor_->file())) {
744 WriteFieldDocComment(printer, descriptor_);
745 printer->Print(variables_,
746 "$deprecation$public java.util.List<java.lang.Integer>\n"
747 "get$capitalized_name$ValueList() {\n"
748 " return java.util.Collections.unmodifiableList($name$_);\n"
749 "}\n");
750 WriteFieldDocComment(printer, descriptor_);
751 printer->Print(variables_,
752 "$deprecation$public int get$capitalized_name$Value(int index) {\n"
753 " return $name$_.get(index);\n"
754 "}\n");
755 WriteFieldDocComment(printer, descriptor_);
756 printer->Print(variables_,
757 "$deprecation$public Builder set$capitalized_name$Value(\n"
758 " int index, int value) {\n"
759 " ensure$capitalized_name$IsMutable();\n"
760 " $name$_.set(index, value);\n"
761 " $on_changed$\n"
762 " return this;\n"
763 "}\n");
764 WriteFieldDocComment(printer, descriptor_);
765 printer->Print(variables_,
766 "$deprecation$public Builder add$capitalized_name$Value(int value) {\n"
767 " ensure$capitalized_name$IsMutable();\n"
768 " $name$_.add(value);\n"
769 " $on_changed$\n"
770 " return this;\n"
771 "}\n");
772 WriteFieldDocComment(printer, descriptor_);
773 printer->Print(variables_,
774 "$deprecation$public Builder addAll$capitalized_name$Value(\n"
775 " java.lang.Iterable<java.lang.Integer> values) {\n"
776 " ensure$capitalized_name$IsMutable();\n"
777 " for (int value : values) {\n"
778 " $name$_.add(value);\n"
779 " }\n"
780 " $on_changed$\n"
781 " return this;\n"
782 "}\n");
783 }
784 }
785
786 void RepeatedImmutableEnumFieldGenerator::
GenerateFieldBuilderInitializationCode(io::Printer * printer) const787 GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
788 // noop for enums
789 }
790
791 void RepeatedImmutableEnumFieldGenerator::
GenerateInitializationCode(io::Printer * printer) const792 GenerateInitializationCode(io::Printer* printer) const {
793 printer->Print(variables_, "$name$_ = java.util.Collections.emptyList();\n");
794 }
795
796 void RepeatedImmutableEnumFieldGenerator::
GenerateBuilderClearCode(io::Printer * printer) const797 GenerateBuilderClearCode(io::Printer* printer) const {
798 printer->Print(variables_,
799 "$name$_ = java.util.Collections.emptyList();\n"
800 "$clear_mutable_bit_builder$;\n");
801 }
802
803 void RepeatedImmutableEnumFieldGenerator::
GenerateMergingCode(io::Printer * printer) const804 GenerateMergingCode(io::Printer* printer) const {
805 // The code below does two optimizations:
806 // 1. If the other list is empty, there's nothing to do. This ensures we
807 // don't allocate a new array if we already have an immutable one.
808 // 2. If the other list is non-empty and our current list is empty, we can
809 // reuse the other list which is guaranteed to be immutable.
810 printer->Print(variables_,
811 "if (!other.$name$_.isEmpty()) {\n"
812 " if ($name$_.isEmpty()) {\n"
813 " $name$_ = other.$name$_;\n"
814 " $clear_mutable_bit_builder$;\n"
815 " } else {\n"
816 " ensure$capitalized_name$IsMutable();\n"
817 " $name$_.addAll(other.$name$_);\n"
818 " }\n"
819 " $on_changed$\n"
820 "}\n");
821 }
822
823 void RepeatedImmutableEnumFieldGenerator::
GenerateBuildingCode(io::Printer * printer) const824 GenerateBuildingCode(io::Printer* printer) const {
825 // The code below ensures that the result has an immutable list. If our
826 // list is immutable, we can just reuse it. If not, we make it immutable.
827 printer->Print(variables_,
828 "if ($get_mutable_bit_builder$) {\n"
829 " $name$_ = java.util.Collections.unmodifiableList($name$_);\n"
830 " $clear_mutable_bit_builder$;\n"
831 "}\n"
832 "result.$name$_ = $name$_;\n");
833 }
834
835 void RepeatedImmutableEnumFieldGenerator::
GenerateParsingCode(io::Printer * printer) const836 GenerateParsingCode(io::Printer* printer) const {
837 // Read and store the enum
838 if (SupportUnknownEnumValue(descriptor_->file())) {
839 printer->Print(variables_,
840 "int rawValue = input.readEnum();\n"
841 "if (!$get_mutable_bit_parser$) {\n"
842 " $name$_ = new java.util.ArrayList<java.lang.Integer>();\n"
843 " $set_mutable_bit_parser$;\n"
844 "}\n"
845 "$name$_.add(rawValue);\n");
846 } else {
847 printer->Print(variables_,
848 "int rawValue = input.readEnum();\n"
849 "$type$ value = $type$.$for_number$(rawValue);\n"
850 "if (value == null) {\n");
851 if (PreserveUnknownFields(descriptor_->containing_type())) {
852 printer->Print(variables_,
853 " unknownFields.mergeVarintField($number$, rawValue);\n");
854 }
855 printer->Print(variables_,
856 "} else {\n"
857 " if (!$get_mutable_bit_parser$) {\n"
858 " $name$_ = new java.util.ArrayList<java.lang.Integer>();\n"
859 " $set_mutable_bit_parser$;\n"
860 " }\n"
861 " $name$_.add(rawValue);\n"
862 "}\n");
863 }
864 }
865
866 void RepeatedImmutableEnumFieldGenerator::
GenerateParsingCodeFromPacked(io::Printer * printer) const867 GenerateParsingCodeFromPacked(io::Printer* printer) const {
868 // Wrap GenerateParsingCode's contents with a while loop.
869
870 printer->Print(variables_,
871 "int length = input.readRawVarint32();\n"
872 "int oldLimit = input.pushLimit(length);\n"
873 "while(input.getBytesUntilLimit() > 0) {\n");
874 printer->Indent();
875
876 GenerateParsingCode(printer);
877
878 printer->Outdent();
879 printer->Print(variables_,
880 "}\n"
881 "input.popLimit(oldLimit);\n");
882 }
883
884 void RepeatedImmutableEnumFieldGenerator::
GenerateParsingDoneCode(io::Printer * printer) const885 GenerateParsingDoneCode(io::Printer* printer) const {
886 printer->Print(variables_,
887 "if ($get_mutable_bit_parser$) {\n"
888 " $name$_ = java.util.Collections.unmodifiableList($name$_);\n"
889 "}\n");
890 }
891
892 void RepeatedImmutableEnumFieldGenerator::
GenerateSerializationCode(io::Printer * printer) const893 GenerateSerializationCode(io::Printer* printer) const {
894 if (descriptor_->is_packed()) {
895 printer->Print(variables_,
896 "if (get$capitalized_name$List().size() > 0) {\n"
897 " output.writeUInt32NoTag($tag$);\n"
898 " output.writeUInt32NoTag($name$MemoizedSerializedSize);\n"
899 "}\n"
900 "for (int i = 0; i < $name$_.size(); i++) {\n"
901 " output.writeEnumNoTag($name$_.get(i));\n"
902 "}\n");
903 } else {
904 printer->Print(variables_,
905 "for (int i = 0; i < $name$_.size(); i++) {\n"
906 " output.writeEnum($number$, $name$_.get(i));\n"
907 "}\n");
908 }
909 }
910
911 void RepeatedImmutableEnumFieldGenerator::
GenerateSerializedSizeCode(io::Printer * printer) const912 GenerateSerializedSizeCode(io::Printer* printer) const {
913 printer->Print(variables_,
914 "{\n"
915 " int dataSize = 0;\n");
916 printer->Indent();
917
918 printer->Print(variables_,
919 "for (int i = 0; i < $name$_.size(); i++) {\n"
920 " dataSize += com.google.protobuf.CodedOutputStream\n"
921 " .computeEnumSizeNoTag($name$_.get(i));\n"
922 "}\n");
923 printer->Print(
924 "size += dataSize;\n");
925 if (descriptor_->is_packed()) {
926 printer->Print(variables_,
927 "if (!get$capitalized_name$List().isEmpty()) {"
928 " size += $tag_size$;\n"
929 " size += com.google.protobuf.CodedOutputStream\n"
930 " .computeUInt32SizeNoTag(dataSize);\n"
931 "}");
932 } else {
933 printer->Print(variables_,
934 "size += $tag_size$ * $name$_.size();\n");
935 }
936
937 // cache the data size for packed fields.
938 if (descriptor_->is_packed()) {
939 printer->Print(variables_,
940 "$name$MemoizedSerializedSize = dataSize;\n");
941 }
942
943 printer->Outdent();
944 printer->Print("}\n");
945 }
946
947 void RepeatedImmutableEnumFieldGenerator::
GenerateEqualsCode(io::Printer * printer) const948 GenerateEqualsCode(io::Printer* printer) const {
949 printer->Print(variables_,
950 "result = result && $name$_.equals(other.$name$_);\n");
951 }
952
953 void RepeatedImmutableEnumFieldGenerator::
GenerateHashCode(io::Printer * printer) const954 GenerateHashCode(io::Printer* printer) const {
955 printer->Print(variables_,
956 "if (get$capitalized_name$Count() > 0) {\n"
957 " hash = (37 * hash) + $constant_name$;\n"
958 " hash = (53 * hash) + $name$_.hashCode();\n"
959 "}\n");
960 }
961
GetBoxedType() const962 string RepeatedImmutableEnumFieldGenerator::GetBoxedType() const {
963 return name_resolver_->GetImmutableClassName(descriptor_->enum_type());
964 }
965
966 } // namespace java
967 } // namespace compiler
968 } // namespace protobuf
969 } // namespace google
970