{"id":12447,"date":"2015-08-16T20:46:00","date_gmt":"2015-08-16T19:46:00","guid":{"rendered":"https:\/\/touk.pl\/blog\/?guid=384ff5f0e5e61d3474b3d4a02cbf67aa"},"modified":"2022-07-28T15:24:03","modified_gmt":"2022-07-28T13:24:03","slug":"all-field-constructor-in-groovy","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2015\/08\/16\/all-field-constructor-in-groovy\/","title":{"rendered":"All field constructor in Groovy"},"content":{"rendered":"<p>TupleConstructor annotation in Groovy generate constructors for class with each of its properties (eventually also fields). The class below<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">@TupleConstructor(includeFields = true)\r\nclass Person {\r\n\u00a0 \u00a0String firstName\r\n\u00a0 \u00a0String lastName\r\n\u00a0 \u00a0private boolean male\r\n}\r\n<\/pre>\n<p>will have constructors: Person(), Persion(String), Person(String, String) and Person(String, String, boolean). You could test it using code below.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">class TupleConstructorTest extends GroovyTestCase{\r\n\u00a0 \u00a0 @Test\r\n\u00a0 \u00a0 void testSimpleTupleConstructorShouldGenerateConstructor() {\r\n\u00a0 \u00a0 \u00a0 \u00a0 assertScript '''\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 import groovy.transform.TupleConstructor\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 @TupleConstructor(includeFields = true)\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 class Person {\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 private final String firstName\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 private final String lastName\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 private final boolean male\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 String toString(){ \"$firstName $lastName $male\" }\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \r\n            assert Person.constructors.size() == 4\r\n            assert new Person().toString() == 'null null false'\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 assert new Person('John').toString() == 'John null false'\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 assert new Person('John','Smith').toString() == 'John Smith false'\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 assert new Person('John','Smith', true).toString() == 'John Smith true'\r\n\u00a0 \u00a0 \u00a0 \u00a0 '''\r\n\u00a0 \u00a0 }\r\n}<\/pre>\n<p>I almost always create classes with all private final fields and generate constructior with all fields using my IDE.<\/p>\n<p>So I have prepared new transformation AllFieldConstructor which bases on TupleConstructor and generates only constructor with all fields as parameters.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">class AllFieldConstructorTest extends GroovyTestCase{\r\n\u00a0 \u00a0 @Test\r\n\u00a0 \u00a0 void testSimpleTupleConstructorShouldGenerateConstructor() {\r\n\u00a0 \u00a0 \u00a0 \u00a0 assertScript '''\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 import com.blogspot.przybyszd.transformations.AllFieldConstructor\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 @AllFieldConstructor\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 class Person {\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 private final String firstName\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 private final String lastName\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 private final boolean male\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 String toString(){ \"$firstName $lastName $male\" }\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 assert Person.constructors.size() == 1\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 assert new Person('John','Smith', true).toString() == 'John Smith true'\r\n\u00a0 \u00a0 \u00a0 \u00a0 '''\r\n\u00a0 \u00a0 }\r\n}<\/pre>\n<p>The sources are available <a href=\"https:\/\/github.com\/alien11689\/groovy-transformations\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"TupleConstructor annotation in Groovy generate constructors for class with each of its properties (eventually also fields). The class below@TupleConstructor(includeFields = true)class Person {&nbsp; &nbsp;String firstName&nbsp; &nbsp;String lastName&amp;nb&#8230;\n","protected":false},"author":54,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[50],"class_list":{"0":"post-12447","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-groovy"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/54"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=12447"}],"version-history":[{"count":7,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12447\/revisions"}],"predecessor-version":[{"id":14677,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12447\/revisions\/14677"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=12447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=12447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=12447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}