{"id":5037,"date":"2012-01-29T17:40:29","date_gmt":"2012-01-29T12:10:29","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=5037"},"modified":"2016-12-19T15:06:25","modified_gmt":"2016-12-19T09:36:25","slug":"groovy-annotations-for-tostring-and-equalsandhashcode","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/groovy-annotations-for-tostring-and-equalsandhashcode\/","title":{"rendered":"Groovy annotations for ToString and EqualsAndHashCode"},"content":{"rendered":"<p>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.<\/p>\n<p>[java]<br \/>\n@ToString(includeNames = true, includeFields = true, excludes = &#8216;dateCreated,lastUpdated,metaClass&#8217;)<br \/>\n@EqualsAndHashCode<br \/>\nclass Item {<br \/>\n    String name<br \/>\n    Float price<br \/>\n    boolean active = true<br \/>\n    Date dateCreated<br \/>\n    Date lastUpdated<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Before adding this annotation my domain class toString looks like this<\/p>\n<p>[java]<br \/>\nItem item = new Item(name: &amp;amp;quot;Chips&amp;amp;quot;, active: false, price: 15)<br \/>\nprintln &amp;amp;quot;To String output -: &amp;amp;quot; + item \/\/To String output -: com.intelligrape.myapp.Item : null<br \/>\n[\/java]<\/p>\n<p>Now I get the following output for Item object toString<\/p>\n<p>[java]<br \/>\nItem item = new Item(name: &amp;amp;quot;Chips&amp;amp;quot;, active: false, price: 15)<br \/>\nprintln &amp;amp;quot;To String output -: &amp;amp;quot; + item \/\/To String output -: com.intelligrape.myapp.Item(name:Chips, price:15.0, active:false)<br \/>\n[\/java]<\/p>\n<p>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<\/p>\n<p>[java]<br \/>\n@artifact.package@<br \/>\nimport groovy.transform.EqualsAndHashCode<br \/>\nimport groovy.transform.ToString<\/p>\n<p>@ToString(includeNames = true, includeFields = true, excludes = &#8216;dateCreated,lastUpdated,metaClass&#8217;)<br \/>\n@EqualsAndHashCode<br \/>\nclass @artifact.name@ {<\/p>\n<p>    Date dateCreated<br \/>\n    Date lastUpdated<br \/>\n}<br \/>\n[\/java]<br \/>\n<\/p>\n<p>Hope it helps<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/uday\/\">Uday Pratap Singh<\/a><br \/>\n<a href=\"mailto:uday@intelligrape.com\">uday@intelligrape.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":14},"categories":[7],"tags":[753,759,614,757,756,758,754,752],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5037"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=5037"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5037\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=5037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=5037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=5037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}