blob: b67eeb5326c8f255df4c84198eb13277d734e543 [file] [log] [blame]
liujisi@google.com33165fe2010-11-02 13:14:58 +00001// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
liujisi@google.com33165fe2010-11-02 13:14:58 +00004//
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 <map>
37#include <string>
38
Feng Xiaoeee38b02015-08-22 18:25:48 -070039#include <google/protobuf/stubs/logging.h>
liujisi@google.com33165fe2010-11-02 13:14:58 +000040#include <google/protobuf/stubs/common.h>
jieluo@google.com4de8f552014-07-18 00:47:59 +000041#include <google/protobuf/compiler/java/java_context.h>
42#include <google/protobuf/compiler/java/java_doc_comment.h>
liujisi@google.com33165fe2010-11-02 13:14:58 +000043#include <google/protobuf/compiler/java/java_helpers.h>
jieluo@google.com4de8f552014-07-18 00:47:59 +000044#include <google/protobuf/compiler/java/java_name_resolver.h>
45#include <google/protobuf/compiler/java/java_string_field.h>
liujisi@google.com33165fe2010-11-02 13:14:58 +000046#include <google/protobuf/io/printer.h>
47#include <google/protobuf/wire_format.h>
48#include <google/protobuf/stubs/strutil.h>
49
50namespace google {
51namespace protobuf {
52namespace compiler {
53namespace java {
54
55using internal::WireFormat;
56using internal::WireFormatLite;
57
58namespace {
59
60void SetPrimitiveVariables(const FieldDescriptor* descriptor,
61 int messageBitIndex,
62 int builderBitIndex,
jieluo@google.com4de8f552014-07-18 00:47:59 +000063 const FieldGeneratorInfo* info,
64 ClassNameResolver* name_resolver,
liujisi@google.com33165fe2010-11-02 13:14:58 +000065 map<string, string>* variables) {
jieluo@google.com4de8f552014-07-18 00:47:59 +000066 SetCommonFieldVariables(descriptor, info, variables);
67
liujisi@google.com33165fe2010-11-02 13:14:58 +000068 (*variables)["empty_list"] = "com.google.protobuf.LazyStringArrayList.EMPTY";
69
jieluo@google.com4de8f552014-07-18 00:47:59 +000070 (*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver);
71 (*variables)["default_init"] =
72 "= " + ImmutableDefaultValue(descriptor, name_resolver);
liujisi@google.com33165fe2010-11-02 13:14:58 +000073 (*variables)["capitalized_type"] = "String";
74 (*variables)["tag"] = SimpleItoa(WireFormat::MakeTag(descriptor));
75 (*variables)["tag_size"] = SimpleItoa(
76 WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
77 (*variables)["null_check"] =
78 " if (value == null) {\n"
79 " throw new NullPointerException();\n"
80 " }\n";
Feng Xiaoeee38b02015-08-22 18:25:48 -070081 (*variables)["writeString"] =
82 "com.google.protobuf.GeneratedMessage.writeString";
83 (*variables)["computeStringSize"] =
84 "com.google.protobuf.GeneratedMessage.computeStringSize";
liujisi@google.com33165fe2010-11-02 13:14:58 +000085
86 // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
87 // by the proto compiler
88 (*variables)["deprecation"] = descriptor->options().deprecated()
89 ? "@java.lang.Deprecated " : "";
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070090 (*variables)["on_changed"] = "onChanged();";
liujisi@google.com33165fe2010-11-02 13:14:58 +000091
jieluo@google.com4de8f552014-07-18 00:47:59 +000092 if (SupportFieldPresence(descriptor->file())) {
93 // For singular messages and builders, one bit is used for the hasField bit.
94 (*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex);
95 (*variables)["get_has_field_bit_builder"] = GenerateGetBit(builderBitIndex);
liujisi@google.com33165fe2010-11-02 13:14:58 +000096
jieluo@google.com4de8f552014-07-18 00:47:59 +000097 // Note that these have a trailing ";".
98 (*variables)["set_has_field_bit_message"] =
99 GenerateSetBit(messageBitIndex) + ";";
100 (*variables)["set_has_field_bit_builder"] =
101 GenerateSetBit(builderBitIndex) + ";";
102 (*variables)["clear_has_field_bit_builder"] =
103 GenerateClearBit(builderBitIndex) + ";";
liujisi@google.com33165fe2010-11-02 13:14:58 +0000104
jieluo@google.com4de8f552014-07-18 00:47:59 +0000105 (*variables)["is_field_present_message"] = GenerateGetBit(messageBitIndex);
106 } else {
107 (*variables)["set_has_field_bit_message"] = "";
108 (*variables)["set_has_field_bit_builder"] = "";
109 (*variables)["clear_has_field_bit_builder"] = "";
110
111 (*variables)["is_field_present_message"] =
112 "!get" + (*variables)["capitalized_name"] + "Bytes().isEmpty()";
113 }
114
115 // For repeated builders, one bit is used for whether the array is immutable.
liujisi@google.com33165fe2010-11-02 13:14:58 +0000116 (*variables)["get_mutable_bit_builder"] = GenerateGetBit(builderBitIndex);
117 (*variables)["set_mutable_bit_builder"] = GenerateSetBit(builderBitIndex);
118 (*variables)["clear_mutable_bit_builder"] = GenerateClearBit(builderBitIndex);
119
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000120 // For repeated fields, one bit is used for whether the array is immutable
121 // in the parsing constructor.
122 (*variables)["get_mutable_bit_parser"] =
123 GenerateGetBitMutableLocal(builderBitIndex);
124 (*variables)["set_mutable_bit_parser"] =
125 GenerateSetBitMutableLocal(builderBitIndex);
126
liujisi@google.com33165fe2010-11-02 13:14:58 +0000127 (*variables)["get_has_field_bit_from_local"] =
128 GenerateGetBitFromLocal(builderBitIndex);
129 (*variables)["set_has_field_bit_to_local"] =
130 GenerateSetBitToLocal(messageBitIndex);
131}
132
133} // namespace
134
135// ===================================================================
136
jieluo@google.com4de8f552014-07-18 00:47:59 +0000137ImmutableStringFieldGenerator::
138ImmutableStringFieldGenerator(const FieldDescriptor* descriptor,
139 int messageBitIndex,
140 int builderBitIndex,
141 Context* context)
liujisi@google.com33165fe2010-11-02 13:14:58 +0000142 : descriptor_(descriptor), messageBitIndex_(messageBitIndex),
jieluo@google.com4de8f552014-07-18 00:47:59 +0000143 builderBitIndex_(builderBitIndex), context_(context),
144 name_resolver_(context->GetNameResolver()) {
liujisi@google.com33165fe2010-11-02 13:14:58 +0000145 SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex,
jieluo@google.com4de8f552014-07-18 00:47:59 +0000146 context->GetFieldGeneratorInfo(descriptor),
147 name_resolver_, &variables_);
liujisi@google.com33165fe2010-11-02 13:14:58 +0000148}
149
jieluo@google.com4de8f552014-07-18 00:47:59 +0000150ImmutableStringFieldGenerator::~ImmutableStringFieldGenerator() {}
liujisi@google.com33165fe2010-11-02 13:14:58 +0000151
jieluo@google.com4de8f552014-07-18 00:47:59 +0000152int ImmutableStringFieldGenerator::GetNumBitsForMessage() const {
liujisi@google.com33165fe2010-11-02 13:14:58 +0000153 return 1;
154}
155
jieluo@google.com4de8f552014-07-18 00:47:59 +0000156int ImmutableStringFieldGenerator::GetNumBitsForBuilder() const {
liujisi@google.com33165fe2010-11-02 13:14:58 +0000157 return 1;
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 vaid 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.
jieluo@google.com4de8f552014-07-18 00:47:59 +0000192void ImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000193GenerateInterfaceMembers(io::Printer* printer) const {
jieluo@google.com4de8f552014-07-18 00:47:59 +0000194 if (SupportFieldPresence(descriptor_->file())) {
195 WriteFieldDocComment(printer, descriptor_);
196 printer->Print(variables_,
197 "$deprecation$boolean has$capitalized_name$();\n");
198 }
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000199 WriteFieldDocComment(printer, descriptor_);
200 printer->Print(variables_,
liujisi@google.com02d70152011-05-04 06:15:22 +0000201 "$deprecation$java.lang.String get$capitalized_name$();\n");
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000202 WriteFieldDocComment(printer, descriptor_);
203 printer->Print(variables_,
204 "$deprecation$com.google.protobuf.ByteString\n"
205 " get$capitalized_name$Bytes();\n");
liujisi@google.com33165fe2010-11-02 13:14:58 +0000206}
207
jieluo@google.com4de8f552014-07-18 00:47:59 +0000208void ImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000209GenerateMembers(io::Printer* printer) const {
210 printer->Print(variables_,
Bo Yang5db21732015-05-21 14:28:59 -0700211 "private volatile java.lang.Object $name$_;\n");
jieluo@google.com4de8f552014-07-18 00:47:59 +0000212 PrintExtraFieldInfo(variables_, printer);
213
214 if (SupportFieldPresence(descriptor_->file())) {
215 WriteFieldDocComment(printer, descriptor_);
216 printer->Print(variables_,
217 "$deprecation$public boolean has$capitalized_name$() {\n"
218 " return $get_has_field_bit_message$;\n"
219 "}\n");
220 }
liujisi@google.com33165fe2010-11-02 13:14:58 +0000221
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000222 WriteFieldDocComment(printer, descriptor_);
liujisi@google.com33165fe2010-11-02 13:14:58 +0000223 printer->Print(variables_,
liujisi@google.com02d70152011-05-04 06:15:22 +0000224 "$deprecation$public java.lang.String get$capitalized_name$() {\n"
liujisi@google.com9cf65b72011-04-08 03:40:29 +0000225 " java.lang.Object ref = $name$_;\n"
liujisi@google.com02d70152011-05-04 06:15:22 +0000226 " if (ref instanceof java.lang.String) {\n"
227 " return (java.lang.String) ref;\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000228 " } else {\n"
229 " com.google.protobuf.ByteString bs = \n"
230 " (com.google.protobuf.ByteString) ref;\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000231 " java.lang.String s = bs.toStringUtf8();\n");
232 if (CheckUtf8(descriptor_)) {
233 printer->Print(variables_,
234 " $name$_ = s;\n");
235 } else {
236 printer->Print(variables_,
237 " if (bs.isValidUtf8()) {\n"
238 " $name$_ = s;\n"
239 " }\n");
240 }
241 printer->Print(variables_,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000242 " return s;\n"
243 " }\n"
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000244 "}\n");
245 WriteFieldDocComment(printer, descriptor_);
246 printer->Print(variables_,
247 "$deprecation$public com.google.protobuf.ByteString\n"
248 " get$capitalized_name$Bytes() {\n"
liujisi@google.com9cf65b72011-04-08 03:40:29 +0000249 " java.lang.Object ref = $name$_;\n"
liujisi@google.com02d70152011-05-04 06:15:22 +0000250 " if (ref instanceof java.lang.String) {\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000251 " com.google.protobuf.ByteString b = \n"
liujisi@google.com02d70152011-05-04 06:15:22 +0000252 " com.google.protobuf.ByteString.copyFromUtf8(\n"
253 " (java.lang.String) ref);\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000254 " $name$_ = b;\n"
255 " return b;\n"
256 " } else {\n"
257 " return (com.google.protobuf.ByteString) ref;\n"
258 " }\n"
259 "}\n");
260}
261
jieluo@google.com4de8f552014-07-18 00:47:59 +0000262void ImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000263GenerateBuilderMembers(io::Printer* printer) const {
264 printer->Print(variables_,
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000265 "private java.lang.Object $name$_ $default_init$;\n");
jieluo@google.com4de8f552014-07-18 00:47:59 +0000266 if (SupportFieldPresence(descriptor_->file())) {
267 WriteFieldDocComment(printer, descriptor_);
268 printer->Print(variables_,
269 "$deprecation$public boolean has$capitalized_name$() {\n"
270 " return $get_has_field_bit_builder$;\n"
271 "}\n");
272 }
liujisi@google.com33165fe2010-11-02 13:14:58 +0000273
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000274 WriteFieldDocComment(printer, descriptor_);
liujisi@google.com33165fe2010-11-02 13:14:58 +0000275 printer->Print(variables_,
liujisi@google.com02d70152011-05-04 06:15:22 +0000276 "$deprecation$public java.lang.String get$capitalized_name$() {\n"
liujisi@google.com9cf65b72011-04-08 03:40:29 +0000277 " java.lang.Object ref = $name$_;\n"
liujisi@google.com02d70152011-05-04 06:15:22 +0000278 " if (!(ref instanceof java.lang.String)) {\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000279 " com.google.protobuf.ByteString bs =\n"
280 " (com.google.protobuf.ByteString) ref;\n"
281 " java.lang.String s = bs.toStringUtf8();\n");
282 if (CheckUtf8(descriptor_)) {
283 printer->Print(variables_,
284 " $name$_ = s;\n");
285 } else {
286 printer->Print(variables_,
287 " if (bs.isValidUtf8()) {\n"
288 " $name$_ = s;\n"
289 " }\n");
290 }
291 printer->Print(variables_,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000292 " return s;\n"
293 " } else {\n"
liujisi@google.com02d70152011-05-04 06:15:22 +0000294 " return (java.lang.String) ref;\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000295 " }\n"
296 "}\n");
297
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000298 WriteFieldDocComment(printer, descriptor_);
299 printer->Print(variables_,
300 "$deprecation$public com.google.protobuf.ByteString\n"
301 " get$capitalized_name$Bytes() {\n"
302 " java.lang.Object ref = $name$_;\n"
303 " if (ref instanceof String) {\n"
304 " com.google.protobuf.ByteString b = \n"
305 " com.google.protobuf.ByteString.copyFromUtf8(\n"
306 " (java.lang.String) ref);\n"
307 " $name$_ = b;\n"
308 " return b;\n"
309 " } else {\n"
310 " return (com.google.protobuf.ByteString) ref;\n"
311 " }\n"
312 "}\n");
313
314 WriteFieldDocComment(printer, descriptor_);
liujisi@google.com33165fe2010-11-02 13:14:58 +0000315 printer->Print(variables_,
liujisi@google.com02d70152011-05-04 06:15:22 +0000316 "$deprecation$public Builder set$capitalized_name$(\n"
317 " java.lang.String value) {\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000318 "$null_check$"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000319 " $set_has_field_bit_builder$\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000320 " $name$_ = value;\n"
321 " $on_changed$\n"
322 " return this;\n"
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000323 "}\n");
324 WriteFieldDocComment(printer, descriptor_);
325 printer->Print(variables_,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000326 "$deprecation$public Builder clear$capitalized_name$() {\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000327 " $clear_has_field_bit_builder$\n");
liujisi@google.com33165fe2010-11-02 13:14:58 +0000328 // The default value is not a simple literal so we want to avoid executing
329 // it multiple times. Instead, get the default out of the default instance.
330 printer->Print(variables_,
331 " $name$_ = getDefaultInstance().get$capitalized_name$();\n");
332 printer->Print(variables_,
333 " $on_changed$\n"
334 " return this;\n"
335 "}\n");
336
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000337 WriteFieldDocComment(printer, descriptor_);
liujisi@google.com33165fe2010-11-02 13:14:58 +0000338 printer->Print(variables_,
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000339 "$deprecation$public Builder set$capitalized_name$Bytes(\n"
340 " com.google.protobuf.ByteString value) {\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000341 "$null_check$");
342 if (CheckUtf8(descriptor_)) {
343 printer->Print(variables_,
344 " checkByteStringIsUtf8(value);\n");
345 }
346 printer->Print(variables_,
347 " $set_has_field_bit_builder$\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000348 " $name$_ = value;\n"
349 " $on_changed$\n"
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000350 " return this;\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000351 "}\n");
352}
353
jieluo@google.com4de8f552014-07-18 00:47:59 +0000354void ImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000355GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
356 // noop for primitives
357}
358
jieluo@google.com4de8f552014-07-18 00:47:59 +0000359void ImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000360GenerateInitializationCode(io::Printer* printer) const {
361 printer->Print(variables_, "$name$_ = $default$;\n");
362}
363
jieluo@google.com4de8f552014-07-18 00:47:59 +0000364void ImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000365GenerateBuilderClearCode(io::Printer* printer) const {
366 printer->Print(variables_,
367 "$name$_ = $default$;\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000368 "$clear_has_field_bit_builder$\n");
liujisi@google.com33165fe2010-11-02 13:14:58 +0000369}
370
jieluo@google.com4de8f552014-07-18 00:47:59 +0000371void ImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000372GenerateMergingCode(io::Printer* printer) const {
jieluo@google.com4de8f552014-07-18 00:47:59 +0000373 if (SupportFieldPresence(descriptor_->file())) {
374 // Allow a slight breach of abstraction here in order to avoid forcing
375 // all string fields to Strings when copying fields from a Message.
376 printer->Print(variables_,
377 "if (other.has$capitalized_name$()) {\n"
378 " $set_has_field_bit_builder$\n"
379 " $name$_ = other.$name$_;\n"
380 " $on_changed$\n"
381 "}\n");
382 } else {
383 printer->Print(variables_,
384 "if (!other.get$capitalized_name$().isEmpty()) {\n"
385 " $name$_ = other.$name$_;\n"
386 " $on_changed$\n"
387 "}\n");
388 }
liujisi@google.com33165fe2010-11-02 13:14:58 +0000389}
390
jieluo@google.com4de8f552014-07-18 00:47:59 +0000391void ImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000392GenerateBuildingCode(io::Printer* printer) const {
jieluo@google.com4de8f552014-07-18 00:47:59 +0000393 if (SupportFieldPresence(descriptor_->file())) {
394 printer->Print(variables_,
395 "if ($get_has_field_bit_from_local$) {\n"
396 " $set_has_field_bit_to_local$;\n"
397 "}\n");
398 }
liujisi@google.com33165fe2010-11-02 13:14:58 +0000399 printer->Print(variables_,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000400 "result.$name$_ = $name$_;\n");
401}
402
jieluo@google.com4de8f552014-07-18 00:47:59 +0000403void ImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000404GenerateParsingCode(io::Printer* printer) const {
jieluo@google.com4de8f552014-07-18 00:47:59 +0000405 if (CheckUtf8(descriptor_)) {
406 printer->Print(variables_,
Feng Xiaoe841bac2015-12-11 17:09:20 -0800407 "java.lang.String s = input.readStringRequireUtf8();\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000408 "$set_has_field_bit_message$\n"
409 "$name$_ = s;\n");
410 } else {
411 printer->Print(variables_,
412 "com.google.protobuf.ByteString bs = input.readBytes();\n"
413 "$set_has_field_bit_message$\n"
414 "$name$_ = bs;\n");
415 }
liujisi@google.com33165fe2010-11-02 13:14:58 +0000416}
417
jieluo@google.com4de8f552014-07-18 00:47:59 +0000418void ImmutableStringFieldGenerator::
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000419GenerateParsingDoneCode(io::Printer* printer) const {
420 // noop for strings.
421}
422
jieluo@google.com4de8f552014-07-18 00:47:59 +0000423void ImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000424GenerateSerializationCode(io::Printer* printer) const {
425 printer->Print(variables_,
jieluo@google.com4de8f552014-07-18 00:47:59 +0000426 "if ($is_field_present_message$) {\n"
Feng Xiaoeee38b02015-08-22 18:25:48 -0700427 " $writeString$(output, $number$, $name$_);\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000428 "}\n");
429}
430
jieluo@google.com4de8f552014-07-18 00:47:59 +0000431void ImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000432GenerateSerializedSizeCode(io::Printer* printer) const {
433 printer->Print(variables_,
jieluo@google.com4de8f552014-07-18 00:47:59 +0000434 "if ($is_field_present_message$) {\n"
Feng Xiaoeee38b02015-08-22 18:25:48 -0700435 " size += $computeStringSize$($number$, $name$_);\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000436 "}\n");
437}
438
jieluo@google.com4de8f552014-07-18 00:47:59 +0000439void ImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000440GenerateEqualsCode(io::Printer* printer) const {
441 printer->Print(variables_,
442 "result = result && get$capitalized_name$()\n"
443 " .equals(other.get$capitalized_name$());\n");
444}
445
jieluo@google.com4de8f552014-07-18 00:47:59 +0000446void ImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000447GenerateHashCode(io::Printer* printer) const {
448 printer->Print(variables_,
449 "hash = (37 * hash) + $constant_name$;\n");
450 printer->Print(variables_,
451 "hash = (53 * hash) + get$capitalized_name$().hashCode();\n");
452}
453
jieluo@google.com4de8f552014-07-18 00:47:59 +0000454string ImmutableStringFieldGenerator::GetBoxedType() const {
liujisi@google.com02d70152011-05-04 06:15:22 +0000455 return "java.lang.String";
liujisi@google.com33165fe2010-11-02 13:14:58 +0000456}
457
jieluo@google.com4de8f552014-07-18 00:47:59 +0000458// ===================================================================
459
460ImmutableStringOneofFieldGenerator::
461ImmutableStringOneofFieldGenerator(const FieldDescriptor* descriptor,
462 int messageBitIndex,
463 int builderBitIndex,
464 Context* context)
465 : ImmutableStringFieldGenerator(
466 descriptor, messageBitIndex, builderBitIndex, context) {
467 const OneofGeneratorInfo* info =
468 context->GetOneofGeneratorInfo(descriptor->containing_oneof());
469 SetCommonOneofVariables(descriptor, info, &variables_);
470}
471
472ImmutableStringOneofFieldGenerator::
473~ImmutableStringOneofFieldGenerator() {}
474
475void ImmutableStringOneofFieldGenerator::
476GenerateMembers(io::Printer* printer) const {
477 PrintExtraFieldInfo(variables_, printer);
478
479 if (SupportFieldPresence(descriptor_->file())) {
480 WriteFieldDocComment(printer, descriptor_);
481 printer->Print(variables_,
482 "$deprecation$public boolean has$capitalized_name$() {\n"
483 " return $has_oneof_case_message$;\n"
484 "}\n");
485 }
486
487 WriteFieldDocComment(printer, descriptor_);
488 printer->Print(variables_,
489 "$deprecation$public java.lang.String get$capitalized_name$() {\n"
490 " java.lang.Object ref $default_init$;\n"
491 " if ($has_oneof_case_message$) {\n"
492 " ref = $oneof_name$_;\n"
493 " }\n"
494 " if (ref instanceof java.lang.String) {\n"
495 " return (java.lang.String) ref;\n"
496 " } else {\n"
497 " com.google.protobuf.ByteString bs = \n"
498 " (com.google.protobuf.ByteString) ref;\n"
499 " java.lang.String s = bs.toStringUtf8();\n");
500 if (CheckUtf8(descriptor_)) {
501 printer->Print(variables_,
502 " if ($has_oneof_case_message$) {\n"
503 " $oneof_name$_ = s;\n"
504 " }\n");
505 } else {
506 printer->Print(variables_,
507 " if (bs.isValidUtf8() && ($has_oneof_case_message$)) {\n"
508 " $oneof_name$_ = s;\n"
509 " }\n");
510 }
511 printer->Print(variables_,
512 " return s;\n"
513 " }\n"
514 "}\n");
515 WriteFieldDocComment(printer, descriptor_);
516
517 printer->Print(variables_,
518 "$deprecation$public com.google.protobuf.ByteString\n"
519 " get$capitalized_name$Bytes() {\n"
520 " java.lang.Object ref $default_init$;\n"
521 " if ($has_oneof_case_message$) {\n"
522 " ref = $oneof_name$_;\n"
523 " }\n"
524 " if (ref instanceof java.lang.String) {\n"
525 " com.google.protobuf.ByteString b = \n"
526 " com.google.protobuf.ByteString.copyFromUtf8(\n"
527 " (java.lang.String) ref);\n"
528 " if ($has_oneof_case_message$) {\n"
529 " $oneof_name$_ = b;\n"
530 " }\n"
531 " return b;\n"
532 " } else {\n"
533 " return (com.google.protobuf.ByteString) ref;\n"
534 " }\n"
535 "}\n");
536}
537
538void ImmutableStringOneofFieldGenerator::
539GenerateBuilderMembers(io::Printer* printer) const {
540 if (SupportFieldPresence(descriptor_->file())) {
541 WriteFieldDocComment(printer, descriptor_);
542 printer->Print(variables_,
543 "$deprecation$public boolean has$capitalized_name$() {\n"
544 " return $has_oneof_case_message$;\n"
545 "}\n");
546 }
547
548 WriteFieldDocComment(printer, descriptor_);
549 printer->Print(variables_,
550 "$deprecation$public java.lang.String get$capitalized_name$() {\n"
551 " java.lang.Object ref $default_init$;\n"
552 " if ($has_oneof_case_message$) {\n"
553 " ref = $oneof_name$_;\n"
554 " }\n"
555 " if (!(ref instanceof java.lang.String)) {\n"
556 " com.google.protobuf.ByteString bs =\n"
557 " (com.google.protobuf.ByteString) ref;\n"
558 " java.lang.String s = bs.toStringUtf8();\n"
559 " if ($has_oneof_case_message$) {\n");
560 if (CheckUtf8(descriptor_)) {
561 printer->Print(variables_,
562 " $oneof_name$_ = s;\n");
563 } else {
564 printer->Print(variables_,
565 " if (bs.isValidUtf8()) {\n"
566 " $oneof_name$_ = s;\n"
567 " }\n");
568 }
569 printer->Print(variables_,
570 " }\n"
571 " return s;\n"
572 " } else {\n"
573 " return (java.lang.String) ref;\n"
574 " }\n"
575 "}\n");
576
577 WriteFieldDocComment(printer, descriptor_);
578 printer->Print(variables_,
579 "$deprecation$public com.google.protobuf.ByteString\n"
580 " get$capitalized_name$Bytes() {\n"
581 " java.lang.Object ref $default_init$;\n"
582 " if ($has_oneof_case_message$) {\n"
583 " ref = $oneof_name$_;\n"
584 " }\n"
585 " if (ref instanceof String) {\n"
586 " com.google.protobuf.ByteString b = \n"
587 " com.google.protobuf.ByteString.copyFromUtf8(\n"
588 " (java.lang.String) ref);\n"
589 " if ($has_oneof_case_message$) {\n"
590 " $oneof_name$_ = b;\n"
591 " }\n"
592 " return b;\n"
593 " } else {\n"
594 " return (com.google.protobuf.ByteString) ref;\n"
595 " }\n"
596 "}\n");
597
598 WriteFieldDocComment(printer, descriptor_);
599 printer->Print(variables_,
600 "$deprecation$public Builder set$capitalized_name$(\n"
601 " java.lang.String value) {\n"
602 "$null_check$"
603 " $set_oneof_case_message$;\n"
604 " $oneof_name$_ = value;\n"
605 " $on_changed$\n"
606 " return this;\n"
607 "}\n");
608 WriteFieldDocComment(printer, descriptor_);
609 printer->Print(variables_,
610 "$deprecation$public Builder clear$capitalized_name$() {\n"
611 " if ($has_oneof_case_message$) {\n"
612 " $clear_oneof_case_message$;\n"
613 " $oneof_name$_ = null;\n"
614 " $on_changed$\n"
615 " }\n"
616 " return this;\n"
617 "}\n");
618
619 WriteFieldDocComment(printer, descriptor_);
620 printer->Print(variables_,
621 "$deprecation$public Builder set$capitalized_name$Bytes(\n"
622 " com.google.protobuf.ByteString value) {\n"
623 "$null_check$");
624 if (CheckUtf8(descriptor_)) {
625 printer->Print(variables_,
626 " checkByteStringIsUtf8(value);\n");
627 }
628 printer->Print(variables_,
629 " $set_oneof_case_message$;\n"
630 " $oneof_name$_ = value;\n"
631 " $on_changed$\n"
632 " return this;\n"
633 "}\n");
634}
635
636void ImmutableStringOneofFieldGenerator::
637GenerateMergingCode(io::Printer* printer) const {
638 // Allow a slight breach of abstraction here in order to avoid forcing
639 // all string fields to Strings when copying fields from a Message.
640 printer->Print(variables_,
641 "$set_oneof_case_message$;\n"
642 "$oneof_name$_ = other.$oneof_name$_;\n"
643 "$on_changed$\n");
644}
645
646void ImmutableStringOneofFieldGenerator::
647GenerateBuildingCode(io::Printer* printer) const {
648 printer->Print(variables_,
649 "if ($has_oneof_case_message$) {\n"
650 " result.$oneof_name$_ = $oneof_name$_;\n"
651 "}\n");
652}
653
654void ImmutableStringOneofFieldGenerator::
655GenerateParsingCode(io::Printer* printer) const {
656 if (CheckUtf8(descriptor_)) {
657 printer->Print(variables_,
Feng Xiaoe841bac2015-12-11 17:09:20 -0800658 "java.lang.String s = input.readStringRequireUtf8();\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000659 "$set_oneof_case_message$;\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800660 "$oneof_name$_ = s;\n");
jieluo@google.com4de8f552014-07-18 00:47:59 +0000661 } else {
662 printer->Print(variables_,
663 "com.google.protobuf.ByteString bs = input.readBytes();\n"
664 "$set_oneof_case_message$;\n"
665 "$oneof_name$_ = bs;\n");
666 }
667}
668
669void ImmutableStringOneofFieldGenerator::
670GenerateSerializationCode(io::Printer* printer) const {
671 printer->Print(variables_,
672 "if ($has_oneof_case_message$) {\n"
Feng Xiaoeee38b02015-08-22 18:25:48 -0700673 " $writeString$(output, $number$, $oneof_name$_);\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000674 "}\n");
675}
676
677void ImmutableStringOneofFieldGenerator::
678GenerateSerializedSizeCode(io::Printer* printer) const {
679 printer->Print(variables_,
680 "if ($has_oneof_case_message$) {\n"
Feng Xiaoeee38b02015-08-22 18:25:48 -0700681 " size += $computeStringSize$($number$, $oneof_name$_);\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000682 "}\n");
683}
liujisi@google.com33165fe2010-11-02 13:14:58 +0000684
685// ===================================================================
686
jieluo@google.com4de8f552014-07-18 00:47:59 +0000687RepeatedImmutableStringFieldGenerator::
688RepeatedImmutableStringFieldGenerator(const FieldDescriptor* descriptor,
689 int messageBitIndex,
690 int builderBitIndex,
691 Context* context)
liujisi@google.com33165fe2010-11-02 13:14:58 +0000692 : descriptor_(descriptor), messageBitIndex_(messageBitIndex),
jieluo@google.com4de8f552014-07-18 00:47:59 +0000693 builderBitIndex_(builderBitIndex), context_(context),
694 name_resolver_(context->GetNameResolver()) {
liujisi@google.com33165fe2010-11-02 13:14:58 +0000695 SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex,
jieluo@google.com4de8f552014-07-18 00:47:59 +0000696 context->GetFieldGeneratorInfo(descriptor),
697 name_resolver_, &variables_);
liujisi@google.com33165fe2010-11-02 13:14:58 +0000698}
699
jieluo@google.com4de8f552014-07-18 00:47:59 +0000700RepeatedImmutableStringFieldGenerator::
701~RepeatedImmutableStringFieldGenerator() {}
liujisi@google.com33165fe2010-11-02 13:14:58 +0000702
jieluo@google.com4de8f552014-07-18 00:47:59 +0000703int RepeatedImmutableStringFieldGenerator::GetNumBitsForMessage() const {
liujisi@google.com33165fe2010-11-02 13:14:58 +0000704 return 0;
705}
706
jieluo@google.com4de8f552014-07-18 00:47:59 +0000707int RepeatedImmutableStringFieldGenerator::GetNumBitsForBuilder() const {
liujisi@google.com33165fe2010-11-02 13:14:58 +0000708 return 1;
709}
710
jieluo@google.com4de8f552014-07-18 00:47:59 +0000711void RepeatedImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000712GenerateInterfaceMembers(io::Printer* printer) const {
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000713 WriteFieldDocComment(printer, descriptor_);
liujisi@google.com33165fe2010-11-02 13:14:58 +0000714 printer->Print(variables_,
jieluo@google.com4de8f552014-07-18 00:47:59 +0000715 "$deprecation$com.google.protobuf.ProtocolStringList\n"
716 " get$capitalized_name$List();\n");
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000717 WriteFieldDocComment(printer, descriptor_);
718 printer->Print(variables_,
719 "$deprecation$int get$capitalized_name$Count();\n");
720 WriteFieldDocComment(printer, descriptor_);
721 printer->Print(variables_,
liujisi@google.com02d70152011-05-04 06:15:22 +0000722 "$deprecation$java.lang.String get$capitalized_name$(int index);\n");
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000723 WriteFieldDocComment(printer, descriptor_);
724 printer->Print(variables_,
725 "$deprecation$com.google.protobuf.ByteString\n"
726 " get$capitalized_name$Bytes(int index);\n");
liujisi@google.com33165fe2010-11-02 13:14:58 +0000727}
728
729
jieluo@google.com4de8f552014-07-18 00:47:59 +0000730void RepeatedImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000731GenerateMembers(io::Printer* printer) const {
732 printer->Print(variables_,
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000733 "private com.google.protobuf.LazyStringList $name$_;\n");
jieluo@google.com4de8f552014-07-18 00:47:59 +0000734 PrintExtraFieldInfo(variables_, printer);
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000735 WriteFieldDocComment(printer, descriptor_);
736 printer->Print(variables_,
jieluo@google.com4de8f552014-07-18 00:47:59 +0000737 "$deprecation$public com.google.protobuf.ProtocolStringList\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000738 " get$capitalized_name$List() {\n"
739 " return $name$_;\n" // note: unmodifiable list
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000740 "}\n");
741 WriteFieldDocComment(printer, descriptor_);
742 printer->Print(variables_,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000743 "$deprecation$public int get$capitalized_name$Count() {\n"
744 " return $name$_.size();\n"
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000745 "}\n");
746 WriteFieldDocComment(printer, descriptor_);
747 printer->Print(variables_,
liujisi@google.com02d70152011-05-04 06:15:22 +0000748 "$deprecation$public java.lang.String get$capitalized_name$(int index) {\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000749 " return $name$_.get(index);\n"
750 "}\n");
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000751 WriteFieldDocComment(printer, descriptor_);
752 printer->Print(variables_,
753 "$deprecation$public com.google.protobuf.ByteString\n"
754 " get$capitalized_name$Bytes(int index) {\n"
755 " return $name$_.getByteString(index);\n"
756 "}\n");
liujisi@google.com33165fe2010-11-02 13:14:58 +0000757}
758
jieluo@google.com4de8f552014-07-18 00:47:59 +0000759void RepeatedImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000760GenerateBuilderMembers(io::Printer* printer) const {
761 // One field is the list and the bit field keeps track of whether the
762 // list is immutable. If it's immutable, the invariant is that it must
763 // either an instance of Collections.emptyList() or it's an ArrayList
764 // wrapped in a Collections.unmodifiableList() wrapper and nobody else has
765 // a refererence to the underlying ArrayList. This invariant allows us to
766 // share instances of lists between protocol buffers avoiding expensive
767 // memory allocations. Note, immutable is a strong guarantee here -- not
768 // just that the list cannot be modified via the reference but that the
769 // list can never be modified.
770 printer->Print(variables_,
771 "private com.google.protobuf.LazyStringList $name$_ = $empty_list$;\n");
772
773 printer->Print(variables_,
774 "private void ensure$capitalized_name$IsMutable() {\n"
775 " if (!$get_mutable_bit_builder$) {\n"
776 " $name$_ = new com.google.protobuf.LazyStringArrayList($name$_);\n"
777 " $set_mutable_bit_builder$;\n"
778 " }\n"
779 "}\n");
780
781 // Note: We return an unmodifiable list because otherwise the caller
782 // could hold on to the returned list and modify it after the message
783 // has been built, thus mutating the message which is supposed to be
784 // immutable.
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000785 WriteFieldDocComment(printer, descriptor_);
liujisi@google.com33165fe2010-11-02 13:14:58 +0000786 printer->Print(variables_,
jieluo@google.com4de8f552014-07-18 00:47:59 +0000787 "$deprecation$public com.google.protobuf.ProtocolStringList\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000788 " get$capitalized_name$List() {\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000789 " return $name$_.getUnmodifiableView();\n"
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000790 "}\n");
791 WriteFieldDocComment(printer, descriptor_);
792 printer->Print(variables_,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000793 "$deprecation$public int get$capitalized_name$Count() {\n"
794 " return $name$_.size();\n"
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000795 "}\n");
796 WriteFieldDocComment(printer, descriptor_);
797 printer->Print(variables_,
liujisi@google.com02d70152011-05-04 06:15:22 +0000798 "$deprecation$public java.lang.String get$capitalized_name$(int index) {\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000799 " return $name$_.get(index);\n"
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000800 "}\n");
801 WriteFieldDocComment(printer, descriptor_);
802 printer->Print(variables_,
803 "$deprecation$public com.google.protobuf.ByteString\n"
804 " get$capitalized_name$Bytes(int index) {\n"
805 " return $name$_.getByteString(index);\n"
806 "}\n");
807 WriteFieldDocComment(printer, descriptor_);
808 printer->Print(variables_,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000809 "$deprecation$public Builder set$capitalized_name$(\n"
liujisi@google.com02d70152011-05-04 06:15:22 +0000810 " int index, java.lang.String value) {\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000811 "$null_check$"
812 " ensure$capitalized_name$IsMutable();\n"
813 " $name$_.set(index, value);\n"
814 " $on_changed$\n"
815 " return this;\n"
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000816 "}\n");
817 WriteFieldDocComment(printer, descriptor_);
818 printer->Print(variables_,
liujisi@google.com02d70152011-05-04 06:15:22 +0000819 "$deprecation$public Builder add$capitalized_name$(\n"
820 " java.lang.String value) {\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000821 "$null_check$"
822 " ensure$capitalized_name$IsMutable();\n"
823 " $name$_.add(value);\n"
824 " $on_changed$\n"
825 " return this;\n"
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000826 "}\n");
827 WriteFieldDocComment(printer, descriptor_);
828 printer->Print(variables_,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000829 "$deprecation$public Builder addAll$capitalized_name$(\n"
liujisi@google.com02d70152011-05-04 06:15:22 +0000830 " java.lang.Iterable<java.lang.String> values) {\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000831 " ensure$capitalized_name$IsMutable();\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000832 " com.google.protobuf.AbstractMessageLite.Builder.addAll(\n"
833 " values, $name$_);\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000834 " $on_changed$\n"
835 " return this;\n"
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000836 "}\n");
837 WriteFieldDocComment(printer, descriptor_);
838 printer->Print(variables_,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000839 "$deprecation$public Builder clear$capitalized_name$() {\n"
840 " $name$_ = $empty_list$;\n"
841 " $clear_mutable_bit_builder$;\n"
842 " $on_changed$\n"
843 " return this;\n"
844 "}\n");
845
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000846 WriteFieldDocComment(printer, descriptor_);
liujisi@google.com33165fe2010-11-02 13:14:58 +0000847 printer->Print(variables_,
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000848 "$deprecation$public Builder add$capitalized_name$Bytes(\n"
849 " com.google.protobuf.ByteString value) {\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000850 "$null_check$");
851 if (CheckUtf8(descriptor_)) {
852 printer->Print(variables_,
853 " checkByteStringIsUtf8(value);\n");
854 }
855 printer->Print(variables_,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000856 " ensure$capitalized_name$IsMutable();\n"
857 " $name$_.add(value);\n"
858 " $on_changed$\n"
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000859 " return this;\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000860 "}\n");
861}
862
jieluo@google.com4de8f552014-07-18 00:47:59 +0000863void RepeatedImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000864GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
865 // noop for primitives
866}
867
jieluo@google.com4de8f552014-07-18 00:47:59 +0000868void RepeatedImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000869GenerateInitializationCode(io::Printer* printer) const {
870 printer->Print(variables_, "$name$_ = $empty_list$;\n");
871}
872
jieluo@google.com4de8f552014-07-18 00:47:59 +0000873void RepeatedImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000874GenerateBuilderClearCode(io::Printer* printer) const {
875 printer->Print(variables_,
876 "$name$_ = $empty_list$;\n"
877 "$clear_mutable_bit_builder$;\n");
878}
879
jieluo@google.com4de8f552014-07-18 00:47:59 +0000880void RepeatedImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000881GenerateMergingCode(io::Printer* printer) const {
882 // The code below does two optimizations:
883 // 1. If the other list is empty, there's nothing to do. This ensures we
884 // don't allocate a new array if we already have an immutable one.
885 // 2. If the other list is non-empty and our current list is empty, we can
886 // reuse the other list which is guaranteed to be immutable.
887 printer->Print(variables_,
888 "if (!other.$name$_.isEmpty()) {\n"
889 " if ($name$_.isEmpty()) {\n"
890 " $name$_ = other.$name$_;\n"
891 " $clear_mutable_bit_builder$;\n"
892 " } else {\n"
893 " ensure$capitalized_name$IsMutable();\n"
894 " $name$_.addAll(other.$name$_);\n"
895 " }\n"
896 " $on_changed$\n"
897 "}\n");
898}
899
jieluo@google.com4de8f552014-07-18 00:47:59 +0000900void RepeatedImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000901GenerateBuildingCode(io::Printer* printer) const {
902 // The code below ensures that the result has an immutable list. If our
903 // list is immutable, we can just reuse it. If not, we make it immutable.
904
905 printer->Print(variables_,
906 "if ($get_mutable_bit_builder$) {\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000907 " $name$_ = $name$_.getUnmodifiableView();\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000908 " $clear_mutable_bit_builder$;\n"
909 "}\n"
910 "result.$name$_ = $name$_;\n");
911}
912
jieluo@google.com4de8f552014-07-18 00:47:59 +0000913void RepeatedImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000914GenerateParsingCode(io::Printer* printer) const {
jieluo@google.com4de8f552014-07-18 00:47:59 +0000915 if (CheckUtf8(descriptor_)) {
916 printer->Print(variables_,
Feng Xiaoe841bac2015-12-11 17:09:20 -0800917 "java.lang.String s = input.readStringRequireUtf8();\n");
jieluo@google.com4de8f552014-07-18 00:47:59 +0000918 } else {
919 printer->Print(variables_,
920 "com.google.protobuf.ByteString bs = input.readBytes();\n");
921 }
liujisi@google.com33165fe2010-11-02 13:14:58 +0000922 printer->Print(variables_,
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000923 "if (!$get_mutable_bit_parser$) {\n"
924 " $name$_ = new com.google.protobuf.LazyStringArrayList();\n"
925 " $set_mutable_bit_parser$;\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000926 "}\n");
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700927 if (CheckUtf8(descriptor_)) {
jieluo@google.com4de8f552014-07-18 00:47:59 +0000928 printer->Print(variables_,
929 "$name$_.add(s);\n");
930 } else {
931 printer->Print(variables_,
932 "$name$_.add(bs);\n");
933 }
liujisi@google.com33165fe2010-11-02 13:14:58 +0000934}
935
jieluo@google.com4de8f552014-07-18 00:47:59 +0000936void RepeatedImmutableStringFieldGenerator::
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000937GenerateParsingDoneCode(io::Printer* printer) const {
938 printer->Print(variables_,
939 "if ($get_mutable_bit_parser$) {\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000940 " $name$_ = $name$_.getUnmodifiableView();\n"
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000941 "}\n");
942}
943
jieluo@google.com4de8f552014-07-18 00:47:59 +0000944void RepeatedImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000945GenerateSerializationCode(io::Printer* printer) const {
Feng Xiaoe841bac2015-12-11 17:09:20 -0800946 printer->Print(variables_,
947 "for (int i = 0; i < $name$_.size(); i++) {\n"
948 " $writeString$(output, $number$, $name$_.getRaw(i));\n"
949 "}\n");
liujisi@google.com33165fe2010-11-02 13:14:58 +0000950}
951
jieluo@google.com4de8f552014-07-18 00:47:59 +0000952void RepeatedImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000953GenerateSerializedSizeCode(io::Printer* printer) const {
954 printer->Print(variables_,
955 "{\n"
956 " int dataSize = 0;\n");
957 printer->Indent();
958
959 printer->Print(variables_,
960 "for (int i = 0; i < $name$_.size(); i++) {\n"
Feng Xiaoeee38b02015-08-22 18:25:48 -0700961 " dataSize += computeStringSizeNoTag($name$_.getRaw(i));\n"
liujisi@google.com33165fe2010-11-02 13:14:58 +0000962 "}\n");
963
964 printer->Print(
965 "size += dataSize;\n");
966
Feng Xiaoe841bac2015-12-11 17:09:20 -0800967 printer->Print(variables_,
968 "size += $tag_size$ * get$capitalized_name$List().size();\n");
liujisi@google.com33165fe2010-11-02 13:14:58 +0000969
970 printer->Outdent();
971 printer->Print("}\n");
972}
973
jieluo@google.com4de8f552014-07-18 00:47:59 +0000974void RepeatedImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000975GenerateEqualsCode(io::Printer* printer) const {
976 printer->Print(variables_,
977 "result = result && get$capitalized_name$List()\n"
978 " .equals(other.get$capitalized_name$List());\n");
979}
980
jieluo@google.com4de8f552014-07-18 00:47:59 +0000981void RepeatedImmutableStringFieldGenerator::
liujisi@google.com33165fe2010-11-02 13:14:58 +0000982GenerateHashCode(io::Printer* printer) const {
983 printer->Print(variables_,
984 "if (get$capitalized_name$Count() > 0) {\n"
985 " hash = (37 * hash) + $constant_name$;\n"
986 " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n"
987 "}\n");
988}
989
jieluo@google.com4de8f552014-07-18 00:47:59 +0000990string RepeatedImmutableStringFieldGenerator::GetBoxedType() const {
liujisi@google.com33165fe2010-11-02 13:14:58 +0000991 return "String";
992}
993
994} // namespace java
995} // namespace compiler
996} // namespace protobuf
997} // namespace google