{"id":6740,"date":"2012-09-24T20:12:48","date_gmt":"2012-09-24T14:42:48","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6740"},"modified":"2015-08-26T15:28:57","modified_gmt":"2015-08-26T09:58:57","slug":"metaprogramming-with-metaclass","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/metaprogramming-with-metaclass\/","title":{"rendered":"MetaProgramming with MetaClass"},"content":{"rendered":"<p>As we all know that <strong>groovy<\/strong> is a dynamic programming language, so we can add any method to any class at runtime. First, let\u2019s see a small and simple example of <strong>metaProgamming<\/strong> given below, in which we are adding a method (isEvent) into an Integer class.<\/p>\n<p>[groovy]<br \/>\nInteger.metaClass.isEven = { -&gt; \/\/ only (-&gt;) sign indicates that isEven() method is no argument method<br \/>\n    delegate%2 == 0<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p>When we see the above program few questions arise in our mind, what is <strong>metaClass<\/strong>? What is <strong>delegate<\/strong>?? How <strong>metaprogramming<\/strong> is actually working?? How to disable <strong>MetaProgramming<\/strong>?? isEven() method a static or instance method?? etc&#8230; Let\u2019s move on to every question one by one.<\/p>\n<p>1. What is <strong>metaClass<\/strong>?? In <strong>Groovy<\/strong> Language, every object has an object of <strong>MetaClass<\/strong> class with name <strong>metaClass<\/strong>. This <strong>metaClass<\/strong> object is responsible for holding all the information related to that object. Whenever you perform any operation on that object, <strong>Groovy&#8217;s<\/strong> dispatch mechanism routes the call through that <strong>Metaclass<\/strong> object(<strong>metaClass<\/strong>). So if you want to change the behaviour of any object\/class, you will have to alter the <strong>MetaClass<\/strong> object attached to that class\/object, and it will alter the behaviour of that class\/object at run time.<\/p>\n<p>2. What is <strong>delegate<\/strong>?? We can say <strong>delegate<\/strong> is reference of that object who is invoking that isEven() method.<\/p>\n<p>3. How <strong>metaprogramming<\/strong> is actually working?? Whenever we call a method of class\/object, it first gets information of that object from <strong>metaClass<\/strong> attached to it, and then calls the appropriate method. In above program we are asking Integer <strong>metaClass<\/strong>, that there is an isEven() method in integer class, whose definition is followed by it. Now when we will call \u201canyInteger.isEven()\u201d, it will excute isEven() method and will return true\/false accordingly, e.g.<\/p>\n<p>[groovy]<br \/>\n9.isEven() \/\/ false<br \/>\n8.isEven() \/\/ true<br \/>\n[\/groovy]<\/p>\n<p>4. How to disable <strong>MetaProgramming<\/strong>?? Once a method is added into any class\/object, that method will be available in complete application. If you want remove that method from a particular situation you will have to assign <strong>null<\/strong> into<strong> Integer.metaClass<\/strong>. e.g.<\/p>\n<p>[groovy]<br \/>\nInteger.metaClass = null<br \/>\n7.isEven() \/\/ will throw MissingMethodException<br \/>\n[\/groovy]<\/p>\n<p>5. isEven() method is static or instance method?? isEven() is an instance method. To add a static method at run time, you will have to follow signature given below:<\/p>\n<p>[groovy]<br \/>\nInteger.metaClass.static.isEven = { number -&gt;<br \/>\n    number%2 == 0<br \/>\n}<br \/>\nInteger.isEven(1) \/\/ false<br \/>\nInteger.isEven(2) \/\/ true<br \/>\n[\/groovy]<\/p>\n<p>6. isEven() method is added to that object only?? No, isEven() method is added to all the Integer objects, if we want to add isEven() in a particular object only then we have to use that object reference .metaClass instead of Integer.metaClass, e.g.<\/p>\n<p>[groovy]<br \/>\nInteger aNumber = 9<br \/>\naNumber.metaClass.isEven = { -&gt;<br \/>\n    delegate%2 == 0<br \/>\n}<br \/>\nprintln aNumber.isEven() \/\/ false<br \/>\nprintln 2.isEven() \/\/ will throw MissingMethodException.<br \/>\n[\/groovy]<\/p>\n<p>7. How to add multiple methods?? To add the multiple method into single class see below example:<\/p>\n<p>[groovy]<br \/>\nInteger.metaClass {<br \/>\n    isEven { -&gt; delegate%2 == 0 }<br \/>\n    isOdd { -&gt; delegate%2 != 0 }<br \/>\n    \/\/ other methods<br \/>\n}<\/p>\n<p>println 6.isEven() \/\/ true<br \/>\nprintln 6.isOdd()  \/\/ false<br \/>\n[\/groovy]<\/p>\n<p><strong><a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit-kumar\/\">More Blogs by Me<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As we all know that groovy is a dynamic programming language, so we can add any method to any class at runtime. First, let\u2019s see a small and simple example of metaProgamming given below, in which we are adding a method (isEvent) into an Integer class. [groovy] Integer.metaClass.isEven = { -&gt; \/\/ only (-&gt;) sign [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[7],"tags":[885,9,632,145],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6740"}],"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\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6740"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6740\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}