diff options
author | Jisi Liu <jisi.liu@gmail.com> | 2015-02-18 16:39:01 -0800 |
---|---|---|
committer | Jisi Liu <jisi.liu@gmail.com> | 2015-02-19 16:43:08 -0800 |
commit | 7b72a24a2038dcb3409bb91d90f5a06abac3959c (patch) | |
tree | 41a702e329f6f709b49f20d40a9c23a08dcd2b73 /src | |
parent | b9e9469b4d7d81878c6bfd326cc6351abdbaf1d5 (diff) | |
download | protobuf-7b72a24a2038dcb3409bb91d90f5a06abac3959c.tar.gz protobuf-7b72a24a2038dcb3409bb91d90f5a06abac3959c.tar.bz2 protobuf-7b72a24a2038dcb3409bb91d90f5a06abac3959c.zip |
fix equal and hash for bytes field for javanano oneof
Diffstat (limited to 'src')
4 files changed, 46 insertions, 22 deletions
diff --git a/src/google/protobuf/compiler/javanano/javanano_field.cc b/src/google/protobuf/compiler/javanano/javanano_field.cc index 07517567..e40db620 100644 --- a/src/google/protobuf/compiler/javanano/javanano_field.cc +++ b/src/google/protobuf/compiler/javanano/javanano_field.cc @@ -168,26 +168,48 @@ void SetCommonOneofVariables(const FieldDescriptor* descriptor, SimpleItoa(descriptor->number()); } -void GenerateOneofFieldEquals(const map<string, string>& variables, +void GenerateOneofFieldEquals(const FieldDescriptor* descriptor, + const map<string, string>& variables, io::Printer* printer) { - printer->Print(variables, - "if (this.has$capitalized_name$()) {\n" - " if (!this.$oneof_name$_.equals(other.$oneof_name$_)) {\n" - " return false;\n" - " }\n" - "} else {\n" - " if (other.has$capitalized_name$()) {\n" - " return false;\n" - " }\n" - "}\n"); - + if (GetJavaType(descriptor) == JAVATYPE_BYTES) { + printer->Print(variables, + "if (this.has$capitalized_name$()) {\n" + " if (!other.has$capitalized_name$() ||\n" + " !java.util.Arrays.equals((byte[]) this.$oneof_name$_,\n" + " (byte[]) other.$oneof_name$_)) {\n" + " return false;\n" + " }\n" + "} else {\n" + " if (other.has$capitalized_name$()) {\n" + " return false;\n" + " }\n" + "}\n"); + } else { + printer->Print(variables, + "if (this.has$capitalized_name$()) {\n" + " if (!this.$oneof_name$_.equals(other.$oneof_name$_)) {\n" + " return false;\n" + " }\n" + "} else {\n" + " if (other.has$capitalized_name$()) {\n" + " return false;\n" + " }\n" + "}\n"); + } } -void GenerateOneofFieldHashCode(const map<string, string>& variables, +void GenerateOneofFieldHashCode(const FieldDescriptor* descriptor, + const map<string, string>& variables, io::Printer* printer) { - printer->Print(variables, - "result = 31 * result +\n" - " ($has_oneof_case$ ? this.$oneof_name$_.hashCode() : 0);\n"); + if (GetJavaType(descriptor) == JAVATYPE_BYTES) { + printer->Print(variables, + "result = 31 * result + ($has_oneof_case$\n" + " ? java.util.Arrays.hashCode((byte[]) this.$oneof_name$_) : 0);\n"); + } else { + printer->Print(variables, + "result = 31 * result +\n" + " ($has_oneof_case$ ? this.$oneof_name$_.hashCode() : 0);\n"); + } } } // namespace javanano diff --git a/src/google/protobuf/compiler/javanano/javanano_field.h b/src/google/protobuf/compiler/javanano/javanano_field.h index b31b517b..c2cf091c 100644 --- a/src/google/protobuf/compiler/javanano/javanano_field.h +++ b/src/google/protobuf/compiler/javanano/javanano_field.h @@ -114,9 +114,11 @@ class FieldGeneratorMap { void SetCommonOneofVariables(const FieldDescriptor* descriptor, map<string, string>* variables); -void GenerateOneofFieldEquals(const map<string, string>& variables, +void GenerateOneofFieldEquals(const FieldDescriptor* descriptor, + const map<string, string>& variables, io::Printer* printer); -void GenerateOneofFieldHashCode(const map<string, string>& variables, +void GenerateOneofFieldHashCode(const FieldDescriptor* descriptor, + const map<string, string>& variables, io::Printer* printer); } // namespace javanano diff --git a/src/google/protobuf/compiler/javanano/javanano_message_field.cc b/src/google/protobuf/compiler/javanano/javanano_message_field.cc index 6e12bf63..181c4060 100644 --- a/src/google/protobuf/compiler/javanano/javanano_message_field.cc +++ b/src/google/protobuf/compiler/javanano/javanano_message_field.cc @@ -214,12 +214,12 @@ GenerateSerializedSizeCode(io::Printer* printer) const { void MessageOneofFieldGenerator:: GenerateEqualsCode(io::Printer* printer) const { - GenerateOneofFieldEquals(variables_, printer); + GenerateOneofFieldEquals(descriptor_, variables_, printer); } void MessageOneofFieldGenerator:: GenerateHashCodeCode(io::Printer* printer) const { - GenerateOneofFieldHashCode(variables_, printer); + GenerateOneofFieldHashCode(descriptor_, variables_, printer); } // =================================================================== diff --git a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc index 038db6e2..106a7506 100644 --- a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc +++ b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc @@ -767,12 +767,12 @@ void PrimitiveOneofFieldGenerator::GenerateSerializedSizeCode( void PrimitiveOneofFieldGenerator::GenerateEqualsCode( io::Printer* printer) const { - GenerateOneofFieldEquals(variables_, printer); + GenerateOneofFieldEquals(descriptor_, variables_, printer); } void PrimitiveOneofFieldGenerator::GenerateHashCodeCode( io::Printer* printer) const { - GenerateOneofFieldHashCode(variables_, printer); + GenerateOneofFieldHashCode(descriptor_, variables_, printer); } // =================================================================== |