diff options
Diffstat (limited to 'src/google/protobuf/compiler/javanano')
3 files changed, 48 insertions, 33 deletions
diff --git a/src/google/protobuf/compiler/javanano/javanano_enum_field.cc b/src/google/protobuf/compiler/javanano/javanano_enum_field.cc index cdb3d09..fe67d76 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"); } } diff --git a/src/google/protobuf/compiler/javanano/javanano_message_field.cc b/src/google/protobuf/compiler/javanano/javanano_message_field.cc index 04f1c14..027428f 100644 --- a/src/google/protobuf/compiler/javanano/javanano_message_field.cc +++ b/src/google/protobuf/compiler/javanano/javanano_message_field.cc @@ -235,34 +235,36 @@ GenerateMergingCode(io::Printer* printer) const { " .getRepeatedFieldArrayLength(input, $tag$);\n" "int i = this.$name$ == null ? 0 : this.$name$.length;\n" "$type$[] newArray = new $type$[i + arrayLength];\n" - "if (this.$name$ != null) {\n" - " System.arraycopy(this.$name$, 0, newArray, 0, i);\n" + "if (i != 0) {\n" + " java.lang.System.arraycopy(this.$name$, 0, newArray, 0, i);\n" "}\n" - "this.$name$ = newArray;\n" - "for (; i < this.$name$.length - 1; i++) {\n" - " this.$name$[i] = new $type$();\n"); + "for (; i < newArray.length - 1; i++) {\n" + " newArray[i] = new $type$();\n"); if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) { printer->Print(variables_, - " input.readGroup(this.$name$[i], $number$);\n"); + " input.readGroup(newArray[i], $number$);\n"); } else { printer->Print(variables_, - " input.readMessage(this.$name$[i]);\n"); + " input.readMessage(newArray[i]);\n"); } printer->Print(variables_, " input.readTag();\n" "}\n" "// Last one without readTag.\n" - "this.$name$[i] = new $type$();\n"); + "newArray[i] = new $type$();\n"); if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) { printer->Print(variables_, - "input.readGroup(this.$name$[i], $number$);\n"); + "input.readGroup(newArray[i], $number$);\n"); } else { printer->Print(variables_, - "input.readMessage(this.$name$[i]);\n"); + "input.readMessage(newArray[i]);\n"); } + + printer->Print(variables_, + "this.$name$ = newArray;\n"); } void RepeatedMessageFieldGenerator:: diff --git a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc index 8097be8..0562a6e 100644 --- a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc +++ b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc @@ -519,35 +519,40 @@ 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.read$capitalized_type$();\n" + "int i = this.$name$ == null ? 0 : this.$name$.length;\n" + "$type$[] newArray = new $type$[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.read$capitalized_type$();\n" + "}\n" + "this.$name$ = newArray;\n" "input.popLimit(limit);\n"); } else { printer->Print(variables_, "int arrayLength = com.google.protobuf.nano.WireFormatNano\n" " .getRepeatedFieldArrayLength(input, $tag$);\n" - "int i = this.$name$.length;\n"); + "int i = this.$name$ == null ? 0 : this.$name$.length;\n"); if (GetJavaType(descriptor_) == JAVATYPE_BYTES) { printer->Print(variables_, - "byte[][] newArray = new byte[i + arrayLength][];\n" - "System.arraycopy(this.$name$, 0, newArray, 0, i);\n" - "this.$name$ = newArray;\n"); + "byte[][] newArray = new byte[i + arrayLength][];\n"); } else { printer->Print(variables_, - "$type$[] newArray = new $type$[i + arrayLength];\n" - "System.arraycopy(this.$name$, 0, newArray, 0, i);\n" - "this.$name$ = newArray;\n"); + "$type$[] newArray = new $type$[i + arrayLength];\n"); } printer->Print(variables_, - "for (; i < this.$name$.length - 1; i++) {\n" - " this.$name$[i] = input.read$capitalized_type$();\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.read$capitalized_type$();\n" " input.readTag();\n" "}\n" "// Last one without readTag.\n" - "this.$name$[i] = input.read$capitalized_type$();\n"); + "newArray[i] = input.read$capitalized_type$();\n" + "this.$name$ = newArray;\n"); } } |