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 // Author: jonp@google.com (Jon Perlow)
33 // Based on original Protocol Buffers design by
34 // Sanjay Ghemawat, Jeff Dean, and others.
35
36 #include <google/protobuf/compiler/java/java_string_field.h>
37
38 #include <cstdint>
39 #include <map>
40 #include <string>
41
42 #include <google/protobuf/stubs/logging.h>
43 #include <google/protobuf/stubs/common.h>
44 #include <google/protobuf/io/printer.h>
45 #include <google/protobuf/wire_format.h>
46 #include <google/protobuf/stubs/strutil.h>
47 #include <google/protobuf/compiler/java/java_context.h>
48 #include <google/protobuf/compiler/java/java_doc_comment.h>
49 #include <google/protobuf/compiler/java/java_helpers.h>
50 #include <google/protobuf/compiler/java/java_name_resolver.h>
51
52 namespace google {
53 namespace protobuf {
54 namespace compiler {
55 namespace java {
56
57 using internal::WireFormat;
58 using internal::WireFormatLite;
59
60 namespace {
61
SetPrimitiveVariables(const FieldDescriptor * descriptor,int messageBitIndex,int builderBitIndex,const FieldGeneratorInfo * info,ClassNameResolver * name_resolver,std::map<std::string,std::string> * variables)62 void SetPrimitiveVariables(const FieldDescriptor* descriptor,
63 int messageBitIndex, int builderBitIndex,
64 const FieldGeneratorInfo* info,
65 ClassNameResolver* name_resolver,
66 std::map<std::string, std::string>* variables) {
67 SetCommonFieldVariables(descriptor, info, variables);
68
69 (*variables)["empty_list"] = "com.google.protobuf.LazyStringArrayList.EMPTY";
70
71 (*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver);
72 (*variables)["default_init"] =
73 "= " + ImmutableDefaultValue(descriptor, name_resolver);
74 (*variables)["capitalized_type"] = "String";
75 (*variables)["tag"] =
76 StrCat(static_cast<int32_t>(WireFormat::MakeTag(descriptor)));
77 (*variables)["tag_size"] = StrCat(
78 WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
79 (*variables)["null_check"] =
80 " if (value == null) {\n"
81 " throw new NullPointerException();\n"
82 " }\n";
83 (*variables)["isStringEmpty"] = "com.google.protobuf.GeneratedMessage" +
84 GeneratedCodeVersionSuffix() +
85 ".isStringEmpty";
86 (*variables)["writeString"] = "com.google.protobuf.GeneratedMessage" +
87 GeneratedCodeVersionSuffix() + ".writeString";
88 (*variables)["computeStringSize"] = "com.google.protobuf.GeneratedMessage" +
89 GeneratedCodeVersionSuffix() +
90 ".computeStringSize";
91
92 // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
93 // by the proto compiler
94 (*variables)["deprecation"] =
95 descriptor->options().deprecated() ? "@java.lang.Deprecated " : "";
96 (*variables)["kt_deprecation"] =
97 descriptor->options().deprecated()
98 ? "@kotlin.Deprecated(message = \"Field " + (*variables)["name"] +
99 " is deprecated\") "
100 : "";
101 (*variables)["on_changed"] = "onChanged();";
102
103 if (HasHasbit(descriptor)) {
104 // For singular messages and builders, one bit is used for the hasField bit.
105 (*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex);
106 (*variables)["get_has_field_bit_builder"] = GenerateGetBit(builderBitIndex);
107
108 // Note that these have a trailing ";".
109 (*variables)["set_has_field_bit_message"] =
110 GenerateSetBit(messageBitIndex) + ";";
111 (*variables)["set_has_field_bit_builder"] =
112 GenerateSetBit(builderBitIndex) + ";";
113 (*variables)["clear_has_field_bit_builder"] =
114 GenerateClearBit(builderBitIndex) + ";";
115
116 (*variables)["is_field_present_message"] = GenerateGetBit(messageBitIndex);
117 } else {
118 (*variables)["set_has_field_bit_message"] = "";
119 (*variables)["set_has_field_bit_builder"] = "";
120 (*variables)["clear_has_field_bit_builder"] = "";
121
122 (*variables)["is_field_present_message"] =
123 "!" + (*variables)["isStringEmpty"] + "(" + (*variables)["name"] + "_)";
124 }
125
126 // For repeated builders, one bit is used for whether the array is immutable.
127 (*variables)["get_mutable_bit_builder"] = GenerateGetBit(builderBitIndex);
128 (*variables)["set_mutable_bit_builder"] = GenerateSetBit(builderBitIndex);
129 (*variables)["clear_mutable_bit_builder"] = GenerateClearBit(builderBitIndex);
130
131 (*variables)["get_has_field_bit_from_local"] =
132 GenerateGetBitFromLocal(builderBitIndex);
133 (*variables)["set_has_field_bit_to_local"] =
134 GenerateSetBitToLocal(messageBitIndex);
135 }
136
137 } // namespace
138
139 // ===================================================================
140
ImmutableStringFieldGenerator(const FieldDescriptor * descriptor,int messageBitIndex,int builderBitIndex,Context * context)141 ImmutableStringFieldGenerator::ImmutableStringFieldGenerator(
142 const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex,
143 Context* context)
144 : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
145 SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex,
146 context->GetFieldGeneratorInfo(descriptor),
147 name_resolver_, &variables_);
148 }
149
~ImmutableStringFieldGenerator()150 ImmutableStringFieldGenerator::~ImmutableStringFieldGenerator() {}
151
GetNumBitsForMessage() const152 int ImmutableStringFieldGenerator::GetNumBitsForMessage() const {
153 return HasHasbit(descriptor_) ? 1 : 0;
154 }
155
GetNumBitsForBuilder() const156 int ImmutableStringFieldGenerator::GetNumBitsForBuilder() const {
157 return GetNumBitsForMessage();
158 }
159
160 // A note about how strings are handled. This code used to just store a String
161 // in the Message. This had two issues:
162 //
163 // 1. It wouldn't roundtrip byte arrays that were not valid UTF-8 encoded
164 // strings, but rather fields that were raw bytes incorrectly marked
165 // as strings in the proto file. This is common because in the proto1
166 // syntax, string was the way to indicate bytes and C++ engineers can
167 // easily make this mistake without affecting the C++ API. By converting to
168 // strings immediately, some java code might corrupt these byte arrays as
169 // it passes through a java server even if the field was never accessed by
170 // application code.
171 //
172 // 2. There's a performance hit to converting between bytes and strings and
173 // it many cases, the field is never even read by the application code. This
174 // avoids unnecessary conversions in the common use cases.
175 //
176 // So now, the field for String is maintained as an Object reference which can
177 // either store a String or a ByteString. The code uses an instanceof check
178 // to see which one it has and converts to the other one if needed. It remembers
179 // the last value requested (in a thread safe manner) as this is most likely
180 // the one needed next. The thread safety is such that if two threads both
181 // convert the field because the changes made by each thread were not visible to
182 // the other, they may cause a conversion to happen more times than would
183 // otherwise be necessary. This was deemed better than adding synchronization
184 // overhead. It will not cause any corruption issues or affect the behavior of
185 // the API. The instanceof check is also highly optimized in the JVM and we
186 // decided it was better to reduce the memory overhead by not having two
187 // separate fields but rather use dynamic type checking.
188 //
189 // For single fields, the logic for this is done inside the generated code. For
190 // repeated fields, the logic is done in LazyStringArrayList and
191 // UnmodifiableLazyStringList.
GenerateInterfaceMembers(io::Printer * printer) const192 void ImmutableStringFieldGenerator::GenerateInterfaceMembers(
193 io::Printer* printer) const {
194 if (HasHazzer(descriptor_)) {
195 WriteFieldAccessorDocComment(printer, descriptor_, HAZZER);
196 printer->Print(variables_,
197 "$deprecation$boolean has$capitalized_name$();\n");
198 }
199 WriteFieldAccessorDocComment(printer, descriptor_, GETTER);
200 printer->Print(variables_,
201 "$deprecation$java.lang.String get$capitalized_name$();\n");
202 WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER);
203 printer->Print(variables_,
204 "$deprecation$com.google.protobuf.ByteString\n"
205 " get$capitalized_name$Bytes();\n");
206 }
207
GenerateMembers(io::Printer * printer) const208 void ImmutableStringFieldGenerator::GenerateMembers(
209 io::Printer* printer) const {
210 printer->Print(variables_, "private volatile java.lang.Object $name$_;\n");
211 PrintExtraFieldInfo(variables_, printer);
212
213 if (HasHazzer(descriptor_)) {
214 WriteFieldAccessorDocComment(printer, descriptor_, HAZZER);
215 printer->Print(
216 variables_,
217 "@java.lang.Override\n"
218 "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
219 " return $get_has_field_bit_message$;\n"
220 "}\n");
221 printer->Annotate("{", "}", descriptor_);
222 }
223
224 WriteFieldAccessorDocComment(printer, descriptor_, GETTER);
225 printer->Print(
226 variables_,
227 "@java.lang.Override\n"
228 "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n"
229 " java.lang.Object ref = $name$_;\n"
230 " if (ref instanceof java.lang.String) {\n"
231 " return (java.lang.String) ref;\n"
232 " } else {\n"
233 " com.google.protobuf.ByteString bs = \n"
234 " (com.google.protobuf.ByteString) ref;\n"
235 " java.lang.String s = bs.toStringUtf8();\n");
236 printer->Annotate("{", "}", descriptor_);
237 if (CheckUtf8(descriptor_)) {
238 printer->Print(variables_, " $name$_ = s;\n");
239 } else {
240 printer->Print(variables_,
241 " if (bs.isValidUtf8()) {\n"
242 " $name$_ = s;\n"
243 " }\n");
244 }
245 printer->Print(variables_,
246 " return s;\n"
247 " }\n"
248 "}\n");
249 WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER);
250 printer->Print(variables_,
251 "@java.lang.Override\n"
252 "$deprecation$public com.google.protobuf.ByteString\n"
253 " ${$get$capitalized_name$Bytes$}$() {\n"
254 " java.lang.Object ref = $name$_;\n"
255 " if (ref instanceof java.lang.String) {\n"
256 " com.google.protobuf.ByteString b = \n"
257 " com.google.protobuf.ByteString.copyFromUtf8(\n"
258 " (java.lang.String) ref);\n"
259 " $name$_ = b;\n"
260 " return b;\n"
261 " } else {\n"
262 " return (com.google.protobuf.ByteString) ref;\n"
263 " }\n"
264 "}\n");
265 printer->Annotate("{", "}", descriptor_);
266 }
267
GenerateBuilderMembers(io::Printer * printer) const268 void ImmutableStringFieldGenerator::GenerateBuilderMembers(
269 io::Printer* printer) const {
270 printer->Print(variables_,
271 "private java.lang.Object $name$_ $default_init$;\n");
272 if (HasHazzer(descriptor_)) {
273 WriteFieldAccessorDocComment(printer, descriptor_, HAZZER);
274 printer->Print(
275 variables_,
276 "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
277 " return $get_has_field_bit_builder$;\n"
278 "}\n");
279 printer->Annotate("{", "}", descriptor_);
280 }
281
282 WriteFieldAccessorDocComment(printer, descriptor_, GETTER);
283 printer->Print(
284 variables_,
285 "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n"
286 " java.lang.Object ref = $name$_;\n"
287 " if (!(ref instanceof java.lang.String)) {\n"
288 " com.google.protobuf.ByteString bs =\n"
289 " (com.google.protobuf.ByteString) ref;\n"
290 " java.lang.String s = bs.toStringUtf8();\n");
291 printer->Annotate("{", "}", descriptor_);
292 if (CheckUtf8(descriptor_)) {
293 printer->Print(variables_, " $name$_ = s;\n");
294 } else {
295 printer->Print(variables_,
296 " if (bs.isValidUtf8()) {\n"
297 " $name$_ = s;\n"
298 " }\n");
299 }
300 printer->Print(variables_,
301 " return s;\n"
302 " } else {\n"
303 " return (java.lang.String) ref;\n"
304 " }\n"
305 "}\n");
306
307 WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER);
308 printer->Print(variables_,
309 "$deprecation$public com.google.protobuf.ByteString\n"
310 " ${$get$capitalized_name$Bytes$}$() {\n"
311 " java.lang.Object ref = $name$_;\n"
312 " if (ref instanceof String) {\n"
313 " com.google.protobuf.ByteString b = \n"
314 " com.google.protobuf.ByteString.copyFromUtf8(\n"
315 " (java.lang.String) ref);\n"
316 " $name$_ = b;\n"
317 " return b;\n"
318 " } else {\n"
319 " return (com.google.protobuf.ByteString) ref;\n"
320 " }\n"
321 "}\n");
322 printer->Annotate("{", "}", descriptor_);
323
324 WriteFieldAccessorDocComment(printer, descriptor_, SETTER,
325 /* builder */ true);
326 printer->Print(variables_,
327 "$deprecation$public Builder ${$set$capitalized_name$$}$(\n"
328 " java.lang.String value) {\n"
329 "$null_check$"
330 " $set_has_field_bit_builder$\n"
331 " $name$_ = value;\n"
332 " $on_changed$\n"
333 " return this;\n"
334 "}\n");
335 printer->Annotate("{", "}", descriptor_);
336 WriteFieldAccessorDocComment(printer, descriptor_, CLEARER,
337 /* builder */ true);
338 printer->Print(
339 variables_,
340 "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n"
341 " $clear_has_field_bit_builder$\n");
342 printer->Annotate("{", "}", descriptor_);
343 // The default value is not a simple literal so we want to avoid executing
344 // it multiple times. Instead, get the default out of the default instance.
345 printer->Print(variables_,
346 " $name$_ = getDefaultInstance().get$capitalized_name$();\n");
347 printer->Print(variables_,
348 " $on_changed$\n"
349 " return this;\n"
350 "}\n");
351
352 WriteFieldStringBytesAccessorDocComment(printer, descriptor_, SETTER,
353 /* builder */ true);
354 printer->Print(
355 variables_,
356 "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n"
357 " com.google.protobuf.ByteString value) {\n"
358 "$null_check$");
359 printer->Annotate("{", "}", descriptor_);
360 if (CheckUtf8(descriptor_)) {
361 printer->Print(variables_, " checkByteStringIsUtf8(value);\n");
362 }
363 printer->Print(variables_,
364 " $set_has_field_bit_builder$\n"
365 " $name$_ = value;\n"
366 " $on_changed$\n"
367 " return this;\n"
368 "}\n");
369 }
370
GenerateKotlinDslMembers(io::Printer * printer) const371 void ImmutableStringFieldGenerator::GenerateKotlinDslMembers(
372 io::Printer* printer) const {
373 WriteFieldDocComment(printer, descriptor_);
374 printer->Print(variables_,
375 "$kt_deprecation$public var $kt_name$: kotlin.String\n"
376 " @JvmName(\"${$get$kt_capitalized_name$$}$\")\n"
377 " get() = $kt_dsl_builder$.${$get$capitalized_name$$}$()\n"
378 " @JvmName(\"${$set$kt_capitalized_name$$}$\")\n"
379 " set(value) {\n"
380 " $kt_dsl_builder$.${$set$capitalized_name$$}$(value)\n"
381 " }\n");
382
383 WriteFieldAccessorDocComment(printer, descriptor_, CLEARER,
384 /* builder */ false);
385 printer->Print(variables_,
386 "public fun ${$clear$kt_capitalized_name$$}$() {\n"
387 " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n"
388 "}\n");
389
390 if (HasHazzer(descriptor_)) {
391 WriteFieldAccessorDocComment(printer, descriptor_, HAZZER);
392 printer->Print(
393 variables_,
394 "public fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n"
395 " return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n"
396 "}\n");
397 }
398 }
399
GenerateFieldBuilderInitializationCode(io::Printer * printer) const400 void ImmutableStringFieldGenerator::GenerateFieldBuilderInitializationCode(
401 io::Printer* printer) const {
402 // noop for primitives
403 }
404
GenerateInitializationCode(io::Printer * printer) const405 void ImmutableStringFieldGenerator::GenerateInitializationCode(
406 io::Printer* printer) const {
407 printer->Print(variables_, "$name$_ = $default$;\n");
408 }
409
GenerateBuilderClearCode(io::Printer * printer) const410 void ImmutableStringFieldGenerator::GenerateBuilderClearCode(
411 io::Printer* printer) const {
412 printer->Print(variables_,
413 "$name$_ = $default$;\n"
414 "$clear_has_field_bit_builder$\n");
415 }
416
GenerateMergingCode(io::Printer * printer) const417 void ImmutableStringFieldGenerator::GenerateMergingCode(
418 io::Printer* printer) const {
419 if (HasHazzer(descriptor_)) {
420 // Allow a slight breach of abstraction here in order to avoid forcing
421 // all string fields to Strings when copying fields from a Message.
422 printer->Print(variables_,
423 "if (other.has$capitalized_name$()) {\n"
424 " $set_has_field_bit_builder$\n"
425 " $name$_ = other.$name$_;\n"
426 " $on_changed$\n"
427 "}\n");
428 } else {
429 printer->Print(variables_,
430 "if (!other.get$capitalized_name$().isEmpty()) {\n"
431 " $name$_ = other.$name$_;\n"
432 " $on_changed$\n"
433 "}\n");
434 }
435 }
436
GenerateBuildingCode(io::Printer * printer) const437 void ImmutableStringFieldGenerator::GenerateBuildingCode(
438 io::Printer* printer) const {
439 if (HasHazzer(descriptor_)) {
440 printer->Print(variables_,
441 "if ($get_has_field_bit_from_local$) {\n"
442 " $set_has_field_bit_to_local$;\n"
443 "}\n");
444 }
445 printer->Print(variables_, "result.$name$_ = $name$_;\n");
446 }
447
GenerateBuilderParsingCode(io::Printer * printer) const448 void ImmutableStringFieldGenerator::GenerateBuilderParsingCode(
449 io::Printer* printer) const {
450 if (CheckUtf8(descriptor_)) {
451 printer->Print(variables_,
452 "$name$_ = input.readStringRequireUtf8();\n"
453 "$set_has_field_bit_builder$\n");
454 } else {
455 printer->Print(variables_,
456 "$name$_ = input.readBytes();\n"
457 "$set_has_field_bit_builder$\n");
458 }
459 }
460
GenerateSerializationCode(io::Printer * printer) const461 void ImmutableStringFieldGenerator::GenerateSerializationCode(
462 io::Printer* printer) const {
463 printer->Print(variables_,
464 "if ($is_field_present_message$) {\n"
465 " $writeString$(output, $number$, $name$_);\n"
466 "}\n");
467 }
468
GenerateSerializedSizeCode(io::Printer * printer) const469 void ImmutableStringFieldGenerator::GenerateSerializedSizeCode(
470 io::Printer* printer) const {
471 printer->Print(variables_,
472 "if ($is_field_present_message$) {\n"
473 " size += $computeStringSize$($number$, $name$_);\n"
474 "}\n");
475 }
476
GenerateEqualsCode(io::Printer * printer) const477 void ImmutableStringFieldGenerator::GenerateEqualsCode(
478 io::Printer* printer) const {
479 printer->Print(variables_,
480 "if (!get$capitalized_name$()\n"
481 " .equals(other.get$capitalized_name$())) return false;\n");
482 }
483
GenerateHashCode(io::Printer * printer) const484 void ImmutableStringFieldGenerator::GenerateHashCode(
485 io::Printer* printer) const {
486 printer->Print(variables_, "hash = (37 * hash) + $constant_name$;\n");
487 printer->Print(variables_,
488 "hash = (53 * hash) + get$capitalized_name$().hashCode();\n");
489 }
490
GetBoxedType() const491 std::string ImmutableStringFieldGenerator::GetBoxedType() const {
492 return "java.lang.String";
493 }
494
495 // ===================================================================
496
ImmutableStringOneofFieldGenerator(const FieldDescriptor * descriptor,int messageBitIndex,int builderBitIndex,Context * context)497 ImmutableStringOneofFieldGenerator::ImmutableStringOneofFieldGenerator(
498 const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex,
499 Context* context)
500 : ImmutableStringFieldGenerator(descriptor, messageBitIndex,
501 builderBitIndex, context) {
502 const OneofGeneratorInfo* info =
503 context->GetOneofGeneratorInfo(descriptor->containing_oneof());
504 SetCommonOneofVariables(descriptor, info, &variables_);
505 }
506
~ImmutableStringOneofFieldGenerator()507 ImmutableStringOneofFieldGenerator::~ImmutableStringOneofFieldGenerator() {}
508
GenerateMembers(io::Printer * printer) const509 void ImmutableStringOneofFieldGenerator::GenerateMembers(
510 io::Printer* printer) const {
511 PrintExtraFieldInfo(variables_, printer);
512 GOOGLE_DCHECK(HasHazzer(descriptor_));
513 WriteFieldAccessorDocComment(printer, descriptor_, HAZZER);
514 printer->Print(variables_,
515 "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
516 " return $has_oneof_case_message$;\n"
517 "}\n");
518 printer->Annotate("{", "}", descriptor_);
519
520 WriteFieldAccessorDocComment(printer, descriptor_, GETTER);
521 printer->Print(
522 variables_,
523 "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n"
524 " java.lang.Object ref $default_init$;\n"
525 " if ($has_oneof_case_message$) {\n"
526 " ref = $oneof_name$_;\n"
527 " }\n"
528 " if (ref instanceof java.lang.String) {\n"
529 " return (java.lang.String) ref;\n"
530 " } else {\n"
531 " com.google.protobuf.ByteString bs = \n"
532 " (com.google.protobuf.ByteString) ref;\n"
533 " java.lang.String s = bs.toStringUtf8();\n");
534 printer->Annotate("{", "}", descriptor_);
535 if (CheckUtf8(descriptor_)) {
536 printer->Print(variables_,
537 " if ($has_oneof_case_message$) {\n"
538 " $oneof_name$_ = s;\n"
539 " }\n");
540 } else {
541 printer->Print(variables_,
542 " if (bs.isValidUtf8() && ($has_oneof_case_message$)) {\n"
543 " $oneof_name$_ = s;\n"
544 " }\n");
545 }
546 printer->Print(variables_,
547 " return s;\n"
548 " }\n"
549 "}\n");
550 WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER);
551
552 printer->Print(variables_,
553 "$deprecation$public com.google.protobuf.ByteString\n"
554 " ${$get$capitalized_name$Bytes$}$() {\n"
555 " java.lang.Object ref $default_init$;\n"
556 " if ($has_oneof_case_message$) {\n"
557 " ref = $oneof_name$_;\n"
558 " }\n"
559 " if (ref instanceof java.lang.String) {\n"
560 " com.google.protobuf.ByteString b = \n"
561 " com.google.protobuf.ByteString.copyFromUtf8(\n"
562 " (java.lang.String) ref);\n"
563 " if ($has_oneof_case_message$) {\n"
564 " $oneof_name$_ = b;\n"
565 " }\n"
566 " return b;\n"
567 " } else {\n"
568 " return (com.google.protobuf.ByteString) ref;\n"
569 " }\n"
570 "}\n");
571 printer->Annotate("{", "}", descriptor_);
572 }
573
GenerateBuilderMembers(io::Printer * printer) const574 void ImmutableStringOneofFieldGenerator::GenerateBuilderMembers(
575 io::Printer* printer) const {
576 GOOGLE_DCHECK(HasHazzer(descriptor_));
577 WriteFieldAccessorDocComment(printer, descriptor_, HAZZER);
578 printer->Print(variables_,
579 "@java.lang.Override\n"
580 "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
581 " return $has_oneof_case_message$;\n"
582 "}\n");
583 printer->Annotate("{", "}", descriptor_);
584
585 WriteFieldAccessorDocComment(printer, descriptor_, GETTER);
586 printer->Print(
587 variables_,
588 "@java.lang.Override\n"
589 "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n"
590 " java.lang.Object ref $default_init$;\n"
591 " if ($has_oneof_case_message$) {\n"
592 " ref = $oneof_name$_;\n"
593 " }\n"
594 " if (!(ref instanceof java.lang.String)) {\n"
595 " com.google.protobuf.ByteString bs =\n"
596 " (com.google.protobuf.ByteString) ref;\n"
597 " java.lang.String s = bs.toStringUtf8();\n"
598 " if ($has_oneof_case_message$) {\n");
599 printer->Annotate("{", "}", descriptor_);
600 if (CheckUtf8(descriptor_)) {
601 printer->Print(variables_, " $oneof_name$_ = s;\n");
602 } else {
603 printer->Print(variables_,
604 " if (bs.isValidUtf8()) {\n"
605 " $oneof_name$_ = s;\n"
606 " }\n");
607 }
608 printer->Print(variables_,
609 " }\n"
610 " return s;\n"
611 " } else {\n"
612 " return (java.lang.String) ref;\n"
613 " }\n"
614 "}\n");
615
616 WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER);
617 printer->Print(variables_,
618 "@java.lang.Override\n"
619 "$deprecation$public com.google.protobuf.ByteString\n"
620 " ${$get$capitalized_name$Bytes$}$() {\n"
621 " java.lang.Object ref $default_init$;\n"
622 " if ($has_oneof_case_message$) {\n"
623 " ref = $oneof_name$_;\n"
624 " }\n"
625 " if (ref instanceof String) {\n"
626 " com.google.protobuf.ByteString b = \n"
627 " com.google.protobuf.ByteString.copyFromUtf8(\n"
628 " (java.lang.String) ref);\n"
629 " if ($has_oneof_case_message$) {\n"
630 " $oneof_name$_ = b;\n"
631 " }\n"
632 " return b;\n"
633 " } else {\n"
634 " return (com.google.protobuf.ByteString) ref;\n"
635 " }\n"
636 "}\n");
637 printer->Annotate("{", "}", descriptor_);
638
639 WriteFieldAccessorDocComment(printer, descriptor_, SETTER,
640 /* builder */ true);
641 printer->Print(variables_,
642 "$deprecation$public Builder ${$set$capitalized_name$$}$(\n"
643 " java.lang.String value) {\n"
644 "$null_check$"
645 " $set_oneof_case_message$;\n"
646 " $oneof_name$_ = value;\n"
647 " $on_changed$\n"
648 " return this;\n"
649 "}\n");
650 printer->Annotate("{", "}", descriptor_);
651 WriteFieldAccessorDocComment(printer, descriptor_, CLEARER,
652 /* builder */ true);
653 printer->Print(
654 variables_,
655 "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n"
656 " if ($has_oneof_case_message$) {\n"
657 " $clear_oneof_case_message$;\n"
658 " $oneof_name$_ = null;\n"
659 " $on_changed$\n"
660 " }\n"
661 " return this;\n"
662 "}\n");
663 printer->Annotate("{", "}", descriptor_);
664
665 WriteFieldStringBytesAccessorDocComment(printer, descriptor_, SETTER,
666 /* builder */ true);
667 printer->Print(
668 variables_,
669 "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n"
670 " com.google.protobuf.ByteString value) {\n"
671 "$null_check$");
672 printer->Annotate("{", "}", descriptor_);
673 if (CheckUtf8(descriptor_)) {
674 printer->Print(variables_, " checkByteStringIsUtf8(value);\n");
675 }
676 printer->Print(variables_,
677 " $set_oneof_case_message$;\n"
678 " $oneof_name$_ = value;\n"
679 " $on_changed$\n"
680 " return this;\n"
681 "}\n");
682 }
683
GenerateBuilderClearCode(io::Printer * printer) const684 void ImmutableStringOneofFieldGenerator::GenerateBuilderClearCode(
685 io::Printer* printer) const {
686 // No-Op: String fields in oneofs are correctly cleared by clearing the oneof
687 }
688
GenerateMergingCode(io::Printer * printer) const689 void ImmutableStringOneofFieldGenerator::GenerateMergingCode(
690 io::Printer* printer) const {
691 // Allow a slight breach of abstraction here in order to avoid forcing
692 // all string fields to Strings when copying fields from a Message.
693 printer->Print(variables_,
694 "$set_oneof_case_message$;\n"
695 "$oneof_name$_ = other.$oneof_name$_;\n"
696 "$on_changed$\n");
697 }
698
GenerateBuildingCode(io::Printer * printer) const699 void ImmutableStringOneofFieldGenerator::GenerateBuildingCode(
700 io::Printer* printer) const {
701 printer->Print(variables_,
702 "if ($has_oneof_case_message$) {\n"
703 " result.$oneof_name$_ = $oneof_name$_;\n"
704 "}\n");
705 }
706
GenerateBuilderParsingCode(io::Printer * printer) const707 void ImmutableStringOneofFieldGenerator::GenerateBuilderParsingCode(
708 io::Printer* printer) const {
709 if (CheckUtf8(descriptor_)) {
710 printer->Print(variables_,
711 "java.lang.String s = input.readStringRequireUtf8();\n"
712 "$set_oneof_case_message$;\n"
713 "$oneof_name$_ = s;\n");
714 } else {
715 printer->Print(variables_,
716 "com.google.protobuf.ByteString bs = input.readBytes();\n"
717 "$set_oneof_case_message$;\n"
718 "$oneof_name$_ = bs;\n");
719 }
720 }
721
GenerateSerializationCode(io::Printer * printer) const722 void ImmutableStringOneofFieldGenerator::GenerateSerializationCode(
723 io::Printer* printer) const {
724 printer->Print(variables_,
725 "if ($has_oneof_case_message$) {\n"
726 " $writeString$(output, $number$, $oneof_name$_);\n"
727 "}\n");
728 }
729
GenerateSerializedSizeCode(io::Printer * printer) const730 void ImmutableStringOneofFieldGenerator::GenerateSerializedSizeCode(
731 io::Printer* printer) const {
732 printer->Print(variables_,
733 "if ($has_oneof_case_message$) {\n"
734 " size += $computeStringSize$($number$, $oneof_name$_);\n"
735 "}\n");
736 }
737
738 // ===================================================================
739
RepeatedImmutableStringFieldGenerator(const FieldDescriptor * descriptor,int messageBitIndex,int builderBitIndex,Context * context)740 RepeatedImmutableStringFieldGenerator::RepeatedImmutableStringFieldGenerator(
741 const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex,
742 Context* context)
743 : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
744 SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex,
745 context->GetFieldGeneratorInfo(descriptor),
746 name_resolver_, &variables_);
747 }
748
749 RepeatedImmutableStringFieldGenerator::
~RepeatedImmutableStringFieldGenerator()750 ~RepeatedImmutableStringFieldGenerator() {}
751
GetNumBitsForMessage() const752 int RepeatedImmutableStringFieldGenerator::GetNumBitsForMessage() const {
753 return 0;
754 }
755
GetNumBitsForBuilder() const756 int RepeatedImmutableStringFieldGenerator::GetNumBitsForBuilder() const {
757 return 1;
758 }
759
GenerateInterfaceMembers(io::Printer * printer) const760 void RepeatedImmutableStringFieldGenerator::GenerateInterfaceMembers(
761 io::Printer* printer) const {
762 WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER);
763 printer->Print(
764 variables_,
765 // NOTE: the same method in the implementation class actually returns
766 // com.google.protobuf.ProtocolStringList (a subclass of List). It's
767 // changed between protobuf 2.5.0 release and protobuf 2.6.1 release.
768 // To retain binary compatibility with both 2.5.0 and 2.6.1 generated
769 // code, we make this interface method return List so both methods
770 // with different return types exist in the compiled byte code.
771 "$deprecation$java.util.List<java.lang.String>\n"
772 " get$capitalized_name$List();\n");
773 WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT);
774 printer->Print(variables_,
775 "$deprecation$int get$capitalized_name$Count();\n");
776 WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER);
777 printer->Print(
778 variables_,
779 "$deprecation$java.lang.String get$capitalized_name$(int index);\n");
780 WriteFieldStringBytesAccessorDocComment(printer, descriptor_,
781 LIST_INDEXED_GETTER);
782 printer->Print(variables_,
783 "$deprecation$com.google.protobuf.ByteString\n"
784 " get$capitalized_name$Bytes(int index);\n");
785 }
786
GenerateMembers(io::Printer * printer) const787 void RepeatedImmutableStringFieldGenerator::GenerateMembers(
788 io::Printer* printer) const {
789 printer->Print(variables_,
790 "private com.google.protobuf.LazyStringList $name$_;\n");
791 PrintExtraFieldInfo(variables_, printer);
792 WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER);
793 printer->Print(variables_,
794 "$deprecation$public com.google.protobuf.ProtocolStringList\n"
795 " ${$get$capitalized_name$List$}$() {\n"
796 " return $name$_;\n" // note: unmodifiable list
797 "}\n");
798 printer->Annotate("{", "}", descriptor_);
799 WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT);
800 printer->Print(
801 variables_,
802 "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n"
803 " return $name$_.size();\n"
804 "}\n");
805 printer->Annotate("{", "}", descriptor_);
806 WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER);
807 printer->Print(variables_,
808 "$deprecation$public java.lang.String "
809 "${$get$capitalized_name$$}$(int index) {\n"
810 " return $name$_.get(index);\n"
811 "}\n");
812 printer->Annotate("{", "}", descriptor_);
813 WriteFieldStringBytesAccessorDocComment(printer, descriptor_,
814 LIST_INDEXED_GETTER);
815 printer->Print(variables_,
816 "$deprecation$public com.google.protobuf.ByteString\n"
817 " ${$get$capitalized_name$Bytes$}$(int index) {\n"
818 " return $name$_.getByteString(index);\n"
819 "}\n");
820 printer->Annotate("{", "}", descriptor_);
821 }
822
GenerateBuilderMembers(io::Printer * printer) const823 void RepeatedImmutableStringFieldGenerator::GenerateBuilderMembers(
824 io::Printer* printer) const {
825 // One field is the list and the bit field keeps track of whether the
826 // list is immutable. If it's immutable, the invariant is that it must
827 // either an instance of Collections.emptyList() or it's an ArrayList
828 // wrapped in a Collections.unmodifiableList() wrapper and nobody else has
829 // a reference to the underlying ArrayList. This invariant allows us to
830 // share instances of lists between protocol buffers avoiding expensive
831 // memory allocations. Note, immutable is a strong guarantee here -- not
832 // just that the list cannot be modified via the reference but that the
833 // list can never be modified.
834 printer->Print(
835 variables_,
836 "private com.google.protobuf.LazyStringList $name$_ = $empty_list$;\n");
837
838 printer->Print(
839 variables_,
840 "private void ensure$capitalized_name$IsMutable() {\n"
841 " if (!$get_mutable_bit_builder$) {\n"
842 " $name$_ = new com.google.protobuf.LazyStringArrayList($name$_);\n"
843 " $set_mutable_bit_builder$;\n"
844 " }\n"
845 "}\n");
846
847 // Note: We return an unmodifiable list because otherwise the caller
848 // could hold on to the returned list and modify it after the message
849 // has been built, thus mutating the message which is supposed to be
850 // immutable.
851 WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER);
852 printer->Print(variables_,
853 "$deprecation$public com.google.protobuf.ProtocolStringList\n"
854 " ${$get$capitalized_name$List$}$() {\n"
855 " return $name$_.getUnmodifiableView();\n"
856 "}\n");
857 printer->Annotate("{", "}", descriptor_);
858 WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT);
859 printer->Print(
860 variables_,
861 "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n"
862 " return $name$_.size();\n"
863 "}\n");
864 printer->Annotate("{", "}", descriptor_);
865 WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER);
866 printer->Print(variables_,
867 "$deprecation$public java.lang.String "
868 "${$get$capitalized_name$$}$(int index) {\n"
869 " return $name$_.get(index);\n"
870 "}\n");
871 printer->Annotate("{", "}", descriptor_);
872 WriteFieldStringBytesAccessorDocComment(printer, descriptor_,
873 LIST_INDEXED_GETTER);
874 printer->Print(variables_,
875 "$deprecation$public com.google.protobuf.ByteString\n"
876 " ${$get$capitalized_name$Bytes$}$(int index) {\n"
877 " return $name$_.getByteString(index);\n"
878 "}\n");
879 printer->Annotate("{", "}", descriptor_);
880 WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER,
881 /* builder */ true);
882 printer->Print(variables_,
883 "$deprecation$public Builder ${$set$capitalized_name$$}$(\n"
884 " int index, java.lang.String value) {\n"
885 "$null_check$"
886 " ensure$capitalized_name$IsMutable();\n"
887 " $name$_.set(index, value);\n"
888 " $on_changed$\n"
889 " return this;\n"
890 "}\n");
891 printer->Annotate("{", "}", descriptor_);
892 WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER,
893 /* builder */ true);
894 printer->Print(variables_,
895 "$deprecation$public Builder ${$add$capitalized_name$$}$(\n"
896 " java.lang.String value) {\n"
897 "$null_check$"
898 " ensure$capitalized_name$IsMutable();\n"
899 " $name$_.add(value);\n"
900 " $on_changed$\n"
901 " return this;\n"
902 "}\n");
903 printer->Annotate("{", "}", descriptor_);
904 WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER,
905 /* builder */ true);
906 printer->Print(variables_,
907 "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n"
908 " java.lang.Iterable<java.lang.String> values) {\n"
909 " ensure$capitalized_name$IsMutable();\n"
910 " com.google.protobuf.AbstractMessageLite.Builder.addAll(\n"
911 " values, $name$_);\n"
912 " $on_changed$\n"
913 " return this;\n"
914 "}\n");
915 printer->Annotate("{", "}", descriptor_);
916 WriteFieldAccessorDocComment(printer, descriptor_, CLEARER,
917 /* builder */ true);
918 printer->Print(
919 variables_,
920 "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n"
921 " $name$_ = $empty_list$;\n"
922 " $clear_mutable_bit_builder$;\n"
923 " $on_changed$\n"
924 " return this;\n"
925 "}\n");
926 printer->Annotate("{", "}", descriptor_);
927
928 WriteFieldStringBytesAccessorDocComment(printer, descriptor_, LIST_ADDER,
929 /* builder */ true);
930 printer->Print(
931 variables_,
932 "$deprecation$public Builder ${$add$capitalized_name$Bytes$}$(\n"
933 " com.google.protobuf.ByteString value) {\n"
934 "$null_check$");
935 printer->Annotate("{", "}", descriptor_);
936 if (CheckUtf8(descriptor_)) {
937 printer->Print(variables_, " checkByteStringIsUtf8(value);\n");
938 }
939 printer->Print(variables_,
940 " ensure$capitalized_name$IsMutable();\n"
941 " $name$_.add(value);\n"
942 " $on_changed$\n"
943 " return this;\n"
944 "}\n");
945 }
946
GenerateKotlinDslMembers(io::Printer * printer) const947 void RepeatedImmutableStringFieldGenerator::GenerateKotlinDslMembers(
948 io::Printer* printer) const {
949 printer->Print(
950 variables_,
951 "/**\n"
952 " * An uninstantiable, behaviorless type to represent the field in\n"
953 " * generics.\n"
954 " */\n"
955 "@kotlin.OptIn"
956 "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n"
957 "public class ${$$kt_capitalized_name$Proxy$}$ private constructor()"
958 " : com.google.protobuf.kotlin.DslProxy()\n");
959
960 // property for List<String>
961 WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER);
962 printer->Print(variables_,
963 "$kt_deprecation$public val $kt_name$: "
964 "com.google.protobuf.kotlin.DslList"
965 "<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>\n"
966 " @kotlin.jvm.JvmSynthetic\n"
967 " get() = com.google.protobuf.kotlin.DslList(\n"
968 " $kt_dsl_builder$.${$get$capitalized_name$List$}$()\n"
969 " )\n");
970
971 // List<String>.add(String)
972 WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER,
973 /* builder */ false);
974 printer->Print(variables_,
975 "@kotlin.jvm.JvmSynthetic\n"
976 "@kotlin.jvm.JvmName(\"add$kt_capitalized_name$\")\n"
977 "public fun com.google.protobuf.kotlin.DslList"
978 "<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
979 "add(value: kotlin.String) {\n"
980 " $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n"
981 "}\n");
982
983 // List<String> += String
984 WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER,
985 /* builder */ false);
986 printer->Print(variables_,
987 "@kotlin.jvm.JvmSynthetic\n"
988 "@kotlin.jvm.JvmName(\"plusAssign$kt_capitalized_name$\")\n"
989 "@Suppress(\"NOTHING_TO_INLINE\")\n"
990 "public inline operator fun com.google.protobuf.kotlin.DslList"
991 "<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
992 "plusAssign(value: kotlin.String) {\n"
993 " add(value)\n"
994 "}\n");
995
996 // List<String>.addAll(Iterable<String>)
997 WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER,
998 /* builder */ false);
999 printer->Print(
1000 variables_,
1001 "@kotlin.jvm.JvmSynthetic\n"
1002 "@kotlin.jvm.JvmName(\"addAll$kt_capitalized_name$\")\n"
1003 "public fun com.google.protobuf.kotlin.DslList"
1004 "<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
1005 "addAll(values: kotlin.collections.Iterable<kotlin.String>) {\n"
1006 " $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n"
1007 "}\n");
1008
1009 // List<String> += Iterable<String>
1010 WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER,
1011 /* builder */ false);
1012 printer->Print(
1013 variables_,
1014 "@kotlin.jvm.JvmSynthetic\n"
1015 "@kotlin.jvm.JvmName(\"plusAssignAll$kt_capitalized_name$\")\n"
1016 "@Suppress(\"NOTHING_TO_INLINE\")\n"
1017 "public inline operator fun com.google.protobuf.kotlin.DslList"
1018 "<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
1019 "plusAssign(values: kotlin.collections.Iterable<kotlin.String>) {\n"
1020 " addAll(values)\n"
1021 "}\n");
1022
1023 // List<String>[Int] = String
1024 WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER,
1025 /* builder */ false);
1026 printer->Print(
1027 variables_,
1028 "@kotlin.jvm.JvmSynthetic\n"
1029 "@kotlin.jvm.JvmName(\"set$kt_capitalized_name$\")\n"
1030 "public operator fun com.google.protobuf.kotlin.DslList"
1031 "<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
1032 "set(index: kotlin.Int, value: kotlin.String) {\n"
1033 " $kt_dsl_builder$.${$set$capitalized_name$$}$(index, value)\n"
1034 "}");
1035
1036 WriteFieldAccessorDocComment(printer, descriptor_, CLEARER,
1037 /* builder */ false);
1038 printer->Print(variables_,
1039 "@kotlin.jvm.JvmSynthetic\n"
1040 "@kotlin.jvm.JvmName(\"clear$kt_capitalized_name$\")\n"
1041 "public fun com.google.protobuf.kotlin.DslList"
1042 "<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
1043 "clear() {\n"
1044 " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n"
1045 "}");
1046 }
1047
1048 void RepeatedImmutableStringFieldGenerator::
GenerateFieldBuilderInitializationCode(io::Printer * printer) const1049 GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
1050 // noop for primitives
1051 }
1052
GenerateInitializationCode(io::Printer * printer) const1053 void RepeatedImmutableStringFieldGenerator::GenerateInitializationCode(
1054 io::Printer* printer) const {
1055 printer->Print(variables_, "$name$_ = $empty_list$;\n");
1056 }
1057
GenerateBuilderClearCode(io::Printer * printer) const1058 void RepeatedImmutableStringFieldGenerator::GenerateBuilderClearCode(
1059 io::Printer* printer) const {
1060 printer->Print(variables_,
1061 "$name$_ = $empty_list$;\n"
1062 "$clear_mutable_bit_builder$;\n");
1063 }
1064
GenerateMergingCode(io::Printer * printer) const1065 void RepeatedImmutableStringFieldGenerator::GenerateMergingCode(
1066 io::Printer* printer) const {
1067 // The code below does two optimizations:
1068 // 1. If the other list is empty, there's nothing to do. This ensures we
1069 // don't allocate a new array if we already have an immutable one.
1070 // 2. If the other list is non-empty and our current list is empty, we can
1071 // reuse the other list which is guaranteed to be immutable.
1072 printer->Print(variables_,
1073 "if (!other.$name$_.isEmpty()) {\n"
1074 " if ($name$_.isEmpty()) {\n"
1075 " $name$_ = other.$name$_;\n"
1076 " $clear_mutable_bit_builder$;\n"
1077 " } else {\n"
1078 " ensure$capitalized_name$IsMutable();\n"
1079 " $name$_.addAll(other.$name$_);\n"
1080 " }\n"
1081 " $on_changed$\n"
1082 "}\n");
1083 }
1084
GenerateBuildingCode(io::Printer * printer) const1085 void RepeatedImmutableStringFieldGenerator::GenerateBuildingCode(
1086 io::Printer* printer) const {
1087 // The code below ensures that the result has an immutable list. If our
1088 // list is immutable, we can just reuse it. If not, we make it immutable.
1089
1090 printer->Print(variables_,
1091 "if ($get_mutable_bit_builder$) {\n"
1092 " $name$_ = $name$_.getUnmodifiableView();\n"
1093 " $clear_mutable_bit_builder$;\n"
1094 "}\n"
1095 "result.$name$_ = $name$_;\n");
1096 }
1097
GenerateBuilderParsingCode(io::Printer * printer) const1098 void RepeatedImmutableStringFieldGenerator::GenerateBuilderParsingCode(
1099 io::Printer* printer) const {
1100 if (CheckUtf8(descriptor_)) {
1101 printer->Print(variables_,
1102 "java.lang.String s = input.readStringRequireUtf8();\n"
1103 "ensure$capitalized_name$IsMutable();\n"
1104 "$name$_.add(s);\n");
1105 } else {
1106 printer->Print(variables_,
1107 "com.google.protobuf.ByteString bs = input.readBytes();\n"
1108 "ensure$capitalized_name$IsMutable();\n"
1109 "$name$_.add(bs);\n");
1110 }
1111 }
1112
GenerateSerializationCode(io::Printer * printer) const1113 void RepeatedImmutableStringFieldGenerator::GenerateSerializationCode(
1114 io::Printer* printer) const {
1115 printer->Print(variables_,
1116 "for (int i = 0; i < $name$_.size(); i++) {\n"
1117 " $writeString$(output, $number$, $name$_.getRaw(i));\n"
1118 "}\n");
1119 }
1120
GenerateSerializedSizeCode(io::Printer * printer) const1121 void RepeatedImmutableStringFieldGenerator::GenerateSerializedSizeCode(
1122 io::Printer* printer) const {
1123 printer->Print(variables_,
1124 "{\n"
1125 " int dataSize = 0;\n");
1126 printer->Indent();
1127
1128 printer->Print(variables_,
1129 "for (int i = 0; i < $name$_.size(); i++) {\n"
1130 " dataSize += computeStringSizeNoTag($name$_.getRaw(i));\n"
1131 "}\n");
1132
1133 printer->Print("size += dataSize;\n");
1134
1135 printer->Print(variables_,
1136 "size += $tag_size$ * get$capitalized_name$List().size();\n");
1137
1138 printer->Outdent();
1139 printer->Print("}\n");
1140 }
1141
GenerateEqualsCode(io::Printer * printer) const1142 void RepeatedImmutableStringFieldGenerator::GenerateEqualsCode(
1143 io::Printer* printer) const {
1144 printer->Print(
1145 variables_,
1146 "if (!get$capitalized_name$List()\n"
1147 " .equals(other.get$capitalized_name$List())) return false;\n");
1148 }
1149
GenerateHashCode(io::Printer * printer) const1150 void RepeatedImmutableStringFieldGenerator::GenerateHashCode(
1151 io::Printer* printer) const {
1152 printer->Print(
1153 variables_,
1154 "if (get$capitalized_name$Count() > 0) {\n"
1155 " hash = (37 * hash) + $constant_name$;\n"
1156 " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n"
1157 "}\n");
1158 }
1159
GetBoxedType() const1160 std::string RepeatedImmutableStringFieldGenerator::GetBoxedType() const {
1161 return "String";
1162 }
1163
1164 } // namespace java
1165 } // namespace compiler
1166 } // namespace protobuf
1167 } // namespace google
1168