• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_lite.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"] =
70       "com.google.protobuf.GeneratedMessageLite.emptyProtobufList()";
71 
72   (*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver);
73   (*variables)["default_init"] =
74       "= " + ImmutableDefaultValue(descriptor, name_resolver);
75   (*variables)["capitalized_type"] = "java.lang.String";
76   (*variables)["tag"] =
77       StrCat(static_cast<int32_t>(WireFormat::MakeTag(descriptor)));
78   (*variables)["tag_size"] = StrCat(
79       WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
80   // We use `x.getClass()` as a null check because it generates less bytecode
81   // than an `if (x == null) { throw ... }` statement.
82   (*variables)["null_check"] =
83       "  java.lang.Class<?> valueClass = value.getClass();\n";
84 
85   // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
86   // by the proto compiler
87   (*variables)["deprecation"] =
88       descriptor->options().deprecated() ? "@java.lang.Deprecated " : "";
89   (*variables)["kt_deprecation"] =
90       descriptor->options().deprecated()
91           ? "@kotlin.Deprecated(message = \"Field " + (*variables)["name"] +
92                 " is deprecated\") "
93           : "";
94   (*variables)["required"] = descriptor->is_required() ? "true" : "false";
95 
96   if (HasHasbit(descriptor)) {
97     // For singular messages and builders, one bit is used for the hasField bit.
98     (*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex);
99 
100     // Note that these have a trailing ";".
101     (*variables)["set_has_field_bit_message"] =
102         GenerateSetBit(messageBitIndex) + ";";
103     (*variables)["clear_has_field_bit_message"] =
104         GenerateClearBit(messageBitIndex) + ";";
105 
106     (*variables)["is_field_present_message"] = GenerateGetBit(messageBitIndex);
107   } else {
108     (*variables)["set_has_field_bit_message"] = "";
109     (*variables)["clear_has_field_bit_message"] = "";
110 
111     (*variables)["is_field_present_message"] =
112         "!" + (*variables)["name"] + "_.isEmpty()";
113   }
114 
115   (*variables)["get_has_field_bit_from_local"] =
116       GenerateGetBitFromLocal(builderBitIndex);
117   (*variables)["set_has_field_bit_to_local"] =
118       GenerateSetBitToLocal(messageBitIndex);
119 }
120 
121 }  // namespace
122 
123 // ===================================================================
124 
ImmutableStringFieldLiteGenerator(const FieldDescriptor * descriptor,int messageBitIndex,Context * context)125 ImmutableStringFieldLiteGenerator::ImmutableStringFieldLiteGenerator(
126     const FieldDescriptor* descriptor, int messageBitIndex, Context* context)
127     : descriptor_(descriptor),
128       messageBitIndex_(messageBitIndex),
129       name_resolver_(context->GetNameResolver()) {
130   SetPrimitiveVariables(descriptor, messageBitIndex, 0,
131                         context->GetFieldGeneratorInfo(descriptor),
132                         name_resolver_, &variables_);
133 }
134 
~ImmutableStringFieldLiteGenerator()135 ImmutableStringFieldLiteGenerator::~ImmutableStringFieldLiteGenerator() {}
136 
GetNumBitsForMessage() const137 int ImmutableStringFieldLiteGenerator::GetNumBitsForMessage() const {
138   return HasHasbit(descriptor_) ? 1 : 0;
139 }
140 
141 // A note about how strings are handled. In the SPEED and CODE_SIZE runtimes,
142 // strings are not stored as java.lang.String in the Message because of two
143 // issues:
144 //
145 //  1. It wouldn't roundtrip byte arrays that were not valid UTF-8 encoded
146 //     strings, but rather fields that were raw bytes incorrectly marked
147 //     as strings in the proto file. This is common because in the proto1
148 //     syntax, string was the way to indicate bytes and C++ engineers can
149 //     easily make this mistake without affecting the C++ API. By converting to
150 //     strings immediately, some java code might corrupt these byte arrays as
151 //     it passes through a java server even if the field was never accessed by
152 //     application code.
153 //
154 //  2. There's a performance hit to converting between bytes and strings and
155 //     it many cases, the field is never even read by the application code. This
156 //     avoids unnecessary conversions in the common use cases.
157 //
158 // In the LITE_RUNTIME, we store strings as java.lang.String because we assume
159 // that the users of this runtime are not subject to proto1 constraints and are
160 // running code on devices that are user facing. That is, the developers are
161 // properly incentivized to only fetch the data they need to read and wish to
162 // reduce the number of allocations incurred when running on a user's device.
163 
164 // TODO(dweis): Consider dropping all of the *Bytes() methods. They really
165 //     shouldn't be necessary or used on devices.
GenerateInterfaceMembers(io::Printer * printer) const166 void ImmutableStringFieldLiteGenerator::GenerateInterfaceMembers(
167     io::Printer* printer) const {
168   if (HasHazzer(descriptor_)) {
169     WriteFieldAccessorDocComment(printer, descriptor_, HAZZER);
170     printer->Print(variables_,
171                    "$deprecation$boolean has$capitalized_name$();\n");
172   }
173   WriteFieldAccessorDocComment(printer, descriptor_, GETTER);
174   printer->Print(variables_,
175                  "$deprecation$java.lang.String get$capitalized_name$();\n");
176   WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER);
177   printer->Print(variables_,
178                  "$deprecation$com.google.protobuf.ByteString\n"
179                  "    get$capitalized_name$Bytes();\n");
180 }
181 
GenerateMembers(io::Printer * printer) const182 void ImmutableStringFieldLiteGenerator::GenerateMembers(
183     io::Printer* printer) const {
184   printer->Print(variables_, "private java.lang.String $name$_;\n");
185   PrintExtraFieldInfo(variables_, printer);
186 
187   if (HasHazzer(descriptor_)) {
188     WriteFieldAccessorDocComment(printer, descriptor_, HAZZER);
189     printer->Print(
190         variables_,
191         "@java.lang.Override\n"
192         "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
193         "  return $get_has_field_bit_message$;\n"
194         "}\n");
195     printer->Annotate("{", "}", descriptor_);
196   }
197 
198   WriteFieldAccessorDocComment(printer, descriptor_, GETTER);
199   printer->Print(
200       variables_,
201       "@java.lang.Override\n"
202       "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n"
203       "  return $name$_;\n"
204       "}\n");
205   printer->Annotate("{", "}", descriptor_);
206   WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER);
207   printer->Print(
208       variables_,
209       "@java.lang.Override\n"
210       "$deprecation$public com.google.protobuf.ByteString\n"
211       "    ${$get$capitalized_name$Bytes$}$() {\n"
212       "  return com.google.protobuf.ByteString.copyFromUtf8($name$_);\n"
213       "}\n");
214   printer->Annotate("{", "}", descriptor_);
215 
216   WriteFieldAccessorDocComment(printer, descriptor_, SETTER);
217   printer->Print(variables_,
218                  "private void set$capitalized_name$(\n"
219                  "    java.lang.String value) {\n"
220                  "$null_check$"
221                  "  $set_has_field_bit_message$\n"
222                  "  $name$_ = value;\n"
223                  "}\n");
224   WriteFieldAccessorDocComment(printer, descriptor_, CLEARER);
225   printer->Print(variables_,
226                  "private void clear$capitalized_name$() {\n"
227                  "  $clear_has_field_bit_message$\n"
228                  // The default value is not a simple literal so we want to
229                  // avoid executing it multiple times.  Instead, get the default
230                  // out of the default instance.
231                  "  $name$_ = getDefaultInstance().get$capitalized_name$();\n"
232                  "}\n");
233 
234   WriteFieldStringBytesAccessorDocComment(printer, descriptor_, SETTER);
235   printer->Print(variables_,
236                  "private void set$capitalized_name$Bytes(\n"
237                  "    com.google.protobuf.ByteString value) {\n");
238   if (CheckUtf8(descriptor_)) {
239     printer->Print(variables_, "  checkByteStringIsUtf8(value);\n");
240   }
241   printer->Print(variables_,
242                  "  $name$_ = value.toStringUtf8();\n"
243                  "  $set_has_field_bit_message$\n"
244                  "}\n");
245 }
246 
GenerateBuilderMembers(io::Printer * printer) const247 void ImmutableStringFieldLiteGenerator::GenerateBuilderMembers(
248     io::Printer* printer) const {
249   if (HasHazzer(descriptor_)) {
250     WriteFieldAccessorDocComment(printer, descriptor_, HAZZER);
251     printer->Print(
252         variables_,
253         "@java.lang.Override\n"
254         "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
255         "  return instance.has$capitalized_name$();\n"
256         "}\n");
257     printer->Annotate("{", "}", descriptor_);
258   }
259 
260   WriteFieldAccessorDocComment(printer, descriptor_, GETTER);
261   printer->Print(
262       variables_,
263       "@java.lang.Override\n"
264       "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n"
265       "  return instance.get$capitalized_name$();\n"
266       "}\n");
267   printer->Annotate("{", "}", descriptor_);
268 
269   WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER);
270   printer->Print(variables_,
271                  "@java.lang.Override\n"
272                  "$deprecation$public com.google.protobuf.ByteString\n"
273                  "    ${$get$capitalized_name$Bytes$}$() {\n"
274                  "  return instance.get$capitalized_name$Bytes();\n"
275                  "}\n");
276   printer->Annotate("{", "}", descriptor_);
277 
278   WriteFieldAccessorDocComment(printer, descriptor_, SETTER,
279                                /* builder */ true);
280   printer->Print(variables_,
281                  "$deprecation$public Builder ${$set$capitalized_name$$}$(\n"
282                  "    java.lang.String value) {\n"
283                  "  copyOnWrite();\n"
284                  "  instance.set$capitalized_name$(value);\n"
285                  "  return this;\n"
286                  "}\n");
287   printer->Annotate("{", "}", descriptor_);
288   WriteFieldAccessorDocComment(printer, descriptor_, CLEARER,
289                                /* builder */ true);
290   printer->Print(
291       variables_,
292       "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n"
293       "  copyOnWrite();\n"
294       "  instance.clear$capitalized_name$();\n"
295       "  return this;\n"
296       "}\n");
297   printer->Annotate("{", "}", descriptor_);
298 
299   WriteFieldStringBytesAccessorDocComment(printer, descriptor_, SETTER,
300                                           /* builder */ true);
301   printer->Print(
302       variables_,
303       "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n"
304       "    com.google.protobuf.ByteString value) {\n"
305       "  copyOnWrite();\n"
306       "  instance.set$capitalized_name$Bytes(value);\n"
307       "  return this;\n"
308       "}\n");
309   printer->Annotate("{", "}", descriptor_);
310 }
311 
GenerateKotlinDslMembers(io::Printer * printer) const312 void ImmutableStringFieldLiteGenerator::GenerateKotlinDslMembers(
313     io::Printer* printer) const {
314   WriteFieldDocComment(printer, descriptor_);
315   printer->Print(variables_,
316                  "$kt_deprecation$public var $kt_name$: kotlin.String\n"
317                  "  @JvmName(\"${$get$kt_capitalized_name$$}$\")\n"
318                  "  get() = $kt_dsl_builder$.${$get$capitalized_name$$}$()\n"
319                  "  @JvmName(\"${$set$kt_capitalized_name$$}$\")\n"
320                  "  set(value) {\n"
321                  "    $kt_dsl_builder$.${$set$capitalized_name$$}$(value)\n"
322                  "  }\n");
323 
324   WriteFieldAccessorDocComment(printer, descriptor_, CLEARER,
325                                /* builder */ false);
326   printer->Print(variables_,
327                  "public fun ${$clear$kt_capitalized_name$$}$() {\n"
328                  "  $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n"
329                  "}\n");
330 
331   if (HasHazzer(descriptor_)) {
332     WriteFieldAccessorDocComment(printer, descriptor_, HAZZER);
333     printer->Print(
334         variables_,
335         "public fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n"
336         "  return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n"
337         "}\n");
338   }
339 }
340 
GenerateFieldInfo(io::Printer * printer,std::vector<uint16_t> * output) const341 void ImmutableStringFieldLiteGenerator::GenerateFieldInfo(
342     io::Printer* printer, std::vector<uint16_t>* output) const {
343   WriteIntToUtf16CharSequence(descriptor_->number(), output);
344   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
345                               output);
346   if (HasHasbit(descriptor_)) {
347     WriteIntToUtf16CharSequence(messageBitIndex_, output);
348   }
349   printer->Print(variables_, "\"$name$_\",\n");
350 }
351 
GenerateInitializationCode(io::Printer * printer) const352 void ImmutableStringFieldLiteGenerator::GenerateInitializationCode(
353     io::Printer* printer) const {
354   printer->Print(variables_, "$name$_ = $default$;\n");
355 }
356 
GetBoxedType() const357 std::string ImmutableStringFieldLiteGenerator::GetBoxedType() const {
358   return "java.lang.String";
359 }
360 
361 // ===================================================================
362 
ImmutableStringOneofFieldLiteGenerator(const FieldDescriptor * descriptor,int messageBitIndex,Context * context)363 ImmutableStringOneofFieldLiteGenerator::ImmutableStringOneofFieldLiteGenerator(
364     const FieldDescriptor* descriptor, int messageBitIndex, Context* context)
365     : ImmutableStringFieldLiteGenerator(descriptor, messageBitIndex, context) {
366   const OneofGeneratorInfo* info =
367       context->GetOneofGeneratorInfo(descriptor->containing_oneof());
368   SetCommonOneofVariables(descriptor, info, &variables_);
369 }
370 
371 ImmutableStringOneofFieldLiteGenerator::
~ImmutableStringOneofFieldLiteGenerator()372     ~ImmutableStringOneofFieldLiteGenerator() {}
373 
GenerateMembers(io::Printer * printer) const374 void ImmutableStringOneofFieldLiteGenerator::GenerateMembers(
375     io::Printer* printer) const {
376   PrintExtraFieldInfo(variables_, printer);
377   GOOGLE_DCHECK(HasHazzer(descriptor_));
378   WriteFieldAccessorDocComment(printer, descriptor_, HAZZER);
379   printer->Print(variables_,
380                  "@java.lang.Override\n"
381                  "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
382                  "  return $has_oneof_case_message$;\n"
383                  "}\n");
384   printer->Annotate("{", "}", descriptor_);
385 
386   WriteFieldAccessorDocComment(printer, descriptor_, GETTER);
387   printer->Print(
388       variables_,
389       "@java.lang.Override\n"
390       "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n"
391       "  java.lang.String ref $default_init$;\n"
392       "  if ($has_oneof_case_message$) {\n"
393       "    ref = (java.lang.String) $oneof_name$_;\n"
394       "  }\n"
395       "  return ref;\n"
396       "}\n");
397   printer->Annotate("{", "}", descriptor_);
398 
399   WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER);
400   printer->Print(variables_,
401                  "@java.lang.Override\n"
402                  "$deprecation$public com.google.protobuf.ByteString\n"
403                  "    ${$get$capitalized_name$Bytes$}$() {\n"
404                  "  java.lang.String ref $default_init$;\n"
405                  "  if ($has_oneof_case_message$) {\n"
406                  "    ref = (java.lang.String) $oneof_name$_;\n"
407                  "  }\n"
408                  "  return com.google.protobuf.ByteString.copyFromUtf8(ref);\n"
409                  "}\n");
410   printer->Annotate("{", "}", descriptor_);
411 
412   WriteFieldAccessorDocComment(printer, descriptor_, SETTER);
413   printer->Print(variables_,
414                  "private void ${$set$capitalized_name$$}$(\n"
415                  "    java.lang.String value) {\n"
416                  "$null_check$"
417                  "  $set_oneof_case_message$;\n"
418                  "  $oneof_name$_ = value;\n"
419                  "}\n");
420   printer->Annotate("{", "}", descriptor_);
421   WriteFieldAccessorDocComment(printer, descriptor_, CLEARER);
422   printer->Print(variables_,
423                  "private void ${$clear$capitalized_name$$}$() {\n"
424                  "  if ($has_oneof_case_message$) {\n"
425                  "    $clear_oneof_case_message$;\n"
426                  "    $oneof_name$_ = null;\n"
427                  "  }\n"
428                  "}\n");
429   printer->Annotate("{", "}", descriptor_);
430 
431   WriteFieldStringBytesAccessorDocComment(printer, descriptor_, SETTER);
432   printer->Print(variables_,
433                  "private void ${$set$capitalized_name$Bytes$}$(\n"
434                  "    com.google.protobuf.ByteString value) {\n");
435   printer->Annotate("{", "}", descriptor_);
436   if (CheckUtf8(descriptor_)) {
437     printer->Print(variables_, "  checkByteStringIsUtf8(value);\n");
438   }
439   printer->Print(variables_,
440                  "  $oneof_name$_ = value.toStringUtf8();\n"
441                  "  $set_oneof_case_message$;\n"
442                  "}\n");
443 }
444 
GenerateFieldInfo(io::Printer * printer,std::vector<uint16_t> * output) const445 void ImmutableStringOneofFieldLiteGenerator::GenerateFieldInfo(
446     io::Printer* printer, std::vector<uint16_t>* output) const {
447   WriteIntToUtf16CharSequence(descriptor_->number(), output);
448   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
449                               output);
450   WriteIntToUtf16CharSequence(descriptor_->containing_oneof()->index(), output);
451 }
452 
GenerateBuilderMembers(io::Printer * printer) const453 void ImmutableStringOneofFieldLiteGenerator::GenerateBuilderMembers(
454     io::Printer* printer) const {
455   GOOGLE_DCHECK(HasHazzer(descriptor_));
456   WriteFieldAccessorDocComment(printer, descriptor_, HAZZER);
457   printer->Print(variables_,
458                  "@java.lang.Override\n"
459                  "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n"
460                  "  return instance.has$capitalized_name$();\n"
461                  "}\n");
462   printer->Annotate("{", "}", descriptor_);
463 
464   WriteFieldAccessorDocComment(printer, descriptor_, GETTER);
465   printer->Print(
466       variables_,
467       "@java.lang.Override\n"
468       "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n"
469       "  return instance.get$capitalized_name$();\n"
470       "}\n");
471   printer->Annotate("{", "}", descriptor_);
472 
473   WriteFieldStringBytesAccessorDocComment(printer, descriptor_, GETTER);
474   printer->Print(variables_,
475                  "@java.lang.Override\n"
476                  "$deprecation$public com.google.protobuf.ByteString\n"
477                  "    ${$get$capitalized_name$Bytes$}$() {\n"
478                  "  return instance.get$capitalized_name$Bytes();\n"
479                  "}\n");
480   printer->Annotate("{", "}", descriptor_);
481 
482   WriteFieldAccessorDocComment(printer, descriptor_, SETTER,
483                                /* builder */ true);
484   printer->Print(variables_,
485                  "$deprecation$public Builder ${$set$capitalized_name$$}$(\n"
486                  "    java.lang.String value) {\n"
487                  "  copyOnWrite();\n"
488                  "  instance.set$capitalized_name$(value);\n"
489                  "  return this;\n"
490                  "}\n");
491   printer->Annotate("{", "}", descriptor_);
492   WriteFieldAccessorDocComment(printer, descriptor_, CLEARER,
493                                /* builder */ true);
494   printer->Print(
495       variables_,
496       "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n"
497       "  copyOnWrite();\n"
498       "  instance.clear$capitalized_name$();\n"
499       "  return this;\n"
500       "}\n");
501   printer->Annotate("{", "}", descriptor_);
502 
503   WriteFieldStringBytesAccessorDocComment(printer, descriptor_, SETTER,
504                                           /* builder */ true);
505   printer->Print(
506       variables_,
507       "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n"
508       "    com.google.protobuf.ByteString value) {\n"
509       "  copyOnWrite();\n"
510       "  instance.set$capitalized_name$Bytes(value);\n"
511       "  return this;\n"
512       "}\n");
513   printer->Annotate("{", "}", descriptor_);
514 }
515 
516 // ===================================================================
517 
518 RepeatedImmutableStringFieldLiteGenerator::
RepeatedImmutableStringFieldLiteGenerator(const FieldDescriptor * descriptor,int messageBitIndex,Context * context)519     RepeatedImmutableStringFieldLiteGenerator(const FieldDescriptor* descriptor,
520                                               int messageBitIndex,
521                                               Context* context)
522     : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
523   SetPrimitiveVariables(descriptor, messageBitIndex, 0,
524                         context->GetFieldGeneratorInfo(descriptor),
525                         name_resolver_, &variables_);
526 }
527 
528 RepeatedImmutableStringFieldLiteGenerator::
~RepeatedImmutableStringFieldLiteGenerator()529     ~RepeatedImmutableStringFieldLiteGenerator() {}
530 
GetNumBitsForMessage() const531 int RepeatedImmutableStringFieldLiteGenerator::GetNumBitsForMessage() const {
532   return 0;
533 }
534 
GenerateInterfaceMembers(io::Printer * printer) const535 void RepeatedImmutableStringFieldLiteGenerator::GenerateInterfaceMembers(
536     io::Printer* printer) const {
537   WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER);
538   printer->Print(variables_,
539                  "$deprecation$java.util.List<java.lang.String>\n"
540                  "    get$capitalized_name$List();\n");
541   WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT);
542   printer->Print(variables_,
543                  "$deprecation$int get$capitalized_name$Count();\n");
544   WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER);
545   printer->Print(
546       variables_,
547       "$deprecation$java.lang.String get$capitalized_name$(int index);\n");
548   WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER);
549   printer->Print(variables_,
550                  "$deprecation$com.google.protobuf.ByteString\n"
551                  "    get$capitalized_name$Bytes(int index);\n");
552 }
553 
GenerateMembers(io::Printer * printer) const554 void RepeatedImmutableStringFieldLiteGenerator::GenerateMembers(
555     io::Printer* printer) const {
556   printer->Print(
557       variables_,
558       "private com.google.protobuf.Internal.ProtobufList<java.lang.String> "
559       "$name$_;\n");
560   PrintExtraFieldInfo(variables_, printer);
561   WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER);
562   printer->Print(variables_,
563                  "@java.lang.Override\n"
564                  "$deprecation$public java.util.List<java.lang.String> "
565                  "${$get$capitalized_name$List$}$() {\n"
566                  "  return $name$_;\n"  // note:  unmodifiable list
567                  "}\n");
568   printer->Annotate("{", "}", descriptor_);
569   WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT);
570   printer->Print(
571       variables_,
572       "@java.lang.Override\n"
573       "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n"
574       "  return $name$_.size();\n"
575       "}\n");
576   printer->Annotate("{", "}", descriptor_);
577   WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER);
578   printer->Print(variables_,
579                  "@java.lang.Override\n"
580                  "$deprecation$public java.lang.String "
581                  "${$get$capitalized_name$$}$(int index) {\n"
582                  "  return $name$_.get(index);\n"
583                  "}\n");
584   printer->Annotate("{", "}", descriptor_);
585   WriteFieldStringBytesAccessorDocComment(printer, descriptor_,
586                                           LIST_INDEXED_GETTER);
587   printer->Print(variables_,
588                  "@java.lang.Override\n"
589                  "$deprecation$public com.google.protobuf.ByteString\n"
590                  "    ${$get$capitalized_name$Bytes$}$(int index) {\n"
591                  "  return com.google.protobuf.ByteString.copyFromUtf8(\n"
592                  "      $name$_.get(index));\n"
593                  "}\n");
594   printer->Annotate("{", "}", descriptor_);
595 
596   printer->Print(
597       variables_,
598       "private void ensure$capitalized_name$IsMutable() {\n"
599       // Use a temporary to avoid a redundant iget-object.
600       "  com.google.protobuf.Internal.ProtobufList<java.lang.String> tmp =\n"
601       "      $name$_;"
602       "  if (!tmp.isModifiable()) {\n"
603       "    $name$_ =\n"
604       "        com.google.protobuf.GeneratedMessageLite.mutableCopy(tmp);\n"
605       "   }\n"
606       "}\n");
607 
608   WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER);
609   printer->Print(variables_,
610                  "private void set$capitalized_name$(\n"
611                  "    int index, java.lang.String value) {\n"
612                  "$null_check$"
613                  "  ensure$capitalized_name$IsMutable();\n"
614                  "  $name$_.set(index, value);\n"
615                  "}\n");
616   WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER);
617   printer->Print(variables_,
618                  "private void add$capitalized_name$(\n"
619                  "    java.lang.String value) {\n"
620                  "$null_check$"
621                  "  ensure$capitalized_name$IsMutable();\n"
622                  "  $name$_.add(value);\n"
623                  "}\n");
624   WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER);
625   printer->Print(variables_,
626                  "private void addAll$capitalized_name$(\n"
627                  "    java.lang.Iterable<java.lang.String> values) {\n"
628                  "  ensure$capitalized_name$IsMutable();\n"
629                  "  com.google.protobuf.AbstractMessageLite.addAll(\n"
630                  "      values, $name$_);\n"
631                  "}\n");
632   WriteFieldAccessorDocComment(printer, descriptor_, CLEARER);
633   printer->Print(variables_,
634                  "private void clear$capitalized_name$() {\n"
635                  "  $name$_ = $empty_list$;\n"
636                  "}\n");
637 
638   WriteFieldStringBytesAccessorDocComment(printer, descriptor_, LIST_ADDER);
639   printer->Print(variables_,
640                  "private void add$capitalized_name$Bytes(\n"
641                  "    com.google.protobuf.ByteString value) {\n");
642   if (CheckUtf8(descriptor_)) {
643     printer->Print(variables_, "  checkByteStringIsUtf8(value);\n");
644   }
645   printer->Print(variables_,
646                  "  ensure$capitalized_name$IsMutable();\n"
647                  "  $name$_.add(value.toStringUtf8());\n"
648                  "}\n");
649 }
650 
GenerateBuilderMembers(io::Printer * printer) const651 void RepeatedImmutableStringFieldLiteGenerator::GenerateBuilderMembers(
652     io::Printer* printer) const {
653   WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER);
654   printer->Print(variables_,
655                  "@java.lang.Override\n"
656                  "$deprecation$public java.util.List<java.lang.String>\n"
657                  "    ${$get$capitalized_name$List$}$() {\n"
658                  "  return java.util.Collections.unmodifiableList(\n"
659                  "      instance.get$capitalized_name$List());\n"
660                  "}\n");
661   printer->Annotate("{", "}", descriptor_);
662   WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT);
663   printer->Print(
664       variables_,
665       "@java.lang.Override\n"
666       "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n"
667       "  return instance.get$capitalized_name$Count();\n"
668       "}\n");
669   printer->Annotate("{", "}", descriptor_);
670   WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_GETTER);
671   printer->Print(variables_,
672                  "@java.lang.Override\n"
673                  "$deprecation$public java.lang.String "
674                  "${$get$capitalized_name$$}$(int index) {\n"
675                  "  return instance.get$capitalized_name$(index);\n"
676                  "}\n");
677   printer->Annotate("{", "}", descriptor_);
678   WriteFieldStringBytesAccessorDocComment(printer, descriptor_,
679                                           LIST_INDEXED_GETTER);
680   printer->Print(variables_,
681                  "@java.lang.Override\n"
682                  "$deprecation$public com.google.protobuf.ByteString\n"
683                  "    ${$get$capitalized_name$Bytes$}$(int index) {\n"
684                  "  return instance.get$capitalized_name$Bytes(index);\n"
685                  "}\n");
686   printer->Annotate("{", "}", descriptor_);
687   WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER,
688                                /* builder */ true);
689   printer->Print(variables_,
690                  "$deprecation$public Builder ${$set$capitalized_name$$}$(\n"
691                  "    int index, java.lang.String value) {\n"
692                  "  copyOnWrite();\n"
693                  "  instance.set$capitalized_name$(index, value);\n"
694                  "  return this;\n"
695                  "}\n");
696   printer->Annotate("{", "}", descriptor_);
697   WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER,
698                                /* builder */ true);
699   printer->Print(variables_,
700                  "$deprecation$public Builder ${$add$capitalized_name$$}$(\n"
701                  "    java.lang.String value) {\n"
702                  "  copyOnWrite();\n"
703                  "  instance.add$capitalized_name$(value);\n"
704                  "  return this;\n"
705                  "}\n");
706   printer->Annotate("{", "}", descriptor_);
707   WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER,
708                                /* builder */ true);
709   printer->Print(variables_,
710                  "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n"
711                  "    java.lang.Iterable<java.lang.String> values) {\n"
712                  "  copyOnWrite();\n"
713                  "  instance.addAll$capitalized_name$(values);\n"
714                  "  return this;\n"
715                  "}\n");
716   printer->Annotate("{", "}", descriptor_);
717   WriteFieldAccessorDocComment(printer, descriptor_, CLEARER,
718                                /* builder */ true);
719   printer->Print(
720       variables_,
721       "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n"
722       "  copyOnWrite();\n"
723       "  instance.clear$capitalized_name$();\n"
724       "  return this;\n"
725       "}\n");
726   printer->Annotate("{", "}", descriptor_);
727 
728   WriteFieldStringBytesAccessorDocComment(printer, descriptor_, LIST_ADDER,
729                                           /* builder */ true);
730   printer->Print(
731       variables_,
732       "$deprecation$public Builder ${$add$capitalized_name$Bytes$}$(\n"
733       "    com.google.protobuf.ByteString value) {\n"
734       "  copyOnWrite();\n"
735       "  instance.add$capitalized_name$Bytes(value);\n"
736       "  return this;\n"
737       "}\n");
738   printer->Annotate("{", "}", descriptor_);
739 }
740 
GenerateKotlinDslMembers(io::Printer * printer) const741 void RepeatedImmutableStringFieldLiteGenerator::GenerateKotlinDslMembers(
742     io::Printer* printer) const {
743   printer->Print(
744       variables_,
745       "/**\n"
746       " * An uninstantiable, behaviorless type to represent the field in\n"
747       " * generics.\n"
748       " */\n"
749       "@kotlin.OptIn"
750       "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n"
751       "public class ${$$kt_capitalized_name$Proxy$}$ private constructor()"
752       " : com.google.protobuf.kotlin.DslProxy()\n");
753 
754   // property for List<String>
755   WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER);
756   printer->Print(
757       variables_,
758       "$kt_deprecation$public val $kt_name$: "
759       "com.google.protobuf.kotlin.DslList"
760       "<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>\n"
761       "  @kotlin.OptIn"
762       "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n"
763       "  get() = com.google.protobuf.kotlin.DslList(\n"
764       "    $kt_dsl_builder$.${$get$capitalized_name$List$}$()\n"
765       "  )\n");
766 
767   // List<String>.add(String)
768   WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER,
769                                /* builder */ false);
770   printer->Print(variables_,
771                  "@kotlin.jvm.JvmSynthetic\n"
772                  "@kotlin.jvm.JvmName(\"add$kt_capitalized_name$\")\n"
773                  "public fun com.google.protobuf.kotlin.DslList"
774                  "<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
775                  "add(value: kotlin.String) {\n"
776                  "  $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n"
777                  "}\n");
778 
779   // List<String> += String
780   WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER,
781                                /* builder */ false);
782   printer->Print(variables_,
783                  "@kotlin.jvm.JvmSynthetic\n"
784                  "@kotlin.jvm.JvmName(\"plusAssign$kt_capitalized_name$\")\n"
785                  "@Suppress(\"NOTHING_TO_INLINE\")\n"
786                  "public inline operator fun com.google.protobuf.kotlin.DslList"
787                  "<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
788                  "plusAssign(value: kotlin.String) {\n"
789                  "  add(value)\n"
790                  "}\n");
791 
792   // List<String>.addAll(Iterable<String>)
793   WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER,
794                                /* builder */ false);
795   printer->Print(
796       variables_,
797       "@kotlin.jvm.JvmSynthetic\n"
798       "@kotlin.jvm.JvmName(\"addAll$kt_capitalized_name$\")\n"
799       "public fun com.google.protobuf.kotlin.DslList"
800       "<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
801       "addAll(values: kotlin.collections.Iterable<kotlin.String>) {\n"
802       "  $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n"
803       "}\n");
804 
805   // List<String> += Iterable<String>
806   WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER,
807                                /* builder */ false);
808   printer->Print(
809       variables_,
810       "@kotlin.jvm.JvmSynthetic\n"
811       "@kotlin.jvm.JvmName(\"plusAssignAll$kt_capitalized_name$\")\n"
812       "@Suppress(\"NOTHING_TO_INLINE\")\n"
813       "public inline operator fun com.google.protobuf.kotlin.DslList"
814       "<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
815       "plusAssign(values: kotlin.collections.Iterable<kotlin.String>) {\n"
816       "  addAll(values)\n"
817       "}\n");
818 
819   // List<String>[Int] = String
820   WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER,
821                                /* builder */ false);
822   printer->Print(
823       variables_,
824       "@kotlin.jvm.JvmSynthetic\n"
825       "@kotlin.jvm.JvmName(\"set$kt_capitalized_name$\")\n"
826       "public operator fun com.google.protobuf.kotlin.DslList"
827       "<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
828       "set(index: kotlin.Int, value: kotlin.String) {\n"
829       "  $kt_dsl_builder$.${$set$capitalized_name$$}$(index, value)\n"
830       "}");
831 
832   WriteFieldAccessorDocComment(printer, descriptor_, CLEARER,
833                                /* builder */ false);
834   printer->Print(variables_,
835                  "@kotlin.jvm.JvmSynthetic\n"
836                  "@kotlin.jvm.JvmName(\"clear$kt_capitalized_name$\")\n"
837                  "public fun com.google.protobuf.kotlin.DslList"
838                  "<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
839                  "clear() {\n"
840                  "  $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n"
841                  "}");
842 }
843 
GenerateFieldInfo(io::Printer * printer,std::vector<uint16_t> * output) const844 void RepeatedImmutableStringFieldLiteGenerator::GenerateFieldInfo(
845     io::Printer* printer, std::vector<uint16_t>* output) const {
846   WriteIntToUtf16CharSequence(descriptor_->number(), output);
847   WriteIntToUtf16CharSequence(GetExperimentalJavaFieldType(descriptor_),
848                               output);
849   printer->Print(variables_, "\"$name$_\",\n");
850 }
851 
GenerateInitializationCode(io::Printer * printer) const852 void RepeatedImmutableStringFieldLiteGenerator::GenerateInitializationCode(
853     io::Printer* printer) const {
854   printer->Print(variables_, "$name$_ = $empty_list$;\n");
855 }
856 
GetBoxedType() const857 std::string RepeatedImmutableStringFieldLiteGenerator::GetBoxedType() const {
858   return "java.lang.String";
859 }
860 
861 }  // namespace java
862 }  // namespace compiler
863 }  // namespace protobuf
864 }  // namespace google
865