<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Better Software and beyond... &#187; MetaObjectProtocol</title>
	<atom:link href="http://www.thorsten-kamann.de/tag/metaobjectprotocol/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thorsten-kamann.de</link>
	<description>About architecture, development, quality and more</description>
	<lastBuildDate>Tue, 25 Oct 2011 20:16:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Dynamic Groovy Pt. 1</title>
		<link>http://www.thorsten-kamann.de/2009/06/03/dynamic-groovy-part1/</link>
		<comments>http://www.thorsten-kamann.de/2009/06/03/dynamic-groovy-part1/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 22:34:03 +0000</pubDate>
		<dc:creator>thorque</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[MetaObjectProtocol]]></category>
		<category><![CDATA[MOP]]></category>
		<category><![CDATA[Test]]></category>
		<category><![CDATA[Unit]]></category>
		<category><![CDATA[Unittest]]></category>

		<guid isPermaLink="false">http://www.thorsten-kamann.de/wordpress/?p=181</guid>
		<description><![CDATA[<a href="http://www.thorsten-kamann.de/2009/06/03/dynamic-groovy-part1/" title="Dynamic Groovy Pt. 1"></a>Certainly you know that Groovy is one of the dynamic languages published in the last years. There are some languages with this approach: Ruby, JRuby, Python, PHP, JavaScript and many more. Groovy is a little bit special if you are &#8230;<p class="read-more"><a href="http://www.thorsten-kamann.de/2009/06/03/dynamic-groovy-part1/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.thorsten-kamann.de/2009/06/03/dynamic-groovy-part1/" title="Dynamic Groovy Pt. 1"></a><p>Certainly you know that <a title="Groovy" href="http://groovy.codehaus.org" target="_blank">Groovy</a> is one of the dynamic languages published in the last years. There are some languages with this approach: <a title="Ruby" href="http://www.ruby-lang.org" target="_blank">Ruby</a>, <a title="JRuby" href="http://jruby.codehaus.org/" target="_blank">JRuby</a>, <a title="Python" href="http://www.python.org/" target="_blank">Python</a>, <a title="PHP" href="http://www.php.net/" target="_blank">PHP</a>, <a title="JavaScript" href="https://developer.mozilla.org/en/JavaScript" target="_blank">JavaScript</a> and many more.</p>
<p>Groovy is a little bit special if you are a Java-Developer. Groovy is built on the top of the <a title="Java" href="http://www.java.net" target="_blank">Java-Platform</a>. So it is easy to learn the concepts and syntax of this nice language. Aside of powerful syntax enhancements there are the dynamic approach. With the Meta Object Protocol (MOP) you are able to analyze and change every Object.</p>
<h3>Getting all properties of an Object</h3>
<p>Properties in Groovy are a little bit special. In difference to fields a property always have a Getter-/Setter-Method. It is very easy to get all of such properties:</p>
<pre lang="groovy">def someObject = new SomeJavaObject()
someObject.metaClass.getProperties().each{
    println it.name
}</pre>
<p>With this codefragment you get a list of all properties of the Object <span style="font-family: andale mono,times;">someObject</span>. The content of this list are <span style="font-family: andale mono,times;">groovy.lang.MetaBeanProperty</span>. With this class you can</p>
<ul>
<li>get and set the field</li>
<li>get and set the Getter and Setter separately</li>
<li>query for a special property</li>
</ul>
<p>You can see in Picture 1 the Classdiagram of the MetabeanProperty class. With the help of this class you can easily analyze and change existing Groovy classes.</p>
<div class="img alignnone size-full wp-image-194" style="width:434px;">
	<img src="http://www.bobdveloper.de/wp-content/uploads/2009/06/groovy_lang_metabeanproperty3.png" alt="groovy.lang.MetaBeanProperty" width="434" height="353" />
	<div>groovy.lang.MetaBeanProperty</div>
</div><br />
<strong>Picture 1: Classdiagram of <span style="font-family: andale mono,times;">groovy.lang.MetaBeanProperty</span></strong></p>
<h3>What is a MetaClass?</h3>
<p>Every Class has some meta informations. This are informations about fields, properties and methods. Additionally it exists an invokeMethod. This method is used to call dynamically methods of Groovy Classes. The behaviour of this method is similar to the Reflection mechnism of Java (you know the package <span style="font-family: andale mono,times;">java.lang.reflect</span>?).</p>
<p><strong><div class="img alignnone size-full wp-image-197" style="width:472px;">
	<img src="http://www.bobdveloper.de/wp-content/uploads/2009/06/groovy_lang_metaclass.png" alt="groovy.lang.MetaClass" width="472" height="373" />
	<div>groovy.lang.MetaClass</div>
</div><br />
</strong><strong>Picture 2: Classstructure of <span style="font-family: andale mono,times;">groovy.lang.MetaClass</span></strong></p>
<h3>Dynamically adding method to classes</h3>
<p>With the help of this class you are able to add new methods to an existing class. Even final (and immutable) classes could be extended:</p>
<pre lang="groovy" escaped="true">String.metaClass.toFirstUpper &lt;&lt; {
	delegate[0].toUpperCase() + delegate.substring(1)
}</pre>
<p>This little fragment adds one method to the final class java.lang.String. After this code is executed you can access this from your¬† Groovy Code. Please attend that this only works if you are calling the additional methods from Groovy. You cannot call it from your Java Code, because the Compiler doesn&#8217;t know anything of this additonally method. But you can use reflection with a simple invokeMethod to use the dynamically added method.</p>
<p>In Groovy you can use the new method directly:</p>
<pre lang="groovy">def myString = "thisIsASimpleString"
assert "ThisIsASimpleString" == myString.toFirstUpper()</pre>
<p>If you use Java you need to use Reflection</p>
<pre lang="java">String myString = "thisIsASimpleString";
Method m = String.class.getMethod("toFirstUpper", new Class[]{});
assert "ThisIsASimpleString" == m.invokeMethod(myString, new Object[]{});</pre>
<h3>Adding static methods</h3>
<p>This works for static methods, too.</p>
<pre escaped="true" lang="groovy">public class Customer{
   String firstName
   String lastName
}

Customer.metaClass.static.create &lt;&lt; { String first, String last -&gt;
    new Customer(firstName: first, lastName: last)
}

assert "Thorsten" == Customer.create("Thorsten", "Kamann").firstNam</pre>
<h3>Conclusion</h3>
<p>This was Part 1 of Dynamic Groovy. In the next part we see how to add dynamically add constructors, properties, and adding methods to interfaces.</p>
<h3>Links</h3>
<ul>
<li><a href="http://groovy.codehaus.org/ExpandoMetaClass+-+Methods" target="_blank">http://groovy.codehaus.org/ExpandoMetaClass</a></li>
<li><a href="http://groovy.codehaus.org/ExpandoMetaClass+-+Methods" target="_blank">http://groovy.codehaus.org/ExpandoMetaClass+-+Methods</a></li>
<li><a href="http://groovy.codehaus.org/ExpandoMetaClass+-+Static+Methods" target="_blank">http://groovy.codehaus.org/ExpandoMetaClass+-+Static+Methods</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.thorsten-kamann.de/2009/06/03/dynamic-groovy-part1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

