{"id":4188,"date":"2011-09-15T23:47:29","date_gmt":"2011-09-15T18:17:29","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=4188"},"modified":"2011-09-15T23:47:29","modified_gmt":"2011-09-15T18:17:29","slug":"playing-with-call-using-groovy-metaprogramming","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/playing-with-call-using-groovy-metaprogramming\/","title":{"rendered":"Playing with call() using Groovy Metaprogramming"},"content":{"rendered":"<p>In groovy you can use <code>\"()\"<\/code> with any <code>groovy object<\/code> which in turn evaluates to invocation of <code>call()<\/code> method on that object.<br \/>\nFor example, the following are valid Groovy statements :<br \/>\n[groovy]<br \/>\nList myList=[&quot;one&quot;, &quot;two&quot;]<br \/>\nmyList()<br \/>\n10()<br \/>\n500 6+5<br \/>\n[name:&quot;Bhagwat&quot;, profession:&quot;Software engineer&quot;] name<br \/>\n[\/groovy]<br \/>\nObviously they will <em>throw Runtime exception (MissingMethod\/MissingProperty)<\/em> instead of <em>compilation error<\/em>. Using <em>meta programming<\/em> support you can make them happen  even though they are Java objects.<\/p>\n<p>Think of this sample groovy code :<br \/>\n[groovy]<br \/>\nList myList=[&quot;Apple&quot;, &quot;Banana&quot;, &quot;Orange&quot;]<br \/>\nmyList()<br \/>\n[\/groovy]<br \/>\nAfter executing the above code you will get an exception : <em>&#8220;No signature of method: java.util.ArrayList.call() is applicable for argument types: () values: []&#8221;<\/em>.<\/p>\n<p>Here <code>myList()<\/code> statement is equivalent to <code>myList.call()<\/code>. This gives us a clue that we can catch method calls like above. Lets inject the <code>call<\/code>  method using groovy metaprogramming in List interface :<\/p>\n<p>[groovy]<br \/>\nList.metaClass.call={index-&gt;<br \/>\n delegate.getAt(index) \t\/\/delegate[index]<br \/>\n}<\/p>\n<p>\/* Now the following statements work : *\/<br \/>\nList myList=[&quot;Apple&quot;, &quot;Banana&quot;, &quot;Orange&quot;]<\/p>\n<p>myList[1]\t\t\/\/ Using the overloaded operator [] bygroovy<\/p>\n<p>myList.call(1) \t\/\/ index will be 1 ; will return &quot;Banana&quot;<br \/>\nmyList(1)\t\t\/\/  recall the syntax used in Microsoft VB.net to access Array<br \/>\nmyList 1\t\t\/\/ you can omit parenthesis if there is at least one argument<br \/>\n[\/groovy]<\/p>\n<p>We can move one step ahead by passing a <code>Map<\/code> to the closure to make the method having named parameters :<br \/>\n[groovy]<br \/>\nList.metaClass.call={Map namedArgs-&gt;<br \/>\n    namedArgs.each{<br \/>\n\tdelegate[it.key]=it.value<br \/>\n    }<br \/>\n}<\/p>\n<p>List demo=[]<br \/>\ndemo(0:&quot;Java&quot;, 1: &quot;Groovy&quot;, 2: &quot;Scala&quot;)<\/p>\n<p>demo(0) \/\/ &quot;Java&quot;<br \/>\ndemo(1) \/\/ &quot;Groovy&quot;<br \/>\n[\/groovy]<\/p>\n<p>When I was learning computer science, I used to write statements like :<\/p>\n<p>[groovy]<br \/>\nx=5<br \/>\nprintln 5+2(5+x(4)\/2)  \/\/ should be 35<br \/>\n[\/groovy]<br \/>\nBut that always threw an exception, can you make this work as expected? Give it a try if you really think this blog taught you something. Compare your solution here <a  targe=\"_blank\" href=\"http:\/\/groovyconsole.appspot.com\/script\/557002\">http:\/\/groovyconsole.appspot.com\/script\/557002<\/a>.<br \/>\n<br \/>Hope this let you think in groovy way.<br \/>\nBhagwat Kumar<br \/>\nbhagwat@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Groovy you can use &#8220;()&#8221; with any object which in turn is same as calling call() method on that object. You can write code which are more human readable using this groovy feature. This is somewhat overriding &#8220;()&#8221; operator. The blog adds Microsoft VB array access like syntax to Java List.<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5},"categories":[1],"tags":[630,631,242,632,633],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4188"}],"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\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=4188"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4188\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=4188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=4188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=4188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}