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?

6 Answers 6

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.

1
  • 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: wiki.jiayun.org/Commons4E
    – Didier L
    Apr 24, 2012 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();
    }
5
  • Which code template is that for eclipse, or how do I make a new one?
    – daveb
    Nov 11, 2008 at 15:35
  • Please see this post about code templates (existing and new ones): eclipse.dzone.com/news/visual-guide-templates-eclipse Nov 11, 2008 at 15:56
  • 4
    $CLASSNAME$ no longer works as of eclipse 3.4; the new variable is ${enclosing_type}. other $..$ should be replaces as well.
    – yanchenko
    Jan 26, 2009 at 1:05
  • Nice solution. Will it use all variables. Mar 31, 2010 at 13:29
  • Please update your link to point to the specific blog article.
    – mR_fr0g
    Mar 26, 2012 at 11:22
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

0
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();
}
-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();
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.