Project: JDT
Bug 191297 [hashcode/equals] Incorrect generation of the equals method when it already exists and the class extends Object.
Reported: 2007-06-06 12:22:41 | Status: verified fixed

Build ID: M20070212-1330 Steps To Reproduce: 1. Create a class Foo with one attribute "int i;" 2. Implements the method equals like this : " public boolean equals(Object o) { return i == ((Foo)o).i; }" 3. Create a test class or a JUnit test to test the method equals. (i.e : "assert new Foo().equals(new Foo())"). 4. Run the test : the method equals return true. 5. Now use the function "Source > Generate hashCode() and equals()...". You'll get a warning because the method equals already exists. 6. Run the test : the method equals return false. More information: The bug is due to the call to super.equals(obj) : in the class Object the implementation of equals is : "return this == obj" which returns true only if we call the method equals with the same references. If there's no method equals in the class and then we use the function "Source > Generate hashCode() and equals()..." then it works because there is no call to super.equals(obj). Actually it is replaced with "if (obj == null)" .

Files changed in fix:
org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/GenerateHashCodeEqualsOperation.java