diff options
author | Andrew Flynn <flynn@google.com> | 2013-12-04 13:10:07 -0800 |
---|---|---|
committer | Andrew Flynn <flynn@google.com> | 2013-12-09 11:40:03 -0800 |
commit | 102cfe65b9b9e2b458c3d0ec69d47e8fc40dc726 (patch) | |
tree | 196b5f09c23bb1dddc242ef002106b78afdaff1a /src/google/protobuf/compiler/javanano | |
parent | c4c4a562672068fb89ea4e65408dd53de2ed8f1f (diff) | |
download | external_protobuf-102cfe65b9b9e2b458c3d0ec69d47e8fc40dc726.zip external_protobuf-102cfe65b9b9e2b458c3d0ec69d47e8fc40dc726.tar.gz external_protobuf-102cfe65b9b9e2b458c3d0ec69d47e8fc40dc726.tar.bz2 |
Nano: don't generate accessor methods for nested methods
For nested message objects, don't generate accessor methods because they have
a default value that is not a valid value (null), so there is no reason to have
get/set/has/clear methods for them. Clients and protos (while serializing) can
check against the invalid value to see if it's been set.
Change-Id: Ic63400889581271b8cbcd9c45c84519d4921fd4b
Diffstat (limited to 'src/google/protobuf/compiler/javanano')
3 files changed, 6 insertions, 119 deletions
diff --git a/src/google/protobuf/compiler/javanano/javanano_field.cc b/src/google/protobuf/compiler/javanano/javanano_field.cc index 62e133e..2581669 100644 --- a/src/google/protobuf/compiler/javanano/javanano_field.cc +++ b/src/google/protobuf/compiler/javanano/javanano_field.cc @@ -78,8 +78,9 @@ FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor, const Params FieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field, const Params ¶ms, int* next_has_bit_index) { + JavaType java_type = GetJavaType(field); if (field->is_repeated()) { - switch (GetJavaType(field)) { + switch (java_type) { case JAVATYPE_MESSAGE: return new RepeatedMessageFieldGenerator(field, params); case JAVATYPE_ENUM: @@ -87,14 +88,13 @@ FieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field, default: return new RepeatedPrimitiveFieldGenerator(field, params); } - } else if (params.optional_field_accessors() && field->is_optional()) { + } else if (params.optional_field_accessors() && field->is_optional() + && java_type != JAVATYPE_MESSAGE) { // We need a has-bit for each primitive/enum field because their default // values could be same as explicitly set values. But we don't need it // for a message field because they have no defaults and Nano uses 'null' // for unset messages, which cannot be set explicitly. - switch (GetJavaType(field)) { - case JAVATYPE_MESSAGE: - return new AccessorMessageFieldGenerator(field, params); + switch (java_type) { case JAVATYPE_ENUM: return new AccessorEnumFieldGenerator( field, params, (*next_has_bit_index)++); @@ -103,7 +103,7 @@ FieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field, field, params, (*next_has_bit_index)++); } } else { - switch (GetJavaType(field)) { + switch (java_type) { case JAVATYPE_MESSAGE: return new MessageFieldGenerator(field, params); case JAVATYPE_ENUM: diff --git a/src/google/protobuf/compiler/javanano/javanano_message_field.cc b/src/google/protobuf/compiler/javanano/javanano_message_field.cc index d9abea3..74d3f85 100644 --- a/src/google/protobuf/compiler/javanano/javanano_message_field.cc +++ b/src/google/protobuf/compiler/javanano/javanano_message_field.cc @@ -149,97 +149,6 @@ GenerateHashCodeCode(io::Printer* printer) const { // =================================================================== -AccessorMessageFieldGenerator:: -AccessorMessageFieldGenerator( - const FieldDescriptor* descriptor, const Params& params) - : FieldGenerator(params), descriptor_(descriptor) { - SetMessageVariables(params, descriptor, &variables_); -} - -AccessorMessageFieldGenerator::~AccessorMessageFieldGenerator() {} - -void AccessorMessageFieldGenerator:: -GenerateMembers(io::Printer* printer) const { - printer->Print(variables_, - "private $type$ $name$_;\n" - "public $type$ get$capitalized_name$() {\n" - " return $name$_;\n" - "}\n" - "public $message_name$ set$capitalized_name$($type$ value) {\n" - " if (value == null) {\n" - " throw new java.lang.NullPointerException();\n" - " }\n" - " $name$_ = value;\n" - " return this;\n" - "}\n" - "public boolean has$capitalized_name$() {\n" - " return $name$_ != null;\n" - "}\n" - "public $message_name$ clear$capitalized_name$() {\n" - " $name$_ = null;\n" - " return this;" - "}\n"); -} - -void AccessorMessageFieldGenerator:: -GenerateClearCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_ = null;\n"); -} - -void AccessorMessageFieldGenerator:: -GenerateMergingCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($name$_ == null) {\n" - " $name$_ = new $type$();\n" - "}\n"); - - if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) { - printer->Print(variables_, - "input.readGroup($name$_, $number$);\n"); - } else { - printer->Print(variables_, - "input.readMessage($name$_);\n"); - } -} - -void AccessorMessageFieldGenerator:: -GenerateSerializationCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($name$_ != null) {\n" - " output.write$group_or_message$($number$, $name$_);\n" - "}\n"); -} - -void AccessorMessageFieldGenerator:: -GenerateSerializedSizeCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($name$_ != null) {\n" - " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n" - " .compute$group_or_message$Size($number$, $name$_);\n" - "}\n"); -} - -void AccessorMessageFieldGenerator:: -GenerateEqualsCode(io::Printer* printer) const { - printer->Print(variables_, - "if ($name$_ == null) {\n" - " if (other.$name$_ != null) {\n" - " return false;\n" - " }\n" - "} else if (!$name$_.equals(other.$name$_)) {\n" - " return false;\n" - "}\n"); -} - -void AccessorMessageFieldGenerator:: -GenerateHashCodeCode(io::Printer* printer) const { - printer->Print(variables_, - "result = 31 * result + ($name$_ == null ? 0 : $name$_.hashCode());\n"); -} - -// =================================================================== - RepeatedMessageFieldGenerator:: RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor, const Params& params) : FieldGenerator(params), descriptor_(descriptor) { diff --git a/src/google/protobuf/compiler/javanano/javanano_message_field.h b/src/google/protobuf/compiler/javanano/javanano_message_field.h index b724351..55e2610 100644 --- a/src/google/protobuf/compiler/javanano/javanano_message_field.h +++ b/src/google/protobuf/compiler/javanano/javanano_message_field.h @@ -65,28 +65,6 @@ class MessageFieldGenerator : public FieldGenerator { GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageFieldGenerator); }; -class AccessorMessageFieldGenerator : public FieldGenerator { - public: - explicit AccessorMessageFieldGenerator( - const FieldDescriptor* descriptor, const Params& params); - ~AccessorMessageFieldGenerator(); - - // implements FieldGenerator --------------------------------------- - void GenerateMembers(io::Printer* printer) const; - void GenerateClearCode(io::Printer* printer) const; - void GenerateMergingCode(io::Printer* printer) const; - void GenerateSerializationCode(io::Printer* printer) const; - void GenerateSerializedSizeCode(io::Printer* printer) const; - void GenerateEqualsCode(io::Printer* printer) const; - void GenerateHashCodeCode(io::Printer* printer) const; - - private: - const FieldDescriptor* descriptor_; - map<string, string> variables_; - - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(AccessorMessageFieldGenerator); -}; - class RepeatedMessageFieldGenerator : public FieldGenerator { public: explicit RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor, |