Commit 7b72a24a authored by Jisi Liu's avatar Jisi Liu

fix equal and hash for bytes field for javanano oneof

parent b9e9469b
...@@ -168,26 +168,48 @@ void SetCommonOneofVariables(const FieldDescriptor* descriptor, ...@@ -168,26 +168,48 @@ void SetCommonOneofVariables(const FieldDescriptor* descriptor,
SimpleItoa(descriptor->number()); SimpleItoa(descriptor->number());
} }
void GenerateOneofFieldEquals(const map<string, string>& variables, void GenerateOneofFieldEquals(const FieldDescriptor* descriptor,
const map<string, string>& variables,
io::Printer* printer) { io::Printer* printer) {
printer->Print(variables, if (GetJavaType(descriptor) == JAVATYPE_BYTES) {
"if (this.has$capitalized_name$()) {\n" printer->Print(variables,
" if (!this.$oneof_name$_.equals(other.$oneof_name$_)) {\n" "if (this.has$capitalized_name$()) {\n"
" return false;\n" " if (!other.has$capitalized_name$() ||\n"
" }\n" " !java.util.Arrays.equals((byte[]) this.$oneof_name$_,\n"
"} else {\n" " (byte[]) other.$oneof_name$_)) {\n"
" if (other.has$capitalized_name$()) {\n" " return false;\n"
" return false;\n" " }\n"
" }\n" "} else {\n"
"}\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) { io::Printer* printer) {
printer->Print(variables, if (GetJavaType(descriptor) == JAVATYPE_BYTES) {
"result = 31 * result +\n" printer->Print(variables,
" ($has_oneof_case$ ? this.$oneof_name$_.hashCode() : 0);\n"); "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 } // namespace javanano
......
...@@ -114,9 +114,11 @@ class FieldGeneratorMap { ...@@ -114,9 +114,11 @@ class FieldGeneratorMap {
void SetCommonOneofVariables(const FieldDescriptor* descriptor, void SetCommonOneofVariables(const FieldDescriptor* descriptor,
map<string, string>* variables); map<string, string>* variables);
void GenerateOneofFieldEquals(const map<string, string>& variables, void GenerateOneofFieldEquals(const FieldDescriptor* descriptor,
const map<string, string>& variables,
io::Printer* printer); io::Printer* printer);
void GenerateOneofFieldHashCode(const map<string, string>& variables, void GenerateOneofFieldHashCode(const FieldDescriptor* descriptor,
const map<string, string>& variables,
io::Printer* printer); io::Printer* printer);
} // namespace javanano } // namespace javanano
......
...@@ -214,12 +214,12 @@ GenerateSerializedSizeCode(io::Printer* printer) const { ...@@ -214,12 +214,12 @@ GenerateSerializedSizeCode(io::Printer* printer) const {
void MessageOneofFieldGenerator:: void MessageOneofFieldGenerator::
GenerateEqualsCode(io::Printer* printer) const { GenerateEqualsCode(io::Printer* printer) const {
GenerateOneofFieldEquals(variables_, printer); GenerateOneofFieldEquals(descriptor_, variables_, printer);
} }
void MessageOneofFieldGenerator:: void MessageOneofFieldGenerator::
GenerateHashCodeCode(io::Printer* printer) const { GenerateHashCodeCode(io::Printer* printer) const {
GenerateOneofFieldHashCode(variables_, printer); GenerateOneofFieldHashCode(descriptor_, variables_, printer);
} }
// =================================================================== // ===================================================================
......
...@@ -767,12 +767,12 @@ void PrimitiveOneofFieldGenerator::GenerateSerializedSizeCode( ...@@ -767,12 +767,12 @@ void PrimitiveOneofFieldGenerator::GenerateSerializedSizeCode(
void PrimitiveOneofFieldGenerator::GenerateEqualsCode( void PrimitiveOneofFieldGenerator::GenerateEqualsCode(
io::Printer* printer) const { io::Printer* printer) const {
GenerateOneofFieldEquals(variables_, printer); GenerateOneofFieldEquals(descriptor_, variables_, printer);
} }
void PrimitiveOneofFieldGenerator::GenerateHashCodeCode( void PrimitiveOneofFieldGenerator::GenerateHashCodeCode(
io::Printer* printer) const { io::Printer* printer) const {
GenerateOneofFieldHashCode(variables_, printer); GenerateOneofFieldHashCode(descriptor_, variables_, printer);
} }
// =================================================================== // ===================================================================
......
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