Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 130544 - [hashcode/equals] ineffective generated code [code generation]
Summary: [hashcode/equals] ineffective generated code [code generation]
Status: CLOSED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.2   Edit
Hardware: All All
: P5 minor with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2006-03-06 09:23 EST by x y CLA
Modified: 2019-06-02 01:36 EDT (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description x y CLA 2006-03-06 09:23:39 EST
// Generated by Eclipse.
       // Note, that even if field_ == other.field_ (and both not null),
       // they would be compared by field_.equals(other.field_),
       // which could be rather expensive.
        if (field_ == null)
        {
            if (other.field_ != null)
                return false;
        }
        else if (!field_.equals(other.field_))
            return false;
        
        // Faster implementation        
        if (field_ != other.field_ && field_ != null && !field_.equals(other.field_))
        {
            return false;
        }
Comment 1 x y CLA 2006-03-06 10:39:14 EST
Sorry, effective implementation should be:
if (field_ != other.field_ && (field_ == null || !field_.equals(other.field_)))
        {
            return false;
        }
Comment 2 Osvaldo Pinali Doederlein CLA 2006-04-30 15:01:54 EDT
Other small improvements (each too small to deserve its own bug...):

a) If a field of referenced type is declared final, and initialized only to non-null values, then you could skip the null checks.

b) Have an option to generate hashCode() without multiplications to combine the hashcodes of multipl fields, using instead the XOR operator (^), which is good enough when you're sure that all or most individual fields have high-quality hashcodes (well distributed over the possible 32 bits).

c) If the number of selected fields is only 1, use a simplified code pattern, remarkably for hashCode() (up to RC2 it generates a 5-line method, when all you need is something like "return field == null ? 0 : field.hashCode()").

d) Have another option to generate shorter code, even at expense of readability and maintenability. Current code is better if I ever want to change it manually; but if I always re-generate the methods automatically when necessary, I only care for code size, which reduces clutter. For example, instead of:

  public int hashCode () {
    final int PRIME = 31;
    int result = 1;
    result = PRIME * result + field1;
    result = PRIME * result + ((field2 == null) ? 0 : field2.hashCode());
    return result;
  }

we could have only one line per field:

  public int hashCode () {
    return ((field1 == null) ? 0 : field1.hashCode()) +
      31 * ((field2 == null) ? 0 : field2.hashCode()) +
      961 * ((field3 == null) ? 0 : field3.hashCode());
  }

(961=31^2. The XOR option would eliminate this weirdness and "magic numbers".)

Another good idea in the lines of "hiding clutter of tool-gnerated code" is folding by default these methods.  In Editor / Folding, we could have a list of method signatures that are folded by default, to include such drudgery as these two methods, plus toString(), compareTo()... but since this problem will be soon fixed by JSR-250's @Generated annotation, I guess we can just wait for that.
Comment 3 Gunnar Wagenknecht CLA 2006-06-17 17:59:19 EDT
Maybe the getClass() != obj.getClass() can be configurable, too. For beans, that may be persisted using a database the persistence layer might choose to return an overwritten bean (for example, for customized loading behavior). In this case, the check would be getClass ().isAssignableFrom(obj.getClass())).
Comment 4 Tobias Widmer CLA 2006-06-19 05:02:32 EDT
In the next I-build there is a preference to replace the class comparison with a simple instanceof. That probably helps for the scenario in comment 3.
Comment 5 Eric Bodden CLA 2006-08-15 16:36:12 EDT
This fix si superflous, because by convention the object stored in the field should for itself whether "this==other" ("this" being this evry object). So all you loose is a (cheap) virtual method call.
Comment 6 Tobias Widmer CLA 2006-08-16 05:06:08 EDT
What fix are you referring to?
Comment 7 Nicolas Dordet CLA 2009-10-02 05:44:05 EDT
Others interesting implementations can be found here : 
http://www.aqris.com/display/DEV/2008/08/06/Adding+equals+implementation+macro+in+Eclipse
Comment 8 Eclipse Genie CLA 2019-06-02 01:36:29 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.