<?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>RealityForge.org: Meta-Programming in Java</title>
    <link>/articles/2006/03/09/meta-programming-in-java</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>A little short for a storm trooper</description>
    <item>
      <title>Meta-Programming in Java</title>
      <description>&lt;p&gt;I have been looking over my old code repository and I came across &lt;acronym title="Generated Entity Layer"&gt;GEL&lt;/acronym&gt;. &lt;span class="caps"&gt;GEL&lt;/span&gt; was a toolkit that allowed me to define entities and events for &lt;a href="http://en.wikipedia.org/wiki/High_Level_Architecture" title="High Level Architecture"&gt;HLA&lt;/a&gt;   using an &lt;span class="caps"&gt;XML&lt;/span&gt; data definition language.&lt;/p&gt;


	&lt;p&gt;The &lt;acronym title="High Level Architecture"&gt;HLA&lt;/acronym&gt; is a family of specifications developed to promote intereoperability and reuse of simulation assets. Unfortunately there is a significant development effort associated with construction of a &lt;span class="caps"&gt;HLA&lt;/span&gt;-compliant federation. Less than 2% of the 2500+ lines of code in the &amp;#8220;Hello World&amp;#8221; sample &lt;span class="caps"&gt;HLA&lt;/span&gt; application relates to simulation logic. The integration code is a significant cost and there is often a high-level of coupling between the simulation code and the integration code.&lt;/p&gt;


	&lt;p&gt;&lt;acronym title="Generated Entity Layer"&gt;GEL&lt;/acronym&gt; was created to simplify &lt;acronym title="High Level Architecture"&gt;HLA&lt;/acronym&gt; developers task by generating all the integration code from a simulation model defined in an &lt;span class="caps"&gt;XML&lt;/span&gt; document. The &lt;span class="caps"&gt;HLA&lt;/span&gt; developer had the sole responsibility of writing the simulation/logic code. Think of &lt;span class="caps"&gt;GEL&lt;/span&gt; as a a poor-mans &lt;a href="http://en.wikipedia.org/wiki/Model_Driven_Architecture"&gt;MDA&lt;/a&gt;  (I even planned to have a set of separate generators that would allow the simulation objects to be hosted in a Quake3 game). This approach rocks as you only need to fix and update the code in one place (the generator) and you are far more productive at the higher level of abstraction.&lt;/p&gt;


	&lt;p&gt;Each simulation object in the &lt;span class="caps"&gt;GEL XML&lt;/span&gt; document resulted in the generation of several Java classes by &lt;a href="http://jakarta.apache.org/velocity/"&gt;velocity&lt;/a&gt;.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;a &lt;code&gt;Model&lt;/code&gt; class for holding data of a specific instance.&lt;/li&gt;
		&lt;li&gt;a &lt;code&gt;ModelListener&lt;/code&gt; class that receives events relating to a &lt;code&gt;Model&lt;/code&gt; instance.&lt;/li&gt;
		&lt;li&gt;a &lt;code&gt;HLAPeer&lt;/code&gt; class that reflects the data in a &lt;code&gt;Model&lt;/code&gt; instance into the &lt;span class="caps"&gt;HLA&lt;/span&gt; runtime.&lt;/li&gt;
		&lt;li&gt;a &lt;code&gt;PersistPeer&lt;/code&gt; class that persists the data in a &lt;code&gt;Model&lt;/code&gt; instance.&lt;/li&gt;
		&lt;li&gt;a number of &lt;a href="http://www.martinfowler.com/ap2/valueObject.html"&gt;Value Object&lt;/a&gt; classes to represent various events&lt;/li&gt;
		&lt;li&gt;etc&amp;#8230;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;I had long believed in having a single high-level model of the domain objects and generating. Prior to &lt;span class="caps"&gt;XML I&lt;/span&gt; had used &lt;span class="caps"&gt;SGML&lt;/span&gt;, INI and custom formats to represent the model. On a number of occasions I have attempted to move the model data into the code to make it easier to keep up to date. I have used C/C++&amp;#8217;s macro expansions, XDoclet-like processors and Java 1.5 Annotations at various times.&lt;/p&gt;


	&lt;p&gt;Six months using &lt;a href="http://www.rubyonrails.org/"&gt;Ruby On Rails&amp;#8217;&lt;/a&gt; has changed my world view. The model classes are very close to the level at which I would create my domain model. The good thing about ruby is that you can always add more language constructs to make sure the code matches your domain model. (I know Lisp-ers have always raved about this but I have never been able to use lisp in a commercial environment). Things like &lt;a href="http://api.rubyonrails.com/classes/ActiveRecord/Acts/List/ClassMethods.html"&gt;acts_as_list&lt;/a&gt; to order your domain objects, &lt;a href="http://ar-versioned.rubyforge.org/"&gt;acts_as_versioned&lt;/a&gt; to version your domain objects or &lt;a href="http://www.railtie.net/articles/2006/02/05/rails-acts_as_threaded-plugin"&gt;acts_as_threaded&lt;/a&gt; to have a threaded tree representation radically simplify the representation of domain objects.&lt;/p&gt;


	&lt;p&gt;I have been thinking about how I would do this in Java but most of the neat stuff just does not seem possible without the introduction of a  &lt;a href="http://en.wikipedia.org/wiki/Meta-object_protocol"&gt;Meta-object Protocol&lt;/a&gt;  or some sort of load-time class file modification.&lt;/p&gt;


	&lt;p&gt;Consider a simple simulation object. In ruby I would represent it via&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Kettle&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;Model&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Base&lt;/span&gt;
  &lt;span class="ident"&gt;shared_attr&lt;/span&gt; &lt;span class="symbol"&gt;:temperature&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:float&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:resolution&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;1.0&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:accuracy&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;1.0&lt;/span&gt; 
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;In java there is no simple equivalent. I could just define the model in a class like below. However this would probably need to be instrumented during loading so that the runtime could monitor changes to the shared attribute.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;public class Kettle extends BaseModel
{
  @SharedAttribute(resolution = 1.0, accuracy = 1.0 )
  private float m_temperature;

  public float getTemperature()
  {
    return m_temperature;
  }

  public void setTemperature(final float temperature)
  {
    m_temperature = temperature;
  }
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Instrumentation in whatever form it takes is fairly complex even if you use something like AspectJ that is perfect for this scenario. Much more complex than writing the equivalent acts_as_* plugin.&lt;/p&gt;


	&lt;p&gt;The above only works when the instrumentation code does not need to add any methods. However if you want a simulation object to support shared events you need to be able to subscribe, unsubscribe and generate these events which means you need the same boilerplate code repeated over and over.&lt;/p&gt;


	&lt;p&gt;I can not think of anyway to do this in java so I guess it is back to generating the Java classes from a central model file. Pity I think I liked the ruby way better.&lt;/p&gt;</description>
      <pubDate>Thu, 09 Mar 2006 16:33:00 +1100</pubDate>
      <guid isPermaLink="false">urn:uuid:46b9155f-329f-4367-b264-7956dfc0babe</guid>
      <author>Peter Donald</author>
      <link>http://www.realityforge.org/articles/2006/03/09/meta-programming-in-java</link>
      <category>Java</category>
      <category>java</category>
      <category>ruby</category>
      <category>meta</category>
      <category>programming</category>
      <category>HLA</category>
      <category>MDA</category>
      <trackback:ping>http://www.realityforge.org/articles/trackback/32</trackback:ping>
    </item>
    <item>
      <title>"Meta-Programming in Java" by Peter Donald</title>
      <description>&lt;p&gt;I have been meaning to give groovy a try for a long time. I think you have given me just the motivation I needed to finally go and have a look. Thanks!&lt;/p&gt;</description>
      <pubDate>Sat, 11 Mar 2006 11:48:40 +1100</pubDate>
      <guid isPermaLink="false">urn:uuid:3e5f85dd-42bf-4a6f-b4a2-276d0b7b12e4</guid>
      <link>http://www.realityforge.org/articles/2006/03/09/meta-programming-in-java#comment-35</link>
    </item>
    <item>
      <title>"Meta-Programming in Java" by tog &lt;guillaume.alleon@gmail.com&gt;</title>
      <description>&lt;p&gt;Hi Peter,&lt;/p&gt;


	&lt;p&gt;Actually I think the Java solution you are looking for exists. Groovy is an agile &amp;#38; dynamic language for the &lt;span class='caps'&gt;JVM&lt;/span&gt; using a &lt;span class='caps'&gt;MOP&lt;/span&gt;. Moreover &lt;span class='caps'&gt;GRAILS&lt;/span&gt; a framework similar to Ruby On Rails is under active development and you should definitely give it a try. You will find more information on &lt;a href='http://groovy.codehaus.org' rel="nofollow"&gt;http://groovy.codehaus.org&lt;/a&gt; and &lt;a href='http://grails.codehaus.org' rel="nofollow"&gt;http://grails.codehaus.org&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Have fun with Groovy
Cheers
tog&lt;/p&gt;</description>
      <pubDate>Fri, 10 Mar 2006 09:13:55 +1100</pubDate>
      <guid isPermaLink="false">urn:uuid:94f8d5d5-f0f7-46b7-b939-d660be7b0a4c</guid>
      <link>http://www.realityforge.org/articles/2006/03/09/meta-programming-in-java#comment-33</link>
    </item>
  </channel>
</rss>
