aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMax Cai <maxtroy@google.com>2013-10-16 16:26:50 +0100
committerMax Cai <maxtroy@google.com>2013-10-16 16:28:03 +0100
commitb15a1b379f896e371ca3375bdf51dd4a9a0268af (patch)
tree5efc9c514a88a8f0268bafc468c5742e9ec63461 /src
parentc50f605c572a425b2fa696fedc5e61ac6f66d2a9 (diff)
downloadexternal_protobuf-b15a1b379f896e371ca3375bdf51dd4a9a0268af.zip
external_protobuf-b15a1b379f896e371ca3375bdf51dd4a9a0268af.tar.gz
external_protobuf-b15a1b379f896e371ca3375bdf51dd4a9a0268af.tar.bz2
Feature request: set() and clear() accessors return this
Also pre-inlines set() and has() in serialization code. This could theoretically help ProGuard: the message class size is usually large, and because of this only, it may refuse to inline an accessor into the serialization code, and as a result keeps the accessor intact. Chances are, after pre-inlining all accessor calls within the message class, those accessors become unused or single-use, so there are more reasons for ProGuard to inline and then remove them. Change-Id: I57decbe0b2533c1be21439de0aad15f49c7024dd
Diffstat (limited to 'src')
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_enum_field.cc13
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_message_field.cc14
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_primitive_field.cc17
3 files changed, 27 insertions, 17 deletions
diff --git a/src/google/protobuf/compiler/javanano/javanano_enum_field.cc b/src/google/protobuf/compiler/javanano/javanano_enum_field.cc
index 4366161..3c4b978 100644
--- a/src/google/protobuf/compiler/javanano/javanano_enum_field.cc
+++ b/src/google/protobuf/compiler/javanano/javanano_enum_field.cc
@@ -182,16 +182,18 @@ GenerateMembers(io::Printer* printer) const {
"public int get$capitalized_name$() {\n"
" return $name$_;\n"
"}\n"
- "public void set$capitalized_name$(int value) {\n"
+ "public $message_name$ set$capitalized_name$(int value) {\n"
" $name$_ = value;\n"
" $set_has$;\n"
+ " return this;\n"
"}\n"
"public boolean has$capitalized_name$() {\n"
" return $get_has$;\n"
"}\n"
- "public void clear$capitalized_name$() {\n"
+ "public $message_name$ clear$capitalized_name$() {\n"
" $name$_ = $default$;\n"
" $clear_has$;\n"
+ " return this;\n"
"}\n");
}
@@ -204,13 +206,14 @@ GenerateClearCode(io::Printer* printer) const {
void AccessorEnumFieldGenerator::
GenerateMergingCode(io::Printer* printer) const {
printer->Print(variables_,
- "set$capitalized_name$(input.readInt32());\n");
+ "$name$_ = input.readInt32();\n"
+ "$set_has$;\n");
}
void AccessorEnumFieldGenerator::
GenerateSerializationCode(io::Printer* printer) const {
printer->Print(variables_,
- "if (has$capitalized_name$()) {\n"
+ "if ($get_has$) {\n"
" output.writeInt32($number$, $name$_);\n"
"}\n");
}
@@ -218,7 +221,7 @@ GenerateSerializationCode(io::Printer* printer) const {
void AccessorEnumFieldGenerator::
GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_,
- "if (has$capitalized_name$()) {\n"
+ "if ($get_has$) {\n"
" size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
" .computeInt32Size($number$, $name$_);\n"
"}\n");
diff --git a/src/google/protobuf/compiler/javanano/javanano_message_field.cc b/src/google/protobuf/compiler/javanano/javanano_message_field.cc
index 225cf42..02253f9 100644
--- a/src/google/protobuf/compiler/javanano/javanano_message_field.cc
+++ b/src/google/protobuf/compiler/javanano/javanano_message_field.cc
@@ -148,17 +148,19 @@ GenerateMembers(io::Printer* printer) const {
"public $type$ get$capitalized_name$() {\n"
" return $name$_;\n"
"}\n"
- "public void set$capitalized_name$($type$ value) {\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 void clear$capitalized_name$() {\n"
+ "public $message_name$ clear$capitalized_name$() {\n"
" $name$_ = null;\n"
+ " return this;"
"}\n");
}
@@ -171,8 +173,8 @@ GenerateClearCode(io::Printer* printer) const {
void AccessorMessageFieldGenerator::
GenerateMergingCode(io::Printer* printer) const {
printer->Print(variables_,
- "if (!has$capitalized_name$()) {\n"
- " set$capitalized_name$(new $type$());\n"
+ "if ($name$_ == null) {\n"
+ " $name$_ = new $type$();\n"
"}\n");
if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
@@ -187,7 +189,7 @@ GenerateMergingCode(io::Printer* printer) const {
void AccessorMessageFieldGenerator::
GenerateSerializationCode(io::Printer* printer) const {
printer->Print(variables_,
- "if (has$capitalized_name$()) {\n"
+ "if ($name$_ != null) {\n"
" output.write$group_or_message$($number$, $name$_);\n"
"}\n");
}
@@ -195,7 +197,7 @@ GenerateSerializationCode(io::Printer* printer) const {
void AccessorMessageFieldGenerator::
GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_,
- "if (has$capitalized_name$()) {\n"
+ "if ($name$_ != null) {\n"
" size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
" .compute$group_or_message$Size($number$, $name$_);\n"
"}\n");
diff --git a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc
index 0562a6e..0f7512a 100644
--- a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc
+++ b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc
@@ -431,21 +431,25 @@ GenerateMembers(io::Printer* printer) const {
"public $type$ get$capitalized_name$() {\n"
" return $name$_;\n"
"}\n"
- "public void set$capitalized_name$($type$ value) {\n");
+ "public $message_name$ set$capitalized_name$($type$ value) {\n");
if (IsReferenceType(GetJavaType(descriptor_))) {
printer->Print(variables_,
- " if (value == null) throw new java.lang.NullPointerException();\n");
+ " if (value == null) {\n"
+ " throw new java.lang.NullPointerException();\n"
+ " }\n");
}
printer->Print(variables_,
" $name$_ = value;\n"
" $set_has$;\n"
+ " return this;\n"
"}\n"
"public boolean has$capitalized_name$() {\n"
" return $get_has$;\n"
"}\n"
- "public void clear$capitalized_name$() {\n"
+ "public $message_name$ clear$capitalized_name$() {\n"
" $name$_ = $default_copy_if_needed$;\n"
" $clear_has$;\n"
+ " return this;\n"
"}\n");
}
@@ -458,13 +462,14 @@ GenerateClearCode(io::Printer* printer) const {
void AccessorPrimitiveFieldGenerator::
GenerateMergingCode(io::Printer* printer) const {
printer->Print(variables_,
- "set$capitalized_name$(input.read$capitalized_type$());\n");
+ "$name$_ = input.read$capitalized_type$();\n"
+ "$set_has$;\n");
}
void AccessorPrimitiveFieldGenerator::
GenerateSerializationCode(io::Printer* printer) const {
printer->Print(variables_,
- "if (has$capitalized_name$()) {\n"
+ "if ($get_has$) {\n"
" output.write$capitalized_type$($number$, $name$_);\n"
"}\n");
}
@@ -472,7 +477,7 @@ GenerateSerializationCode(io::Printer* printer) const {
void AccessorPrimitiveFieldGenerator::
GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_,
- "if (has$capitalized_name$()) {\n"
+ "if ($get_has$) {\n"
" size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
" .compute$capitalized_type$Size($number$, $name$_);\n"
"}\n");