Reputation: 76171
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?
Upvotes: 12
Views: 13674
Reputation: 1675
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();
}
Upvotes: 1
Reputation: 221
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.
Upvotes: 5
Reputation: 389
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();
}
Upvotes: -1
Reputation: 50227
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.
Upvotes: 11
Reputation: 10229
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
Upvotes: 3
Reputation: 48265
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();
}
Upvotes: 3