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 <google/protobuf/compiler/java/enum_field_lite.h>
36
37#include <cstdint>
38#include <map>
39#include <string>
40
41#include <google/protobuf/stubs/logging.h>
42#include <google/protobuf/stubs/common.h>
43#include <google/protobuf/io/printer.h>
44#include <google/protobuf/wire_format.h>
45#include <google/protobuf/stubs/strutil.h>
46#include <google/protobuf/compiler/java/context.h>
47#include <google/protobuf/compiler/java/doc_comment.h>
48#include <google/protobuf/compiler/java/helpers.h>
49#include <google/protobuf/compiler/java/name_resolver.h>
50
51// Must be last.
52#include <google/protobuf/port_def.inc>
53
54namespace google {
55namespace protobuf {
56namespace compiler {
57namespace java {
58
59namespace {
60bool EnableExperimentalRuntimeForLite() {
61#ifdef PROTOBUF_EXPERIMENT
62 return PROTOBUF_EXPERIMENT;
63#else // PROTOBUF_EXPERIMENT
64 return false;
65#endif // !PROTOBUF_EXPERIMENT
66}
67
68void SetEnumVariables(const FieldDescriptor* descriptor, int messageBitIndex,
69 int builderBitIndex, const FieldGeneratorInfo* info,
70 ClassNameResolver* name_resolver,
71 std::map<std::string, std::string>* variables) {
72 SetCommonFieldVariables(descriptor, info, variables);
73
74 (*variables)["type"] =
75 name_resolver->GetImmutableClassName(descriptor: descriptor->enum_type());
76 (*variables)["kt_type"] = (*variables)["type"];
77 (*variables)["mutable_type"] =
78 name_resolver->GetMutableClassName(descriptor: descriptor->enum_type());
79 (*variables)["default"] = ImmutableDefaultValue(field: descriptor, name_resolver);
80 (*variables)["default_number"] =
81 StrCat(a: descriptor->default_value_enum()->number());
82 (*variables)["tag"] = StrCat(
83 a: static_cast<int32_t>(internal::WireFormat::MakeTag(field: descriptor)));
84 (*variables)["tag_size"] = StrCat(
85 a: internal::WireFormat::TagSize(field_number: descriptor->number(), type: GetType(field: descriptor)));
86 // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
87 // by the proto compiler
88 (*variables)["deprecation"] =
89 descriptor->options().deprecated() ? "@java.lang.Deprecated " : "";
90 (*variables)["kt_deprecation"] =
91 descriptor->options().deprecated()
92 ? "@kotlin.Deprecated(message = \"Field " + (*variables)["name"] +
93 " is deprecated\") "
94 : "";
95 (*variables)["required"] = descriptor->is_required() ? "true" : "false";
96
97 if (HasHasbit(descriptor)) {
98 // For singular messages and builders, one bit is used for the hasField bit.
99 (*variables)["get_has_field_bit_message"] = GenerateGetBit(bitIndex: messageBitIndex);
100
101 // Note that these have a trailing ";".
102 (*variables)["set_has_field_bit_message"] =
103 GenerateSetBit(bitIndex: messageBitIndex) + ";";
104 (*variables)["clear_has_field_bit_message"] =
105 GenerateClearBit(bitIndex: messageBitIndex) + ";";
106
107 (*variables)["is_field_present_message"] = GenerateGetBit(bitIndex: messageBitIndex);
108 } else {
109 (*variables)["set_has_field_bit_message"] = "";
110 (*variables)["clear_has_field_bit_message"] = "";
111
112 (*variables)["is_field_present_message"] =
113 (*variables)["name"] + "_ != " + (*variables)["default"] +
114 ".getNumber()";
115 }
116
117 (*variables)["get_has_field_bit_from_local"] =
118 GenerateGetBitFromLocal(bitIndex: builderBitIndex);
119 (*variables)["set_has_field_bit_to_local"] =
120 GenerateSetBitToLocal(bitIndex: messageBitIndex);
121
122 if (SupportUnknownEnumValue(descriptor: descriptor->file())) {
123 (*variables)["unknown"] = (*variables)["type"] + ".UNRECOGNIZED";
124 } else {
125 (*variables)["unknown"] = (*variables)["default"];
126 }
127
128 // We use `x.getClass()` as a null check because it generates less bytecode
129 // than an `if (x == null) { throw ... }` statement.
130 (*variables)["null_check"] = "value.getClass();\n";
131}
132
133} // namespace
134
135// ===================================================================
136
137ImmutableEnumFieldLiteGenerator::ImmutableEnumFieldLiteGenerator(
138 const FieldDescriptor* descriptor, int messageBitIndex, Context* context)
139 : descriptor_(descriptor),
140 messageBitIndex_(messageBitIndex),
141 context_(context),
142 name_resolver_(context->GetNameResolver()) {
143 SetEnumVariables(descriptor, messageBitIndex, builderBitIndex: 0,
144 info: context->GetFieldGeneratorInfo(field: descriptor), name_resolver: name_resolver_,
145 variables: &variables_);
146}
147
148ImmutableEnumFieldLiteGenerator::~ImmutableEnumFieldLiteGenerator() {}
149
150int ImmutableEnumFieldLiteGenerator::GetNumBitsForMessage() const {
151 return HasHasbit(descriptor: descriptor_) ? 1 : 0;
152}
153
154void ImmutableEnumFieldLiteGenerator::GenerateInterfaceMembers(
155 io::Printer* printer) const {
156 if (HasHazzer(descriptor: descriptor_)) {
157 WriteFieldAccessorDocComment(printer, field: descriptor_, type: HAZZER);
158 printer->Print(variables: variables_,
159 text: "$deprecation$boolean has$capitalized_name$();\n");
160 }
161 if (SupportUnknownEnumValue(descriptor: descriptor_->file())) {
162 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_, type: GETTER);
163 printer->Print(variables: variables_,
164 text: "$deprecation$int get$capitalized_name$Value();\n");
165 }
166 WriteFieldAccessorDocComment(printer, field: descriptor_, type: GETTER);
167 printer->Print(variables: variables_, text: "$deprecation$$type$ get$capitalized_name$();\n");
168}
169
170void ImmutableEnumFieldLiteGenerator::GenerateMembers(
171 io::Printer* printer) const {
172 printer->Print(variables: variables_, text: "private int $name$_;\n");
173 PrintExtraFieldInfo(variables: variables_, printer);
174 if (HasHazzer(descriptor: descriptor_)) {
175 WriteFieldAccessorDocComment(printer, field: descriptor_, type: HAZZER);
176 printer->Print(
177 variables: variables_,
178 text: "@java.lang.Override\n"
179 "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
180 " return $get_has_field_bit_message$;\n"
181 "}\n");
182 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
183 }
184 if (SupportUnknownEnumValue(descriptor: descriptor_->file())) {
185 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_, type: GETTER);
186 printer->Print(
187 variables: variables_,
188 text: "@java.lang.Override\n"
189 "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n"
190 " return $name$_;\n"
191 "}\n");
192 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
193 }
194 WriteFieldAccessorDocComment(printer, field: descriptor_, type: GETTER);
195 printer->Print(variables: variables_,
196 text: "@java.lang.Override\n"
197 "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n"
198 " $type$ result = $type$.forNumber($name$_);\n"
199 " return result == null ? $unknown$ : result;\n"
200 "}\n");
201 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
202
203 // Generate private setters for the builder to proxy into.
204 if (SupportUnknownEnumValue(descriptor: descriptor_->file())) {
205 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_, type: SETTER);
206 printer->Print(variables: variables_,
207 text: "private void set$capitalized_name$Value(int value) {\n"
208 " $set_has_field_bit_message$"
209 " $name$_ = value;\n"
210 "}\n");
211 }
212 WriteFieldAccessorDocComment(printer, field: descriptor_, type: SETTER);
213 printer->Print(variables: variables_,
214 text: "private void set$capitalized_name$($type$ value) {\n"
215 " $name$_ = value.getNumber();\n"
216 " $set_has_field_bit_message$\n"
217 "}\n");
218 WriteFieldAccessorDocComment(printer, field: descriptor_, type: CLEARER);
219 printer->Print(variables: variables_,
220 text: "private void clear$capitalized_name$() {\n"
221 " $clear_has_field_bit_message$\n"
222 " $name$_ = $default_number$;\n"
223 "}\n");
224}
225
226void ImmutableEnumFieldLiteGenerator::GenerateBuilderMembers(
227 io::Printer* printer) const {
228 if (HasHazzer(descriptor: descriptor_)) {
229 WriteFieldAccessorDocComment(printer, field: descriptor_, type: HAZZER);
230 printer->Print(
231 variables: variables_,
232 text: "@java.lang.Override\n"
233 "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
234 " return instance.has$capitalized_name$();\n"
235 "}\n");
236 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
237 }
238 if (SupportUnknownEnumValue(descriptor: descriptor_->file())) {
239 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_, type: GETTER);
240 printer->Print(
241 variables: variables_,
242 text: "@java.lang.Override\n"
243 "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n"
244 " return instance.get$capitalized_name$Value();\n"
245 "}\n");
246 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
247 WriteFieldAccessorDocComment(printer, field: descriptor_, type: SETTER,
248 /* builder */ true);
249 printer->Print(variables: variables_,
250 text: "$deprecation$public Builder "
251 "${$set$capitalized_name$Value$}$(int value) {\n"
252 " copyOnWrite();\n"
253 " instance.set$capitalized_name$Value(value);\n"
254 " return this;\n"
255 "}\n");
256 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
257 }
258 WriteFieldAccessorDocComment(printer, field: descriptor_, type: GETTER);
259 printer->Print(variables: variables_,
260 text: "@java.lang.Override\n"
261 "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n"
262 " return instance.get$capitalized_name$();\n"
263 "}\n");
264 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
265 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_, type: SETTER,
266 /* builder */ true);
267 printer->Print(variables: variables_,
268 text: "$deprecation$public Builder "
269 "${$set$capitalized_name$$}$($type$ value) {\n"
270 " copyOnWrite();\n"
271 " instance.set$capitalized_name$(value);\n"
272 " return this;\n"
273 "}\n");
274 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
275 WriteFieldAccessorDocComment(printer, field: descriptor_, type: CLEARER,
276 /* builder */ true);
277 printer->Print(
278 variables: variables_,
279 text: "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n"
280 " copyOnWrite();\n"
281 " instance.clear$capitalized_name$();\n"
282 " return this;\n"
283 "}\n");
284 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
285}
286
287void ImmutableEnumFieldLiteGenerator::GenerateKotlinDslMembers(
288 io::Printer* printer) const {
289 WriteFieldDocComment(printer, field: descriptor_);
290 printer->Print(variables: variables_,
291 text: "$kt_deprecation$var $kt_name$: $kt_type$\n"
292 " @JvmName(\"${$get$kt_capitalized_name$$}$\")\n"
293 " get() = $kt_dsl_builder$.${$get$capitalized_name$$}$()\n"
294 " @JvmName(\"${$set$kt_capitalized_name$$}$\")\n"
295 " set(value) {\n"
296 " $kt_dsl_builder$.${$set$capitalized_name$$}$(value)\n"
297 " }\n");
298
299 WriteFieldAccessorDocComment(printer, field: descriptor_, type: CLEARER,
300 /* builder */ false);
301 printer->Print(variables: variables_,
302 text: "fun ${$clear$kt_capitalized_name$$}$() {\n"
303 " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n"
304 "}\n");
305
306 if (HasHazzer(descriptor: descriptor_)) {
307 WriteFieldAccessorDocComment(printer, field: descriptor_, type: HAZZER);
308 printer->Print(
309 variables: variables_,
310 text: "fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n"
311 " return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n"
312 "}\n");
313 }
314}
315
316void ImmutableEnumFieldLiteGenerator::GenerateInitializationCode(
317 io::Printer* printer) const {
318 if (!IsDefaultValueJavaDefault(field: descriptor_)) {
319 printer->Print(variables: variables_, text: "$name$_ = $default_number$;\n");
320 }
321}
322
323void ImmutableEnumFieldLiteGenerator::GenerateFieldInfo(
324 io::Printer* printer, std::vector<uint16_t>* output) const {
325 WriteIntToUtf16CharSequence(value: descriptor_->number(), output);
326 WriteIntToUtf16CharSequence(value: GetExperimentalJavaFieldType(field: descriptor_),
327 output);
328 if (HasHasbit(descriptor: descriptor_)) {
329 WriteIntToUtf16CharSequence(value: messageBitIndex_, output);
330 }
331 printer->Print(variables: variables_, text: "\"$name$_\",\n");
332 if (!SupportUnknownEnumValue(field: (descriptor_))) {
333 PrintEnumVerifierLogic(printer, descriptor: descriptor_, variables: variables_,
334 /*var_name=*/"$type$",
335 /*terminating_string=*/",\n",
336 /*enforce_lite=*/context_->EnforceLite());
337 }
338}
339
340std::string ImmutableEnumFieldLiteGenerator::GetBoxedType() const {
341 return name_resolver_->GetImmutableClassName(descriptor: descriptor_->enum_type());
342}
343
344// ===================================================================
345
346ImmutableEnumOneofFieldLiteGenerator::ImmutableEnumOneofFieldLiteGenerator(
347 const FieldDescriptor* descriptor, int messageBitIndex, Context* context)
348 : ImmutableEnumFieldLiteGenerator(descriptor, messageBitIndex, context) {
349 const OneofGeneratorInfo* info =
350 context->GetOneofGeneratorInfo(oneof: descriptor->containing_oneof());
351 SetCommonOneofVariables(descriptor, info, variables: &variables_);
352}
353
354ImmutableEnumOneofFieldLiteGenerator::~ImmutableEnumOneofFieldLiteGenerator() {}
355
356void ImmutableEnumOneofFieldLiteGenerator::GenerateMembers(
357 io::Printer* printer) const {
358 PrintExtraFieldInfo(variables: variables_, printer);
359 GOOGLE_DCHECK(HasHazzer(descriptor_));
360 WriteFieldAccessorDocComment(printer, field: descriptor_, type: HAZZER);
361 printer->Print(variables: variables_,
362 text: "@java.lang.Override\n"
363 "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
364 " return $has_oneof_case_message$;\n"
365 "}\n");
366 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
367
368 if (SupportUnknownEnumValue(descriptor: descriptor_->file())) {
369 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_, type: GETTER);
370 printer->Print(
371 variables: variables_,
372 text: "@java.lang.Override\n"
373 "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n"
374 " if ($has_oneof_case_message$) {\n"
375 " return (java.lang.Integer) $oneof_name$_;\n"
376 " }\n"
377 " return $default_number$;\n"
378 "}\n");
379 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
380 }
381 WriteFieldAccessorDocComment(printer, field: descriptor_, type: GETTER);
382 printer->Print(variables: variables_,
383 text: "@java.lang.Override\n"
384 "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n"
385 " if ($has_oneof_case_message$) {\n"
386 " $type$ result = $type$.forNumber((java.lang.Integer) "
387 "$oneof_name$_);\n"
388 " return result == null ? $unknown$ : result;\n"
389 " }\n"
390 " return $default$;\n"
391 "}\n");
392 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
393
394 // Generate private setters for the builder to proxy into.
395 if (SupportUnknownEnumValue(descriptor: descriptor_->file())) {
396 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_, type: SETTER);
397 printer->Print(variables: variables_,
398 text: "private void set$capitalized_name$Value(int value) {\n"
399 " $set_oneof_case_message$;\n"
400 " $oneof_name$_ = value;\n"
401 "}\n");
402 }
403 WriteFieldAccessorDocComment(printer, field: descriptor_, type: SETTER);
404 printer->Print(variables: variables_,
405 text: "private void set$capitalized_name$($type$ value) {\n"
406 " $oneof_name$_ = value.getNumber();\n"
407 " $set_oneof_case_message$;\n"
408 "}\n");
409 WriteFieldAccessorDocComment(printer, field: descriptor_, type: CLEARER);
410 printer->Print(variables: variables_,
411 text: "private void clear$capitalized_name$() {\n"
412 " if ($has_oneof_case_message$) {\n"
413 " $clear_oneof_case_message$;\n"
414 " $oneof_name$_ = null;\n"
415 " }\n"
416 "}\n");
417}
418
419void ImmutableEnumOneofFieldLiteGenerator::GenerateFieldInfo(
420 io::Printer* printer, std::vector<uint16_t>* output) const {
421 WriteIntToUtf16CharSequence(value: descriptor_->number(), output);
422 WriteIntToUtf16CharSequence(value: GetExperimentalJavaFieldType(field: descriptor_),
423 output);
424 WriteIntToUtf16CharSequence(value: descriptor_->containing_oneof()->index(), output);
425 if (!SupportUnknownEnumValue(field: descriptor_)) {
426 PrintEnumVerifierLogic(printer, descriptor: descriptor_, variables: variables_,
427 /*var_name=*/"$type$",
428 /*terminating_string=*/",\n",
429 /*enforce_lite=*/context_->EnforceLite());
430 }
431}
432
433void ImmutableEnumOneofFieldLiteGenerator::GenerateBuilderMembers(
434 io::Printer* printer) const {
435 GOOGLE_DCHECK(HasHazzer(descriptor_));
436 WriteFieldAccessorDocComment(printer, field: descriptor_, type: HAZZER);
437 printer->Print(variables: variables_,
438 text: "@java.lang.Override\n"
439 "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
440 " return instance.has$capitalized_name$();\n"
441 "}\n");
442 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
443
444 if (SupportUnknownEnumValue(descriptor: descriptor_->file())) {
445 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_, type: GETTER);
446 printer->Print(
447 variables: variables_,
448 text: "@java.lang.Override\n"
449 "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n"
450 " return instance.get$capitalized_name$Value();\n"
451 "}\n");
452 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
453 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_, type: SETTER,
454 /* builder */ true);
455 printer->Print(variables: variables_,
456 text: "$deprecation$public Builder "
457 "${$set$capitalized_name$Value$}$(int value) {\n"
458 " copyOnWrite();\n"
459 " instance.set$capitalized_name$Value(value);\n"
460 " return this;\n"
461 "}\n");
462 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
463 }
464 WriteFieldAccessorDocComment(printer, field: descriptor_, type: GETTER);
465 printer->Print(variables: variables_,
466 text: "@java.lang.Override\n"
467 "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n"
468 " return instance.get$capitalized_name$();\n"
469 "}\n");
470 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
471 WriteFieldAccessorDocComment(printer, field: descriptor_, type: SETTER,
472 /* builder */ true);
473 printer->Print(variables: variables_,
474 text: "$deprecation$public Builder "
475 "${$set$capitalized_name$$}$($type$ value) {\n"
476 " copyOnWrite();\n"
477 " instance.set$capitalized_name$(value);\n"
478 " return this;\n"
479 "}\n");
480 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
481 WriteFieldAccessorDocComment(printer, field: descriptor_, type: CLEARER,
482 /* builder */ true);
483 printer->Print(
484 variables: variables_,
485 text: "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n"
486 " copyOnWrite();\n"
487 " instance.clear$capitalized_name$();\n"
488 " return this;\n"
489 "}\n");
490 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
491}
492
493// ===================================================================
494
495RepeatedImmutableEnumFieldLiteGenerator::
496 RepeatedImmutableEnumFieldLiteGenerator(const FieldDescriptor* descriptor,
497 int messageBitIndex,
498 Context* context)
499 : descriptor_(descriptor),
500 context_(context),
501 name_resolver_(context->GetNameResolver()) {
502 SetEnumVariables(descriptor, messageBitIndex, builderBitIndex: 0,
503 info: context->GetFieldGeneratorInfo(field: descriptor), name_resolver: name_resolver_,
504 variables: &variables_);
505}
506
507RepeatedImmutableEnumFieldLiteGenerator::
508 ~RepeatedImmutableEnumFieldLiteGenerator() {}
509
510int RepeatedImmutableEnumFieldLiteGenerator::GetNumBitsForMessage() const {
511 return 0;
512}
513
514void RepeatedImmutableEnumFieldLiteGenerator::GenerateInterfaceMembers(
515 io::Printer* printer) const {
516 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_GETTER);
517 printer->Print(
518 variables: variables_,
519 text: "$deprecation$java.util.List<$type$> get$capitalized_name$List();\n");
520 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_COUNT);
521 printer->Print(variables: variables_,
522 text: "$deprecation$int get$capitalized_name$Count();\n");
523 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_INDEXED_GETTER);
524 printer->Print(variables: variables_,
525 text: "$deprecation$$type$ get$capitalized_name$(int index);\n");
526 if (SupportUnknownEnumValue(descriptor: descriptor_->file())) {
527 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_, type: LIST_GETTER);
528 printer->Print(variables: variables_,
529 text: "$deprecation$java.util.List<java.lang.Integer>\n"
530 "get$capitalized_name$ValueList();\n");
531 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_,
532 type: LIST_INDEXED_GETTER);
533 printer->Print(variables: variables_,
534 text: "$deprecation$int get$capitalized_name$Value(int index);\n");
535 }
536}
537
538void RepeatedImmutableEnumFieldLiteGenerator::GenerateMembers(
539 io::Printer* printer) const {
540 printer->Print(
541 variables: variables_,
542 text: "private com.google.protobuf.Internal.IntList $name$_;\n"
543 "private static final "
544 "com.google.protobuf.Internal.ListAdapter.Converter<\n"
545 " java.lang.Integer, $type$> $name$_converter_ =\n"
546 " new com.google.protobuf.Internal.ListAdapter.Converter<\n"
547 " java.lang.Integer, $type$>() {\n"
548 " @java.lang.Override\n"
549 " public $type$ convert(java.lang.Integer from) {\n"
550 " $type$ result = $type$.forNumber(from);\n"
551 " return result == null ? $unknown$ : result;\n"
552 " }\n"
553 " };\n");
554 PrintExtraFieldInfo(variables: variables_, printer);
555 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_GETTER);
556 printer->Print(
557 variables: variables_,
558 text: "@java.lang.Override\n"
559 "$deprecation$public java.util.List<$type$> "
560 "${$get$capitalized_name$List$}$() {\n"
561 " return new com.google.protobuf.Internal.ListAdapter<\n"
562 " java.lang.Integer, $type$>($name$_, $name$_converter_);\n"
563 "}\n");
564 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
565 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_COUNT);
566 printer->Print(
567 variables: variables_,
568 text: "@java.lang.Override\n"
569 "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n"
570 " return $name$_.size();\n"
571 "}\n");
572 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
573 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_INDEXED_GETTER);
574 printer->Print(
575 variables: variables_,
576 // NB: Do not use the "$name$_converter_" field; the usage of generics
577 // (and requisite upcasts to Object) prevent optimizations. Even
578 // without any optimizations, the below code is cheaper because it
579 // avoids boxing an int and a checkcast from the generics.
580 text: "@java.lang.Override\n"
581 "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n"
582 " $type$ result = $type$.forNumber($name$_.getInt(index));\n"
583 " return result == null ? $unknown$ : result;\n"
584 "}\n");
585 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
586 if (SupportUnknownEnumValue(descriptor: descriptor_->file())) {
587 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_, type: LIST_GETTER);
588 printer->Print(variables: variables_,
589 text: "@java.lang.Override\n"
590 "$deprecation$public java.util.List<java.lang.Integer>\n"
591 "${$get$capitalized_name$ValueList$}$() {\n"
592 " return $name$_;\n"
593 "}\n");
594 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
595 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_,
596 type: LIST_INDEXED_GETTER);
597 printer->Print(variables: variables_,
598 text: "@java.lang.Override\n"
599 "$deprecation$public int "
600 "${$get$capitalized_name$Value$}$(int index) {\n"
601 " return $name$_.getInt(index);\n"
602 "}\n");
603 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
604 }
605
606 if (!EnableExperimentalRuntimeForLite() && descriptor_->is_packed() &&
607 context_->HasGeneratedMethods(descriptor: descriptor_->containing_type())) {
608 printer->Print(variables: variables_, text: "private int $name$MemoizedSerializedSize;\n");
609 }
610
611 // Generate private setters for the builder to proxy into.
612 printer->Print(
613 variables: variables_,
614 text: "private void ensure$capitalized_name$IsMutable() {\n"
615 // Use a temporary to avoid a redundant iget-object.
616 " com.google.protobuf.Internal.IntList tmp = $name$_;\n"
617 " if (!tmp.isModifiable()) {\n"
618 " $name$_ =\n"
619 " com.google.protobuf.GeneratedMessageLite.mutableCopy(tmp);\n"
620 " }\n"
621 "}\n");
622 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_INDEXED_SETTER);
623 printer->Print(variables: variables_,
624 text: "private void set$capitalized_name$(\n"
625 " int index, $type$ value) {\n"
626 " $null_check$"
627 " ensure$capitalized_name$IsMutable();\n"
628 " $name$_.setInt(index, value.getNumber());\n"
629 "}\n");
630 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_ADDER);
631 printer->Print(variables: variables_,
632 text: "private void add$capitalized_name$($type$ value) {\n"
633 " $null_check$"
634 " ensure$capitalized_name$IsMutable();\n"
635 " $name$_.addInt(value.getNumber());\n"
636 "}\n");
637 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_MULTI_ADDER);
638 printer->Print(variables: variables_,
639 text: "private void addAll$capitalized_name$(\n"
640 " java.lang.Iterable<? extends $type$> values) {\n"
641 " ensure$capitalized_name$IsMutable();\n"
642 " for ($type$ value : values) {\n"
643 " $name$_.addInt(value.getNumber());\n"
644 " }\n"
645 "}\n");
646 WriteFieldAccessorDocComment(printer, field: descriptor_, type: CLEARER);
647 printer->Print(variables: variables_,
648 text: "private void clear$capitalized_name$() {\n"
649 " $name$_ = emptyIntList();\n"
650 "}\n");
651
652 if (SupportUnknownEnumValue(descriptor: descriptor_->file())) {
653 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_, type: SETTER);
654 printer->Print(variables: variables_,
655 text: "private void set$capitalized_name$Value(\n"
656 " int index, int value) {\n"
657 " ensure$capitalized_name$IsMutable();\n"
658 " $name$_.setInt(index, value);\n"
659 "}\n");
660 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_, type: LIST_ADDER);
661 printer->Print(variables: variables_,
662 text: "private void add$capitalized_name$Value(int value) {\n"
663 " ensure$capitalized_name$IsMutable();\n"
664 " $name$_.addInt(value);\n"
665 "}\n");
666 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_,
667 type: LIST_MULTI_ADDER);
668 printer->Print(variables: variables_,
669 text: "private void addAll$capitalized_name$Value(\n"
670 " java.lang.Iterable<java.lang.Integer> values) {\n"
671 " ensure$capitalized_name$IsMutable();\n"
672 " for (int value : values) {\n"
673 " $name$_.addInt(value);\n"
674 " }\n"
675 "}\n");
676 }
677}
678
679void RepeatedImmutableEnumFieldLiteGenerator::GenerateFieldInfo(
680 io::Printer* printer, std::vector<uint16_t>* output) const {
681 WriteIntToUtf16CharSequence(value: descriptor_->number(), output);
682 WriteIntToUtf16CharSequence(value: GetExperimentalJavaFieldType(field: descriptor_),
683 output);
684 printer->Print(variables: variables_, text: "\"$name$_\",\n");
685 if (!SupportUnknownEnumValue(descriptor: descriptor_->file())) {
686 PrintEnumVerifierLogic(printer, descriptor: descriptor_, variables: variables_,
687 /*var_name=*/"$type$",
688 /*terminating_string=*/",\n",
689 /*enforce_lite=*/context_->EnforceLite());
690 }
691}
692
693void RepeatedImmutableEnumFieldLiteGenerator::GenerateBuilderMembers(
694 io::Printer* printer) const {
695 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_GETTER);
696 printer->Print(variables: variables_,
697 text: "@java.lang.Override\n"
698 "$deprecation$public java.util.List<$type$> "
699 "${$get$capitalized_name$List$}$() {\n"
700 " return instance.get$capitalized_name$List();\n"
701 "}\n");
702 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
703 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_COUNT);
704 printer->Print(
705 variables: variables_,
706 text: "@java.lang.Override\n"
707 "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n"
708 " return instance.get$capitalized_name$Count();\n"
709 "}\n");
710 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
711 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_INDEXED_GETTER);
712 printer->Print(
713 variables: variables_,
714 text: "@java.lang.Override\n"
715 "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n"
716 " return instance.get$capitalized_name$(index);\n"
717 "}\n");
718 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
719 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_INDEXED_SETTER,
720 /* builder */ true);
721 printer->Print(variables: variables_,
722 text: "$deprecation$public Builder ${$set$capitalized_name$$}$(\n"
723 " int index, $type$ value) {\n"
724 " copyOnWrite();\n"
725 " instance.set$capitalized_name$(index, value);\n"
726 " return this;\n"
727 "}\n");
728 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
729 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_ADDER,
730 /* builder */ true);
731 printer->Print(variables: variables_,
732 text: "$deprecation$public Builder "
733 "${$add$capitalized_name$$}$($type$ value) {\n"
734 " copyOnWrite();\n"
735 " instance.add$capitalized_name$(value);\n"
736 " return this;\n"
737 "}\n");
738 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
739 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_MULTI_ADDER,
740 /* builder */ true);
741 printer->Print(variables: variables_,
742 text: "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n"
743 " java.lang.Iterable<? extends $type$> values) {\n"
744 " copyOnWrite();\n"
745 " instance.addAll$capitalized_name$(values);"
746 " return this;\n"
747 "}\n");
748 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
749 WriteFieldAccessorDocComment(printer, field: descriptor_, type: CLEARER,
750 /* builder */ true);
751 printer->Print(
752 variables: variables_,
753 text: "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n"
754 " copyOnWrite();\n"
755 " instance.clear$capitalized_name$();\n"
756 " return this;\n"
757 "}\n");
758 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
759
760 if (SupportUnknownEnumValue(descriptor: descriptor_->file())) {
761 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_, type: LIST_GETTER);
762 printer->Print(variables: variables_,
763 text: "@java.lang.Override\n"
764 "$deprecation$public java.util.List<java.lang.Integer>\n"
765 "${$get$capitalized_name$ValueList$}$() {\n"
766 " return java.util.Collections.unmodifiableList(\n"
767 " instance.get$capitalized_name$ValueList());\n"
768 "}\n");
769 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
770 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_,
771 type: LIST_INDEXED_GETTER);
772 printer->Print(variables: variables_,
773 text: "@java.lang.Override\n"
774 "$deprecation$public int "
775 "${$get$capitalized_name$Value$}$(int index) {\n"
776 " return instance.get$capitalized_name$Value(index);\n"
777 "}\n");
778 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
779 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_,
780 type: LIST_INDEXED_SETTER,
781 /* builder */ true);
782 printer->Print(
783 variables: variables_,
784 text: "$deprecation$public Builder ${$set$capitalized_name$Value$}$(\n"
785 " int index, int value) {\n"
786 " copyOnWrite();\n"
787 " instance.set$capitalized_name$Value(index, value);\n"
788 " return this;\n"
789 "}\n");
790 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
791 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_, type: LIST_ADDER,
792 /* builder */ true);
793 printer->Print(variables: variables_,
794 text: "$deprecation$public Builder "
795 "${$add$capitalized_name$Value$}$(int value) {\n"
796 " instance.add$capitalized_name$Value(value);\n"
797 " return this;\n"
798 "}\n");
799 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
800 WriteFieldEnumValueAccessorDocComment(printer, field: descriptor_,
801 type: LIST_MULTI_ADDER,
802 /* builder */ true);
803 printer->Print(
804 variables: variables_,
805 text: "$deprecation$public Builder ${$addAll$capitalized_name$Value$}$(\n"
806 " java.lang.Iterable<java.lang.Integer> values) {\n"
807 " copyOnWrite();\n"
808 " instance.addAll$capitalized_name$Value(values);\n"
809 " return this;\n"
810 "}\n");
811 printer->Annotate(begin_varname: "{", end_varname: "}", descriptor: descriptor_);
812 }
813}
814
815void RepeatedImmutableEnumFieldLiteGenerator::GenerateInitializationCode(
816 io::Printer* printer) const {
817 printer->Print(variables: variables_, text: "$name$_ = emptyIntList();\n");
818}
819
820void RepeatedImmutableEnumFieldLiteGenerator::GenerateKotlinDslMembers(
821 io::Printer* printer) const {
822 printer->Print(
823 variables: variables_,
824 text: "/**\n"
825 " * An uninstantiable, behaviorless type to represent the field in\n"
826 " * generics.\n"
827 " */\n"
828 "@kotlin.OptIn"
829 "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n"
830 "class ${$$kt_capitalized_name$Proxy$}$ private constructor()"
831 " : com.google.protobuf.kotlin.DslProxy()\n");
832
833 WriteFieldDocComment(printer, field: descriptor_);
834 printer->Print(variables: variables_,
835 text: "$kt_deprecation$ val $kt_name$: "
836 "com.google.protobuf.kotlin.DslList"
837 "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>\n"
838 " @kotlin.jvm.JvmSynthetic\n"
839 " get() = com.google.protobuf.kotlin.DslList(\n"
840 " $kt_dsl_builder$.${$get$capitalized_name$List$}$()\n"
841 " )\n");
842
843 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_ADDER,
844 /* builder */ false);
845 printer->Print(variables: variables_,
846 text: "@kotlin.jvm.JvmSynthetic\n"
847 "@kotlin.jvm.JvmName(\"add$kt_capitalized_name$\")\n"
848 "fun com.google.protobuf.kotlin.DslList"
849 "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>."
850 "add(value: $kt_type$) {\n"
851 " $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n"
852 "}");
853
854 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_ADDER,
855 /* builder */ false);
856 printer->Print(variables: variables_,
857 text: "@kotlin.jvm.JvmSynthetic\n"
858 "@kotlin.jvm.JvmName(\"plusAssign$kt_capitalized_name$\")\n"
859 "@Suppress(\"NOTHING_TO_INLINE\")\n"
860 "inline operator fun com.google.protobuf.kotlin.DslList"
861 "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>."
862 "plusAssign(value: $kt_type$) {\n"
863 " add(value)\n"
864 "}");
865
866 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_MULTI_ADDER,
867 /* builder */ false);
868 printer->Print(variables: variables_,
869 text: "@kotlin.jvm.JvmSynthetic\n"
870 "@kotlin.jvm.JvmName(\"addAll$kt_capitalized_name$\")\n"
871 "fun com.google.protobuf.kotlin.DslList"
872 "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>."
873 "addAll(values: kotlin.collections.Iterable<$kt_type$>) {\n"
874 " $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n"
875 "}");
876
877 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_MULTI_ADDER,
878 /* builder */ false);
879 printer->Print(
880 variables: variables_,
881 text: "@kotlin.jvm.JvmSynthetic\n"
882 "@kotlin.jvm.JvmName(\"plusAssignAll$kt_capitalized_name$\")\n"
883 "@Suppress(\"NOTHING_TO_INLINE\")\n"
884 "inline operator fun com.google.protobuf.kotlin.DslList"
885 "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>."
886 "plusAssign(values: kotlin.collections.Iterable<$kt_type$>) {\n"
887 " addAll(values)\n"
888 "}");
889
890 WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_INDEXED_SETTER,
891 /* builder */ false);
892 printer->Print(
893 variables: variables_,
894 text: "@kotlin.jvm.JvmSynthetic\n"
895 "@kotlin.jvm.JvmName(\"set$kt_capitalized_name$\")\n"
896 "operator fun com.google.protobuf.kotlin.DslList"
897 "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>."
898 "set(index: kotlin.Int, value: $kt_type$) {\n"
899 " $kt_dsl_builder$.${$set$capitalized_name$$}$(index, value)\n"
900 "}");
901
902 WriteFieldAccessorDocComment(printer, field: descriptor_, type: CLEARER,
903 /* builder */ false);
904 printer->Print(variables: variables_,
905 text: "@kotlin.jvm.JvmSynthetic\n"
906 "@kotlin.jvm.JvmName(\"clear$kt_capitalized_name$\")\n"
907 "fun com.google.protobuf.kotlin.DslList"
908 "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>."
909 "clear() {\n"
910 " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n"
911 "}");
912}
913
914std::string RepeatedImmutableEnumFieldLiteGenerator::GetBoxedType() const {
915 return name_resolver_->GetImmutableClassName(descriptor: descriptor_->enum_type());
916}
917
918} // namespace java
919} // namespace compiler
920} // namespace protobuf
921} // namespace google
922
923#include <google/protobuf/port_undef.inc>
924