<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>BlackBox : Tag mop</title>
    <link>http://www.warneronstine.com/blog/articles/tag/mop</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Where technology and art disappear</description>
    <item>
      <title>MetaClass/Delegate warning</title>
      <description>&lt;p&gt;I&amp;#8217;ve been working like a fiend on &lt;a href="http://warneronstine.com/cgi-bin/trac.cgi"&gt;Chama&lt;/a&gt; and am just about ready for an alpha release (not much but will have some basic model and dao creation from a groovy dsl paired with database goodness). Like a good programmer I have been building stuff up in chunks so that I can test everything separately and now this morning I started finally integrating everything together and blammo! something strange was happening.&lt;/p&gt;

&lt;p&gt;The first part of this puzzle lies in &lt;a href="http://www.warneronstine.com/cgi-bin/trac.cgi/browser/trunk/src/main/groovy/org/chama/builder/model/QueryBuilder.groovy"&gt;&lt;code&gt;QueryBuilder.groovy&lt;/code&gt;&lt;/a&gt;, this class is a builder that eventually will get called from inside of a finder method (like &lt;code&gt;findAll()&lt;/code&gt;) and get parsed by another meta class, but more on that in a minute. Inside of &lt;code&gt;QueryBuilder&lt;/code&gt; I do some checks on whether we are passing a closure or not, if we are then I set the delegate class to the current &lt;code&gt;QueryBuilder&lt;/code&gt; instance and call the closure. Thus, when the closure continues processing the next interior method of the closure gets called it calls the &lt;code&gt;QueryBuilder&lt;/code&gt; again allowing me to create new restrictions inside the current criteria. Running my tests and everything works as expected. Now for the strange part.&lt;/p&gt;

&lt;p&gt;In the next part I created a new class &lt;a href="http://www.warneronstine.com/cgi-bin/trac.cgi/browser/trunk/src/main/groovy/org/chama/builder/model/JavaModelDAOBuilder.groovy"&gt;&lt;code&gt;JavaModelDAOBuilder.groovy&lt;/code&gt;&lt;/a&gt; which will actually parse a Groovy script and call each method in turn through a custom meta class called &lt;code&gt;MethodInterceptor&lt;/code&gt;. This meta class has now essentially taken first responder position to any method calls that happen on the Groovy script. Which actually sounds good on the surface, until I discovered that a closure is actually &amp;#8220;owned&amp;#8221; by its class. Now that the class has a meta class it will intercept all calls to the closure, completely bypassing the delegate.&lt;/p&gt;

&lt;p&gt;After some digging I can see how this makes sense and after sending a message to the list I got some great responses. The final solution that Jochen(Blackdrag) came up with is to set a flag so that meta classes can &amp;#8220;delegate&amp;#8221; to another delegate. I think that this is definitely a workable solution, but for now I have some kind of hacky solution in the &lt;code&gt;MethodInterceptor&lt;/code&gt; so that it looks for anything that the delegate should actually be handling and pass it back to that instance.&lt;/p&gt;

&lt;p&gt;The good news is that it looks like this will be fixed in the near future, for those of us really pushing Groovy&amp;#8217;s Meta Object Programming (MOP) and I think I&amp;#8217;m going to be joining the dev list so that I can watch those conversations (especially the MOP-related ones). On another note I&amp;#8217;m working on a few more Meta-related posts that will hopefully be of interest to some people ;-).&lt;/p&gt;</description>
      <pubDate>Mon, 04 Jun 2007 20:32:22 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:3499c5f8-4aff-4379-abf9-856da2c0b536</guid>
      <author>Warner Onstine</author>
      <link>http://www.warneronstine.com/blog/articles/2007/06/04/metaclass-delegate-warning</link>
      <category>programming</category>
      <category>groovy</category>
      <category>dsl</category>
      <category>chama</category>
      <category>mop</category>
      <trackback:ping>http://www.warneronstine.com/blog/articles/trackback/294</trackback:ping>
    </item>
  </channel>
</rss>
