Friday, December 19, 2008

Eclipse Code Templates - toString, equals, hashCode

Finally I decided to write a code template that I can use in order to create those infamous methods that should be a part of everyone's DTO. Those methods are toString(), equals(), and hashCode().

The template does assume that you are using the commons-lang library. Feel free to use it and pay it forward:


${:import(org.apache.commons.lang.builder.EqualsBuilder,
org.apache.commons.lang.builder.HashCodeBuilder,
org.apache.commons.lang.builder.ReflectionToStringBuilder)} @Override public boolean equals(Object obj) { return EqualsBuilder.reflectionEquals(this, obj); } @Override public String toString() { return ReflectionToStringBuilder.toString(this); } @Override public int hashCode() { return HashCodeBuilder.reflectionHashCode(this); }
Enjoy.

3 comments:

Yeray Caballero said...

Nice feature, how do you configure this in eclipse?

Thanks

Yeray Caballero said...

Nice feature. How do you configure this one in eclipse?

Thanks

Carlus Henry said...

Yeray,

The way that you confgure templates in Eclipse is:
1. Open Eclipse preferences
2. Navigate to Java->Editor->Templates
3. Click on the new button<
4. And then take the template I wrote and put it in there.

Hope this helps....happy coding.

Carlus