aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/compiler/javanano/javanano_message_field.cc
diff options
context:
space:
mode:
authorGravatar Aurash Mahbod <aurash@google.com>2013-08-14 14:09:52 -0700
committerGravatar Max Cai <maxtroy@google.com>2013-11-05 18:17:06 +0000
commit3f0c348033ecde791622cf6102b433fd3879a6be (patch)
treeedfbd92f4c99bc88db7eb567d4a7adbc5d2b3ac1 /src/google/protobuf/compiler/javanano/javanano_message_field.cc
parent885f959b7726b227dcd351a5456e05928fb5819c (diff)
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.cc20
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 9f8298c7..d9abea36 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");
}