aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/compiler/javanano/javanano_enum_field.cc
diff options
context:
space:
mode:
authorGravatar Max Cai <maxtroy@google.com>2013-10-15 18:11:56 +0100
committerGravatar Max Cai <maxtroy@google.com>2013-10-15 18:11:56 +0100
commitcd3c68b255a1a644399445aafe564d96c5e1f149 (patch)
tree26ef309980d8bbcaa62b49454d665b3ec553d9fd /src/google/protobuf/compiler/javanano/javanano_enum_field.cc
parent80dcae757ef9329f04a81258c3a6dc6d4aa32383 (diff)
Fix repeated field merging semantics.
The public doc states that repeated fields are simply concatenated and doesn't impose a different semantics for packed fields. This CL fixes this for packed fields and adds tests covering all cases. Also fixed a bit of missed null-repeated-field treatments. Change-Id: Ie35277bb1a9f0b8171dc9d07b6adf9b9d3308de2
Diffstat (limited to 'src/google/protobuf/compiler/javanano/javanano_enum_field.cc')
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_enum_field.cc28
1 files changed, 18 insertions, 10 deletions
diff --git a/src/google/protobuf/compiler/javanano/javanano_enum_field.cc b/src/google/protobuf/compiler/javanano/javanano_enum_field.cc
index cdb3d097..fe67d76b 100644
--- a/src/google/protobuf/compiler/javanano/javanano_enum_field.cc
+++ b/src/google/protobuf/compiler/javanano/javanano_enum_field.cc
@@ -269,24 +269,32 @@ GenerateMergingCode(io::Printer* printer) const {
" arrayLength++;\n"
"}\n"
"input.rewindToPosition(startPos);\n"
- "this.$name$ = new $type$[arrayLength];\n"
- "for (int i = 0; i < arrayLength; i++) {\n"
- " this.$name$[i] = input.readInt32();\n"
+ "int i = this.$name$ == null ? 0 : this.$name$.length;\n"
+ "int[] newArray = new int[i + arrayLength];\n"
+ "if (i != 0) {\n"
+ " java.lang.System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
+ "}\n"
+ "for (; i < newArray.length; i++) {\n"
+ " newArray[i] = input.readInt32();\n"
"}\n"
+ "this.$name$ = newArray;\n"
"input.popLimit(limit);\n");
} else {
printer->Print(variables_,
- "int arrayLength = com.google.protobuf.nano.WireFormatNano.getRepeatedFieldArrayLength(input, $tag$);\n"
- "int i = this.$name$.length;\n"
+ "int arrayLength = com.google.protobuf.nano.WireFormatNano.\n"
+ " getRepeatedFieldArrayLength(input, $tag$);\n"
+ "int i = this.$name$ == null ? 0 : this.$name$.length;\n"
"int[] newArray = new int[i + arrayLength];\n"
- "System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
- "this.$name$ = newArray;\n"
- "for (; i < this.$name$.length - 1; i++) {\n"
- " this.$name$[i] = input.readInt32();\n"
+ "if (i != 0) {\n"
+ " java.lang.System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
+ "}\n"
+ "for (; i < newArray.length - 1; i++) {\n"
+ " newArray[i] = input.readInt32();\n"
" input.readTag();\n"
"}\n"
"// Last one without readTag.\n"
- "this.$name$[i] = input.readInt32();\n");
+ "newArray[i] = input.readInt32();\n"
+ "this.$name$ = newArray;\n");
}
}