blob: a93ff4340eb019e4899f896550e895fcc2438250 [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.
3// http://code.google.com/p/protobuf/
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 <map>
37#include <string>
38
39#include <google/protobuf/compiler/java/java_string_field.h>
40#include <google/protobuf/stubs/common.h>
41#include <google/protobuf/compiler/java/java_helpers.h>
42#include <google/protobuf/io/printer.h>
43#include <google/protobuf/wire_format.h>
44#include <google/protobuf/stubs/strutil.h>
45
46namespace google {
47namespace protobuf {
48namespace compiler {
49namespace java {
50
51using internal::WireFormat;
52using internal::WireFormatLite;
53
54namespace {
55
56void SetPrimitiveVariables(const FieldDescriptor* descriptor,
57 int messageBitIndex,
58 int builderBitIndex,
59 map<string, string>* variables) {
60 (*variables)["name"] =
61 UnderscoresToCamelCase(descriptor);
62 (*variables)["capitalized_name"] =
63 UnderscoresToCapitalizedCamelCase(descriptor);
64 (*variables)["constant_name"] = FieldConstantName(descriptor);
65 (*variables)["number"] = SimpleItoa(descriptor->number());
66 (*variables)["empty_list"] = "com.google.protobuf.LazyStringArrayList.EMPTY";
67
68 (*variables)["default"] = DefaultValue(descriptor);
69 (*variables)["default_init"] = ("= " + DefaultValue(descriptor));
70 (*variables)["capitalized_type"] = "String";
71 (*variables)["tag"] = SimpleItoa(WireFormat::MakeTag(descriptor));
72 (*variables)["tag_size"] = SimpleItoa(
73 WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
74 (*variables)["null_check"] =
75 " if (value == null) {\n"
76 " throw new NullPointerException();\n"
77 " }\n";
78
79 // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
80 // by the proto compiler
81 (*variables)["deprecation"] = descriptor->options().deprecated()
82 ? "@java.lang.Deprecated " : "";
83 (*variables)["on_changed"] =
84 HasDescriptorMethods(descriptor->containing_type()) ? "onChanged();" : "";
85
86 // For singular messages and builders, one bit is used for the hasField bit.
87 (*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex);
88
89 (*variables)["get_has_field_bit_builder"] = GenerateGetBit(builderBitIndex);
90 (*variables)["set_has_field_bit_builder"] = GenerateSetBit(builderBitIndex);
91 (*variables)["clear_has_field_bit_builder"] =
92 GenerateClearBit(builderBitIndex);
93
94 // For repated builders, one bit is used for whether the array is immutable.
95 (*variables)["get_mutable_bit_builder"] = GenerateGetBit(builderBitIndex);
96 (*variables)["set_mutable_bit_builder"] = GenerateSetBit(builderBitIndex);
97 (*variables)["clear_mutable_bit_builder"] = GenerateClearBit(builderBitIndex);
98
99 (*variables)["get_has_field_bit_from_local"] =
100 GenerateGetBitFromLocal(builderBitIndex);
101 (*variables)["set_has_field_bit_to_local"] =
102 GenerateSetBitToLocal(messageBitIndex);
103}
104
105} // namespace
106
107// ===================================================================
108
109StringFieldGenerator::
110StringFieldGenerator(const FieldDescriptor* descriptor,
111 int messageBitIndex,
112 int builderBitIndex)
113 : descriptor_(descriptor), messageBitIndex_(messageBitIndex),
114 builderBitIndex_(builderBitIndex) {
115 SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex,
116 &variables_);
117}
118
119StringFieldGenerator::~StringFieldGenerator() {}
120
121int StringFieldGenerator::GetNumBitsForMessage() const {
122 return 1;
123}
124
125int StringFieldGenerator::GetNumBitsForBuilder() const {
126 return 1;
127}
128
129// A note about how strings are handled. This code used to just store a String
130// in the Message. This had two issues:
131//
132// 1. It wouldn't roundtrip byte arrays that were not vaid UTF-8 encoded
133// strings, but rather fields that were raw bytes incorrectly marked
134// as strings in the proto file. This is common because in the proto1
135// syntax, string was the way to indicate bytes and C++ engineers can
136// easily make this mistake without affecting the C++ API. By converting to
137// strings immediately, some java code might corrupt these byte arrays as
138// it passes through a java server even if the field was never accessed by
139// application code.
140//
141// 2. There's a performance hit to converting between bytes and strings and
142// it many cases, the field is never even read by the application code. This
143// avoids unnecessary conversions in the common use cases.
144//
145// So now, the field for String is maintained as an Object reference which can
146// either store a String or a ByteString. The code uses an instanceof check
147// to see which one it has and converts to the other one if needed. It remembers
148// the last value requested (in a thread safe manner) as this is most likely
149// the one needed next. The thread safety is such that if two threads both
150// convert the field because the changes made by each thread were not visible to
151// the other, they may cause a conversion to happen more times than would
152// otherwise be necessary. This was deemed better than adding synchronization
153// overhead. It will not cause any corruption issues or affect the behavior of
154// the API. The instanceof check is also highly optimized in the JVM and we
155// decided it was better to reduce the memory overhead by not having two
156// separate fields but rather use dynamic type checking.
157//
158// For single fields, the logic for this is done inside the generated code. For
159// repeated fields, the logic is done in LazyStringArrayList and
160// UnmodifiableLazyStringList.
161void StringFieldGenerator::
162GenerateInterfaceMembers(io::Printer* printer) const {
163 printer->Print(variables_,
164 "$deprecation$boolean has$capitalized_name$();\n"
165 "$deprecation$String get$capitalized_name$();\n");
166}
167
168void StringFieldGenerator::
169GenerateMembers(io::Printer* printer) const {
170 printer->Print(variables_,
171 "private Object $name$_;\n"
172 "$deprecation$public boolean has$capitalized_name$() {\n"
173 " return $get_has_field_bit_message$;\n"
174 "}\n");
175
176 printer->Print(variables_,
177 "$deprecation$public String get$capitalized_name$() {\n"
178 " Object ref = $name$_;\n"
179 " if (ref instanceof String) {\n"
180 " return (String) ref;\n"
181 " } else {\n"
182 " com.google.protobuf.ByteString bs = \n"
183 " (com.google.protobuf.ByteString) ref;\n"
184 " String s = bs.toStringUtf8();\n"
185 " if (com.google.protobuf.Internal.isValidUtf8(bs)) {\n"
186 " $name$_ = s;\n"
187 " }\n"
188 " return s;\n"
189 " }\n"
190 "}\n"
191 "private com.google.protobuf.ByteString get$capitalized_name$Bytes() {\n"
192 " Object ref = $name$_;\n"
193 " if (ref instanceof String) {\n"
194 " com.google.protobuf.ByteString b = \n"
195 " com.google.protobuf.ByteString.copyFromUtf8((String) ref);\n"
196 " $name$_ = b;\n"
197 " return b;\n"
198 " } else {\n"
199 " return (com.google.protobuf.ByteString) ref;\n"
200 " }\n"
201 "}\n");
202}
203
204void StringFieldGenerator::
205GenerateBuilderMembers(io::Printer* printer) const {
206 printer->Print(variables_,
207 "private Object $name$_ $default_init$;\n"
208 "$deprecation$public boolean has$capitalized_name$() {\n"
209 " return $get_has_field_bit_builder$;\n"
210 "}\n");
211
212 printer->Print(variables_,
213 "$deprecation$public String get$capitalized_name$() {\n"
214 " Object ref = $name$_;\n"
215 " if (!(ref instanceof String)) {\n"
216 " String s = ((com.google.protobuf.ByteString) ref).toStringUtf8();\n"
217 " $name$_ = s;\n"
218 " return s;\n"
219 " } else {\n"
220 " return (String) ref;\n"
221 " }\n"
222 "}\n");
223
224 printer->Print(variables_,
225 "$deprecation$public Builder set$capitalized_name$(String value) {\n"
226 "$null_check$"
227 " $set_has_field_bit_builder$;\n"
228 " $name$_ = value;\n"
229 " $on_changed$\n"
230 " return this;\n"
231 "}\n"
232 "$deprecation$public Builder clear$capitalized_name$() {\n"
233 " $clear_has_field_bit_builder$;\n");
234 // The default value is not a simple literal so we want to avoid executing
235 // it multiple times. Instead, get the default out of the default instance.
236 printer->Print(variables_,
237 " $name$_ = getDefaultInstance().get$capitalized_name$();\n");
238 printer->Print(variables_,
239 " $on_changed$\n"
240 " return this;\n"
241 "}\n");
242
243 printer->Print(variables_,
244 "void set$capitalized_name$(com.google.protobuf.ByteString value) {\n"
245 " $set_has_field_bit_builder$;\n"
246 " $name$_ = value;\n"
247 " $on_changed$\n"
248 "}\n");
249}
250
251void StringFieldGenerator::
252GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
253 // noop for primitives
254}
255
256void StringFieldGenerator::
257GenerateInitializationCode(io::Printer* printer) const {
258 printer->Print(variables_, "$name$_ = $default$;\n");
259}
260
261void StringFieldGenerator::
262GenerateBuilderClearCode(io::Printer* printer) const {
263 printer->Print(variables_,
264 "$name$_ = $default$;\n"
265 "$clear_has_field_bit_builder$;\n");
266}
267
268void StringFieldGenerator::
269GenerateMergingCode(io::Printer* printer) const {
270 printer->Print(variables_,
271 "if (other.has$capitalized_name$()) {\n"
272 " set$capitalized_name$(other.get$capitalized_name$());\n"
273 "}\n");
274}
275
276void StringFieldGenerator::
277GenerateBuildingCode(io::Printer* printer) const {
278 printer->Print(variables_,
279 "if ($get_has_field_bit_from_local$) {\n"
280 " $set_has_field_bit_to_local$;\n"
281 "}\n"
282 "result.$name$_ = $name$_;\n");
283}
284
285void StringFieldGenerator::
286GenerateParsingCode(io::Printer* printer) const {
287 printer->Print(variables_,
288 "$set_has_field_bit_builder$;\n"
289 "$name$_ = input.readBytes();\n");
290}
291
292void StringFieldGenerator::
293GenerateSerializationCode(io::Printer* printer) const {
294 printer->Print(variables_,
295 "if ($get_has_field_bit_message$) {\n"
296 " output.writeBytes($number$, get$capitalized_name$Bytes());\n"
297 "}\n");
298}
299
300void StringFieldGenerator::
301GenerateSerializedSizeCode(io::Printer* printer) const {
302 printer->Print(variables_,
303 "if ($get_has_field_bit_message$) {\n"
304 " size += com.google.protobuf.CodedOutputStream\n"
305 " .computeBytesSize($number$, get$capitalized_name$Bytes());\n"
306 "}\n");
307}
308
309void StringFieldGenerator::
310GenerateEqualsCode(io::Printer* printer) const {
311 printer->Print(variables_,
312 "result = result && get$capitalized_name$()\n"
313 " .equals(other.get$capitalized_name$());\n");
314}
315
316void StringFieldGenerator::
317GenerateHashCode(io::Printer* printer) const {
318 printer->Print(variables_,
319 "hash = (37 * hash) + $constant_name$;\n");
320 printer->Print(variables_,
321 "hash = (53 * hash) + get$capitalized_name$().hashCode();\n");
322}
323
324string StringFieldGenerator::GetBoxedType() const {
325 return "String";
326}
327
328
329// ===================================================================
330
331RepeatedStringFieldGenerator::
332RepeatedStringFieldGenerator(const FieldDescriptor* descriptor,
333 int messageBitIndex,
334 int builderBitIndex)
335 : descriptor_(descriptor), messageBitIndex_(messageBitIndex),
336 builderBitIndex_(builderBitIndex) {
337 SetPrimitiveVariables(descriptor, messageBitIndex, builderBitIndex,
338 &variables_);
339}
340
341RepeatedStringFieldGenerator::~RepeatedStringFieldGenerator() {}
342
343int RepeatedStringFieldGenerator::GetNumBitsForMessage() const {
344 return 0;
345}
346
347int RepeatedStringFieldGenerator::GetNumBitsForBuilder() const {
348 return 1;
349}
350
351void RepeatedStringFieldGenerator::
352GenerateInterfaceMembers(io::Printer* printer) const {
353 printer->Print(variables_,
354 "$deprecation$java.util.List<String> get$capitalized_name$List();\n"
355 "$deprecation$int get$capitalized_name$Count();\n"
356 "$deprecation$String get$capitalized_name$(int index);\n");
357}
358
359
360void RepeatedStringFieldGenerator::
361GenerateMembers(io::Printer* printer) const {
362 printer->Print(variables_,
363 "private com.google.protobuf.LazyStringList $name$_;\n"
364 "$deprecation$public java.util.List<String>\n"
365 " get$capitalized_name$List() {\n"
366 " return $name$_;\n" // note: unmodifiable list
367 "}\n"
368 "$deprecation$public int get$capitalized_name$Count() {\n"
369 " return $name$_.size();\n"
370 "}\n"
371 "$deprecation$public String get$capitalized_name$(int index) {\n"
372 " return $name$_.get(index);\n"
373 "}\n");
374
375 if (descriptor_->options().packed() &&
376 HasGeneratedMethods(descriptor_->containing_type())) {
377 printer->Print(variables_,
378 "private int $name$MemoizedSerializedSize = -1;\n");
379 }
380}
381
382void RepeatedStringFieldGenerator::
383GenerateBuilderMembers(io::Printer* printer) const {
384 // One field is the list and the bit field keeps track of whether the
385 // list is immutable. If it's immutable, the invariant is that it must
386 // either an instance of Collections.emptyList() or it's an ArrayList
387 // wrapped in a Collections.unmodifiableList() wrapper and nobody else has
388 // a refererence to the underlying ArrayList. This invariant allows us to
389 // share instances of lists between protocol buffers avoiding expensive
390 // memory allocations. Note, immutable is a strong guarantee here -- not
391 // just that the list cannot be modified via the reference but that the
392 // list can never be modified.
393 printer->Print(variables_,
394 "private com.google.protobuf.LazyStringList $name$_ = $empty_list$;\n");
395
396 printer->Print(variables_,
397 "private void ensure$capitalized_name$IsMutable() {\n"
398 " if (!$get_mutable_bit_builder$) {\n"
399 " $name$_ = new com.google.protobuf.LazyStringArrayList($name$_);\n"
400 " $set_mutable_bit_builder$;\n"
401 " }\n"
402 "}\n");
403
404 // Note: We return an unmodifiable list because otherwise the caller
405 // could hold on to the returned list and modify it after the message
406 // has been built, thus mutating the message which is supposed to be
407 // immutable.
408 printer->Print(variables_,
409 "$deprecation$public java.util.List<String>\n"
410 " get$capitalized_name$List() {\n"
411 " return java.util.Collections.unmodifiableList($name$_);\n"
412 "}\n"
413 "$deprecation$public int get$capitalized_name$Count() {\n"
414 " return $name$_.size();\n"
415 "}\n"
416 "$deprecation$public String get$capitalized_name$(int index) {\n"
417 " return $name$_.get(index);\n"
418 "}\n"
419 "$deprecation$public Builder set$capitalized_name$(\n"
420 " int index, String value) {\n"
421 "$null_check$"
422 " ensure$capitalized_name$IsMutable();\n"
423 " $name$_.set(index, value);\n"
424 " $on_changed$\n"
425 " return this;\n"
426 "}\n"
427 "$deprecation$public Builder add$capitalized_name$(String value) {\n"
428 "$null_check$"
429 " ensure$capitalized_name$IsMutable();\n"
430 " $name$_.add(value);\n"
431 " $on_changed$\n"
432 " return this;\n"
433 "}\n"
434 "$deprecation$public Builder addAll$capitalized_name$(\n"
435 " java.lang.Iterable<String> values) {\n"
436 " ensure$capitalized_name$IsMutable();\n"
437 " super.addAll(values, $name$_);\n"
438 " $on_changed$\n"
439 " return this;\n"
440 "}\n"
441 "$deprecation$public Builder clear$capitalized_name$() {\n"
442 " $name$_ = $empty_list$;\n"
443 " $clear_mutable_bit_builder$;\n"
444 " $on_changed$\n"
445 " return this;\n"
446 "}\n");
447
448 printer->Print(variables_,
449 "void add$capitalized_name$(com.google.protobuf.ByteString value) {\n"
450 " ensure$capitalized_name$IsMutable();\n"
451 " $name$_.add(value);\n"
452 " $on_changed$\n"
453 "}\n");
454}
455
456void RepeatedStringFieldGenerator::
457GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
458 // noop for primitives
459}
460
461void RepeatedStringFieldGenerator::
462GenerateInitializationCode(io::Printer* printer) const {
463 printer->Print(variables_, "$name$_ = $empty_list$;\n");
464}
465
466void RepeatedStringFieldGenerator::
467GenerateBuilderClearCode(io::Printer* printer) const {
468 printer->Print(variables_,
469 "$name$_ = $empty_list$;\n"
470 "$clear_mutable_bit_builder$;\n");
471}
472
473void RepeatedStringFieldGenerator::
474GenerateMergingCode(io::Printer* printer) const {
475 // The code below does two optimizations:
476 // 1. If the other list is empty, there's nothing to do. This ensures we
477 // don't allocate a new array if we already have an immutable one.
478 // 2. If the other list is non-empty and our current list is empty, we can
479 // reuse the other list which is guaranteed to be immutable.
480 printer->Print(variables_,
481 "if (!other.$name$_.isEmpty()) {\n"
482 " if ($name$_.isEmpty()) {\n"
483 " $name$_ = other.$name$_;\n"
484 " $clear_mutable_bit_builder$;\n"
485 " } else {\n"
486 " ensure$capitalized_name$IsMutable();\n"
487 " $name$_.addAll(other.$name$_);\n"
488 " }\n"
489 " $on_changed$\n"
490 "}\n");
491}
492
493void RepeatedStringFieldGenerator::
494GenerateBuildingCode(io::Printer* printer) const {
495 // The code below ensures that the result has an immutable list. If our
496 // list is immutable, we can just reuse it. If not, we make it immutable.
497
498 printer->Print(variables_,
499 "if ($get_mutable_bit_builder$) {\n"
500 " $name$_ = new com.google.protobuf.UnmodifiableLazyStringList(\n"
501 " $name$_);\n"
502 " $clear_mutable_bit_builder$;\n"
503 "}\n"
504 "result.$name$_ = $name$_;\n");
505}
506
507void RepeatedStringFieldGenerator::
508GenerateParsingCode(io::Printer* printer) const {
509 printer->Print(variables_,
510 "ensure$capitalized_name$IsMutable();\n"
511 "$name$_.add(input.readBytes());\n");
512}
513
514void RepeatedStringFieldGenerator::
515GenerateParsingCodeFromPacked(io::Printer* printer) const {
516 printer->Print(variables_,
517 "int length = input.readRawVarint32();\n"
518 "int limit = input.pushLimit(length);\n"
519 "while (input.getBytesUntilLimit() > 0) {\n"
520 " add$capitalized_name$(input.read$capitalized_type$());\n"
521 "}\n"
522 "input.popLimit(limit);\n");
523}
524
525void RepeatedStringFieldGenerator::
526GenerateSerializationCode(io::Printer* printer) const {
527 if (descriptor_->options().packed()) {
528 printer->Print(variables_,
529 "if (get$capitalized_name$List().size() > 0) {\n"
530 " output.writeRawVarint32($tag$);\n"
531 " output.writeRawVarint32($name$MemoizedSerializedSize);\n"
532 "}\n"
533 "for (int i = 0; i < $name$_.size(); i++) {\n"
534 " output.write$capitalized_type$NoTag($name$_.get(i));\n"
535 "}\n");
536 } else {
537 printer->Print(variables_,
538 "for (int i = 0; i < $name$_.size(); i++) {\n"
539 " output.writeBytes($number$, $name$_.getByteString(i));\n"
540 "}\n");
541 }
542}
543
544void RepeatedStringFieldGenerator::
545GenerateSerializedSizeCode(io::Printer* printer) const {
546 printer->Print(variables_,
547 "{\n"
548 " int dataSize = 0;\n");
549 printer->Indent();
550
551 printer->Print(variables_,
552 "for (int i = 0; i < $name$_.size(); i++) {\n"
553 " dataSize += com.google.protobuf.CodedOutputStream\n"
554 " .computeBytesSizeNoTag($name$_.getByteString(i));\n"
555 "}\n");
556
557 printer->Print(
558 "size += dataSize;\n");
559
560 if (descriptor_->options().packed()) {
561 printer->Print(variables_,
562 "if (!get$capitalized_name$List().isEmpty()) {\n"
563 " size += $tag_size$;\n"
564 " size += com.google.protobuf.CodedOutputStream\n"
565 " .computeInt32SizeNoTag(dataSize);\n"
566 "}\n");
567 } else {
568 printer->Print(variables_,
569 "size += $tag_size$ * get$capitalized_name$List().size();\n");
570 }
571
572 // cache the data size for packed fields.
573 if (descriptor_->options().packed()) {
574 printer->Print(variables_,
575 "$name$MemoizedSerializedSize = dataSize;\n");
576 }
577
578 printer->Outdent();
579 printer->Print("}\n");
580}
581
582void RepeatedStringFieldGenerator::
583GenerateEqualsCode(io::Printer* printer) const {
584 printer->Print(variables_,
585 "result = result && get$capitalized_name$List()\n"
586 " .equals(other.get$capitalized_name$List());\n");
587}
588
589void RepeatedStringFieldGenerator::
590GenerateHashCode(io::Printer* printer) const {
591 printer->Print(variables_,
592 "if (get$capitalized_name$Count() > 0) {\n"
593 " hash = (37 * hash) + $constant_name$;\n"
594 " hash = (53 * hash) + get$capitalized_name$List().hashCode();\n"
595 "}\n");
596}
597
598string RepeatedStringFieldGenerator::GetBoxedType() const {
599 return "String";
600}
601
602} // namespace java
603} // namespace compiler
604} // namespace protobuf
605} // namespace google