{"id":308,"date":"2009-11-25T01:31:28","date_gmt":"2009-11-24T20:01:28","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=308"},"modified":"2016-12-19T15:19:22","modified_gmt":"2016-12-19T09:49:22","slug":"groovy-create-class-with-dynamic-properties","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/groovy-create-class-with-dynamic-properties\/","title":{"rendered":"Groovy: Create class with dynamic properties"},"content":{"rendered":"<p>I recently came across a way in Groovy to define a class which can have dynamic getters and setters and properties can be added on the fly. The following code creates a class &#8220;MyExpandableClass&#8221; and defines two methods in the &#8220;getProperty and setProperty&#8221;. If we define these two methods in a groovy class, then calls to any getter or setter on objects of this class will go through these methods. Groovy also provide an Expando class which provides the same functionality.<\/p>\n<div class=\"wp_syntax\">\n<div class=\"code\">\n<pre lang=\"groovy\">class MyExpandableClass{\r\nMap props=[:];\r\ndef getProperty(String property){\r\nreturn props[property];\r\n}\r\nvoid setProperty(String property, Object newValue){\r\nprops[property] = newValue;\r\n}\r\n}\r\nMyExpandableClass myExpandableObject = new MyExpandableClass()\r\nmyExpandableObject.numberValue=1234\r\nmyExpandableObject.dateValue=new Date()\r\nmyExpandableObject.stringValue=\"TEST\"\r\nprintln myExpandableObject.numberValue\r\nprintln myExpandableObject.dateValue\r\nprintln myExpandableObject.stringValue<\/pre>\n<\/div>\n<\/div>\n<p>In the above example, we can see that we can add any property to the object of &#8220;MyExpandableClass&#8221;. The properties that we add get stored in the map object in the class. We can think of our class as a simple wrapper around the map. Retrieving values from the object is same as calling getters for the class proeprties.<\/p>\n<p>However, if we just have to use a dynamically expandable bean for storing data, then we can simply use groovy&#8217;s &#8220;Expando&#8221; class. Having our own implementation makes sense when we also want to add behaviour to this class.<\/p>\n<p>This was my first taste of groovy&#8217;s metaprogramming powers. It truly is amazing.<\/p>\n<p>Hope you find this useful.<\/p>\n<p>Regards<br \/>\n~~Himanshu Seth~~<\/p>\n<p>http:\/\/www.tothenew.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently came across a way in Groovy to define a class which can have dynamic getters and setters and properties can be added on the fly. The following code creates a class &#8220;MyExpandableClass&#8221; and defines two methods in the &#8220;getProperty and setProperty&#8221;. If we define these two methods in a groovy class, then calls [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":81},"categories":[7],"tags":[146,9,145],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/308"}],"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\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=308"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/308\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}