diff options
author | Feng Xiao <xfxyjwf@gmail.com> | 2017-03-08 17:34:38 -0800 |
---|---|---|
committer | Feng Xiao <xfxyjwf@gmail.com> | 2017-03-08 17:34:38 -0800 |
commit | c31154316bbf9b7f12e3ae24c326dc6834604ba9 (patch) | |
tree | edc2c4b9eed627ab7d4105bb53e49f416e765736 | |
parent | fa1788026cf7c7310f068dbee7fbf71958ec1290 (diff) |
Avoid redundant type casts for oneof bytes fields.
-rw-r--r-- | src/google/protobuf/compiler/java/java_primitive_field.cc | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/src/google/protobuf/compiler/java/java_primitive_field.cc b/src/google/protobuf/compiler/java/java_primitive_field.cc index fa1047e8..840252e7 100644 --- a/src/google/protobuf/compiler/java/java_primitive_field.cc +++ b/src/google/protobuf/compiler/java/java_primitive_field.cc @@ -523,8 +523,17 @@ void ImmutablePrimitiveOneofFieldGenerator:: GenerateSerializationCode(io::Printer* printer) const { printer->Print(variables_, "if ($has_oneof_case_message$) {\n" - " output.write$capitalized_type$(\n" - " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n" + " output.write$capitalized_type$(\n"); + // $type$ and $boxed_type$ is the same for bytes fields so we don't need to + // do redundant casts. + if (GetJavaType(descriptor_) == JAVATYPE_BYTES) { + printer->Print(variables_, + " $number$, ($type$) $oneof_name$_);\n"); + } else { + printer->Print(variables_, + " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n"); + } + printer->Print( "}\n"); } @@ -533,8 +542,17 @@ GenerateSerializedSizeCode(io::Printer* printer) const { printer->Print(variables_, "if ($has_oneof_case_message$) {\n" " size += com.google.protobuf.CodedOutputStream\n" - " .compute$capitalized_type$Size(\n" - " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n" + " .compute$capitalized_type$Size(\n"); + // $type$ and $boxed_type$ is the same for bytes fields so we don't need to + // do redundant casts. + if (GetJavaType(descriptor_) == JAVATYPE_BYTES) { + printer->Print(variables_, + " $number$, ($type$) $oneof_name$_);\n"); + } else { + printer->Print(variables_, + " $number$, ($type$)(($boxed_type$) $oneof_name$_));\n"); + } + printer->Print( "}\n"); } |