<?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>A Concurrent Affair &#187; xajavac</title>
	<atom:link href="http://www.concurrentaffair.org/category/research/xajavac/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.concurrentaffair.org</link>
	<description>a blog about Mathias&#039; work and play.</description>
	<lastBuildDate>Tue, 07 Feb 2012 07:11:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>ANTLRWorks and OpenJDK Compiler Grammar</title>
		<link>http://www.concurrentaffair.org/2008/11/18/antlrworks-and-openjdk-compiler-grammar/</link>
		<comments>http://www.concurrentaffair.org/2008/11/18/antlrworks-and-openjdk-compiler-grammar/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 05:19:18 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[COMP202]]></category>
		<category><![CDATA[DrJava]]></category>
		<category><![CDATA[Pictures]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[Review]]></category>
		<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.concurrentaffair.org/?p=840</guid>
		<description><![CDATA[Now that my qualifying exam is behind me, I've ... <a href="http://www.concurrentaffair.org/2008/11/18/antlrworks-and-openjdk-compiler-grammar/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Now that my qualifying exam is behind me, I&#8217;ve got more time to focus on other things. I have to admit that the exam pretty much occupied all of my thoughts and hogged lots of time, to the degree that I didn&#8217;t prepare as well as I used to for my COMP 202 class. I think that has changed again now.</p>

<p>I&#8217;m also starting to look at the Java multi-stage programming project again, and I discovered that the <a href="http://openjdk.java.net/">OpenJDK</a> people have added a <a href="http://openjdk.java.net/projects/compiler-grammar/">Compiler Grammar</a> project. This project is trying to replace the hand-coded parser for javac by a parser generated from an <a href="http://www.antlr.org/">ANTLR</a> grammar file. I haven&#8217;t fully checked how far this has progressed, but it is interesting. Modifying the grammar to allow annotations on statements and expressions should be easier than rewriting the interpreter by hand. It seems like there is a Java 5 grammar that integrates into javac, but of course there is no <a href="http://groups.csail.mit.edu/pag/jsr308/">JSR 308</a> grammar, so I&#8217;d have to develop that first.</p>

<p>When I looked at ANTLR and the Compiler Grammar project, I also noticed an interesting IDE that has been built to make developing grammars easier: <a href="http://www.antlr.org/works/index.html">ANTLRWorks</a>. It integrates a several checking tools and a debugger and looks tremendously helpful. For example, it can explain how a grammar is ambiguous. In my case, an annotation on a parenthetical expression could be parsed in two different ways, so I&#8217;ll have to be careful there.</p>

<div id="attachment_841" class="wp-caption alignnone" style="width: 310px"><a href="http://www.concurrentaffair.org/wp-content/uploads/2008/11/ambiguouspath.jpg"><img src="http://www.concurrentaffair.org/wp-content/uploads/2008/11/ambiguouspath-300x210.jpg" alt="ANTLR Ambiguous Path Visualization" title="ambiguouspath" width="300" height="210" class="size-medium wp-image-841" /></a><p class="wp-caption-text">ANTLR Ambiguous Path Visualization</p></div>

<p>On MacOS, I&#8217;m still struggling a bit to get the Compiler Grammar project and ANTLRWorks running. It seems like Compiler Grammar needs <a href="http://landonf.bikemonkey.org/static/soylatte/">SoyLatte</a> to work, and I have managed to build the Compiler Grammar javac from the command line, but when I try to run it inside ANTLRWorks, then it seems like it is using the default Apple Java version 1.6.0-dp, which doesn&#8217;t work. Maybe I&#8217;ll take a look at it on Windows first.</p>

<p>I also need to start thinking about the COMP 202 final exam, and about a lecture schedule and programming tasks for COMP 402 next semester.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2008/11/18/antlrworks-and-openjdk-compiler-grammar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Multi-Methods Using Proxies and Annotations</title>
		<link>http://www.concurrentaffair.org/2008/10/03/java-multi-methods-using-proxies-and-annotations/</link>
		<comments>http://www.concurrentaffair.org/2008/10/03/java-multi-methods-using-proxies-and-annotations/#comments</comments>
		<pubDate>Sat, 04 Oct 2008 02:00:04 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.concurrentaffair.org/?p=616</guid>
		<description><![CDATA[After working a little bit with Java proxy clas... <a href="http://www.concurrentaffair.org/2008/10/03/java-multi-methods-using-proxies-and-annotations/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After working a little bit with <a href="http://java.sun.com/j2se/1.3/docs/guide/reflection/proxy.html">Java proxy classes</a> for my yet incomplete support library for <a href="http://www.cs.rice.edu/~mgricken/research/xajavac/">xajavac</a>, which introduces subtyping to annotations, I started to think about what else I could do with them.</p>

<p>Proxies are essentially a wrapper around another object. Instead of calling a method of the wrapped object, a single <code class="codecolorer text mac-classic"><span class="text">invoke</span></code> method is called and provided the <code class="codecolorer text mac-classic"><span class="text">Method</span></code> object of the method that was to be called, along with the parameters:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyProxy <span style="color: #000000; font-weight: bold;">implements</span> java.<span style="color: #006633;">lang</span>.<span style="color: #006633;">reflect</span>.<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainvocationhandler+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InvocationHandler</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> invoke<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> proxy, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amethod+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Method</span></a> m, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Athrowable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Throwable</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// ...</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>Inside this method, the programmer can now do string comparisons on the method name, for example, and dispatch different methods, or perform some kind of pre- and post-processing. I used this, together with a very simple <code class="codecolorer text mac-classic"><span class="text">@MultiMethod</span></code> marker annotation, to create multi-methods in Java.</p>

<p>Java normally only performs static dispatch and single dispatch. In static dispatch, the class of the method is decided at compile time already; in single (dynamic) dispatch, the class containing the method depends on the run-time type of the object. Static dispatch applies to static methods. Single dispatch, on the other hand, works with non-static methods and is the foundation of polymorphism. To demonstrate single dispatch, let&#8217;s consider the classic example of polymorphism:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">interface</span> IAnimal <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> speak<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> Cat <span style="color: #000000; font-weight: bold;">implements</span> IAnimal <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> speak<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Meow&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> Dog <span style="color: #000000; font-weight: bold;">implements</span> IAnimal <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> speak<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Woof&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// ...</span><br />
IAnimal a <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Cat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
a.<span style="color: #006633;">speak</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// prints &quot;Meow&quot;</span><br />
a <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Dog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
a.<span style="color: #006633;">speak</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// prints &quot;Woof&quot;</span></div></td></tr></tbody></table></div>

<p>At the call site, we just have an <code class="codecolorer text mac-classic"><span class="text">IAnimal</span></code>, and we call its <code class="codecolorer text mac-classic"><span class="text">speak()</span></code> method. Depending on whether the run-time type is <code class="codecolorer text mac-classic"><span class="text">Cat</span></code> or <code class="codecolorer text mac-classic"><span class="text">Dog</span></code>, the appropriate method is called. The dispatch is based on the receiver, object whose method is called.</p>

<p>However, the dispatch <em>only</em> depends on the receiver; the run-time types of the arguments do not matter. For example, if we create a <code class="codecolorer text mac-classic"><span class="text">feed(IFood food)</span></code> method in <code class="codecolorer text mac-classic"><span class="text">IAnimal</span></code> and then provide a specialized <code class="codecolorer text mac-classic"><span class="text">feed(Catnip food)</span></code> method in the <code class="codecolorer text mac-classic"><span class="text">Cat</span></code> class, the general <code class="codecolorer text mac-classic"><span class="text">Cat.feed(IFood food)</span></code> method will be called even when the argument is of type <code class="codecolorer text mac-classic"><span class="text">Catnip</span></code>. It is single dispatch, not multiple dispatch:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">interface</span> IFood <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> Catnip <span style="color: #000000; font-weight: bold;">implements</span> IFood <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> PetFood <span style="color: #000000; font-weight: bold;">implements</span> IFood <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">interface</span> IAnimal <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> feed<span style="color: #009900;">&#40;</span>IFood food<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> Cat <span style="color: #000000; font-weight: bold;">implements</span> IAnimal <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> feed<span style="color: #009900;">&#40;</span>IFood food<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Num num&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> feed<span style="color: #009900;">&#40;</span>Catnip food<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Meow, my favorite!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> Dog <span style="color: #000000; font-weight: bold;">implements</span> IAnimal <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> feed<span style="color: #009900;">&#40;</span>IFood food<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gobble gobble&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// ...</span><br />
IAnimal a <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Cat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
a.<span style="color: #006633;">feed</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PetFood<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// prints &quot;Num num&quot;</span><br />
a.<span style="color: #006633;">feed</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Catnip<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// also prints &quot;Num num&quot;, not &quot;Meow, my favorite!&quot;</span><br />
a <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Dog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
a.<span style="color: #006633;">feed</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PetFood<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// prints &quot;Gobble gobble&quot;</span></div></td></tr></tbody></table></div>

<p>If Java were to call the most appropriate method, where the class is determined by the receiver run-time type, and the actual overloaded method by the best match of the argument run-time types, then Java would be performing multiple dispatch. This, however, is not part of the Java Language Specification.</p>

<p>Using proxies, however, it is possible to intercept the call to the <code class="codecolorer text mac-classic"><span class="text">feed(IFood food)</span></code> methods, look through the overloaded methods and compare their parameter types to the provided argument run-time types, and then call the method that is the closest to the run-time types, i.e. where all argument types are supertypes of the argument run-time types, and where the types are most closely related (defined as the number of edges separating the nodes in the type hierarchy).</p>

<p>To get a behavior like the one above, Java programmers (and programmers of most languages) usually employ the Visitor design pattern. Multi-methods (methods with multiple dispatch) make this unnecessary. Here is a more useful example of an abstract syntax tree (AST) consisting of integer and variable leaves and addition and multiplication interior nodes. First the code using the Visitor pattern to achieve double dispatch:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">interface</span> IAST <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> execute<span style="color: #009900;">&#40;</span>IASTVisitor v<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> Num <span style="color: #000000; font-weight: bold;">implements</span> IAST <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> _num<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Num<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _num <span style="color: #339933;">=</span> n<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getNum<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> _num<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> execute<span style="color: #009900;">&#40;</span>IASTVisitor v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> v.<span style="color: #006633;">numCase</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> Var <span style="color: #000000; font-weight: bold;">implements</span> IAST <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> _var<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Var<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _var <span style="color: #339933;">=</span> v<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getVar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> _var<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> execute<span style="color: #009900;">&#40;</span>IASTVisitor v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> v.<span style="color: #006633;">varCase</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> Add <span style="color: #000000; font-weight: bold;">implements</span> IAST <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IAST _left<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IAST _right<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Add<span style="color: #009900;">&#40;</span>IAST l, IAST r<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _left <span style="color: #339933;">=</span> l<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _right <span style="color: #339933;">=</span> r<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> IAST getLeft<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> _left<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> IAST getRight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> _right<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> execute<span style="color: #009900;">&#40;</span>IASTVisitor v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> v.<span style="color: #006633;">addCase</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> Mul <span style="color: #000000; font-weight: bold;">implements</span> IAST <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IAST _left<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IAST _right<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Mul<span style="color: #009900;">&#40;</span>IAST l, IAST r<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _left <span style="color: #339933;">=</span> l<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _right <span style="color: #339933;">=</span> r<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> IAST getLeft<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> _left<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> IAST getRight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> _right<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> execute<span style="color: #009900;">&#40;</span>IASTVisitor v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> v.<span style="color: #006633;">mulCase</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">interface</span> IASTVisitor <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> numCase<span style="color: #009900;">&#40;</span>Num ast<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> varCase<span style="color: #009900;">&#40;</span>Var ast<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> addCase<span style="color: #009900;">&#40;</span>Add ast<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> mulCase<span style="color: #009900;">&#40;</span>Mul ast<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> ToStringVisitor <span style="color: #000000; font-weight: bold;">implements</span> IASTVisitor <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> numCase<span style="color: #009900;">&#40;</span>Num ast<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>ast.<span style="color: #006633;">getNum</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> varCase<span style="color: #009900;">&#40;</span>Var ast<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> ast.<span style="color: #006633;">getVar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> addCase<span style="color: #009900;">&#40;</span>Add ast<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;(&quot;</span><span style="color: #339933;">+</span>ast.<span style="color: #006633;">getLeft</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; + &quot;</span><span style="color: #339933;">+</span>ast.<span style="color: #006633;">getRight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>Mul ast<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;(&quot;</span><span style="color: #339933;">+</span>ast.<span style="color: #006633;">getLeft</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; * &quot;</span><span style="color: #339933;">+</span>ast.<span style="color: #006633;">getRight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//...</span><br />
IAST a <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Mul<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Add<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;x&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> Num<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> Num<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
a.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ToStringVisitor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>

<p>The interesting point here is that each subclass of <code class="codecolorer text mac-classic"><span class="text">IAST</span></code> has to have an <code class="codecolorer text mac-classic"><span class="text">execute(IASTVisitor v)</span></code> method that accepts the visitor. The method implementations in the subclasses then call the corresponding case method in the <code class="codecolorer text mac-classic"><span class="text">IASTVisitor</span></code>. When we execute the visitor on the AST (the host), we achieve double dispatch because there are two instances of single dispatch, once depending on the run-time type of the host, <code class="codecolorer text mac-classic"><span class="text">a</span></code>, and again inside the <code class="codecolorer text mac-classic"><span class="text">execute</span></code> method depending on the run-time type of the visitor, <code class="codecolorer text mac-classic"><span class="text">v</span></code>.</p>

<p>With multi-methods, we can now remove the <code class="codecolorer text mac-classic"><span class="text">execute</span></code> methods in the <code class="codecolorer text mac-classic"><span class="text">IAST</span></code> hierarchy. We now just have an overloaded <code class="codecolorer text mac-classic"><span class="text">apply</span></code> method in <code class="codecolorer text mac-classic"><span class="text">IASTVisitor</span></code> where the type of the parameter changes and also determines which method gets invoked:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">interface</span> IAST <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> Num <span style="color: #000000; font-weight: bold;">implements</span> IAST <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> _num<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Num<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _num <span style="color: #339933;">=</span> n<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getNum<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> _num<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> Var <span style="color: #000000; font-weight: bold;">implements</span> IAST <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> _var<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Var<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _var <span style="color: #339933;">=</span> v<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getVar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> _var<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> Add <span style="color: #000000; font-weight: bold;">implements</span> IAST <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IAST _left<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IAST _right<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Add<span style="color: #009900;">&#40;</span>IAST l, IAST r<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _left <span style="color: #339933;">=</span> l<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _right <span style="color: #339933;">=</span> r<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> IAST getLeft<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> _left<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> IAST getRight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> _right<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> Mul <span style="color: #000000; font-weight: bold;">implements</span> IAST <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IAST _left<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IAST _right<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Mul<span style="color: #009900;">&#40;</span>IAST l, IAST r<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _left <span style="color: #339933;">=</span> l<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _right <span style="color: #339933;">=</span> r<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> IAST getLeft<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> _left<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> IAST getRight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> _right<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
@MultiMethod<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> IASTVisitor <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>IAST ast<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>Num ast<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>Var ast<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>Add ast<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>Mul ast<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> ToStringVisitor <span style="color: #000000; font-weight: bold;">implements</span> IASTVisitor <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> IASTVisitor create<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; IASTVisitor v <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ToStringVisitor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; v.<span style="color: #006633;">proxy</span> <span style="color: #339933;">=</span> MMProxy.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> v.<span style="color: #006633;">proxy</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> IASTVisitor proxy<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>IAST ast<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> AssertionError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Should never happen.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>Num ast<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>ast.<span style="color: #006633;">getNum</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>Var ast<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> ast.<span style="color: #006633;">getVar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>Add ast<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;(&quot;</span><span style="color: #339933;">+</span>proxy.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>ast.<span style="color: #006633;">getLeft</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; + &quot;</span><span style="color: #339933;">+</span>proxy.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>ast.<span style="color: #006633;">getRight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>Mul ast<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;(&quot;</span><span style="color: #339933;">+</span>proxy.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>ast.<span style="color: #006633;">getLeft</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; * &quot;</span><span style="color: #339933;">+</span>proxy.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>ast.<span style="color: #006633;">getRight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//...</span><br />
IAST a <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Mul<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Add<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;x&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> Num<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> Num<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ToStringVisitor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>

<p>The <code class="codecolorer text mac-classic"><span class="text">@MultiMethod</span></code> marker annotation states that all methods in the <code class="codecolorer text mac-classic"><span class="text">IASTVisitor</span></code> interface and all its subclasses should use multiple dispatch. To minimize overhead, this annotation can also be placed just in front of individual methods, then only those methods will use multiple dispatch.</p>

<p>There needs to be a method that takes the supertype of the AST classes, <code class="codecolorer text mac-classic"><span class="text">IAST</span></code>, even though that method should never be called, because there are no objects of that type (unless you create anonymous inner classes, or add another subclass, e.g. <code class="codecolorer text mac-classic"><span class="text">Sub</span></code>, for which there is no overloaded method). We therefore throw an exception there. Then there are the method that handle the concrete subclasses. This time, they all have the same name and overload the general method above that accepts the supertype.</p>

<p>Since we need to create the proxy that wraps around the object with the multi-methods, i.e. around the visitor here, we cannot just create the visitor using the <code class="codecolorer text mac-classic"><span class="text">new</span></code> keyword. Instead, we have a static <code class="codecolorer text mac-classic"><span class="text">create()</span></code> method that creates a new object and creates the proxy around it, and then returns it typed as interface. This is important! It must be returned as an <code class="codecolorer text mac-classic"><span class="text">IASTVisitor</span></code>, not as a <code class="codecolorer text mac-classic"><span class="text">ToStringVisitor</span></code>, because as it turns out, the proxies can only simulate interfaces, not classes. The created proxy <em>is</em> an <code class="codecolorer text mac-classic"><span class="text">IASTVisitor</span></code>, but it is <em>not</em> a <code class="codecolorer text mac-classic"><span class="text">ToStringVisitor</span></code>, even though it wraps around one.</p>

<p>Another very important difference is apparent in the methods for the composites <code class="codecolorer text mac-classic"><span class="text">Add</span></code> and <code class="codecolorer text mac-classic"><span class="text">Mul</span></code>, where recursion is necessary. Using the visitor, we could recurse into the subtrees using <code class="codecolorer text mac-classic"><span class="text">ast.getLeft().execute(this)</span></code>, where <code class="codecolorer text mac-classic"><span class="text">this</span></code> was the visitor itself. Since we want the multiple dispatch behavior in the recursion too, we need to use the proxy object for the recursion, <em>not</em> the visitor itself. That means we cannot use <code class="codecolorer text mac-classic"><span class="text">this</span></code>, because <code class="codecolorer text mac-classic"><span class="text">this</span></code> is the visitor, not the proxy. Instead, we use the <code class="codecolorer text mac-classic"><span class="text">proxy</span></code> field, where the <code class="codecolorer text mac-classic"><span class="text">create()</span></code> method stored the created proxy. This is really inelegant and can cause numerous problems: If we accidentally use <code class="codecolorer text mac-classic"><span class="text">this</span></code>, we lose the multiple dispatch; we could also make a mistake and overwrite the <code class="codecolorer text mac-classic"><span class="text">proxy</span></code> field, with <code class="codecolorer text mac-classic"><span class="text">null</span></code> for example. But I haven&#8217;t found a better way.</p>

<p>Finally, there is a huge limitation: As stated, proxies can only simulate interfaces, not classes. That means that the classes around which we wrap a proxy can <em>only</em> implement interfaces, but <em>not</em> extend other classes. You can never use the <code class="codecolorer text mac-classic"><span class="text">extends</span></code> keyword! That doesn&#8217;t for good code reuse. For example, even though pretty much every visitor needs the <code class="codecolorer text mac-classic"><span class="text">proxy</span></code> field, I cannot hoist it into an abstract class. The following example will cause a <code class="codecolorer text mac-classic"><span class="text">ClassCastException</span></code>:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@MultiMethod<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> IASTVisitor <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>IAST ast<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>Num ast<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>Var ast<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>Add ast<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>Mul ast<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> AASTVisitor <span style="color: #000000; font-weight: bold;">implements</span> IASTVisitor <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> IASTVisitor proxy<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>IAST ast<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> AssertionError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Should never happen.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> ToStringVisitor <span style="color: #000000; font-weight: bold;">extends</span> AASTVisitor <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> IASTVisitor create<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; IASTVisitor v <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ToStringVisitor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; v.<span style="color: #006633;">proxy</span> <span style="color: #339933;">=</span> MMProxy.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> v.<span style="color: #006633;">proxy</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>Num ast<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>ast.<span style="color: #006633;">getNum</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>Var ast<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> ast.<span style="color: #006633;">getVar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>Add ast<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;(&quot;</span><span style="color: #339933;">+</span>proxy.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>ast.<span style="color: #006633;">getLeft</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; + &quot;</span><span style="color: #339933;">+</span>proxy.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>ast.<span style="color: #006633;">getRight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> apply<span style="color: #009900;">&#40;</span>Mul ast<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;(&quot;</span><span style="color: #339933;">+</span>proxy.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>ast.<span style="color: #006633;">getLeft</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; * &quot;</span><span style="color: #339933;">+</span>proxy.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span>ast.<span style="color: #006633;">getRight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>That&#8217;s what we would ideally want to do, but we can&#8217;t. This is really a huge impediment, but right now, without heavier machinery such as a bytecode-rewriting class loader, I can&#8217;t find a way around it. Proxies are promising, but not as powerful as I would like them to be.</p>

<p>I&#8217;m nonetheless making the source code for <a href="http://www.cs.rice.edu/~mgricken/research/mmAnnot/download/mmAnnot.zip">Java multi-methods using proxies and annotations</a> available, even though it&#8217;s definitely a work in progress.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2008/10/03/java-multi-methods-using-proxies-and-annotations/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>And Another Week Gone</title>
		<link>http://www.concurrentaffair.org/2008/09/24/and-another-week-gone/</link>
		<comments>http://www.concurrentaffair.org/2008/09/24/and-another-week-gone/#comments</comments>
		<pubDate>Wed, 24 Sep 2008 14:45:44 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[Graduate School]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.concurrentaffair.org/?p=599</guid>
		<description><![CDATA[The last week was still spent catching up from ... <a href="http://www.concurrentaffair.org/2008/09/24/and-another-week-gone/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The last week was still spent catching up from Ike. I got lucky, I got power back early, but until pretty much today, the area of Houston that I see on my bike commute to work had been without power. There are still lots of uprooted trees that need to be cleared, and the trash bags are piled high along the streets. Traffic lights seem to be working a bit better, and grocery stores are stocked again.</p>

<p>I was supposed to give a presentation right after Ike, but that day Rice was closed, and not only I was affected, so my talk got pushed back by a week. I finally gave it this Monday, on September 22, in the RAP seminar. It was titled <a href="http://www.cs.rice.edu/~mgricken/research/xajavac/download/Java%20Annotations%20for%20Invariant%20Specification.ppt">Java Annotations for Invariant Specification</a> and was about Java annotations in general, how I used them to specify and check program invariants, and how much easier that got once I had extended Java to support subtyping for annotations as well. The talk was generally well received, but many of the comments said I should have given a clear definition of what annotations are at the beginning, not interspersed throughout the lecture. I didn&#8217;t want to make it so syntax-heavy at the beginning, but I guess I was wrong. I also ran out of time towards the end.</p>

<p>After that, I spent the last two days configuring the <a href="http://groups.csail.mit.edu/pag/jsr308/">JSR 308 prototype compiler</a> on my Mac and my PC at home. It&#8217;s not as easy as it sounds, but I finally got it to work, except for the tests. Yesterday I also transferred the changes from <a href="http://www.cs.rice.edu/~mgricken/research/xajavac/">xajavac</a> that allow subtyping for annotations into the JSR 308 compiler. But that&#8217;s still a work in progress.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2008/09/24/and-another-week-gone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is It Safe?</title>
		<link>http://www.concurrentaffair.org/2008/08/08/is-it-safe/</link>
		<comments>http://www.concurrentaffair.org/2008/08/08/is-it-safe/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 17:42:17 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[Graduate School]]></category>
		<category><![CDATA[OOP Book]]></category>
		<category><![CDATA[Ramblings]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.concurrentaffair.org/?p=543</guid>
		<description><![CDATA[For the last couple of days I have been contemp... <a href="http://www.concurrentaffair.org/2008/08/08/is-it-safe/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>For the last couple of days I have been contemplating buying another fire-proof security file. I currently have two <a href="http://www.sentrysafe.com/products/productDetail.aspx?s=176">Sentry KS-4100</a> files.</p>

<p>I keep a lot of records: Some are important, like identification documents for three countries, my last will and testament, lease agreements, insurance policies, deeds of ownership, computer backups, and, yes, even tax records. Others hopefully aren&#8217;t so important: Receipts for many of the items I have purchased. My credit card has a nice benefit that extends the manufacturer&#8217;s warranty by up to a year, and to claim that benefit, I need the receipt. Also, in case of a disaster that destroys my property, my renters insurance will want to examine the receipts so I can be reimbursed for the purchase or replacement costs, not just what the lost items were worth at the time.</p>

<p>Unfortunately, I&#8217;m running out of room for these receipts. While looking at buying another KS-4100, I more closely inspected its <a href="http://ulstandardsinfonet.ul.com/scopes/0072.html">UL-72</a> certification. This particular Sentry file is manufactured to keep the interior at or below 350°F (177°C) for a fire that rages at 1550°F (855°C) for half an hour, which is apparently typical for an apartment fire.</p>

<p>As we all know, paper burns at <a href="http://en.wikipedia.org/wiki/Fahrenheit_451">451°F</a> (233°C) and it singes a bit sooner, at about 405°F (207°C), so keeping the interior of the file at or below 350°F keeps paper safe. Sentry also states that &#8220;CDs, DVDs, USB drives and memory sticks&#8221; are protected.</p>

<p>CDs and DVDs are mostly made of polycarbonate, with a melting point 513°F (267°C); jewel cases are mostly polystyrene that melts at 464°F (240°C), so CDs, DVDs and their cases are protected. I didn&#8217;t check the material of memory sticks and USB drives, although I&#8217;m a little worried about my portable hard drives now.</p>

<p>However, I also keep credit cards and ID cards in there, and they are usually made of polyvinyl chloride (PVC)<sup><a href="#footnote-1-543" id="footnote-link-1-543" title="See the footnote.">1</a></sup><sup>,</sup><sup><a href="#footnote-2-543" id="footnote-link-2-543" title="See the footnote.">2</a></sup>, and I organize the receipts in nice colorful transparent polypropylene (PP) draw-string envelopes. PVC products, and therefore nearly all ID and credit cards, melt at a mere 176°F (80°F), and my PP envelopes melt at 338°F (170°F), just below the temperature the safe could reach while working as certified.</p>

<p>I&#8217;m really experiencing mixed up feelings right now, and I am considering switching to paper envelopes for organization, but the PP envelopes are so much more resilient and practical, and the temperature difference from 338°F to 350°F isn&#8217;t that great. That&#8217;s perhaps a risk I&#8217;m willing to take.</p>

<p>With IDs and credit cards, however, that&#8217;s a completely different matter. I&#8217;m fairly certain that in an apartment fire, the inside of the safe could easily reach 176°F and melt my passports, my international driver&#8217;s licenses and ID cards, and my credit cards (oh shucks, I&#8217;ll just get new ones, Washington Mutual willing, but that&#8217;s another rant for another hour).</p>

<p>I <em>could</em> get a data-storage security file like the <a href="http://www.sentrysafe.com/products/productDetail.aspx?s=97&#038;WT.si_n=ShoppingCart&#038;WT.si_p=CartView">Sentry 6720</a>, which keeps the interior at or below 125°F (52°C). Apparently that&#8217;s good enough for floppy disks, and assumeably for hard drives as well, but unless I&#8217;m mistaken, it&#8217;s still too hot for credit and ID cards! And more importantly, it retails more than ten times the cost of the KS-4100.</p>

<p>How do protect your documents? Am I paranoid? Am I a &#8220;filing rat&#8221;, and you simply don&#8217;t have documents to protect? Please send me your ideas, if you like.</p>
<br />Footnotes:<br /><ol class="footnotes"><li id="footnote-1-543">I have since been corrected that credit cards are usually produced from polyvinyl chloride acetate, or PVCA, but I haven&#8217;t found the melting point of that yet.  [<a href="#footnote-link-1-543">back</a>]</li><li id="footnote-2-543">Apparently, the properties of PVCA can vary a lot, but one low figure that was provided to me was 257°F (125°C), which is still too low to be protected in the KS-4100 security file; PVCA cards would probably be safe in the expensive Sentry 6720 media file mentioned below.  [<a href="#footnote-link-2-543">back</a>]</li></ol>]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2008/08/08/is-it-safe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSR 308 Animosity</title>
		<link>http://www.concurrentaffair.org/2008/05/14/jsr-308-animosity/</link>
		<comments>http://www.concurrentaffair.org/2008/05/14/jsr-308-animosity/#comments</comments>
		<pubDate>Wed, 14 May 2008 07:47:04 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[Ramblings]]></category>
		<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.concurrentaffair.org/?p=526</guid>
		<description><![CDATA[I've come across two blog posts that leave me p... <a href="http://www.concurrentaffair.org/2008/05/14/jsr-308-animosity/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve come across two blog posts that leave me perplexed: First there was <a href="http://www.michaelnygard.com/blog/2008/05/when_should_you_jump_jsr_308_t.html">&#8220;When Should You Jump? JSR 308. That&#8217;s When.&#8221;</a>, and now I just found <a href="http://bc-squared.blogspot.com/2008/05/what-hath-java-wrought.html">&#8220;What Hath Java Wrought&#8221;</a>.</p>

<p>I don&#8217;t know anything about the authors and I have only read the two posts, but statements like &#8220;Every language has a complexity budget. Java blew through it with generics in Java 5.&#8221; and &#8220;First we got Generics. We all know how that went. While it&#8217;s passably better than what was originally there, it&#8217;s still not worth a damn.&#8221; make me furrow my brow. As for generics, I gladly trade in the more complicated syntax for better static typing. Annotation processors can do the same for non-null values, mutability, problems with equality and identity. They can bring linear types into the language, and make pretty much every kind of program analysis I can think of easier.</p>

<p>I don&#8217;t understand why there is such much animosity towards JSR 308. It is much less intrusive than generics were; unless you plan to use raw types, the updated collections libraries virtually forced you to adopt generics to a certain degree. That&#8217;s not the case at all with annotations. If you like you can easily ignore the entire idea.</p>

<p>Now, perhaps I&#8217;m just not a good programmer, and that&#8217;s why I prefer to ride with training wheels like generics, more precise type systems and better warnings, but this kind of humility has never hurt me so far. It does not make sense to be so frugal on the syntax side; the semantic benefits by far outweigh the costs.</p>

<p>I&#8217;m definitely not a fan of how generics were implemented using erasure, and I believe there are shortcomings both in the annotations provided with Java 1.5 and JSR-308 (see <a href="http://www.cs.rice.edu/~mgricken/research/laptjavac/">LAPT-javac</a>, <a href="http://www.cs.rice.edu/~mgricken/research/xajavac/index.shtml">xajavac</a> and <a href="http://www.concurrentaffair.org/?s=lapt-javac">blog posts</a>), but I&#8217;m a firm believer that programmers have benefited a lot from generics, and that they will benefit in a similar way from annotations.</p>

<p>The beautiful thing about annotations is that dealing with them works on two different difficulty levels: Implementing annotations is not perfectly straight-forward, they use a syntax that is a bit different than the one used for regular Java classes, and that can be a bit confusing. Using annotations that have already been implemented, however, is easy; it&#8217;s an absolute piece of cake. This means that the annotations framework, especially with the annotation processors in JSR 308, can be used by more seasoned programmers to provide a lot of value even to programmers who do not know all of the details.</p>

<p>As a programming language researcher, I&#8217;m excited about annotations because they allow me to extend and modify the Java language in a relatively safe, convenient and compatible way. The resulting experimental languages may not be the most elegant ones, but they do the trick. Considering the two different levels of difficulty, and the possibility to completely ignore annotations, I have no idea why programmers would dislike annotations so much that they would abandon a language that can still be used exactly the same as without annotations.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2008/05/14/jsr-308-animosity/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Semi-Weekly Log from 2/18/08 to 2/21/08</title>
		<link>http://www.concurrentaffair.org/2008/02/21/semi-weekly-log-from-21808-to-22108/</link>
		<comments>http://www.concurrentaffair.org/2008/02/21/semi-weekly-log-from-21808-to-22108/#comments</comments>
		<pubDate>Fri, 22 Feb 2008 04:04:01 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[DrJava]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.concurrentaffair.org/index.php/2008/02/21/semi-weekly-log-from-21808-to-22108/</guid>
		<description><![CDATA[Again, another (semi-)weekly log from the DrJav... <a href="http://www.concurrentaffair.org/2008/02/21/semi-weekly-log-from-21808-to-22108/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Again, another (semi-)weekly log from the DrJava blog. I&#8217;m really trying to crank out the basic design and most of the features quickly, and this is my major focus right now, because that way I can hopefully focus on a SIGCSE paper I plan to write (deadline: March 19) and the Java extension that allows subtyping for annotation. But here we go:</p>

<p>Changed keyboard shortcut for Debug mode to MASK + D, instead of just D.</p>

<p>Added DrJavaProperties and PropertyMaps instead of a Map of Properties. These DrJavaProperties can be lazy and delay computationally intensive tasks like that of finding all compiled class files until actually necessary. In the &#8220;Preview&#8221;, they may display stale values.</p>

<p>Two basic categories existed before, &#8220;Java&#8221; with the System.getProperties, and &#8220;Config&#8221; with the values of the DrJava configuration. The &#8220;Config&#8221; category was previously named &#8220;DrJava&#8221;, but I thought &#8220;Config&#8221; was a better name. All the entries in &#8220;Config&#8221; start with &#8220;config.&#8221;. The entries in &#8220;Java&#8221; have the names as specified by Sun.</p>

<p>A new category has been added, &#8220;DrJava&#8221;, with just a few sample variables. One of them is &#8220;drjava.current.time.millis&#8221;, which contains the current time in milliseconds since the beginning of the epoche &#8212; useless, but a good first test.</p>

<p>&#8220;drjava.current.file&#8221; contains the currently open document as an absolute path.</p>

<p>&#8220;drjava.all.files&#8221; contains all open files, separated by File.pathSeparator. &#8220;drjava.project.files&#8221;, &#8220;drjava.included.files&#8221; and &#8220;drjava.external.files&#8221; do the same for project files, external files that are saved with the project, and files that are open but not part of the project.</p>

<p>The same &#8220;drjava.&#42;.files&#8221; properties exist as &#8220;drjava.&#42;.files.spaces&#8221;, but here, a space is used to separate the files.</p>

<p>Properties can listen to each other to find out when their values have been invalidated. There is a debug mechanism in place that prevents infinite loops.</p>

<p>These are only some very basic sample properties, and we need lots more, and we need to make sure that we invalidate the values of the properties in all the right places, but it&#8217;s a good start.</p>

<p>On a Linux system, for example, try opening a project and then run an external process with the command line:</p>

<p><pre>tar cfvz test.tar.gz ${drjava.all.files.spaces}</pre></p>

<p>It will create a tar file with all the source files currently open.</p>

<p>Not bad, eh? Please help me figure out what other properties are needed, and how we can easily make them relative to other directories, etc.</p>

<p>This is a massive commit, with far-reaching changes. Please update your working copy of DrJava to the newest revision and test the changes, the new features, and also the old ones to make sure I haven&#8217;t broken anything.</p>

<p>Thanks!</p>

<p><pre>M    src/edu/rice/cs/drjava/config/OptionConstants.java
A    src/edu/rice/cs/drjava/config/DrJavaProperty.java
A    src/edu/rice/cs/drjava/config/JavaSystemProperty.java
A    src/edu/rice/cs/drjava/config/PropertyMaps.java
A    src/edu/rice/cs/drjava/config/ConfigProperty.java
A    src/edu/rice/cs/drjava/config/EagerProperty.java
M    src/edu/rice/cs/drjava/ui/MainFrame.java
M    src/edu/rice/cs/drjava/ui/InsertVariableDialog.java
M    src/edu/rice/cs/drjava/ui/ExecuteExternalDialog.java
M    src/edu/rice/cs/util/JVMProcessCreator.java
M    src/edu/rice/cs/util/StringOps.java
M    src/edu/rice/cs/util/StringOpsTest.java
M    src/edu/rice/cs/util/ProcessCreator.java</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2008/02/21/semi-weekly-log-from-21808-to-22108/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Still Working on Subtyped Annotation API</title>
		<link>http://www.concurrentaffair.org/2007/12/23/still-working-on-subtyped-annotation-api/</link>
		<comments>http://www.concurrentaffair.org/2007/12/23/still-working-on-subtyped-annotation-api/#comments</comments>
		<pubDate>Sun, 23 Dec 2007 19:59:54 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[Concurrent Unit Testing]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.superscalar.org/blog/index.php/2007/12/23/still-working-on-subtyped-annotation-api/</guid>
		<description><![CDATA[Just when I thought I had a complete working AP... <a href="http://www.concurrentaffair.org/2007/12/23/still-working-on-subtyped-annotation-api/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Just when I thought I had a complete working API for my annotations with subtyping, and after I had written a special version of my invariant checker that uses annotations with subtyping, I ran into some trouble with more complicated annotations that contain other annotations. For example:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@Not<span style="color: #009900;">&#40;</span>@And<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>@Or<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>@ThreadWithName<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @ThreadWithName<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bar&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@And<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>@EventThread,@SynchronizedThis<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testNotAndOrTwoAndTwo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;testNotAndOrTwoAndTwo!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>Internally, the JVM instantiates these annotation classes using proxies, just like I do. These proxies are created at runtime and therefore, of course, do not have any associated class files. My API was still trying to find them and failing. I think I&#8217;ve got most of those issues fixed now, but the holidays have taken over, and I was also busy with DrJava for a few days.</p>

<p>I still need to write a few more tests. I&#8217;m already very happy with the way these invariant annotations look like. The annotation expressed above would probably have been several hundred lines of code before, just because there was no general way to perform Boolean AND and OR operations on any kind of invariant annotation. They pretty much had to be custom-written.</p>

<p>Now that I have been working a bit with proxies, I remembered the idea about the <a href="http://www.concurrentaffair.org/index.php/2007/11/09/automatic-delegation-would-be-nice/"><code class="codecolorer text mac-classic"><span class="text">@DelegatesTo</span></code> annotation</a> that I had a while back. I think proxies would make the implementation of that really simple. I still have to generate the interface, and the type checker of the Java compiler is still a problem, as explained in the original posting, but proxies would probably make all bytecode rewriting unnecessary. I could do it all using proxies and reflection. Again, I think it&#8217;s worth investigating.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2007/12/23/still-working-on-subtyped-annotation-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>@Inherited for Invariant Annotations?</title>
		<link>http://www.concurrentaffair.org/2007/12/03/inherited-for-invariant-annotations/</link>
		<comments>http://www.concurrentaffair.org/2007/12/03/inherited-for-invariant-annotations/#comments</comments>
		<pubDate>Mon, 03 Dec 2007 21:15:00 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[Concurrent Unit Testing]]></category>
		<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.superscalar.org/blog/index.php/2007/12/03/inherited-for-invariant-annotations/</guid>
		<description><![CDATA[When I wrote the API code for xajavac, my Java ... <a href="http://www.concurrentaffair.org/2007/12/03/inherited-for-invariant-annotations/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When I wrote the API code for <a href="http://www.cs.rice.edu/~mgricken/research/xajavac">xajavac</a>, my Java compiler variant that allows subtyping for annotations, I discovered the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/annotation/Inherited.html"><code class="codecolorer text mac-classic"><span class="text">@Inherited</span></code></a> annotation for the first time. This annotation is used on another annotation and indicates that the other annotation should be inherited from superclasses to subclasses. Specifically, annotations with <code class="codecolorer text mac-classic"><span class="text">@Inherited</span></code> also appear in the array of annotations returned by <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getAnnotations()"><code class="codecolorer text mac-classic"><span class="text">Class.getAnnotations()</span></code></a> if they annotated any of the superclasses of the class in question; in contrast, the array returned by <code class="codecolorer text mac-classic"><span class="text">Class.getDeclaredAnnotations</span></code> only returns the annotations that are actually annotation the class itself.</p>

<p>When I wrote the implementation of the invariant checker outlined in my Master&#8217;s thesis, I implemented and described an inheritance mechanism for these invariant annotations that didn&#8217;t rely on the <code class="codecolorer text mac-classic"><span class="text">@Inherited</span></code> annotation. I introduced separates meta-annotation, <code class="codecolorer text mac-classic"><span class="text">@PredicateLink</span></code> and <code class="codecolorer text mac-classic"><span class="text">@Combine</span></code>, which I needed anyway and which also incorporated the meaning of <code class="codecolorer text mac-classic"><span class="text">@Inherited</span></code>.</p>

<p>Now that I have discovered <code class="codecolorer text mac-classic"><span class="text">@Inherited</span></code>, I&#8217;m wondering whether I could or even should use it. That would mean making non-inheritance the default behavior, and letting <code class="codecolorer text mac-classic"><span class="text">@Inherited</span></code> imply the inheriting case. I don&#8217;t think that&#8217;s the behavior I want. A <code class="codecolorer text mac-classic"><span class="text">@NotInherited</span></code> meta-annotation would be better.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2007/12/03/inherited-for-invariant-annotations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Small Things</title>
		<link>http://www.concurrentaffair.org/2007/11/28/small-things/</link>
		<comments>http://www.concurrentaffair.org/2007/11/28/small-things/#comments</comments>
		<pubDate>Thu, 29 Nov 2007 01:12:31 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[Concurrent Unit Testing]]></category>
		<category><![CDATA[DrJava]]></category>
		<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.superscalar.org/blog/index.php/2007/11/28/small-things/</guid>
		<description><![CDATA[In the last few days, I've worked on a few smal... <a href="http://www.concurrentaffair.org/2007/11/28/small-things/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In the last few days, I&#8217;ve worked on a few small things: I have created a <a href="http://www.cs.rice.edu/~mgricken/research/xajavac/download/ant/">compiler adapter for Ant</a> that allows me to use my <a href="http://www.cs.rice.edu/~mgricken/research/xajavac">xajavac</a> compiler &#8212; once you have annotations with subtyping it&#8217;s hard to let go of them again. On Monday, I made a <a href="http://drjava.svn.sourceforge.net/viewvc/drjava?view=rev&#038;revision=4267">bugfix for the remote control feature of DrJava</a>, and today I tried to address <a href="http://drjava.svn.sourceforge.net/viewvc/drjava?view=rev&#038;revision=4268">another issue</a> that came up in a <a href="http://sourceforge.net/tracker/index.php?func=detail&#038;aid=1831946&#038;group_id=44253&#038;atid=438935">bug report</a>.</p>

<p>I&#8217;ve also started on a version of the invariant checker that uses annotations with subtyping, but that hasn&#8217;t progressed too far yet; I first had to solve the Ant problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2007/11/28/small-things/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multiple Annotations of the Same Type</title>
		<link>http://www.concurrentaffair.org/2007/11/21/multiple-annotations-of-the-same-type/</link>
		<comments>http://www.concurrentaffair.org/2007/11/21/multiple-annotations-of-the-same-type/#comments</comments>
		<pubDate>Thu, 22 Nov 2007 01:34:06 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.superscalar.org/blog/index.php/2007/11/21/multiple-annotations-of-the-same-type/</guid>
		<description><![CDATA[Since I already made one small modification tod... <a href="http://www.concurrentaffair.org/2007/11/21/multiple-annotations-of-the-same-type/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Since I already made one small modification today, I also looked at allowing multiple annotations of the same type on an annotated element. Again, it was a very simple change in the compiler.</p>

<p>Both the modified compiler and the extended annotation framework now allow you to repeat the same annotation multiple times. Unfortunately, if the original reflection API is used, Java throws a <code class="codecolorer text mac-classic"><span class="text">&nbsp;java.lang.annotation.AnnotationFormatError: Duplicate annotation for class: interface TestMulti$MultiAnnotation: @TestMulti$MultiAnnotation(value=bar)</span></code> exception (or similar), so make sure to use the new API in subannot.jar if you use this feature.</p>

<p>Since I couldn&#8217;t rely on Java&#8217;s reflection API anymore (because of the exception), I had to rework my framework a bit. The most complicated change involved the distinction between <code class="codecolorer text mac-classic"><span class="text">getAnnotations()</span></code> and <code class="codecolorer text mac-classic"><span class="text">getDeclaredAnnotations()</span></code>. I now have to inherit annotations to subclasses myself.</p>

<p>All that is done now, though. I still need to make a more thorough write-up and announce xajavac on several discussion boards. I don&#8217;t know when I&#8217;ll do that, though; I&#8217;ve had an incredibly productive day, but I&#8217;ve been working for 12 hours straight already. On the other hand, I&#8217;m rained in at the office and everyone else has left for Thanksgiving&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2007/11/21/multiple-annotations-of-the-same-type/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Final Modifier for Annotations with Subtyping</title>
		<link>http://www.concurrentaffair.org/2007/11/21/final-modifier-for-annotations-with-subtyping/</link>
		<comments>http://www.concurrentaffair.org/2007/11/21/final-modifier-for-annotations-with-subtyping/#comments</comments>
		<pubDate>Wed, 21 Nov 2007 19:46:33 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.superscalar.org/blog/index.php/2007/11/21/final-modifier-for-annotations-with-subtyping/</guid>
		<description><![CDATA[The JSR 308 proposal mentioned that there might... <a href="http://www.concurrentaffair.org/2007/11/21/final-modifier-for-annotations-with-subtyping/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The JSR 308 proposal mentioned that there might be <a href="http://groups.csail.mit.edu/pag/jsr308/java-annotation-design.html#htoc33">trust issues</a> for annotations that can be subclassed outside a trusted framework. I contend that this is not a severe problem since it occurs with normal classes already.</p>

<p>To still address this issue, I decided to allow the <code class="codecolorer text mac-classic"><span class="text">final</span></code> modifier for annotations: An annotation that is <code class="codecolorer text mac-classic"><span class="text">final</span></code> cannot be subclassed, just like a <code class="codecolorer text mac-classic"><span class="text">final</span></code> class cannot be extended.</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">final</span> @<span style="color: #000000; font-weight: bold;">interface</span> FinalAnnotation <span style="color: #009900;">&#123;</span><br />
&nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> value<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// ERROR: cannot inherit from final FinalAnnotation</span><br />
@<span style="color: #000000; font-weight: bold;">interface</span> SubAnnotation <span style="color: #000000; font-weight: bold;">extends</span> FinalAnnotation <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>Again, the change involved only half a dozen lines.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2007/11/21/final-modifier-for-annotations-with-subtyping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Referenced in JSR 308 Proposal</title>
		<link>http://www.concurrentaffair.org/2007/11/19/referenced-in-jsr-308-proposal/</link>
		<comments>http://www.concurrentaffair.org/2007/11/19/referenced-in-jsr-308-proposal/#comments</comments>
		<pubDate>Mon, 19 Nov 2007 18:53:34 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[Ramblings]]></category>
		<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.superscalar.org/blog/index.php/2007/11/19/referenced-in-jsr-308-proposal/</guid>
		<description><![CDATA[I just noticed that the Related Work section of... <a href="http://www.concurrentaffair.org/2007/11/19/referenced-in-jsr-308-proposal/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I just noticed that the <a href="http://groups.csail.mit.edu/pag/jsr308/java-annotation-design.html#sec:related-work">Related Work</a> section of the JSR 308 proposal references my <a href="http://www.cs.rice.edu/~mgricken/research/laptjavac">LAPT-javac</a> modification for local variable annotations.</p>

<p>At last! I was a little bit disappointed by the <a href="http://www.concurrentaffair.org/index.php/2006/10/11/comments-and-questions-about-lapt-javac/">original feedback</a> I received for LAPT-javac.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2007/11/19/referenced-in-jsr-308-proposal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Added Formal Grammar Changes and API Description to xajavac Website</title>
		<link>http://www.concurrentaffair.org/2007/11/19/added-formal-grammar-changes-and-api-description-to-xajavac-website/</link>
		<comments>http://www.concurrentaffair.org/2007/11/19/added-formal-grammar-changes-and-api-description-to-xajavac-website/#comments</comments>
		<pubDate>Mon, 19 Nov 2007 18:07:57 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.superscalar.org/blog/index.php/2007/11/19/added-formal-grammar-changes-and-api-description-to-xajavac-website/</guid>
		<description><![CDATA[I finally finished modifying the Java grammar t... <a href="http://www.concurrentaffair.org/2007/11/19/added-formal-grammar-changes-and-api-description-to-xajavac-website/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I finally finished modifying the Java grammar to faithfully represent my changes that enable subtyping for annotations. I also added a description of the extended API I had to develop to allow reflection for annotations with subtyping. I have also decided to remove the treatment for local variable annotations, originally implemented in <a href="http://www.cs.rice.edu/~mgricken/research/laptjavac">LAPT-javac</a>, from <a href="http://www.cs.rice.edu/~mgricken/research/xajavac/">xajavac</a> now that the JSR 308 proposal provides a different format.</p>

<p>All of this information, along with the binaries for the modified compiler and the API, is available on the <a href="http://www.cs.rice.edu/~mgricken/research/xajavac/">xajavac website</a>.</p>

<p>Now I should write a more formal tech report about this, I should contact Michael Ernst of the <a href="http://groups.csail.mit.edu/pag/jsr308/">JSR 308: Annotations on Types</a> project, and I should modify the code I wrote to for my MS thesis to take advantage of annotations with subtyping.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2007/11/19/added-formal-grammar-changes-and-api-description-to-xajavac-website/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSR 308 Proposal Available</title>
		<link>http://www.concurrentaffair.org/2007/11/13/jsr-308-proposal-available/</link>
		<comments>http://www.concurrentaffair.org/2007/11/13/jsr-308-proposal-available/#comments</comments>
		<pubDate>Tue, 13 Nov 2007 23:39:07 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.superscalar.org/blog/index.php/2007/11/13/jsr-308-proposal-available/</guid>
		<description><![CDATA[The proposal for JSR 308, &#34;Annotations on Java ... <a href="http://www.concurrentaffair.org/2007/11/13/jsr-308-proposal-available/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://groups.csail.mit.edu/pag/jsr308/">proposal for JSR 308, &#8220;Annotations on Java Types&#8221;</a>, is available now. I&#8217;ve read it diagonally once, and it looks pretty nice. They&#8217;re finally adding class and runtime retention for local variables, something my <a href="http://www.concurrentaffair.org/index.php/2006/10/08/hacking-javac/">LAPT-javac</a> already did in October 2006. xajavac also incorporates my own non-standard representation for local variable annotations. Now that a standard representation is emerging, I&#8217;ll either have to change my implementation to make it adhere to the JSR 308 proposal, or I&#8217;ll have to drop it. That&#8217;s not a surprise, though; when I wrote LAPT-javac, I fully expected that it would become obsolete soon.</p>

<p><a href="http://groups.csail.mit.edu/pag/jsr308/java-annotation-design.html#htoc33">Subtyping for annotations</a> is also mentioned, but to my delight only under &#8220;Out-of-scope issues&#8221;, a.k.a. suggestions. So at least in the near future, xajavac seems to be the only project to incorporate annotations with subtyping into Java.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2007/11/13/jsr-308-proposal-available/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Work Website Redesign and Miscellaneous To Do</title>
		<link>http://www.concurrentaffair.org/2007/11/13/work-website-redesign-and-miscellaneous-to-do/</link>
		<comments>http://www.concurrentaffair.org/2007/11/13/work-website-redesign-and-miscellaneous-to-do/#comments</comments>
		<pubDate>Tue, 13 Nov 2007 07:32:34 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[Ramblings]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.superscalar.org/blog/index.php/2007/11/13/work-website-redesign-and-miscellaneous-to-do/</guid>
		<description><![CDATA[I just finished redesigning my work website. It... <a href="http://www.concurrentaffair.org/2007/11/13/work-website-redesign-and-miscellaneous-to-do/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I just finished redesigning my <a href="http://www.cs.rice.edu/~mgricken/">work website</a>. It&#8217;s now in the same style as the <a href="http://drjava.org/">DrJava</a> and the <a href="http://www.concutest.org/">Concutest</a> websites. I liked the other design, but for lengthy text documents, the new design is just more practical.</p>

<p>While all tests of annotations with subtyping that I have tried so far have succeeded, I have realized that I need to test all my programs, not only the new subannot software, with primitive types. It&#8217;s embarrassing, but until a couple of days ago, I didn&#8217;t know that <code class="codecolorer text mac-classic"><span class="text">int.class</span></code> existed. I think now I have to put a <code class="codecolorer text mac-classic"><span class="text">switch</span></code> statement in front of every line that uses a type name so I can filter out <code class="codecolorer text mac-classic"><span class="text">'I'</span></code>s and <code class="codecolorer text mac-classic"><span class="text">'J'</span></code>s, etc. and replace them properly with <code class="codecolorer text mac-classic"><span class="text">int.class</span></code> and <code class="codecolorer text mac-classic"><span class="text">long.class</span></code>.</p>

<p>And I need to start writing that tech report about annotations with subtyping.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2007/11/13/work-website-redesign-and-miscellaneous-to-do/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Subtyping for Array Annotations Now Working</title>
		<link>http://www.concurrentaffair.org/2007/11/12/subtyping-for-array-annotations-now-working/</link>
		<comments>http://www.concurrentaffair.org/2007/11/12/subtyping-for-array-annotations-now-working/#comments</comments>
		<pubDate>Mon, 12 Nov 2007 22:47:19 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.superscalar.org/blog/index.php/2007/11/12/subtyping-for-array-annotations-not-working-yet/</guid>
		<description><![CDATA[I'm creating more and more tests for annotation... <a href="http://www.concurrentaffair.org/2007/11/12/subtyping-for-array-annotations-now-working/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m creating more and more tests for annotations with subtyping, and while most of it seems to work, I ran into one very important problem: Right now, my arrays do not support polymorphism yet. Consider the following:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@<span style="color: #000000; font-weight: bold;">interface</span> TopLevel <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
@<span style="color: #000000; font-weight: bold;">interface</span> Leaf <span style="color: #000000; font-weight: bold;">extends</span> TopLevel <span style="color: #009900;">&#123;</span><br />
&nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> value<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
@<span style="color: #000000; font-weight: bold;">interface</span> Interior <span style="color: #000000; font-weight: bold;">extends</span> TopLevel <span style="color: #009900;">&#123;</span><br />
&nbsp; TopLevel<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> value<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
@Interior<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>@Leaf<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Interior<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>@Leaf<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bar&quot;</span><span style="color: #009900;">&#41;</span>,@Leaf<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fee&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Leaf<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fum&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">class</span> Something <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>I definitely want polymorphic arrays. In the example above, the <code class="codecolorer text mac-classic"><span class="text">@Interior</span></code> declares an array of <code class="codecolorer text mac-classic"><span class="text">@TopLevel</span></code> interfaces, but since <code class="codecolorer text mac-classic"><span class="text">@Interior</span></code> and <code class="codecolorer text mac-classic"><span class="text">@Leaf</span></code> are both subclasses of <code class="codecolorer text mac-classic"><span class="text">@TopLevel</span></code>, I should be able to store them in the array as well.</p>

<p>The type checker and compilers don&#8217;t have a problem with this, and everything gets encoded correctly in the class file, but my runtime library has a bug: It creates an array of type <code class="codecolorer text mac-classic"><span class="text">TopLevel</span></code>, which is correct, but then also imposes the type <code class="codecolorer text mac-classic"><span class="text">TopLevel</span></code> for all its members! That means all members get &#8220;sliced&#8221; down to type <code class="codecolorer text mac-classic"><span class="text">TopLevel</span></code>, so they don&#8217;t have any of their additional members present.</p>

<p>I have to use the type from the array declaration and use it to create the array. Then I have to use the type of each annotation for the annotation instance. Unfortunately, this information isn&#8217;t conveniently present. But I&#8217;ll work it out.</p>

<p><strong>Update</strong></p>

<p>I just fixed the problem with subtyping in arrays as described above.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2007/11/12/subtyping-for-array-annotations-now-working/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automatic Delegation Would Be Nice</title>
		<link>http://www.concurrentaffair.org/2007/11/09/automatic-delegation-would-be-nice/</link>
		<comments>http://www.concurrentaffair.org/2007/11/09/automatic-delegation-would-be-nice/#comments</comments>
		<pubDate>Sat, 10 Nov 2007 02:34:02 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[Research]]></category>
		<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.superscalar.org/blog/index.php/2007/11/09/automatic-delegation-would-be-nice/</guid>
		<description><![CDATA[Last night I finished the core of the extended ... <a href="http://www.concurrentaffair.org/2007/11/09/automatic-delegation-would-be-nice/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Last night I finished the core of the extended <code class="codecolorer text mac-classic"><span class="text">Class</span></code> implementation supporting annotations with subtyping. I load default values, but I punted on the order of the members in the <code class="codecolorer text mac-classic"><span class="text">toString()</span></code> method. As far as I know, it is implementation-specific anyway.</p>

<p>Today I decided not to go to the office, but instead work from home. My workstation here has a better setup for coding, and I felt I was at the office long enough yesterday and that my presence wasn&#8217;t necessary for grading anymore, because as far as I know the missing grad TA had returned.</p>

<p>So I spent most of today writing extended versions of the <code class="codecolorer text mac-classic"><span class="text">Constructor</span></code>, <code class="codecolorer text mac-classic"><span class="text">Method</span></code>, <code class="codecolorer text mac-classic"><span class="text">Field</span></code> and <code class="codecolorer text mac-classic"><span class="text">Package</span></code> classes. I was able to factor most common code out, so implementing the core functionality wasn&#8217;t hard. Now I&#8217;m working on the supporting methods, e.g. those that allow you to get a <code class="codecolorer text mac-classic"><span class="text">Method</span></code> from a <code class="codecolorer text mac-classic"><span class="text">Class</span></code> object, without having to refer back to Java&#8217;s original <code class="codecolorer text mac-classic"><span class="text">Class</span></code> object.</p>

<p>In essence, I&#8217;m creating gigantic wrappers for Java&#8217;s classes, and I&#8217;m forwarding each call to the corresponding method in the original class. It would be nice if this could be automated, and in fact, I think that could easily be done, again through annotations and bytecode rewriting. Consider the following listing:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">class</span> A <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> bar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
@DelegateTo<span style="color: #009900;">&#40;</span>A.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">class</span> B <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
<br />
@DelegateTo<span style="color: #009900;">&#40;</span>A.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">class</span> C <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> bar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>The <code class="codecolorer text mac-classic"><span class="text">A</span></code> class defines two methods, <code class="codecolorer text mac-classic"><span class="text">foo()</span></code> and <code class="codecolorer text mac-classic"><span class="text">bar()</span></code>. Classes <code class="codecolorer text mac-classic"><span class="text">B</span></code> and <code class="codecolorer text mac-classic"><span class="text">C</span></code> are then annotated with <code class="codecolorer text mac-classic"><span class="text">@DelegateTo(A.class)</span></code>. When the bytecode rewriting framework sees this annotation, it automatically adds all of <code class="codecolorer text mac-classic"><span class="text">A</span></code>&#8216;s methods to <code class="codecolorer text mac-classic"><span class="text">B</span></code> and <code class="codecolorer text mac-classic"><span class="text">C</span></code> and generates bytecode to forward the calls. In the case of </code>C</code>, only calls to <code class="codecolorer text mac-classic"><span class="text">foo()&lt;/cod&gt; are automatically forwarded, because an implementation of &lt;code inline=&quot;true&quot; escaped=&quot;true&quot;&gt;bar()</span></code> has been provided.</p>

<p>The problem with this is that many times some processing has to be done before and after the delegation. For example, in the <code class="codecolorer text mac-classic"><span class="text">public MethodEx getMethod(String name, ClassEx... parameterTypes)()</span></code> method of my extended <code class="codecolorer text mac-classic"><span class="text">Class</span></code> class, I have to extract the original <code class="codecolorer text mac-classic"><span class="text">java.lang.Class</span></code> objects from the elements in the <code class="codecolorer text mac-classic"><span class="text">paramTypes</span></code> array, then pass the new array to the original method, and then finally create a new extended <code class="codecolorer text mac-classic"><span class="text">MethodEx</span></code> instance using the return value from the forwarded call:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ClassEx<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// ...</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> MethodEx getMethod<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name, ClassEx... <span style="color: #006633;">parameterTypes</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Anosuchmethodexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">NoSuchMethodException</span></a>, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asecurityexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">SecurityException</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> pt <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span>parameterTypes.<span style="color: #006633;">length</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>pt.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; pt<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> parameterTypes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">java</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> MethodEx<span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span>name, pt<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// ...</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>This kind of processing is very common. Even though I'm writing several dozen methods that delegate, there are only about half a dozen ways to process data. This made me think of C++'s conversion operators (e.g. <code class="codecolorer text mac-classic"><span class="text">operator int() { return ...; }</span></code>), which are basically user-designed casts with the ability to perform processing.</p>

<p>In the above example, the two processing methods I need are from <code class="codecolorer text mac-classic"><span class="text">ClassEx[]</span></code> to <code class="codecolorer text mac-classic"><span class="text">Class[]</span></code>, and from <code class="codecolorer text mac-classic"><span class="text">Method</span></code> to <code class="codecolorer text mac-classic"><span class="text">MethodEx</span></code>. I could write a class with static methods to perform these conversions:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">class</span> Conversions <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> convertToClassArray<span style="color: #009900;">&#40;</span>ClassEx<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> output <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span>input.<span style="color: #006633;">length</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>pt.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; output<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> input<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">java</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> output<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> MethodEx convertToMethodEx<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amethod+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Method</span></a> input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> MethodEx<span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>The names aren't really important, because the bytecode rewriting framework would look for a method with the correct input and output types for the conversion it has to perform. Some care needs to be taken, though, because Java doesn't allow overloading if only the return type differs. That's why I have suffixed the methods above with a description of the return type.</p>

<p>Now I can change the <code class="codecolorer text mac-classic"><span class="text">@DelegateTo&lt;code inline=&quot;true&quot; escaped=&quot;true&quot;&gt; annotation to specify a class that contains the conversion methods:<br />
<br />
&lt;code lang=&quot;java&quot;&gt;@DelegateTo(target=java.lang.Class.class, conversions=Conversions.class)<br />
public class ClassEx&lt;T&gt; { }</span></code></p>

<p>The bytecode rewriting framework will add all methods from <code class="codecolorer text mac-classic"><span class="text">java.lang.Class</span></code> to <code class="codecolorer text mac-classic"><span class="text">ClassEx</span></code>, but all occurrences of <code class="codecolorer text mac-classic"><span class="text">Class[]</span></code> will be replaced by <code class="codecolorer text mac-classic"><span class="text">ClassEx[]</span></code>, and all occurrences of <code class="codecolorer text mac-classic"><span class="text">Method</span></code> will be replaced by </code>MethodEx</code>. Instead of having a method <code class="codecolorer text mac-classic"><span class="text">public Method getMethod(String name, Class... parameterTypes)()</span></code> like <code class="codecolorer text mac-classic"><span class="text">java.lang.Class</span></code>, <code class="codecolorer text mac-classic"><span class="text">ClassEx</span></code> will have a method <code class="codecolorer text mac-classic"><span class="text">public MethodEx getMethod(String name, ClassEx... parameterTypes)()</span></code>. The <code class="codecolorer text mac-classic"><span class="text">String name</span></code> is not processed in any way, because there is no conversion method from <code class="codecolorer text mac-classic"><span class="text">String</span></code> to <code class="codecolorer text mac-classic"><span class="text">String</span></code>, but <code class="codecolorer text mac-classic"><span class="text">Class... parameterTypes</span></code> will be changed to <code class="codecolorer text mac-classic"><span class="text">ClassEx... parameterTypes</span></code>, and the <code class="codecolorer text mac-classic"><span class="text">Class[] convertToClassArray(ClassEx[] input)</span></code> will be used for the conversion. The return value will be changed from <code class="codecolorer text mac-classic"><span class="text">Method</span></code> to <code class="codecolorer text mac-classic"><span class="text">MethodEx</span></code>, and the <code class="codecolorer text mac-classic"><span class="text">MethodEx convertToMethodEx(Method input)</span></code> method will be used for the conversion.</p>

<p>It probably makes sense to allow an array of classes with conversion methods, since the conversion methods may be spread out across several classes. The framework will have to check that there is no ambiguity as to which conversion method is used, i.e. there may not be two or more methods in all specified classes with the same input and output types. It may also be a good idea to allow different conversion methods for input and output. That would lead us to a syntax like this:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@DelegateTo<span style="color: #009900;">&#40;</span>target<span style="color: #339933;">=</span>java.<span style="color: #006633;">lang</span>.<span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #000000; font-weight: bold;">class</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span>InputConversions.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span>OutputConversions.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ClassEx<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> InputConversions <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> convertToClassArray<span style="color: #009900;">&#40;</span>ClassEx<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> output <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span>input.<span style="color: #006633;">length</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>pt.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; output<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> input<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">java</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> output<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> OutputConversions <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> MethodEx convertToMethodEx<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amethod+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Method</span></a> input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> MethodEx<span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>Considering that this will handle the conversions for more than one method, I think the effort would well be worth it.</p>

<p>There's still a problem with type checking, of course: Since classes for which the forwarding methods should be generated automatically don't actually contain the methods in the source code, the Java compiler will emit errors whenever the methods are used. Compiling should therefore probably happen in three phases: First, all classes except the ones to be generated automatically are compiled; second, the automatic bytecode rewriting is done; third, all classes except the ones generated automatically are compiled again.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2007/11/09/automatic-delegation-would-be-nice/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Long Day</title>
		<link>http://www.concurrentaffair.org/2007/11/08/long-day/</link>
		<comments>http://www.concurrentaffair.org/2007/11/08/long-day/#comments</comments>
		<pubDate>Fri, 09 Nov 2007 00:06:44 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[Graduate School]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.superscalar.org/blog/index.php/2007/11/08/long-day/</guid>
		<description><![CDATA[This was a long but productive day so far: I go... <a href="http://www.concurrentaffair.org/2007/11/08/long-day/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This was a long but productive day so far: I got to the office at 10 AM to grade COMP 311 midterm exams because one of the graduate TAs was out of town. Due to scheduling conflicts, we didn&#8217;t actually grade, but that was fine with me, instead I had nothing better to do than work on subtyping for annotations.</p>

<p>I still don&#8217;t load default values, but annotations and arrays as members are now implemented. There&#8217;s still one small problem: I don&#8217;t quite know the order in which annotation members appear in the return value of a <code class="codecolorer text mac-classic"><span class="text">toString()</span></code> call. At first I thought the members were sorted alphabetically, so I implemented my <code class="codecolorer text mac-classic"><span class="text">toString()</span></code> method that way, but now that I have a few more test cases, I see both <code class="codecolorer text mac-classic"><span class="text">@TestTwoAnnotation(s=&quot;foo&quot;, i=123)</span></code> and <code class="codecolorer text mac-classic"><span class="text">@TestTwoAnnotation(i=123, s=&quot;foo&quot;)</span></code> as output, and so far I can&#8217;t a difference in their contexts.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2007/11/08/long-day/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>From the Trenches</title>
		<link>http://www.concurrentaffair.org/2007/11/08/from-the-trenches/</link>
		<comments>http://www.concurrentaffair.org/2007/11/08/from-the-trenches/#comments</comments>
		<pubDate>Thu, 08 Nov 2007 22:12:14 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[Research]]></category>
		<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.superscalar.org/blog/index.php/2007/11/08/from-the-trenches/</guid>
		<description><![CDATA[I'm still working on the API improvements to su... <a href="http://www.concurrentaffair.org/2007/11/08/from-the-trenches/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m still working on the API improvements to support annotations with subtyping. It turned out there is much more to do than I thought: Reading the class files, parsing the annotations and then creating the corresponding runtime objects is quite a bit of work, not difficult but tedious. I still have to deal with annotations as members, and with arrays of annotations and arrays of arrays. Additionally, I have to read out the default values of annotation members; right now, if a member isn&#8217;t specified, its value is <code class="codecolorer text mac-classic"><span class="text">null</span></code>.</p>

<p>The high level parts of my framework make extensive use of the Visitor and Template Method design patterns. Unfortunately, I made a mistake again that I&#8217;ve made often, and that I&#8217;ve pointed out very often too. Consider the visitor interface and the abstract class implementing it:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">interface</span> IVisitor<span style="color: #339933;">&lt;</span>R,P<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> R intCase<span style="color: #009900;">&#40;</span>IntLeaf host, P param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> R addCase<span style="color: #009900;">&#40;</span>AddNode host, P param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> R mulCase<span style="color: #009900;">&#40;</span>MulNode host, P param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> ADefaultVisitor<span style="color: #339933;">&lt;</span>R,P<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> R defaultCase<span style="color: #009900;">&#40;</span>INode host, P param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> R intCase<span style="color: #009900;">&#40;</span>IntLeaf host, P param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> defaultCase<span style="color: #009900;">&#40;</span>host, param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> R addCase<span style="color: #009900;">&#40;</span>AddNode host, P param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> defaultCase<span style="color: #009900;">&#40;</span>host, param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> R mulCase<span style="color: #009900;">&#40;</span>MulNode host, P param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> defaultCase<span style="color: #009900;">&#40;</span>host, param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>The abstract class implements a very simple Template Method pattern: It adds an abstract method that needs to be implemented, and then implements all the methods from the interface so that they call the new method. A default visitor like this is useful when most cases of a visitor are treated the same, e.g. during error checking when only one case is acceptable and all other cases should throw an exception:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">class</span> LeafOnlyVisitor<span style="color: #339933;">&lt;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Avoid+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Void</span></a>,Void<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Avoid+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Void</span></a> defaultCase<span style="color: #009900;">&#40;</span>INode host, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Avoid+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Void</span></a> param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// called by addCase and mulCase</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aruntimeexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">RuntimeException</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;leaf expected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Avoid+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Void</span></a> intCase<span style="color: #009900;">&#40;</span>IntLeaf host, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Avoid+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Void</span></a> param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Avoid+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Void</span></a>.<span style="color: #006633;">TYPE</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>Unfortunately, it&#8217;s very easy to accidentally change the signature of the method that we&#8217;re trying to override, and therefore not override the method but add another method, one that is never called. In the first of the two examples below, the method name is misspelled; in the second, the type of the first parameter is different.</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">class</span> LeafOnlyVisitorFlawed<span style="color: #339933;">&lt;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Avoid+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Void</span></a>,Void<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Avoid+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Void</span></a> defaultCase<span style="color: #009900;">&#40;</span>INode host, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Avoid+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Void</span></a> param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// called by addCase and mulCase</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aruntimeexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">RuntimeException</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;leaf expected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Avoid+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Void</span></a> untCase<span style="color: #009900;">&#40;</span>IntLeaf host, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Avoid+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Void</span></a> param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Avoid+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Void</span></a>.<span style="color: #006633;">TYPE</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> LeafOnlyVisitorFlawed2<span style="color: #339933;">&lt;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Avoid+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Void</span></a>,Void<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Avoid+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Void</span></a> defaultCase<span style="color: #009900;">&#40;</span>INode host, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Avoid+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Void</span></a> param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// called by addCase and mulCase</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aruntimeexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">RuntimeException</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;leaf expected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Avoid+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Void</span></a> intCase<span style="color: #009900;">&#40;</span>INode host, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Avoid+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Void</span></a> param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Avoid+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Void</span></a>.<span style="color: #006633;">TYPE</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>In both cases, there is no compiler error, because all methods from the interface and the method from the abstract class have been implemented, so it&#8217;s often difficult to discover this problem. I just realized how nice it would be to specify that subclasses of a class may only override methods, never add new methods. Using annotations, the syntax could look like this:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@OverrideOnly<br />
<span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> ADefaultVisitor<span style="color: #339933;">&lt;</span>R,P<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> R defaultCase<span style="color: #009900;">&#40;</span>INode host, P param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> R intCase<span style="color: #009900;">&#40;</span>IntLeaf host, P param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> defaultCase<span style="color: #009900;">&#40;</span>host, param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> R addCase<span style="color: #009900;">&#40;</span>AddNode host, P param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> defaultCase<span style="color: #009900;">&#40;</span>host, param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> R mulCase<span style="color: #009900;">&#40;</span>MulNode host, P param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> defaultCase<span style="color: #009900;">&#40;</span>host, param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>When the classes <code class="codecolorer text mac-classic"><span class="text">LeafOnlyVisitorFlawed</span></code> and <code class="codecolorer text mac-classic"><span class="text">LeafOnlyVisitorFlawed2</span></code> accidentally add a method instead of overriding one, an annotation-based checker could emit errors.</p>

<p>To allow better code reuse, it probably makes sense to allow private methods to be added to a class marked <code class="codecolorer text mac-classic"><span class="text">@OverrideOnly</span></code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2007/11/08/from-the-trenches/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Subtyping for Annotations</title>
		<link>http://www.concurrentaffair.org/2007/11/05/subtyping-for-annotations/</link>
		<comments>http://www.concurrentaffair.org/2007/11/05/subtyping-for-annotations/#comments</comments>
		<pubDate>Mon, 05 Nov 2007 22:06:55 +0000</pubDate>
		<dc:creator>Mathias</dc:creator>
				<category><![CDATA[Research]]></category>
		<category><![CDATA[xajavac]]></category>

		<guid isPermaLink="false">http://www.superscalar.org/blog/index.php/2007/11/05/subtyping-for-annotations/</guid>
		<description><![CDATA[Last night, I managed to change the Java compil... <a href="http://www.concurrentaffair.org/2007/11/05/subtyping-for-annotations/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Last night, I managed to change the Java compiler to parse and encode annotations with inheritance. I&#8217;ve complained about this shortcoming many times already: Even though subtyping (inheritance) is one of the core concepts in Java, and even though annotations are represented using (slightly enhanced) interfaces, which support inheritance, Java disallows inheritance for annotations. I never quite understood why.</p>

<p>I&#8217;ve now created a modified compiler, based on the <a href="http://www.cs.rice.edu/~mgricken/research/laptjavac/">LAPT-javac</a> compiler I wrote a while back. The new compiler is called xajavac, for &#8220;eXtended Annotation javac&#8221;, and supports annotations on local variables, like LAPT-javac, and subtyping for annotations.</p>

<p>Now I can write the following:</p>

<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@<span style="color: #000000; font-weight: bold;">interface</span> InvariantAnnotation <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
@<span style="color: #000000; font-weight: bold;">interface</span> OnlyThreadWithName <span style="color: #000000; font-weight: bold;">extends</span> InvariantAnnotation <span style="color: #009900;">&#123;</span><br />
&nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> value<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
@<span style="color: #000000; font-weight: bold;">interface</span> OnlyEventThread <span style="color: #000000; font-weight: bold;">extends</span> InvariantAnnotation <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span><br />
@<span style="color: #000000; font-weight: bold;">interface</span> Or <span style="color: #000000; font-weight: bold;">extends</span> InvariantAnnotation <span style="color: #009900;">&#123;</span><br />
&nbsp; InvariantAnnotation value<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
@Or<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>@OnlyThreadWithName<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;main&quot;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp;@OnlyEventThread<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000066; font-weight: bold;">void</span> foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>I&#8217;m now working on providing an API that supports access to these annotations at runtime.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.concurrentaffair.org/2007/11/05/subtyping-for-annotations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

