Commit 0652d701 authored by Max Cai's avatar Max Cai

Remove all field initializers and let ctor call clear().

The field initializers have basically caused the compiled <init> method
to inline the whole clear() method, which means if ProGuard is not used
or failed to inline or remove clear(), there are two big chunks of code
that do the same thing. So why not just call clear() from the ctor.

Change-Id: Ief71e2b03db2e059b3bfa98309649368089ffab0
parent 3289fe1a
...@@ -89,11 +89,11 @@ EnumFieldGenerator::~EnumFieldGenerator() {} ...@@ -89,11 +89,11 @@ EnumFieldGenerator::~EnumFieldGenerator() {}
void EnumFieldGenerator:: void EnumFieldGenerator::
GenerateMembers(io::Printer* printer) const { GenerateMembers(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
"public $type$ $name$ = $default$;\n"); "public $type$ $name$;\n");
if (params_.generate_has()) { if (params_.generate_has()) {
printer->Print(variables_, printer->Print(variables_,
"public boolean has$capitalized_name$ = false;\n"); "public boolean has$capitalized_name$;\n");
} }
} }
...@@ -178,7 +178,7 @@ AccessorEnumFieldGenerator::~AccessorEnumFieldGenerator() {} ...@@ -178,7 +178,7 @@ AccessorEnumFieldGenerator::~AccessorEnumFieldGenerator() {}
void AccessorEnumFieldGenerator:: void AccessorEnumFieldGenerator::
GenerateMembers(io::Printer* printer) const { GenerateMembers(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
"private int $name$_ = $default$;\n" "private int $name$_;\n"
"public int get$capitalized_name$() {\n" "public int get$capitalized_name$() {\n"
" return $name$_;\n" " return $name$_;\n"
"}\n" "}\n"
...@@ -241,7 +241,7 @@ RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() {} ...@@ -241,7 +241,7 @@ RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() {}
void RepeatedEnumFieldGenerator:: void RepeatedEnumFieldGenerator::
GenerateMembers(io::Printer* printer) const { GenerateMembers(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
"public $type$[] $name$ = $repeated_default$;\n"); "public $type$[] $name$;\n");
if (descriptor_->options().packed()) { if (descriptor_->options().packed()) {
printer->Print(variables_, printer->Print(variables_,
"private int $name$MemoizedSerializedSize;\n"); "private int $name$MemoizedSerializedSize;\n");
......
...@@ -144,7 +144,9 @@ void MessageGenerator::Generate(io::Printer* printer) { ...@@ -144,7 +144,9 @@ void MessageGenerator::Generate(io::Printer* printer) {
printer->Indent(); printer->Indent();
printer->Print( printer->Print(
"public static final $classname$ EMPTY_ARRAY[] = {};\n" "public static final $classname$ EMPTY_ARRAY[] = {};\n"
"public $classname$() {}\n" "public $classname$() {\n"
" clear();\n"
"}\n"
"\n", "\n",
"classname", descriptor_->name()); "classname", descriptor_->name());
...@@ -244,7 +246,7 @@ GenerateMessageSerializationMethods(io::Printer* printer) { ...@@ -244,7 +246,7 @@ GenerateMessageSerializationMethods(io::Printer* printer) {
printer->Print( printer->Print(
"}\n" "}\n"
"\n" "\n"
"private int cachedSize = -1;\n" "private int cachedSize;\n"
"@Override\n" "@Override\n"
"public int getCachedSize() {\n" "public int getCachedSize() {\n"
" if (cachedSize < 0) {\n" " if (cachedSize < 0) {\n"
......
...@@ -84,7 +84,7 @@ MessageFieldGenerator::~MessageFieldGenerator() {} ...@@ -84,7 +84,7 @@ MessageFieldGenerator::~MessageFieldGenerator() {}
void MessageFieldGenerator:: void MessageFieldGenerator::
GenerateMembers(io::Printer* printer) const { GenerateMembers(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
"public $type$ $name$ = null;\n"); "public $type$ $name$;\n");
} }
void MessageFieldGenerator:: void MessageFieldGenerator::
...@@ -144,7 +144,7 @@ AccessorMessageFieldGenerator::~AccessorMessageFieldGenerator() {} ...@@ -144,7 +144,7 @@ AccessorMessageFieldGenerator::~AccessorMessageFieldGenerator() {}
void AccessorMessageFieldGenerator:: void AccessorMessageFieldGenerator::
GenerateMembers(io::Printer* printer) const { GenerateMembers(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
"private $type$ $name$_ = null;\n" "private $type$ $name$_;\n"
"public $type$ get$capitalized_name$() {\n" "public $type$ get$capitalized_name$() {\n"
" return $name$_;\n" " return $name$_;\n"
"}\n" "}\n"
...@@ -218,7 +218,7 @@ RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {} ...@@ -218,7 +218,7 @@ RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {}
void RepeatedMessageFieldGenerator:: void RepeatedMessageFieldGenerator::
GenerateMembers(io::Printer* printer) const { GenerateMembers(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
"public $type$[] $name$ = $type$.EMPTY_ARRAY;\n"); "public $type$[] $name$;\n");
} }
void RepeatedMessageFieldGenerator:: void RepeatedMessageFieldGenerator::
...@@ -231,7 +231,8 @@ void RepeatedMessageFieldGenerator:: ...@@ -231,7 +231,8 @@ void RepeatedMessageFieldGenerator::
GenerateMergingCode(io::Printer* printer) const { GenerateMergingCode(io::Printer* printer) const {
// First, figure out the length of the array, then parse. // First, figure out the length of the array, then parse.
printer->Print(variables_, printer->Print(variables_,
"int arrayLength = com.google.protobuf.nano.WireFormatNano.getRepeatedFieldArrayLength(input, $tag$);\n" "int arrayLength = com.google.protobuf.nano.WireFormatNano"
" .getRepeatedFieldArrayLength(input, $tag$);\n"
"int i = this.$name$.length;\n" "int i = this.$name$.length;\n"
"$type$[] newArray = new $type$[i + arrayLength];\n" "$type$[] newArray = new $type$[i + arrayLength];\n"
"System.arraycopy(this.$name$, 0, newArray, 0, i);\n" "System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
......
...@@ -315,7 +315,7 @@ GenerateMembers(io::Printer* printer) const { ...@@ -315,7 +315,7 @@ GenerateMembers(io::Printer* printer) const {
} }
printer->Print(variables_, printer->Print(variables_,
"public $type$ $name$ = $default_copy_if_needed$;\n"); "public $type$ $name$;\n");
if (params_.generate_has()) { if (params_.generate_has()) {
printer->Print(variables_, printer->Print(variables_,
...@@ -427,7 +427,7 @@ GenerateMembers(io::Printer* printer) const { ...@@ -427,7 +427,7 @@ GenerateMembers(io::Printer* printer) const {
"private static final $type$ $default_constant$ = $default_constant_value$;\n"); "private static final $type$ $default_constant$ = $default_constant_value$;\n");
} }
printer->Print(variables_, printer->Print(variables_,
"private $type$ $name$_ = $default_copy_if_needed$;\n" "private $type$ $name$_;\n"
"public $type$ get$capitalized_name$() {\n" "public $type$ get$capitalized_name$() {\n"
" return $name$_;\n" " return $name$_;\n"
"}\n" "}\n"
...@@ -495,7 +495,7 @@ RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {} ...@@ -495,7 +495,7 @@ RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {}
void RepeatedPrimitiveFieldGenerator:: void RepeatedPrimitiveFieldGenerator::
GenerateMembers(io::Printer* printer) const { GenerateMembers(io::Printer* printer) const {
printer->Print(variables_, printer->Print(variables_,
"public $type$[] $name$ = $default$;\n"); "public $type$[] $name$;\n");
} }
void RepeatedPrimitiveFieldGenerator:: void RepeatedPrimitiveFieldGenerator::
...@@ -526,7 +526,8 @@ GenerateMergingCode(io::Printer* printer) const { ...@@ -526,7 +526,8 @@ GenerateMergingCode(io::Printer* printer) const {
"input.popLimit(limit);\n"); "input.popLimit(limit);\n");
} else { } else {
printer->Print(variables_, printer->Print(variables_,
"int arrayLength = com.google.protobuf.nano.WireFormatNano.getRepeatedFieldArrayLength(input, $tag$);\n" "int arrayLength = com.google.protobuf.nano.WireFormatNano\n"
" .getRepeatedFieldArrayLength(input, $tag$);\n"
"int i = this.$name$.length;\n"); "int i = this.$name$.length;\n");
if (GetJavaType(descriptor_) == JAVATYPE_BYTES) { if (GetJavaType(descriptor_) == JAVATYPE_BYTES) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment