<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Groovy MetaClass: Overriding a method whilst using the old implementation</title>
	<atom:link href="http://naleid.com/blog/2009/06/01/groovy-metaclass-overriding-a-method-whilst-using-the-old-implementation/feed/" rel="self" type="application/rss+xml" />
	<link>http://naleid.com/blog/2009/06/01/groovy-metaclass-overriding-a-method-whilst-using-the-old-implementation/</link>
	<description>Groovy, Grails and OS X tips and tricks</description>
	<lastBuildDate>Thu, 17 May 2012 00:35:22 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: tednaleid</title>
		<link>http://naleid.com/blog/2009/06/01/groovy-metaclass-overriding-a-method-whilst-using-the-old-implementation/comment-page-1/#comment-2869</link>
		<dc:creator>tednaleid</dc:creator>
		<pubDate>Wed, 24 Nov 2010 21:21:25 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=54#comment-2869</guid>
		<description>@arvind normally you need to be explicit about the closure being zero length when overriding it, so you&#039;d need to do this:

&lt;pre lang=&quot;groovy&quot;&gt;
String.metaClass.length = {-&gt; return 1}
&lt;/pre&gt;

I say &quot;normally&quot; as there&#039;s actually a &lt;a href=&quot;http://jira.codehaus.org/browse/GROOVY-3493&quot; rel=&quot;nofollow&quot;&gt;weird bug/issue in groovy&lt;/a&gt; that affects this particular test case to not make it work (that I wasn&#039;t previously aware of).  Apparently, in the 1.7 versions of groovy, there&#039;s a bug that causes any method that is part of an interface to not be overridden by the metaClass.  Most groovy stuff doesn&#039;t use interfaces so it&#039;s normally not that big of a deal, but it is a big deal with Java stuff.

So I&#039;m able to override this zero param length method with no problem because it isn&#039;t part of an interface:

&lt;pre lang=&quot;groovy&quot;&gt;
class Bar {
    int length() {
        return 3
    }
}

def b = new Bar()

assert 3 == b.length()

b.metaClass.length = {-&gt; return 1}

assert 1 == b.length()
&lt;/pre&gt;

But String.length() gets put onto the String class because &lt;a href=&quot;http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#length()&quot; rel=&quot;nofollow&quot;&gt;length() is an implementation of a method from the CharSequence interface&lt;/a&gt;.</description>
		<content:encoded><![CDATA[<p>@arvind normally you need to be explicit about the closure being zero length when overriding it, so you&#8217;d need to do this:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #aaaadd; font-weight: bold;">String</span>.<span style="color: #006600;">metaClass</span>.<span style="color: #006600;">length</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">-&gt;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>I say &#8220;normally&#8221; as there&#8217;s actually a <a href="http://jira.codehaus.org/browse/GROOVY-3493" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/jira.codehaus.org/browse/GROOVY-3493?referer=');">weird bug/issue in groovy</a> that affects this particular test case to not make it work (that I wasn&#8217;t previously aware of).  Apparently, in the 1.7 versions of groovy, there&#8217;s a bug that causes any method that is part of an interface to not be overridden by the metaClass.  Most groovy stuff doesn&#8217;t use interfaces so it&#8217;s normally not that big of a deal, but it is a big deal with Java stuff.</p>
<p>So I&#8217;m able to override this zero param length method with no problem because it isn&#8217;t part of an interface:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Bar <span style="color: #66cc66;">&#123;</span>
    <span style="color: #993333;">int</span> length<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">3</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> b <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bar<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">==</span> b.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
b.<span style="color: #006600;">metaClass</span>.<span style="color: #006600;">length</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">-&gt;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">==</span> b.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>But String.length() gets put onto the String class because <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#length()" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html_length?referer=');">length() is an implementation of a method from the CharSequence interface</a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arvind</title>
		<link>http://naleid.com/blog/2009/06/01/groovy-metaclass-overriding-a-method-whilst-using-the-old-implementation/comment-page-1/#comment-2868</link>
		<dc:creator>Arvind</dc:creator>
		<pubDate>Wed, 24 Nov 2010 15:26:33 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=54#comment-2868</guid>
		<description>But the same is not happening for methods which doesn&#039;t take parameters. 
For example :

		String.metaClass.concat = { String s -&gt;
			println &#039;Coming herere &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; &#039;
			return 1
		}
		String.metaClass.length = {
			println &#039;Coming herere &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; &#039;
			return 1
		}

		println &quot;Foo&quot;.concat(&quot;bar&quot;)
		println &quot;Foo&quot;.length()


Expected result :

Coming herere &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; 
1
Coming herere &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; 
1

But result produced is :

Coming herere &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; 
1
3


Do you have any idea how to override methods that doesn&#039;t take parameters ?</description>
		<content:encoded><![CDATA[<p>But the same is not happening for methods which doesn&#8217;t take parameters.<br />
For example :</p>
<p>		String.metaClass.concat = { String s -&gt;<br />
			println &#8216;Coming herere &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; &#8216;<br />
			return 1<br />
		}<br />
		String.metaClass.length = {<br />
			println &#8216;Coming herere &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; &#8216;<br />
			return 1<br />
		}</p>
<p>		println &#8220;Foo&#8221;.concat(&#8220;bar&#8221;)<br />
		println &#8220;Foo&#8221;.length()</p>
<p>Expected result :</p>
<p>Coming herere &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br />
1<br />
Coming herere &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br />
1</p>
<p>But result produced is :</p>
<p>Coming herere &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br />
1<br />
3</p>
<p>Do you have any idea how to override methods that doesn&#8217;t take parameters ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Föredrag: Molnarkitektur och Groovy &#38; Grails &#171; Jonas funderingar</title>
		<link>http://naleid.com/blog/2009/06/01/groovy-metaclass-overriding-a-method-whilst-using-the-old-implementation/comment-page-1/#comment-1971</link>
		<dc:creator>Föredrag: Molnarkitektur och Groovy &#38; Grails &#171; Jonas funderingar</dc:creator>
		<pubDate>Wed, 09 Sep 2009 09:17:05 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=54#comment-1971</guid>
		<description>[...] började med att visa enkla exempel på hur man kan använda Groovys metaclass för att enkelt definiera om existerande metoder men även lägga till nya. Eftersom Groovy är [...]</description>
		<content:encoded><![CDATA[<p>[...] började med att visa enkla exempel på hur man kan använda Groovys metaclass för att enkelt definiera om existerande metoder men även lägga till nya. Eftersom Groovy är [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ted Naleid &#187; Modularizing Groovy Config Files With a Dash of Meta-Programming</title>
		<link>http://naleid.com/blog/2009/06/01/groovy-metaclass-overriding-a-method-whilst-using-the-old-implementation/comment-page-1/#comment-1688</link>
		<dc:creator>Ted Naleid &#187; Modularizing Groovy Config Files With a Dash of Meta-Programming</dc:creator>
		<pubDate>Fri, 31 Jul 2009 05:01:23 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=54#comment-1688</guid>
		<description>[...] is a continuation of a sporadic set of blog posts about some practical uses for groovy [...]</description>
		<content:encoded><![CDATA[<p>[...] is a continuation of a sporadic set of blog posts about some practical uses for groovy [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Deepak Mittal</title>
		<link>http://naleid.com/blog/2009/06/01/groovy-metaclass-overriding-a-method-whilst-using-the-old-implementation/comment-page-1/#comment-1448</link>
		<dc:creator>Deepak Mittal</dc:creator>
		<pubDate>Tue, 16 Jun 2009 07:34:42 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=54#comment-1448</guid>
		<description>Thanks Ted for the handy tip. I was looking for doing exactly this. 

Cheers!

-Deepak</description>
		<content:encoded><![CDATA[<p>Thanks Ted for the handy tip. I was looking for doing exactly this. </p>
<p>Cheers!</p>
<p>-Deepak</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh Reed</title>
		<link>http://naleid.com/blog/2009/06/01/groovy-metaclass-overriding-a-method-whilst-using-the-old-implementation/comment-page-1/#comment-1441</link>
		<dc:creator>Josh Reed</dc:creator>
		<pubDate>Mon, 08 Jun 2009 13:42:31 +0000</pubDate>
		<guid isPermaLink="false">http://naleid.com/blog/?p=54#comment-1441</guid>
		<description>Thanks, Ted.  I was just trying to figure out how to do this the other day, so this is quite timely.

Cheers,
Josh</description>
		<content:encoded><![CDATA[<p>Thanks, Ted.  I was just trying to figure out how to do this the other day, so this is quite timely.</p>
<p>Cheers,<br />
Josh</p>
]]></content:encoded>
	</item>
</channel>
</rss>

