Groovy annotations for ToString and EqualsAndHashCode « Intelligrape Groovy & Grails Blogs

Groovy annotations for ToString and EqualsAndHashCode

Posted by Uday Pratap Singh

As I am a lazy programmer most of the time I dont implement toString and equals methods on my grails domain classes. I would like to say thanks to Groovy for helping me out and giving me a ready made recipe for this. Now I just need to annotate my class with ToString and EqualAndHashCode annotation it adds appropriate implementation of these methods for me. Now My domain class looks something like this.

@ToString(includeNames = true, includeFields = true, excludes = 'dateCreated,lastUpdated,metaClass')
@EqualsAndHashCode
class Item {
    String name
    Float price
    boolean active = true
    Date dateCreated
    Date lastUpdated
}

Before adding this annotation my domain class toString looks like this

Item item = new Item(name: "Chips", active: false, price: 15)
println "To String output -: " + item //To String output -: com.intelligrape.myapp.Item : null

Now I get the following output for Item object toString

Item item = new Item(name: "Chips", active: false, price: 15)
println "To String output -: " + item //To String output -: com.intelligrape.myapp.Item(name:Chips, price:15.0, active:false)

To get this annotation on all my domain classed I updated the template of grails domain classes so that whenever I do create-domain-class it give me the annotated domain classes

@artifact.package@
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString

@ToString(includeNames = true, includeFields = true, excludes = 'dateCreated,lastUpdated,metaClass')
@EqualsAndHashCode
class @artifact.name@ {

    Date dateCreated
    Date lastUpdated
}

Hope it helps



## Uday Pratap Singh ##
uday@intelligrape.com
www.IntelliGrape.com/
in.linkedin.com/in/meudaypratap

  • spacer
Tags: equals grails domain class, grails domain class template, groovy annotations, groovy equals, groovy hasCode, groovy toString, hashcode grails domain class, tostring grails domain class
This entry was posted on January 29th, 2012 at 5:40 pm and is filed under Design Pattern, Grails, Groovy . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

5 Responses to “Groovy annotations for ToString and EqualsAndHashCode”

  1. spacer Ken Kousen says:
    January 30, 2012 at 3:32 pm

    If you like @ToString and @EqualsAndHashCode, check out @Canonical groovy.codehaus.org/gapi/groovy/transform/Canonical.html

  2. spacer Uday Pratap Singh says:
    January 30, 2012 at 3:48 pm

    I looked at it when I added these two separately. But yes its make more sense here although it also gives me the functionality of TupleConstructor which I wouldn’t mind to have in my class spacer

  3. » Blog Archive says:
    January 30, 2012 at 7:08 pm

    [...] Groovy annotations for ToString and EqualsAndHashCode [...]

  4. An Army of Solipsists » Blog Archive » This Week in Grails (2012-04) says:
    January 31, 2012 at 7:55 am

    [...] Groovy annotations for ToString and EqualsAndHashCode [...]

  5. GroovyMag - the magazine for Groovy and Grails developers about Groovy programming and Grails development says:
    January 31, 2012 at 10:22 pm

    [...] Groovy annotations for ToString and EqualsAndHashCode, by Uday Pratap Singh (IntelliGrape) [...]

Leave a Reply

Click here to cancel reply.

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.