diff options
author | Aurash Mahbod <aurash@google.com> | 2013-08-14 14:09:52 -0700 |
---|---|---|
committer | Max Cai <maxtroy@google.com> | 2013-11-05 18:17:06 +0000 |
commit | beb57e08a44a140bf52235717f1f907ca857f360 (patch) | |
tree | 461e3f0b97a639536d0b3b34e748eeefc8890917 /src/google/protobuf/compiler/javanano/javanano_message_field.cc | |
parent | bb971d53626cb286f8dc491c15d2731001c4891b (diff) | |
download | external_protobuf-beb57e08a44a140bf52235717f1f907ca857f360.zip external_protobuf-beb57e08a44a140bf52235717f1f907ca857f360.tar.gz external_protobuf-beb57e08a44a140bf52235717f1f907ca857f360.tar.bz2 |
Allow for ref-type arrays containing null elements.
Strip the null elements out before serializing the array.
This is helpful in the cases where the user wants to construct
an array of an inexact size for serialization. For example:
User constructs array of size 5 because they anticipate adding
more than 1 element before serialization. Only 3 get added, so
the array looks like [Obj, Obj, Obj, null, null]. This would
curently crash without this CL.
All repeated fields of ref-type elements can contain null
elements: repeated strings, repeated bytes, and repeated
messages/groups.
Change-Id: I117391c868c9a436536d70d6151780e9cc7e8227
Conflicts:
src/google/protobuf/compiler/javanano/javanano_message_field.cc
Diffstat (limited to 'src/google/protobuf/compiler/javanano/javanano_message_field.cc')
-rw-r--r-- | src/google/protobuf/compiler/javanano/javanano_message_field.cc | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/src/google/protobuf/compiler/javanano/javanano_message_field.cc b/src/google/protobuf/compiler/javanano/javanano_message_field.cc index 9f8298c..d9abea3 100644 --- a/src/google/protobuf/compiler/javanano/javanano_message_field.cc +++ b/src/google/protobuf/compiler/javanano/javanano_message_field.cc @@ -304,9 +304,12 @@ GenerateMergingCode(io::Printer* printer) const { void RepeatedMessageFieldGenerator:: GenerateSerializationCode(io::Printer* printer) const { printer->Print(variables_, - "if (this.$name$ != null) {\n" - " for ($type$ element : this.$name$) {\n" - " output.write$group_or_message$($number$, element);\n" + "if (this.$name$ != null && this.$name$.length > 0) {\n" + " for (int i = 0; i < this.$name$.length; i++) {\n" + " $type$ element = this.$name$[i];\n" + " if (element != null) {\n" + " output.write$group_or_message$($number$, element);\n" + " }\n" " }\n" "}\n"); } @@ -314,10 +317,13 @@ GenerateSerializationCode(io::Printer* printer) const { void RepeatedMessageFieldGenerator:: GenerateSerializedSizeCode(io::Printer* printer) const { printer->Print(variables_, - "if (this.$name$ != null) {\n" - " for ($type$ element : this.$name$) {\n" - " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n" - " .compute$group_or_message$Size($number$, element);\n" + "if (this.$name$ != null && this.$name$.length > 0) {\n" + " for (int i = 0; i < this.$name$.length; i++) {\n" + " $type$ element = this.$name$[i];\n" + " if (element != null) {\n" + " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n" + " .compute$group_or_message$Size($number$, element);\n" + " }\n" " }\n" "}\n"); } |