diff options
Diffstat (limited to 'src/google/protobuf/compiler/javanano/javanano_primitive_field.cc')
-rw-r--r-- | src/google/protobuf/compiler/javanano/javanano_primitive_field.cc | 109 |
1 files changed, 96 insertions, 13 deletions
diff --git a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc index 79357e9..69664d3 100644 --- a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc +++ b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc @@ -260,6 +260,7 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, const Params param default_value = strings::Substitute( "com.google.protobuf.nano.InternalNano.bytesDefaultValue(\"$0\")", CEscape(descriptor->default_value_string())); + (*variables)["default_copy_if_needed"] = (*variables)["default"] + ".clone()"; } else { if (AllAscii(descriptor->default_value_string())) { // All chars are ASCII. In this case CEscape() works fine. @@ -269,8 +270,11 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, const Params param "com.google.protobuf.nano.InternalNano.stringDefaultValue(\"$0\")", CEscape(descriptor->default_value_string())); } + (*variables)["default_copy_if_needed"] = (*variables)["default"]; } (*variables)["default_constant_value"] = default_value; + } else { + (*variables)["default_copy_if_needed"] = (*variables)["default"]; } (*variables)["boxed_type"] = BoxedPrimitiveTypeName(GetJavaType(descriptor)); (*variables)["capitalized_type"] = GetCapitalizedType(descriptor); @@ -280,7 +284,7 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, const Params param if (IsReferenceType(GetJavaType(descriptor))) { (*variables)["null_check"] = " if (value == null) {\n" - " throw new NullPointerException();\n" + " throw new java.lang.NullPointerException();\n" " }\n"; } else { (*variables)["null_check"] = ""; @@ -310,17 +314,9 @@ GenerateMembers(io::Printer* printer) const { // Those primitive types that need a saved default. printer->Print(variables_, "private static final $type$ $default_constant$ = $default_constant_value$;\n"); - if (descriptor_->type() == FieldDescriptor::TYPE_BYTES) { - printer->Print(variables_, - "public $type$ $name$ = $default$.clone();\n"); - } else { - printer->Print(variables_, - "public $type$ $name$ = $default$;\n"); - } - } else { - printer->Print(variables_, - "public $type$ $name$ = $default$;\n"); } + printer->Print(variables_, + "public $type$ $name$ = $default_copy_if_needed$;\n"); if (params_.generate_has()) { printer->Print(variables_, @@ -329,7 +325,18 @@ GenerateMembers(io::Printer* printer) const { } void PrimitiveFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +GenerateClearCode(io::Printer* printer) const { + printer->Print(variables_, + "$name$ = $default_copy_if_needed$;\n"); + + if (params_.generate_has()) { + printer->Print(variables_, + "has$capitalized_name$ = false;\n"); + } +} + +void PrimitiveFieldGenerator:: +GenerateMergingCode(io::Printer* printer) const { printer->Print(variables_, "this.$name$ = input.read$capitalized_type$();\n"); @@ -397,6 +404,76 @@ string PrimitiveFieldGenerator::GetBoxedType() const { // =================================================================== +AccessorPrimitiveFieldGenerator:: +AccessorPrimitiveFieldGenerator(const FieldDescriptor* descriptor, + const Params& params, int has_bit_index) + : FieldGenerator(params), descriptor_(descriptor) { + SetPrimitiveVariables(descriptor, params, &variables_); + SetBitOperationVariables("has", has_bit_index, &variables_); +} + +AccessorPrimitiveFieldGenerator::~AccessorPrimitiveFieldGenerator() {} + +void AccessorPrimitiveFieldGenerator:: +GenerateMembers(io::Printer* printer) const { + if (variables_.find("default_constant_value") != variables_.end()) { + printer->Print(variables_, + "private static final $type$ $default_constant$ = $default_constant_value$;\n"); + } + printer->Print(variables_, + "private $type$ $name$_ = $default_copy_if_needed$;\n" + "public $type$ get$capitalized_name$() {\n" + " return $name$_;\n" + "}\n" + "public void set$capitalized_name$($type$ value) {\n" + "$null_check$" + " $name$_ = value;\n" + " $set_has$;\n" + "}\n" + "public boolean has$capitalized_name$() {\n" + " return $get_has$;\n" + "}\n" + "public void clear$capitalized_name$() {\n" + " $name$_ = $default_copy_if_needed$;\n" + " $clear_has$;\n" + "}\n"); +} + +void AccessorPrimitiveFieldGenerator:: +GenerateClearCode(io::Printer* printer) const { + printer->Print(variables_, + "$name$_ = $default_copy_if_needed$;\n"); +} + +void AccessorPrimitiveFieldGenerator:: +GenerateMergingCode(io::Printer* printer) const { + printer->Print(variables_, + "set$capitalized_name$(input.read$capitalized_type$());\n"); +} + +void AccessorPrimitiveFieldGenerator:: +GenerateSerializationCode(io::Printer* printer) const { + printer->Print(variables_, + "if (has$capitalized_name$()) {\n" + " output.write$capitalized_type$($number$, $name$_);\n" + "}\n"); +} + +void AccessorPrimitiveFieldGenerator:: +GenerateSerializedSizeCode(io::Printer* printer) const { + printer->Print(variables_, + "if (has$capitalized_name$()) {\n" + " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n" + " .compute$capitalized_type$Size($number$, $name$_);\n" + "}\n"); +} + +string AccessorPrimitiveFieldGenerator::GetBoxedType() const { + return BoxedPrimitiveTypeName(GetJavaType(descriptor_)); +} + +// =================================================================== + RepeatedPrimitiveFieldGenerator:: RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor, const Params& params) : FieldGenerator(params), descriptor_(descriptor) { @@ -412,7 +489,13 @@ GenerateMembers(io::Printer* printer) const { } void RepeatedPrimitiveFieldGenerator:: -GenerateParsingCode(io::Printer* printer) const { +GenerateClearCode(io::Printer* printer) const { + printer->Print(variables_, + "$name$ = $default$;\n"); +} + +void RepeatedPrimitiveFieldGenerator:: +GenerateMergingCode(io::Printer* printer) const { // First, figure out the length of the array, then parse. if (descriptor_->options().packed()) { printer->Print(variables_, |