kidok / protobuf

Automatically exported from code.google.com/p/protobuf
0 stars 0 forks source link

Java generated message builder implementations do not implement equals() #594

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Summary
The Java generated message (proto) classes implement equals() and hashCode() as 
one would expect (if the java_generate_equals_and_hash option is true). But the 
Builder class implementations do not provide equals() or hashCode() methods. 
They rely on the default java.lang.Object versions of those methods, which are 
inconsistent with the proto versions.

This is really counter-intuitive, and should either be fixed or at least 
documented somewhere.

What steps will reproduce the problem?
Person.Builder b1 = Person.newBuilder().setName("Commander Jeffrey Sinclair");
Person.Builder b2 = Person.newBuilder().setName("Commander Jeffrey Sinclair");

if (b1.equals(b2) == b1.build().equals(b2.build())) {
  System.out.println(" Builder and Message equals() are the same. ");
} else {
  System.out.println(" Not the same. ");
}

What is the expected output? What do you see instead?
Expected:
  Builder and Message equals() are the same.

Actual:
  Not the same.

What version of the product are you using? On what operating system?
Not sure. 2.4 or 2.5, I think.
Java 7.
On Linux.

Please provide any additional information below.

Original issue reported on code.google.com by jdicami...@google.com on 7 Jan 2014 at 9:29

GoogleCodeExporter commented 9 years ago
We have documented that this is for messages:

  // If set true, then the Java code generator will generate equals() and
  // hashCode() methods for all messages defined in the .proto file. This is
  // purely a speed optimization, as the AbstractMessage base class includes
  // reflection-based implementations of these methods.
  optional bool java_generate_equals_and_hash = 20 [[]default=false];

https://devsite.googleplex.com/protocol-buffers/docs/reference/cpp/google.protob
uf.descriptor.pb?hl=de-DE

Original comment by jie...@google.com on 25 Jul 2014 at 6:10

GoogleCodeExporter commented 9 years ago

Original comment by jie...@google.com on 25 Jul 2014 at 11:12