12

The default generated hashCode and equals implementations are ugly at best.

Is it possible to make eclipse generate ones from HashCodeBuilder and EqualsBuilder, and perhaps even a toString with ToStringBuilder?

daveb
  • 74,111
  • 6
  • 45
  • 51

6 Answers6

11

Take a look at Commons4E

It hasn't been updated in a while, but then I don't guess it needs to change much?

Update: Just checked against 3.4.1 and it works fine.

toolkit
  • 49,809
  • 17
  • 109
  • 135
  • 1
    Since the last update of this plugin is from 2006, it will certainly not support Apache Commons Lang 3. BTW this link might be more useful: http://wiki.jiayun.org/Commons4E – Didier L Apr 24 '12 at 14:31
5

You can configure Eclipse to generate toString() using a custom builder. In our case ToStringBuilder from Apache Commons Lang. You can see here http://azagorneanu.blogspot.com/2011/08/how-to-generate-equals-hashcode.html how to do it.

That blog post contains also Eclipse templates for generating equals(), hashCode() and compareTo() using Apache Commons Lang builders.

3

You can do that with Code Templates in Eclipse.

Here's a solution that I found with examples of HashCodeBuilder and EqualsBuilder.

Template EqualsBuilder:

    public boolean equals(Object o) {
        boolean result = false;

        if (this == o) {
            result = true;
        } else if (o instanceof $CLASSNAME$) {
            $CLASSNAME$ other = ($CLASSNAME$) o;

            result = new org.apache.commons.lang.builder.EqualsBuilder()
                    .append($END$
                    .isEquals();
        }

        return result;
    }

Template HashCodeBuilder:

    public int hashCode() {
        return new org.apache.commons.lang.builder.HashCodeBuilder()
                .append( $END$ )
                .toHashCode();
    }
bruno conde
  • 47,767
  • 15
  • 98
  • 117
3

I use the Eclipse plugin called "Commonclipse"

After installation, you see a new context menu item "commonclipse" when you right click within a java source file. It can generate equals, hashcode, toString and compareTo methods based on the Apache commons libraries.

To install it, use this from within eclipse update: http://commonclipse.sourceforge.net

Chinnery
  • 10,179
  • 2
  • 23
  • 25
1

I made this template checking several answers, websites and testing it on Eclipse Luna. Go to Windows->Preferences and then to Java->Editor->Templates and add it there.

${:import(org.apache.commons.lang3.builder.HashCodeBuilder, org.apache.commons.lang3.builder.EqualsBuilder)}
@Override
public int hashCode() {
    HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
    hashCodeBuilder.append(${field1:field});
    hashCodeBuilder.append(${field2:field});
    hashCodeBuilder.append(${field3:field});
    hashCodeBuilder.append(${field4:field});
    hashCodeBuilder.append(${field5:field});
    return hashCodeBuilder.toHashCode();
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    ${enclosing_type} rhs = (${enclosing_type}) obj;
    EqualsBuilder equalsBuilder = new EqualsBuilder();
    equalsBuilder.append(${field1}, rhs.${field1});
    equalsBuilder.append(${field2}, rhs.${field2});
    equalsBuilder.append(${field3}, rhs.${field3});
    equalsBuilder.append(${field4}, rhs.${field4});
    equalsBuilder.append(${field5}, rhs.${field5});${cursor}
    return equalsBuilder.isEquals();
}
Sebastian D'Agostino
  • 1,575
  • 2
  • 27
  • 44
-1

Eclipse java code templates for eclipse 3.5.0, derived from Bruno Conde's templates:

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    } else if (obj == this) {
        return true;
    } else if (obj.getClass() != this.getClass()) {
        return false;
    }

    ${enclosing_type} other = (${enclosing_type}) obj;
    return new EqualsBuilder()//
            .appendSuper(super.equals(other))//
            .append(${cursor})//
                .isEquals();
}

and

@Override
public int hashCode() {
    return new HashCodeBuilder(${cursor})//
            .append()//
            .toHashCode();
}
Jens Geiregat
  • 129
  • 1
  • 7