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/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/context.h> |
48 | #include <google/protobuf/compiler/java/doc_comment.h> |
49 | #include <google/protobuf/compiler/java/helpers.h> |
50 | #include <google/protobuf/compiler/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 | |
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(field: descriptor, name_resolver); |
73 | (*variables)["default_init" ] = |
74 | "= " + ImmutableDefaultValue(field: descriptor, name_resolver); |
75 | (*variables)["capitalized_type" ] = "java.lang.String" ; |
76 | (*variables)["tag" ] = |
77 | StrCat(a: static_cast<int32_t>(WireFormat::MakeTag(field: descriptor))); |
78 | (*variables)["tag_size" ] = StrCat( |
79 | a: WireFormat::TagSize(field_number: descriptor->number(), type: GetType(field: 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(bitIndex: messageBitIndex); |
99 | |
100 | // Note that these have a trailing ";". |
101 | (*variables)["set_has_field_bit_message" ] = |
102 | GenerateSetBit(bitIndex: messageBitIndex) + ";" ; |
103 | (*variables)["clear_has_field_bit_message" ] = |
104 | GenerateClearBit(bitIndex: messageBitIndex) + ";" ; |
105 | |
106 | (*variables)["is_field_present_message" ] = GenerateGetBit(bitIndex: 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(bitIndex: builderBitIndex); |
117 | (*variables)["set_has_field_bit_to_local" ] = |
118 | GenerateSetBitToLocal(bitIndex: messageBitIndex); |
119 | } |
120 | |
121 | } // namespace |
122 | |
123 | // =================================================================== |
124 | |
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, builderBitIndex: 0, |
131 | info: context->GetFieldGeneratorInfo(field: descriptor), |
132 | name_resolver: name_resolver_, variables: &variables_); |
133 | } |
134 | |
135 | ImmutableStringFieldLiteGenerator::~ImmutableStringFieldLiteGenerator() {} |
136 | |
137 | int ImmutableStringFieldLiteGenerator::GetNumBitsForMessage() const { |
138 | return HasHasbit(descriptor: 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. |
166 | void ImmutableStringFieldLiteGenerator::GenerateInterfaceMembers( |
167 | io::Printer* printer) const { |
168 | if (HasHazzer(descriptor: descriptor_)) { |
169 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: HAZZER); |
170 | printer->Print(variables: variables_, |
171 | text: "$deprecation$boolean has$capitalized_name$();\n" ); |
172 | } |
173 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: GETTER); |
174 | printer->Print(variables: variables_, |
175 | text: "$deprecation$java.lang.String get$capitalized_name$();\n" ); |
176 | WriteFieldStringBytesAccessorDocComment(printer, field: descriptor_, type: GETTER); |
177 | printer->Print(variables: variables_, |
178 | text: "$deprecation$com.google.protobuf.ByteString\n" |
179 | " get$capitalized_name$Bytes();\n" ); |
180 | } |
181 | |
182 | void ImmutableStringFieldLiteGenerator::GenerateMembers( |
183 | io::Printer* printer) const { |
184 | printer->Print(variables: variables_, text: "private java.lang.String $name$_;\n" ); |
185 | PrintExtraFieldInfo(variables: variables_, printer); |
186 | |
187 | if (HasHazzer(descriptor: descriptor_)) { |
188 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: HAZZER); |
189 | printer->Print( |
190 | variables: variables_, |
191 | text: "@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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
196 | } |
197 | |
198 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: GETTER); |
199 | printer->Print( |
200 | variables: variables_, |
201 | text: "@java.lang.Override\n" |
202 | "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" |
203 | " return $name$_;\n" |
204 | "}\n" ); |
205 | printer->Annotate(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
206 | WriteFieldStringBytesAccessorDocComment(printer, field: descriptor_, type: GETTER); |
207 | printer->Print( |
208 | variables: variables_, |
209 | text: "@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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
215 | |
216 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: SETTER); |
217 | printer->Print(variables: variables_, |
218 | text: "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, field: descriptor_, type: CLEARER); |
225 | printer->Print(variables: variables_, |
226 | text: "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, field: descriptor_, type: SETTER); |
235 | printer->Print(variables: variables_, |
236 | text: "private void set$capitalized_name$Bytes(\n" |
237 | " com.google.protobuf.ByteString value) {\n" ); |
238 | if (CheckUtf8(descriptor: descriptor_)) { |
239 | printer->Print(variables: variables_, text: " checkByteStringIsUtf8(value);\n" ); |
240 | } |
241 | printer->Print(variables: variables_, |
242 | text: " $name$_ = value.toStringUtf8();\n" |
243 | " $set_has_field_bit_message$\n" |
244 | "}\n" ); |
245 | } |
246 | |
247 | void ImmutableStringFieldLiteGenerator::GenerateBuilderMembers( |
248 | io::Printer* printer) const { |
249 | if (HasHazzer(descriptor: descriptor_)) { |
250 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: HAZZER); |
251 | printer->Print( |
252 | variables: variables_, |
253 | text: "@java.lang.Override\n" |
254 | "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" |
255 | " return instance.has$capitalized_name$();\n" |
256 | "}\n" ); |
257 | printer->Annotate(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
258 | } |
259 | |
260 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: GETTER); |
261 | printer->Print( |
262 | variables: variables_, |
263 | text: "@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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
268 | |
269 | WriteFieldStringBytesAccessorDocComment(printer, field: descriptor_, type: GETTER); |
270 | printer->Print(variables: variables_, |
271 | text: "@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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
277 | |
278 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: SETTER, |
279 | /* builder */ true); |
280 | printer->Print(variables: variables_, |
281 | text: "$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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
288 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: CLEARER, |
289 | /* builder */ true); |
290 | printer->Print( |
291 | variables: variables_, |
292 | text: "$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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
298 | |
299 | WriteFieldStringBytesAccessorDocComment(printer, field: descriptor_, type: SETTER, |
300 | /* builder */ true); |
301 | printer->Print( |
302 | variables: variables_, |
303 | text: "$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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
310 | } |
311 | |
312 | void ImmutableStringFieldLiteGenerator::GenerateKotlinDslMembers( |
313 | io::Printer* printer) const { |
314 | WriteFieldDocComment(printer, field: descriptor_); |
315 | printer->Print(variables: variables_, |
316 | text: "$kt_deprecation$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, field: descriptor_, type: CLEARER, |
325 | /* builder */ false); |
326 | printer->Print(variables: variables_, |
327 | text: "fun ${$clear$kt_capitalized_name$$}$() {\n" |
328 | " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" |
329 | "}\n" ); |
330 | |
331 | if (HasHazzer(descriptor: descriptor_)) { |
332 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: HAZZER); |
333 | printer->Print( |
334 | variables: variables_, |
335 | text: "fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n" |
336 | " return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n" |
337 | "}\n" ); |
338 | } |
339 | } |
340 | |
341 | void ImmutableStringFieldLiteGenerator::GenerateFieldInfo( |
342 | io::Printer* printer, std::vector<uint16_t>* output) const { |
343 | WriteIntToUtf16CharSequence(value: descriptor_->number(), output); |
344 | WriteIntToUtf16CharSequence(value: GetExperimentalJavaFieldType(field: descriptor_), |
345 | output); |
346 | if (HasHasbit(descriptor: descriptor_)) { |
347 | WriteIntToUtf16CharSequence(value: messageBitIndex_, output); |
348 | } |
349 | printer->Print(variables: variables_, text: "\"$name$_\",\n" ); |
350 | } |
351 | |
352 | void ImmutableStringFieldLiteGenerator::GenerateInitializationCode( |
353 | io::Printer* printer) const { |
354 | printer->Print(variables: variables_, text: "$name$_ = $default$;\n" ); |
355 | } |
356 | |
357 | std::string ImmutableStringFieldLiteGenerator::GetBoxedType() const { |
358 | return "java.lang.String" ; |
359 | } |
360 | |
361 | // =================================================================== |
362 | |
363 | ImmutableStringOneofFieldLiteGenerator::ImmutableStringOneofFieldLiteGenerator( |
364 | const FieldDescriptor* descriptor, int messageBitIndex, Context* context) |
365 | : ImmutableStringFieldLiteGenerator(descriptor, messageBitIndex, context) { |
366 | const OneofGeneratorInfo* info = |
367 | context->GetOneofGeneratorInfo(oneof: descriptor->containing_oneof()); |
368 | SetCommonOneofVariables(descriptor, info, variables: &variables_); |
369 | } |
370 | |
371 | ImmutableStringOneofFieldLiteGenerator:: |
372 | ~ImmutableStringOneofFieldLiteGenerator() {} |
373 | |
374 | void ImmutableStringOneofFieldLiteGenerator::GenerateMembers( |
375 | io::Printer* printer) const { |
376 | PrintExtraFieldInfo(variables: variables_, printer); |
377 | GOOGLE_DCHECK(HasHazzer(descriptor_)); |
378 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: HAZZER); |
379 | printer->Print(variables: variables_, |
380 | text: "@java.lang.Override\n" |
381 | "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" |
382 | " return $has_oneof_case_message$;\n" |
383 | "}\n" ); |
384 | printer->Annotate(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
385 | |
386 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: GETTER); |
387 | printer->Print( |
388 | variables: variables_, |
389 | text: "@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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
398 | |
399 | WriteFieldStringBytesAccessorDocComment(printer, field: descriptor_, type: GETTER); |
400 | printer->Print(variables: variables_, |
401 | text: "@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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
411 | |
412 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: SETTER); |
413 | printer->Print(variables: variables_, |
414 | text: "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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
421 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: CLEARER); |
422 | printer->Print(variables: variables_, |
423 | text: "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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
430 | |
431 | WriteFieldStringBytesAccessorDocComment(printer, field: descriptor_, type: SETTER); |
432 | printer->Print(variables: variables_, |
433 | text: "private void ${$set$capitalized_name$Bytes$}$(\n" |
434 | " com.google.protobuf.ByteString value) {\n" ); |
435 | printer->Annotate(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
436 | if (CheckUtf8(descriptor: descriptor_)) { |
437 | printer->Print(variables: variables_, text: " checkByteStringIsUtf8(value);\n" ); |
438 | } |
439 | printer->Print(variables: variables_, |
440 | text: " $oneof_name$_ = value.toStringUtf8();\n" |
441 | " $set_oneof_case_message$;\n" |
442 | "}\n" ); |
443 | } |
444 | |
445 | void ImmutableStringOneofFieldLiteGenerator::GenerateFieldInfo( |
446 | io::Printer* printer, std::vector<uint16_t>* output) const { |
447 | WriteIntToUtf16CharSequence(value: descriptor_->number(), output); |
448 | WriteIntToUtf16CharSequence(value: GetExperimentalJavaFieldType(field: descriptor_), |
449 | output); |
450 | WriteIntToUtf16CharSequence(value: descriptor_->containing_oneof()->index(), output); |
451 | } |
452 | |
453 | void ImmutableStringOneofFieldLiteGenerator::GenerateBuilderMembers( |
454 | io::Printer* printer) const { |
455 | GOOGLE_DCHECK(HasHazzer(descriptor_)); |
456 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: HAZZER); |
457 | printer->Print(variables: variables_, |
458 | text: "@java.lang.Override\n" |
459 | "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" |
460 | " return instance.has$capitalized_name$();\n" |
461 | "}\n" ); |
462 | printer->Annotate(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
463 | |
464 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: GETTER); |
465 | printer->Print( |
466 | variables: variables_, |
467 | text: "@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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
472 | |
473 | WriteFieldStringBytesAccessorDocComment(printer, field: descriptor_, type: GETTER); |
474 | printer->Print(variables: variables_, |
475 | text: "@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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
481 | |
482 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: SETTER, |
483 | /* builder */ true); |
484 | printer->Print(variables: variables_, |
485 | text: "$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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
492 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: CLEARER, |
493 | /* builder */ true); |
494 | printer->Print( |
495 | variables: variables_, |
496 | text: "$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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
502 | |
503 | WriteFieldStringBytesAccessorDocComment(printer, field: descriptor_, type: SETTER, |
504 | /* builder */ true); |
505 | printer->Print( |
506 | variables: variables_, |
507 | text: "$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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
514 | } |
515 | |
516 | // =================================================================== |
517 | |
518 | RepeatedImmutableStringFieldLiteGenerator:: |
519 | RepeatedImmutableStringFieldLiteGenerator(const FieldDescriptor* descriptor, |
520 | int messageBitIndex, |
521 | Context* context) |
522 | : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) { |
523 | SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex: 0, |
524 | info: context->GetFieldGeneratorInfo(field: descriptor), |
525 | name_resolver: name_resolver_, variables: &variables_); |
526 | } |
527 | |
528 | RepeatedImmutableStringFieldLiteGenerator:: |
529 | ~RepeatedImmutableStringFieldLiteGenerator() {} |
530 | |
531 | int RepeatedImmutableStringFieldLiteGenerator::GetNumBitsForMessage() const { |
532 | return 0; |
533 | } |
534 | |
535 | void RepeatedImmutableStringFieldLiteGenerator::GenerateInterfaceMembers( |
536 | io::Printer* printer) const { |
537 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_GETTER); |
538 | printer->Print(variables: variables_, |
539 | text: "$deprecation$java.util.List<java.lang.String>\n" |
540 | " get$capitalized_name$List();\n" ); |
541 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_COUNT); |
542 | printer->Print(variables: variables_, |
543 | text: "$deprecation$int get$capitalized_name$Count();\n" ); |
544 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_INDEXED_GETTER); |
545 | printer->Print( |
546 | variables: variables_, |
547 | text: "$deprecation$java.lang.String get$capitalized_name$(int index);\n" ); |
548 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_INDEXED_GETTER); |
549 | printer->Print(variables: variables_, |
550 | text: "$deprecation$com.google.protobuf.ByteString\n" |
551 | " get$capitalized_name$Bytes(int index);\n" ); |
552 | } |
553 | |
554 | void RepeatedImmutableStringFieldLiteGenerator::GenerateMembers( |
555 | io::Printer* printer) const { |
556 | printer->Print( |
557 | variables: variables_, |
558 | text: "private com.google.protobuf.Internal.ProtobufList<java.lang.String> " |
559 | "$name$_;\n" ); |
560 | PrintExtraFieldInfo(variables: variables_, printer); |
561 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_GETTER); |
562 | printer->Print(variables: variables_, |
563 | text: "@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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
569 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_COUNT); |
570 | printer->Print( |
571 | variables: variables_, |
572 | text: "@java.lang.Override\n" |
573 | "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" |
574 | " return $name$_.size();\n" |
575 | "}\n" ); |
576 | printer->Annotate(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
577 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_INDEXED_GETTER); |
578 | printer->Print(variables: variables_, |
579 | text: "@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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
585 | WriteFieldStringBytesAccessorDocComment(printer, field: descriptor_, |
586 | type: LIST_INDEXED_GETTER); |
587 | printer->Print(variables: variables_, |
588 | text: "@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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
595 | |
596 | printer->Print( |
597 | variables: variables_, |
598 | text: "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, field: descriptor_, type: LIST_INDEXED_SETTER); |
609 | printer->Print(variables: variables_, |
610 | text: "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, field: descriptor_, type: LIST_ADDER); |
617 | printer->Print(variables: variables_, |
618 | text: "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, field: descriptor_, type: LIST_MULTI_ADDER); |
625 | printer->Print(variables: variables_, |
626 | text: "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, field: descriptor_, type: CLEARER); |
633 | printer->Print(variables: variables_, |
634 | text: "private void clear$capitalized_name$() {\n" |
635 | " $name$_ = $empty_list$;\n" |
636 | "}\n" ); |
637 | |
638 | WriteFieldStringBytesAccessorDocComment(printer, field: descriptor_, type: LIST_ADDER); |
639 | printer->Print(variables: variables_, |
640 | text: "private void add$capitalized_name$Bytes(\n" |
641 | " com.google.protobuf.ByteString value) {\n" ); |
642 | if (CheckUtf8(descriptor: descriptor_)) { |
643 | printer->Print(variables: variables_, text: " checkByteStringIsUtf8(value);\n" ); |
644 | } |
645 | printer->Print(variables: variables_, |
646 | text: " ensure$capitalized_name$IsMutable();\n" |
647 | " $name$_.add(value.toStringUtf8());\n" |
648 | "}\n" ); |
649 | } |
650 | |
651 | void RepeatedImmutableStringFieldLiteGenerator::GenerateBuilderMembers( |
652 | io::Printer* printer) const { |
653 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_GETTER); |
654 | printer->Print(variables: variables_, |
655 | text: "@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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
662 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_COUNT); |
663 | printer->Print( |
664 | variables: variables_, |
665 | text: "@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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
670 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_INDEXED_GETTER); |
671 | printer->Print(variables: variables_, |
672 | text: "@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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
678 | WriteFieldStringBytesAccessorDocComment(printer, field: descriptor_, |
679 | type: LIST_INDEXED_GETTER); |
680 | printer->Print(variables: variables_, |
681 | text: "@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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
687 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_INDEXED_SETTER, |
688 | /* builder */ true); |
689 | printer->Print(variables: variables_, |
690 | text: "$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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
697 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_ADDER, |
698 | /* builder */ true); |
699 | printer->Print(variables: variables_, |
700 | text: "$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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
707 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_MULTI_ADDER, |
708 | /* builder */ true); |
709 | printer->Print(variables: variables_, |
710 | text: "$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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
717 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: CLEARER, |
718 | /* builder */ true); |
719 | printer->Print( |
720 | variables: variables_, |
721 | text: "$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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
727 | |
728 | WriteFieldStringBytesAccessorDocComment(printer, field: descriptor_, type: LIST_ADDER, |
729 | /* builder */ true); |
730 | printer->Print( |
731 | variables: variables_, |
732 | text: "$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(begin_varname: "{" , end_varname: "}" , descriptor: descriptor_); |
739 | } |
740 | |
741 | void RepeatedImmutableStringFieldLiteGenerator::GenerateKotlinDslMembers( |
742 | io::Printer* printer) const { |
743 | printer->Print( |
744 | variables: variables_, |
745 | text: "/**\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 | "class ${$$kt_capitalized_name$Proxy$}$ private constructor()" |
752 | " : com.google.protobuf.kotlin.DslProxy()\n" ); |
753 | |
754 | // property for List<String> |
755 | WriteFieldAccessorDocComment(printer, field: descriptor_, type: LIST_GETTER); |
756 | printer->Print( |
757 | variables: variables_, |
758 | text: "$kt_deprecation$ 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, field: descriptor_, type: LIST_ADDER, |
769 | /* builder */ false); |
770 | printer->Print(variables: variables_, |
771 | text: "@kotlin.jvm.JvmSynthetic\n" |
772 | "@kotlin.jvm.JvmName(\"add$kt_capitalized_name$\")\n" |
773 | "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, field: descriptor_, type: LIST_ADDER, |
781 | /* builder */ false); |
782 | printer->Print(variables: variables_, |
783 | text: "@kotlin.jvm.JvmSynthetic\n" |
784 | "@kotlin.jvm.JvmName(\"plusAssign$kt_capitalized_name$\")\n" |
785 | "@Suppress(\"NOTHING_TO_INLINE\")\n" |
786 | "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, field: descriptor_, type: LIST_MULTI_ADDER, |
794 | /* builder */ false); |
795 | printer->Print( |
796 | variables: variables_, |
797 | text: "@kotlin.jvm.JvmSynthetic\n" |
798 | "@kotlin.jvm.JvmName(\"addAll$kt_capitalized_name$\")\n" |
799 | "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, field: descriptor_, type: LIST_MULTI_ADDER, |
807 | /* builder */ false); |
808 | printer->Print( |
809 | variables: variables_, |
810 | text: "@kotlin.jvm.JvmSynthetic\n" |
811 | "@kotlin.jvm.JvmName(\"plusAssignAll$kt_capitalized_name$\")\n" |
812 | "@Suppress(\"NOTHING_TO_INLINE\")\n" |
813 | "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, field: descriptor_, type: LIST_INDEXED_SETTER, |
821 | /* builder */ false); |
822 | printer->Print( |
823 | variables: variables_, |
824 | text: "@kotlin.jvm.JvmSynthetic\n" |
825 | "@kotlin.jvm.JvmName(\"set$kt_capitalized_name$\")\n" |
826 | "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, field: descriptor_, type: CLEARER, |
833 | /* builder */ false); |
834 | printer->Print(variables: variables_, |
835 | text: "@kotlin.jvm.JvmSynthetic\n" |
836 | "@kotlin.jvm.JvmName(\"clear$kt_capitalized_name$\")\n" |
837 | "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 | |
844 | void RepeatedImmutableStringFieldLiteGenerator::GenerateFieldInfo( |
845 | io::Printer* printer, std::vector<uint16_t>* output) const { |
846 | WriteIntToUtf16CharSequence(value: descriptor_->number(), output); |
847 | WriteIntToUtf16CharSequence(value: GetExperimentalJavaFieldType(field: descriptor_), |
848 | output); |
849 | printer->Print(variables: variables_, text: "\"$name$_\",\n" ); |
850 | } |
851 | |
852 | void RepeatedImmutableStringFieldLiteGenerator::GenerateInitializationCode( |
853 | io::Printer* printer) const { |
854 | printer->Print(variables: variables_, text: "$name$_ = $empty_list$;\n" ); |
855 | } |
856 | |
857 | 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 | |