<?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>dow.ngra.de &#187; Featured</title>
	<atom:link href="http://dow.ngra.de/category/featured/feed/" rel="self" type="application/rss+xml" />
	<link>http://dow.ngra.de</link>
	<description>no buzzwords allowed</description>
	<lastBuildDate>Wed, 04 Aug 2010 13:19:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ClassLoaderLocal: How to avoid ClassLoader leaks on application redeploy</title>
		<link>http://dow.ngra.de/2009/06/15/classloaderlocal-how-to-avoid-classloader-leaks-on-application-redeploy/</link>
		<comments>http://dow.ngra.de/2009/06/15/classloaderlocal-how-to-avoid-classloader-leaks-on-application-redeploy/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 07:51:17 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[creative]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=926</guid>
		<description><![CDATA[var dzone_url = 'http://dow.ngra.de/2009/06/15/classloaderlocal-how-to-avoid-classloader-leaks-on-application-redeploy/';var dzone_title = 'ClassLoaderLocal: How to avoid ClassLoader leaks on application redeploy';var dzone_blurb = '“OutOfMemoryError: PermGen” is a very common message to see after a few redeploys. The reason why it’s so common is that it’s amazingly easy to leak a class loader. In this post I’ll review how we solved this [...]]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript">var dzone_url = 'http://dow.ngra.de/2009/06/15/classloaderlocal-how-to-avoid-classloader-leaks-on-application-redeploy/';</script><script type="text/javascript">var dzone_title = 'ClassLoaderLocal: How to avoid ClassLoader leaks on application redeploy';</script><script type="text/javascript">var dzone_blurb = '“OutOfMemoryError: PermGen” is a very common message to see after a few redeploys. The reason why it’s so common is that it’s amazingly easy to leak a class loader. In this post I’ll review how we solved this problem in JavaRebel, and share the solution with you.';</script><script type="text/javascript">var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script> &#8220;OutOfMemoryError: PermGen&#8221; is a very common message to see after a few redeploys. The reason why it&#8217;s so common is that it&#8217;s amazingly easy to leak a class loader. It&#8217;s enough to hold a single outside reference to an object instantiated from a class loaded by the said class loader to prevent that class loader from being GC-d.</p>
<p>In this post I&#8217;ll review how we solved this problem in <a href="http://www.zeroturnaround.com/javarebel/">JavaRebel</a>, and share the solution with you. It&#8217;s not a magical solution, but it will help alleviate some of the problems introduced in both libraries and applications in Java EE.</p>
<p>The most common way to leak is to register some kind of a callback object and never deregister it. E.g. look at the following code:</p>
<div class="igBar"><span id="ljava-14"><a href="#" onclick="javascript:showPlainTxt('java-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-14">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Core.<span style="color: #006600;">addListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MyListener<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
If <code>Core</code> is a part of the framework/platform/container then it will hold to <code>MyListener</code> long after the application was redeployed and the class loader left hanging. </p>
<p>Let&#8217;s see if we can do anything to solve this. The <code>Core</code> implementation looks something like this:</p>
<div class="igBar"><span id="ljava-15"><a href="#" onclick="javascript:showPlainTxt('java-15'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-15">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Core <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a> listeners = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AArrayList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ArrayList</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #993333;">void</span> addListener<span style="color: #66cc66;">&#40;</span>Listener l<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; listeners.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span>l<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #993333;">void</span> fireListeners<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Exercise for the reader!</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The problem is that <code>listeners</code> provides a strong reference to the <code>Listener</code> object. What if we replace it by a <a href="http://www.ibm.com/developerworks/library/j-refs/">weak one</a>?</p>
<div class="igBar"><span id="ljava-16"><a href="#" onclick="javascript:showPlainTxt('java-16'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-16">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Core <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a> listeners = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AArrayList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ArrayList</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #993333;">void</span> addListener<span style="color: #66cc66;">&#40;</span>Listener l<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; listeners.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AWeakReference+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">WeakReference</span></a><span style="color: #66cc66;">&#40;</span>l<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #993333;">void</span> fireListeners<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Exercise for the reader!</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Unfortunately although this does solve the problem of GC-ing the class loader, it doesn&#8217;t really work. The <code>Listener</code> behind the weak reference will be GC-d at first opportunity and after that it&#8217;ll no longer receive any callbacks. To illustrate why it&#8217;s a problem the code above is basically equivalent to throwing the reference away altogether:</p>
<div class="igBar"><span id="ljava-17"><a href="#" onclick="javascript:showPlainTxt('java-17'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-17">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Core <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a> listeners = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AArrayList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ArrayList</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #993333;">void</span> addListener<span style="color: #66cc66;">&#40;</span>Listener l<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Listener is ignored and GC-d</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Replacing weak reference with a <em>soft</em> one doesn&#8217;t improve the situation, just delays the inevitable a bit further. Both are useful for caches, where objects can be recreated at will, but not in this case where we have an externally created object.</p>
<p>So what do we do? What we&#8217;d like to do is have the <code>Listener</code> reference to depend on the class loader somehow. Unfortunately, to the best of my knowledge, there isn&#8217;t a ready-made solution for that, and there&#8217;s no way to achieve it with any combinations of weak references without causing problems.</p>
<p>What we&#8217;d like to have is an ability to add a strong reference to the class loader: in other words have it carry a custom property:</p>
<div class="igBar"><span id="ljava-18"><a href="#" onclick="javascript:showPlainTxt('java-18'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-18">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">void</span> addListener<span style="color: #66cc66;">&#40;</span>Listener l<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.google.com/search?q=allinurl%3AClassLoader+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ClassLoader</span></a> cl = l.<span style="color: #006600;">getClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getClassLoader</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a> lls = <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a><span style="color: #66cc66;">&#41;</span> cl.<span style="color: #006600;">getProperty</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8220;CoreListeners&#8221;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>lls == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lls = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AArrayList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ArrayList</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; cl.<span style="color: #006600;">putProperty</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8220;CoreListeners&#8221;</span>, lls<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; lls.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span>l<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>That would work, wouldn&#8217;t it? Well, not quite. We also need to save a reference to the class loaders, so that we could later go over all of them. Here the <code>WeakHashMap</code> is useful:</p>
<div class="igBar"><span id="ljava-19"><a href="#" onclick="javascript:showPlainTxt('java-19'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-19">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.google.com/search?q=allinurl%3AMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Map</span></a> classLoaders = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AWeakHashMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">WeakHashMap</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">void</span> addListener<span style="color: #66cc66;">&#40;</span>Listener l<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">//&#8230;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; classLoaders.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>cl, <a href="http://www.google.com/search?q=allinurl%3ABoolean+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Boolean</span></a>.<span style="color: #000000; font-weight: bold;">TRUE</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>There&#8217;s not <code>WeakHashSet</code> in Java, so we&#8217;re just using a boolean flag as the value.</p>
<p>So this would probably work, but unfortunately class loaders don&#8217;t have a getProperty()/putProperty() API. However, it turns out that with a bit of a hack we can simulate it, by generating a unique class per class loader to hold the properties for us. Let&#8217;s see how it&#8217;s done!</p>
<p>We start with a little boilerplate:</p>
<div class="igBar"><span id="ljava-20"><a href="#" onclick="javascript:showPlainTxt('java-20'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-20">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">class</span> ClassLoaderLocalMap <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">static</span> <a href="http://www.google.com/search?q=allinurl%3AMethod+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Method</span></a> defineMethod;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">static</span> <a href="http://www.google.com/search?q=allinurl%3AMethod+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Method</span></a> findLoadedClass;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #993333;">static</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; defineMethod = <a href="http://www.google.com/search?q=allinurl%3AClassLoader+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ClassLoader</span></a>.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006600;">getDeclaredMethod</span><span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">&#8220;defineClass&#8221;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span> </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a>.<span style="color: #000000; font-weight: bold;">class</span>, </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">byte</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #000000; font-weight: bold;">class</span>, </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span>.<span style="color: #000000; font-weight: bold;">class</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span>.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; defineMethod.<span style="color: #006600;">setAccessible</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; findLoadedClass =</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AClassLoader+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ClassLoader</span></a>.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006600;">getDeclaredMethod</span><span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&#8220;findLoadedClass&#8221;</span>, </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; findLoadedClass.<span style="color: #006600;">setAccessible</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;&nbsp; &nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3ANoSuchMethodException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">NoSuchMethodException</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &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?q=allinurl%3ARuntimeException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">RuntimeException</span></a><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span>&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This will give us access to <code>ClassLoader</code> protected methods <code>defineClass()</code> and <code>findLoadedClass()</code> later on. Now let&#8217;s setup the basic API:</p>
<div class="igBar"><span id="ljava-21"><a href="#" onclick="javascript:showPlainTxt('java-21'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-21">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> <span style="color: #993333;">void</span> put<span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.google.com/search?q=allinurl%3AClassLoader+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ClassLoader</span></a> cl, </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Object</span></a>&nbsp; key, </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">// Synchronizing over ClassLoader is safest</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #66cc66;">&#40;</span>cl<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; getLocalMap<span style="color: #66cc66;">&#40;</span>cl<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>key, value<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> <a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> get<span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.google.com/search?q=allinurl%3AClassLoader+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ClassLoader</span></a> cl,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> key<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">// Synchronizing over ClassLoader is safest</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #66cc66;">&#40;</span>cl<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> getLocalMap<span style="color: #66cc66;">&#40;</span>cl<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>key<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><code>getLocalMap()</code> method should return a map of entries associated with the class loader. How should that work?</p>
<p>Next we introduce a map from class loaders to unique holder class names. We also introduce a <code>nextHolderName()</code> method that generates unique names:</p>
<div class="igBar"><span id="ljava-22"><a href="#" onclick="javascript:showPlainTxt('java-22'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-22">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?q=allinurl%3AMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Map</span></a> classLoaderToHolderClassName = </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.google.com/search?q=allinurl%3ACollections+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Collections</span></a>.<span style="color: #006600;">synchronizedMap</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AWeakHashMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">WeakHashMap</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">static</span> volatile <span style="color: #993333;">int</span> counter = <span style="color: #cc66cc;color:#800000;">1</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">static</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> nextHolderName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #ff0000;">&#8220;ClassLoaderLocalMapHolder$$GEN$$&#8221;</span> + counter++;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Finally we can implement the <code>getLocalMap()</code> method (to save space I removed all exception handling):</p>
<div class="igBar"><span id="ljava-23"><a href="#" onclick="javascript:showPlainTxt('java-23'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-23">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">static</span> <a href="http://www.google.com/search?q=allinurl%3AMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Map</span></a> getLocalMap<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AClassLoader+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ClassLoader</span></a> cl<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> holderClassName = </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#41;</span> classLoaderToHolderClassName.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>cl<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>holderClassName == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; holderClassName= nextHolderName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; classLoaderToHolderClassName.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; cl, holderClassName<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">Class</span> holderClass =</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span><span style="color: #66cc66;">&#41;</span> findLoadedClass.<span style="color: #006600;">invoke</span><span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; cl,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Object</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>propertiesClassName<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>holderClass&nbsp; == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #993333;">byte</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> classBytes = </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; buildHolderByteCode<span style="color: #66cc66;">&#40;</span>holderClassName<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; holderClass = <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span><span style="color: #66cc66;">&#41;</span> defineMethod.<span style="color: #006600;">invoke</span><span style="color: #66cc66;">&#40;</span>cl,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Object</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; holderClassName, </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; classBytes, </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AInteger+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Integer</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;color:#800000;">0</span><span style="color: #66cc66;">&#41;</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AInteger+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Integer</span></a><span style="color: #66cc66;">&#40;</span>classBytes.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Map</span></a><span style="color: #66cc66;">&#41;</span> holderClass</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; .<span style="color: #006600;">getDeclaredField</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8220;localMap&#8221;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The last method to implement is <code>buildHolderByteCode</code>. It&#8217;s quite trivial and builds the following class renamed to the unique name:</p>
<div class="igBar"><span id="ljava-24"><a href="#" onclick="javascript:showPlainTxt('java-24'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-24">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ClassLoaderLocalMapHolder$$GEN$$X <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?q=allinurl%3AMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Map</span></a> localMap = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AHashMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">HashMap</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
The code can be derived using <a href="http://asm.ow2.org/doc/tutorial.html">ASMifier</a> with just a little customization, you can look it up in the full source code.</p>
<p>Although we could now easily implement the original example it makes sense to do just a little bit extra effort and introduce a <code>ClassLoaderLocal</code>, with behavior similar to the <code>ThreadLocal</code>:</p>
<div class="igBar"><span id="ljava-25"><a href="#" onclick="javascript:showPlainTxt('java-25'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-25">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ClassLoaderLocal <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> key = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Object</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> get<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AClassLoader+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ClassLoader</span></a> cl<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!ClassLoaderProperties.<span style="color: #006600;">containsKey</span><span style="color: #66cc66;">&#40;</span>cl, key<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> ClassLoaderProperties.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>cl, key<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> set<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AClassLoader+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ClassLoader</span></a> cl, <a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; ClassLoaderProperties.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>cl, key, value<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>So the original example now becomes:</p>
<div class="igBar"><span id="ljava-26"><a href="#" onclick="javascript:showPlainTxt('java-26'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-26">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.google.com/search?q=allinurl%3AMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Map</span></a> classLoaders = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AWeakHashMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">WeakHashMap</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ClassLoaderLocal cll = <span style="color: #000000; font-weight: bold;">new</span> ClassLoaderLocal<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">void</span> addListener<span style="color: #66cc66;">&#40;</span>Listener l<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.google.com/search?q=allinurl%3AClassLoader+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ClassLoader</span></a> cl = l.<span style="color: #006600;">getClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getClassLoader</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a> lls = <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a><span style="color: #66cc66;">&#41;</span> cll.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>cl<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>lls == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lls = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AArrayList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">ArrayList</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; cll.<span style="color: #006600;">set</span><span style="color: #66cc66;">&#40;</span>cl, lls<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; lls.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span>l<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; classLoaders.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>cl, <a href="http://www.google.com/search?q=allinurl%3ABoolean+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Boolean</span></a>.<span style="color: #000000; font-weight: bold;">TRUE</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>In this code if any listener comes from a freed class loader, then it will be GC-d from both <code>Core.classLoaders</code> and <code>ClassLoaderProperties.classLoaderToHolderClassName</code>, as both are <code>WeakHashMap</code>s and there are no strong references to the class loaders. The generated <code>ClassLoaderLocalMapHolder$$GEN$$X</code> class will also be GC-d along with the class loader, so we have effectively eliminated a class loader leak without explicit cleanup calls from the user.</p>
<p>I hope this code will be useful for someone. I cannot give any guarantees whether it will work or not and it&#8217;s clearly a hack (though a solid hack).  Please use it if you actually understand what is happening.</p>
<p>If you see a bug in the code or have a good suggestion, please be sure to comment. There could be a free JavaRebel license in it for you :)</p>
<p>Full source code: <a href='http://dow.ngra.de/wp-content/uploads/2009/06/classloaderlocalmap.java'>ClassLoaderLocalMap.java</a>, <a href='http://dow.ngra.de/wp-content/uploads/2009/06/classloaderlocal.java'>ClassLoaderLocal.java</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2009/06/15/classloaderlocal-how-to-avoid-classloader-leaks-on-application-redeploy/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>The Ultimate Java Puzzler</title>
		<link>http://dow.ngra.de/2009/02/16/the-ultimate-java-puzzler/</link>
		<comments>http://dow.ngra.de/2009/02/16/the-ultimate-java-puzzler/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 15:01:08 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[meme]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=627</guid>
		<description><![CDATA[Why is this particular one the ultimate? Two reasons:

It&#8217;s at the very core of the Java language, not some obscure piece of API.
It melted my brain when I hit it.

UPDATE 2: If you want to test yourself before reading the post take this test. Results are not saved (it&#8217;s a paid feature apparently and I [...]]]></description>
			<content:encoded><![CDATA[<p>Why is this particular one the ultimate? Two reasons:</p>
<ul>
<li>It&#8217;s at the very core of the Java language, not some obscure piece of API.</li>
<li>It melted my brain when I hit it.</li>
</ul>
<p><strong>UPDATE 2:</strong> If you want to test yourself before reading the post <a href="http://www.classmarker.com/embedded_quizzes/?quiz=5ced4e42711fa927925c23e87b51be94">take this test</a>. Results are not saved (it&#8217;s a paid feature apparently and I just don&#8217;t care enough), but you can post them in the comments.</p>
<p>Let&#8217;s start by setting up the puzzler environment. We&#8217;ll have three classes in two packages. Classes <code>C1</code> and <code>C2</code> will be in package <code>p1</code>:</p>
<div class="igBar"><span id="ljava-36"><a href="#" onclick="javascript:showPlainTxt('java-36'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-36">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">package p1;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C1 <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">int</span> m<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;color:#800000;">1</span>;<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C2 <span style="color: #000000; font-weight: bold;">extends</span> C1 <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">int</span> m<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;color:#800000;">2</span>;<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Class <code>C3</code> will be in a separate package <code>p2</code>:</p>
<div class="igBar"><span id="ljava-37"><a href="#" onclick="javascript:showPlainTxt('java-37'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-37">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">package p2;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C3 <span style="color: #000000; font-weight: bold;">extends</span> p1.<span style="color: #006600;">C2</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">int</span> m<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;color:#800000;">3</span>;<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>We will also have the test class <code>p1.Main</code> with the following <code>main</code> method:</p>
<div class="igBar"><span id="ljava-38"><a href="#" onclick="javascript:showPlainTxt('java-38'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-38">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> <span style="color: #993333;">void</span> main<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; C1 c = <span style="color: #000000; font-weight: bold;">new</span> p2.<span style="color: #006600;">C3</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span>c.<span style="color: #006600;">m</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Note that we&#8217;re calling the method of <code>C1</code> on an instance of <code>C3</code>. The output for this example is &#8220;3&#8243; as you&#8217;d expect. Now let&#8217;s change the <code>m()</code> visibility in all three classes to default:</p>
<div class="igBar"><span id="ljava-39"><a href="#" onclick="javascript:showPlainTxt('java-39'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-39">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C1 <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">/*default*/</span> <span style="color: #993333;">int</span> m<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;color:#800000;">1</span>;<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C2 <span style="color: #000000; font-weight: bold;">extends</span> C1 <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">/*default*/</span> <span style="color: #993333;">int</span> m<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;color:#800000;">2</span>;<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C3 <span style="color: #000000; font-weight: bold;">extends</span> p1.<span style="color: #006600;">C2</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">/*default*/</span> <span style="color: #993333;">int</span> m<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;color:#800000;">3</span>;<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
The output will now be &#8220;2&#8243;! </p>
<p>Why is that? The <code>Main</code> class that invokes the method does not see the <code>m()</code> method in the <code>C3</code> class, it being in a separate package. As far as it cares the chain ends with <code>C2</code>. But as <code>C2</code> is in the same package it overrides the <code>m()</code> method in <code>C1</code>. This does not seem too intuitive, but that&#8217;s the way it is.</p>
<p>Now let&#8217;s try something different, let&#8217;s change the modifier of <code>C3.m()</code> back to <code>public</code>. What will that do?</p>
<div class="igBar"><span id="ljava-40"><a href="#" onclick="javascript:showPlainTxt('java-40'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-40">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C1 <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">/*default*/</span> <span style="color: #993333;">int</span> m<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;color:#800000;">1</span>;<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C2 <span style="color: #000000; font-weight: bold;">extends</span> C1 <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">/*default*/</span> <span style="color: #993333;">int</span> m<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;color:#800000;">2</span>;<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C3 <span style="color: #000000; font-weight: bold;">extends</span> p1.<span style="color: #006600;">C2</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">int</span> m<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;color:#800000;">3</span>;<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now <code>Main</code> can clearly see the <code>C3.m()</code> method. But amazingly enough output is still &#8220;2&#8243;! </p>
<p>Apparently <code>C3.m()</code> is not considered to override <code>C2.m()</code> at all. One way to think about it is overriding methods should have access to the super methods (via <code>super.m()</code>). However in this case <code>C3.m()</code> wouldn&#8217;t have access to its super method, as it it not visible to it, being in another package. Therefore <code>C3</code> is considered to be in a completely different invocation chain from <code>C1</code> and <code>C2</code>. Were we to call <code>C3.m()</code> directly from <code>Main</code> the output would actually be &#8220;3&#8243;.</p>
<p>Now let&#8217;s look at one last example. <em>Protected</em> is an interesting visibility. It behaves like <em>default</em> for members in the same package and like <em>public</em> for subclasses. What will happen if we change all of the visibilities to protected?</p>
<div class="igBar"><span id="ljava-41"><a href="#" onclick="javascript:showPlainTxt('java-41'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-41">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C1 <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">int</span> m<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;color:#800000;">1</span>;<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C2 <span style="color: #000000; font-weight: bold;">extends</span> C1 <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">int</span> m<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;color:#800000;">2</span>;<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C3 <span style="color: #000000; font-weight: bold;">extends</span> p1.<span style="color: #006600;">C2</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">int</span> m<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;color:#800000;">3</span>;<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>My reasoning goes like this: as <code>Main</code> is not a subclass of any classes protected should behave as default in this case and output should be &#8220;2&#8243;. However that is not the case. The crucial thing is that <code>C3.m()</code> has access to <code>super.m()</code> and thus the actual output will be &#8220;3&#8243;. </p>
<p>Personally, when I first encountered this accessibility issue I got thoroughly confused and couldn&#8217;t get it until I did all of this examples through. The intuition I got from this is that if and only if you can access <code>super.m()</code> the subclass is a part of the invocation chain.</p>
<p><strong>UPDATE:</strong> Apparently even though the whole thing is <em>obvious</em> to anyone, the intuition I came up with was wrong. A mysterious commenter know only as &#8220;C&#8221; has provided the following example:</p>
<div class="igBar"><span id="ljava-42"><a href="#" onclick="javascript:showPlainTxt('java-42'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-42">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C1 <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">/*default*/</span> <span style="color: #993333;">int</span> m<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;color:#800000;">1</span>;<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C2 <span style="color: #000000; font-weight: bold;">extends</span> C1 <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">/*default*/</span> <span style="color: #993333;">int</span> m<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;color:#800000;">2</span>;<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C3 <span style="color: #000000; font-weight: bold;">extends</span> p1.<span style="color: #006600;">C2</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">/*default*/</span> <span style="color: #993333;">int</span> m<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;color:#800000;">3</span>;<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C4 <span style="color: #000000; font-weight: bold;">extends</span> p2.<span style="color: #006600;">C3</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">/*default*/</span> <span style="color: #993333;">int</span> m<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;color:#800000;">4</span>;<span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Note that <code>C4</code> is in the package <code>p1</code>. If we now change the <code>Main</code> code as follows:</p>
<div class="igBar"><span id="ljava-43"><a href="#" onclick="javascript:showPlainTxt('java-43'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-43">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> <span style="color: #993333;">void</span> main<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; C1 c = <span style="color: #000000; font-weight: bold;">new</span> C4<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span>c.<span style="color: #006600;">m</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
Then it will output &#8220;4&#8243;. However <code>super.m()</code> is not accessible from <code>C4</code> and putting <code>@Override</code> on the <code>C4.m()</code> method will stop the code from compiling. At the same time if we change the <code>main</code> method to:</p>
<div class="igBar"><span id="ljava-44"><a href="#" onclick="javascript:showPlainTxt('java-44'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-44">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> <span style="color: #993333;">void</span> main<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; p2.<span style="color: #006600;">C3</span> c = <span style="color: #000000; font-weight: bold;">new</span> C4<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span>c.<span style="color: #006600;">m</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
The output will be &#8220;3&#8243;. This means that <code>C4.m()</code> overrides <code>C2.m()</code> and <code>C1.m()</code>, but not <code>C3.m()</code>. This also makes the issue even more confusing, and the amended intuition is that <strong>a method in a subclass overrides a method in a superclass if and only if the method in the superclass is accessible from the subclass</strong>. Here <em>superclass</em> can be any ancestor, not necessarily the direct parent and the relation has to be transitive.</p>
<p>For the kicker try reading all of this out from the <a href="http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.doc6.html">JVM specification</a> that selects the method to be invoked:</p>
<blockquote><p>
   <i><br />
    Let C be the class of objectref. The actual method to be invoked is selected by the following lookup procedure:</p>
<ul>
<li>If C contains a declaration for an instance method with the same name and descriptor as the resolved method, and the resolved method is accessible from C, then this is the method to be invoked, and the lookup procedure terminates. </li>
<li>Otherwise, if C has a superclass, this same lookup procedure is performed recursively using the direct superclass of C; the method to be invoked is the result of the recursive invocation of this lookup procedure.</li>
<li>Otherwise, an AbstractMethodError is raised. </li>
</ul>
<p></i>
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2009/02/16/the-ultimate-java-puzzler/feed/</wfw:commentRss>
		<slash:comments>54</slash:comments>
		</item>
		<item>
		<title>Correcting the Billion Dollar Mistake</title>
		<link>http://dow.ngra.de/2009/02/01/correcting-the-billion-dollar-mistake/</link>
		<comments>http://dow.ngra.de/2009/02/01/correcting-the-billion-dollar-mistake/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 20:04:14 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[creative]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=628</guid>
		<description><![CDATA[Last week I visited Stockholm to speak at the JFokus 2009. The event was quite spectacular, but for me the most interesting part occurred on the evening before the conference. I was sitting at the speaker&#8217;s dinner with Rickard Öberg, Kirk Pepperdine, Simon Ritter and a couple of others. For some reason or other I [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I visited Stockholm to speak at the <a href="http://www.jfokus.se/jfokus/">JFokus 2009</a>. The event was quite spectacular, but for me the most interesting part occurred on the evening before the conference. I was sitting at the speaker&#8217;s dinner with Rickard Öberg, Kirk Pepperdine, Simon Ritter and a couple of others. For some reason or other I started talking to Simon about the problem that&#8217;s recently been on my mind. Perhaps it&#8217;s been eating me after legendary Tony Hoare <a href="http://qconlondon.com/london-2009/presentation/Null+References:+The+Billion+Dollar+Mistake">said this</a>:</p>
<blockquote><p>I call it my billion-dollar mistake. It was the invention of the null reference in 1965. [...] This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.</p></blockquote>
<p>Now since I have a background in functional languages, I know that null references are not necessary. Null value is a member of all types, but some types don&#8217;t have or need a natural notion of absence. In fact in Haskell there is no such thing as an &#8220;absent&#8221; value generic to all types &#8212; types either have to declare it explicitly or wrap the underlying value into a <code>Maybe</code> type that has a dedicated <code>Nothing</code> value.</p>
<p>So e.g. the <code>List</code> type has a notion of an empty list encoded into the type:</p>
<pre>
// Roughly: a list is an empty list
// or a pair of a value and a list
data List a = Nil | Cons a (List a)
</pre>
<p>While the only way to encode an absent structure is to use <code>Maybe</code>:</p>
<pre>
// To build a car you need at most one engine
// and zero or more wheels :)
buildCar :: Maybe Engine -> [Wheel]  -> Car
</pre>
<p>So while I was discussing that with Simon, Kirk banged me on the head to get my attention (just joking, banging me on the head is nowhere near enough to get my attention). Apparently they were discussing more or less the same topic with Rickard, and he had a similar solution for Java. I assumed it was the widely discussed <a href="http://www.jetbrains.com/idea/documentation/howto.html"><code>@NotNull</code></a> annotation, but it was way cooler than that.</p>
<p>Basically in the beginning Rickard started by adding <code>@NotNull</code> support to <a href="http://www.qi4j.org/">Qi4J</a> (BTW an amazing piece of engineering, definitely check it out), so that they could automatically insert runtime assertions for the parameters having the <code>@NotNull</code> annotations, something like that:</p>
<div class="igBar"><span id="ljava-47"><a href="#" onclick="javascript:showPlainTxt('java-47'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-47">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">class</span> Test <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #993333;">void</span> transfer<span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; @NotNull Account from, </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Account to, </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #993333;">double</span> amount<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//&#8230;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> <span style="color: #993333;">void</span> main<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Succeeds</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; transfer<span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> Account<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8220;Bugs Bunny&#8221;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #cc66cc;color:#800000;">1000000</span>.<span style="color: #cc66cc;color:#800000;">0</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Throws NullPointerException</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; transfer<span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">new</span> Account<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8220;Bugs Bunny&#8221;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #cc66cc;color:#800000;">1000000</span>.<span style="color: #cc66cc;color:#800000;">0</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>However soon after that they discovered that they were inserting <code>@NotNull</code>-s everywhere. So they reverted the notion and decided that they will generate the not null assertions for <em>all</em> parameters <em>except</em> the ones marked as <code>@Optional</code>. And this was the point when I thought to myself &#8220;Jevgeni, this is exactly what you were looking for!&#8221; So I turn to Rickard and I say &#8220;Rickard, this is exactly what I was looking for! This is amazing!&#8221; In fact this truly is amazing as it&#8217;s exactly captures the semantics of the <code>Maybe</code> type that I liked so much in the functional languages.</p>
<p>After that we go into a discussion whether or not it is possible to validate this assertion during compile-time. Opinions were mixed on this one, though I personally am convinced that it shouldn&#8217;t be too easy (your opinion on the topic is welcome, also it will make a great master&#8217;s thesis topic). Eventually I get an idea and I say &#8220;Rickard, I bet I could implement a <a href="http://www.zeroturnaround.com/javarebel/">JavaRebel</a> plugin that would check this at runtime in about half an hour in 50 lines of code&#8221;. And of course Rickard goes &#8220;No way!&#8221;, so I&#8217;m challenged. Next day I sit down for half an hour, then for another half an hour (there was no internet!) and voila &#8212; I have a working JavaRebel plugin (in less that 50 lines of code) that will make your methods throw an exception if you try to pass a null reference to a parameter not marked as <code>@Optional</code> (the plugin code deserves another post altogether). Of course I have to show this to Rickard (and he goes &#8220;No way!&#8221;) and we agree to somehow join forces to promote this sane approach (starting with having a single namespace for the <code>@Optional</code> annotation).</p>
<p>So what do we get? The previous example now looks like this:</p>
<div class="igBar"><span id="ljava-48"><a href="#" onclick="javascript:showPlainTxt('java-48'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-48">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">@OptionalCheck</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">class</span> Test <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #993333;">void</span> transfer<span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Account from, </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; @Optional Account to, </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #993333;">double</span> amount<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//&#8230;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> <span style="color: #993333;">void</span> main<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Succeeds</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; transfer<span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> Account<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8220;Bugs Bunny&#8221;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #cc66cc;color:#800000;">1000000</span>.<span style="color: #cc66cc;color:#800000;">0</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Throws NullPointerException</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; transfer<span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">new</span> Account<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8220;Bugs Bunny&#8221;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #cc66cc;color:#800000;">1000000</span>.<span style="color: #cc66cc;color:#800000;">0</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
As you can understand if we make all of the code without the <code>@Optional</code> annotation to throw a <code>NullPointerException</code> for nulls we&#8217;ll break a lot of existing code. Therefore at the moment you also have to annotate the class where you want to enable such semantics with <code>@OptionalCheck</code>. </p>
<p>That&#8217;s pretty much it &#8212; you can <a href="http://repos.zeroturnaround.com/maven2/org/zeroturnaround/javarebel-optional-check-plugin/1.0/javarebel-optional-check-plugin-1.0.jar">download the plugin right away</a> and just drop it in the classpath when <a href="http://www.zeroturnaround.com/javarebel/">JavaRebel</a> is enabled (you&#8217;ll need a 2.0 milestone as 1.x was missing the necessary APIs). At the moment both annotations are in the <code>org.optionalalliance</code> package, but when it changes all you have to do is Organize Imports, so I won&#8217;t sweat the naming too much. Please do let us know what do you think of the approach and feel free to advocate it further :)</p>
<p>Cheers,<br />
Jevgeni Kabanov</p>
<p>P.S. The plugin along with the source is also avalable in our Maven repository:</p>
<ul>
<li>URL: <a href="http://repos.zeroturnaround.com/maven2">http://repos.zeroturnaround.com/maven2</a></li>
<li>Group id: <code>org.zeroturnaround</code></li>
<li>Artifact id: <code>javarebel-optional-check-plugin</code></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2009/02/01/correcting-the-billion-dollar-mistake/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>Announcing Squill: Not Another ORM</title>
		<link>http://dow.ngra.de/2008/12/09/announcing-squill-not-another-orm/</link>
		<comments>http://dow.ngra.de/2008/12/09/announcing-squill-not-another-orm/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 13:24:22 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[creative]]></category>
		<category><![CDATA[dsl]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=501</guid>
		<description><![CDATA[Remember that post about Typesafe DSLs that had a part one and no follow up? Well, meanwhile Juhan Aasaru and yours truly were joined by Michael Hunger of jexp.de and JEQUEL and together we have created the Squill project that came right out of the ideas in the paper we wrote with Rein Raudjärv. The [...]]]></description>
			<content:encoded><![CDATA[<p><em>Remember that post about Typesafe DSLs that had a part one and no follow up? Well, meanwhile Juhan Aasaru and yours truly were joined by Michael Hunger of jexp.de and JEQUEL and together we have created the Squill project that came right out of the ideas in the paper we wrote with Rein Raudjärv. The announcement follows, enjoy!</em></p>
<p>It is with great pleasure that we announce the first release of Squill. <a href="https://squill.dev.java.net/releases/squill-with-examples-1.0-M1.zip">Download it now</a> or check out the <a href="https://squill.dev.java.net/docs/quickstart.html">quickstart guide</a>, the <a href="https://squill.dev.java.net/docs/tutorial.html">step-by-step tutorial</a> and the <a href="https://squill.dev.java.net/docs/squill-devoxx08.pdf">Devoxx presentation</a>.</p>
<p><a href="https://squill.dev.java.net">Squill</a> is a slick internal DSL for writing SQL queries in pure Java. It uses the database metadata and generics to catch as many errors as possible during compilation and is almost completely typesafe. </p>
<p>At the same time it is designed to allow everything SQL allows you to do, exactly the way SQL is meant to do it. This means that you&#8217;re encouraged to select only the data you need and no hidden queries are generated for you, leaving you in full control of the query performance. Squill supports database-specific extensions, allowing you to both use advanced features and fully tweak your queries.</p>
<p>Squill also has special support for CRUD operations and table relations, adding some sugar over vanilla SQL. A typical Squill query looks like this:</p>
<div class="igBar"><span id="ljava-50"><a href="#" onclick="javascript:showPlainTxt('java-50'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-50">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ComplaintTable c = <span style="color: #000000; font-weight: bold;">new</span> ComplaintTable<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>Tuple2&lt;String, Integer&gt; tuple2:</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; squill</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; .<span style="color: #006600;">from</span><span style="color: #66cc66;">&#40;</span>c, c.<span style="color: #006600;">customer</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; .<span style="color: #006600;">where</span><span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; gt<span style="color: #66cc66;">&#40;</span>c.<span style="color: #006600;">customer</span>.<span style="color: #006600;">isActive</span>, <span style="color: #cc66cc;color:#800000;">0</span><span style="color: #66cc66;">&#41;</span>, </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; notNull<span style="color: #66cc66;">&#40;</span>c.<span style="color: #006600;">percentSolved</span><span style="color: #66cc66;">&#41;</span>,&nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; notNull<span style="color: #66cc66;">&#40;</span>c.<span style="color: #006600;">refoundSum</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; .<span style="color: #006600;">orderBy</span><span style="color: #66cc66;">&#40;</span>desc<span style="color: #66cc66;">&#40;</span>c.<span style="color: #006600;">customer</span>.<span style="color: #006600;">id</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; .<span style="color: #006600;">selectList</span><span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; c.<span style="color: #006600;">customer</span>.<span style="color: #006600;">lastName</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; c.<span style="color: #006600;">percentSolved</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&#8220;Customer &#8220;</span> + tuple2.<span style="color: #006600;">v1</span> + <span style="color: #ff0000;">&#8221; has a complaint solved &#8220;</span> + tuple2.<span style="color: #006600;">v2</span> + <span style="color: #ff0000;">&#8220;%&#8221;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Squill is a very young project and you can follow (and help) its development by joining the user or developer <a href="https://squill.dev.java.net/servlets/ProjectMailingListList">mailing lists</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2008/12/09/announcing-squill-not-another-orm/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Performance Cutoff</title>
		<link>http://dow.ngra.de/2008/11/10/the-performance-cutoff/</link>
		<comments>http://dow.ngra.de/2008/11/10/the-performance-cutoff/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 09:36:31 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[creative]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=456</guid>
		<description><![CDATA[A few days ago I had a small epiphany on a simple yet important issue. I was trying to squeeze those last few percents of performance out of JavaRebel and it came to the point where I started optimizing individual hotspots method-by-method.
One of the most common ways to improve execution time of a specific method [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago I had a small epiphany on a simple yet important issue. I was trying to squeeze those last few percents of performance out of <a href="http://www.zeroturnaround.com/javarebel/">JavaRebel</a> and it came to the point where I started optimizing individual hotspots method-by-method.</p>
<p>One of the most common ways to improve execution time of a specific method is the <em>cutoff</em>. For many complicated enough methods there are some inputs for which you can return immediately and cut off the main execution path. To explain let me use the following example:</p>
<div class="igBar"><span id="ljava-53"><a href="#" onclick="javascript:showPlainTxt('java-53'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-53">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Output doSomething<span style="color: #66cc66;">&#40;</span>Input input<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">// Do something for 200 ms</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">return</span> output;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
This method could do anything as long as the complexity is constant, we only care how much time it takes on average. I could also have taken some algorithm (e.g. I started with matrix multiplication), but then we&#8217;d have to bring in complexity estimates and I&#8217;d like to keep it simple for now. </p>
<p>Let&#8217;s assume that for some inputs we could calculate the output in 50ms instead of 200ms. We introduce a check and the code is now:</p>
<div class="igBar"><span id="ljava-54"><a href="#" onclick="javascript:showPlainTxt('java-54'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-54">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Output doSomething<span style="color: #66cc66;">&#40;</span>Input input<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>isSimpleInput<span style="color: #66cc66;">&#40;</span>input<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Do something for 50 ms</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> output;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">// Do something for 200 ms</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">return</span> output;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
This is a common way to optimize the method execution time. However the question is if this actually optimized anything? It seems like a stupid question, but let&#8217;s estimate the new average execution time.</p>
<p>To do that we need to know two things. The time <i>t</i> it takes to do the <code>isSimpleInput()</code> check and the proportion <i>p</i> of method calls that are &#8220;simple&#8221;. Let&#8217;s assume that <i>t</i> is 20 ms and 10% of the calls are simple. The average execution time can then be calculated using the <a href="http://en.wikipedia.org/wiki/Expected_value">expected value</a> formula (<i>EV = p * v1 + (1 &#8211; p) * v2</i>):<br />
<i>EV = 0.1 * (20 + 50) + (1 &#8211; 0.1) * (20 + 200)<br />
    = 20 + 0.1 * 50 + 0.9 * 200<br />
    = 20 + 5 + 180 = 205</i></p>
<p>This calculation shows that with this values we have actually increased the average method execution time by 5 ms. Note that we are still winning every time the cutoff occurs (<i>20 + 50 << 200</i>), but we are losing on the average.</p>
<p>The same equation can also be applied to measuring several checks or measuring execution time that is dependent on the input, however it gets increasingly more complicated. For me the main value of this is realizing that every cutoff has an inherent cost which I will now pay in mind.</p>
<p>Before you think this is all just math, the epiphany that came to me was not to <em>add</em> a check, it was to <em>remove</em> one. I realized that a particular check I&#8217;m making is rare and expensive enough to directly influence the method execution time and lo and behold, removing it gave a 30% improvement for the benchmark I was using. </p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2008/11/10/the-performance-cutoff/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Script kiddies have awesome tools</title>
		<link>http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/</link>
		<comments>http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 16:05:08 +0000</pubDate>
		<dc:creator>Toomas Römer</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[report]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=413</guid>
		<description><![CDATA[About 10 years ago a friend of mine showed me an exploit. It was written in C and it tried to spawn a shell at a remote host. It seemed pretty cool. I did not understand the code but the mere idea that almost anybody equipped with a script like that could deface a webpage [...]]]></description>
			<content:encoded><![CDATA[<p>About 10 years ago a friend of mine showed me an exploit. It was written in C and it tried to spawn a shell at a remote host. It seemed pretty cool. I did not understand the code but the mere idea that almost anybody equipped with a script like that could deface a webpage seemed scary.</p>
<p>You did have to compile the c file, have the right devel packages installed and use the correct flags. And then you had to figure out how to use it. A 14yr old could do it.</p>
<p>Today I spent many hours grepping logs, checking the filesystem for new/changed files to figure out how an old Wordpress instance was hacked and what had the hacker done there.</p>
<p>Going through the changed files I stumbled upon a php file which had some code prepended. The script had a very long line that started like this:</p>
<div class="igBar"><span id="lphp-59"><a href="#" onclick="javascript:showPlainTxt('php-59'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-59">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/eval"><span style="color:#000066;">eval</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/gzinflate"><span style="color:#000066;">gzinflate</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/base64_decode"><span style="color:#000066;">base64_decode</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">&#8216;FJ3HcqPsFkUf&#8230;&#8230;.. </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Ok, lets check what does the script do. Lets assign the long string to a variable and base64 decode it and inflate the compression.</p>
<div class="igBar"><span id="lphp-60"><a href="#" onclick="javascript:showPlainTxt('php-60'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-60">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$script</span> =&nbsp; <a href="http://www.php.net/base64_decode"><span style="color:#000066;">base64_decode</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$script</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$script</span> = <a href="http://www.php.net/gzinflate"><span style="color:#000066;">gzinflate</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$script</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#0000FF;">$script</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The output was not what I expected.</p>
<div class="igBar"><span id="lphp-61"><a href="#" onclick="javascript:showPlainTxt('php-61'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-61">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/eval"><span style="color:#000066;">eval</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/gzinflate"><span style="color:#000066;">gzinflate</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/base64_decode"><span style="color:#000066;">base64_decode</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">&#8216;FJ3HjqPcGkUf57&#8230;&#8230;.. </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The strings looked similar and I was already looking for an error in my code. Nope, code is correct. There <strong>is</strong> a <strong>slight</strong> change in the string. It seems it was compressed and encoded couple of times. Wow, it means I can have many evals inside evals. Fun!</p>
<div class="igBar"><span id="lphp-62"><a href="#" onclick="javascript:showPlainTxt('php-62'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-62">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">do</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#FF9933; font-style:italic;">// extract the first 28 characters</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#FF9933; font-style:italic;">// the eval(gzinflate(base64_decode part</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#0000FF;">$start</span> =&nbsp; <a href="http://www.php.net/substr"><span style="color:#000066;">substr</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$string</span>, <span style="color:#CC66CC;color:#800000;">0</span>, <span style="color:#CC66CC;color:#800000;">28</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#FF9933; font-style:italic;">// remove the first 30 chars, the eval(gzinflate(base64_decode(&#8216; part</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#0000FF;">$string</span> = <a href="http://www.php.net/substr"><span style="color:#000066;">substr</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$string</span>, <span style="color:#CC66CC;color:#800000;">30</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#FF9933; font-style:italic;">// remove the last )));</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#0000FF;">$string</span> = <a href="http://www.php.net/substr"><span style="color:#000066;">substr</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$string</span>, <span style="color:#CC66CC;color:#800000;">0</span>, <a href="http://www.php.net/strlen"><span style="color:#000066;">strlen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$string</span><span style="color:#006600; font-weight:bold;">&#41;</span>-<span style="color:#CC66CC;color:#800000;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#0000FF;">$string</span> =&nbsp; <a href="http://www.php.net/base64_decode"><span style="color:#000066;">base64_decode</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$string</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#0000FF;">$string</span> = <a href="http://www.php.net/gzinflate"><span style="color:#000066;">gzinflate</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$string</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">&#8220;Iteration:&#8221;</span>.<span style="color:#0000FF;">$i</span>++.<span style="color:#FF0000;">&#8220;<span style="color:#000099; font-weight:bold;">\n</span>&#8220;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#FF9933; font-style:italic;">// iterate as long as we get a eval(gzinflat start</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">while</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$start</span> == <span style="color:#FF0000;">&#8220;eval(gzinflate(base64_decode&#8221;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>After <b>11</b> iterations I got the code. Kind of reminded me a challenge that was posted to a mailing list and the question was what was the output of the program. That time it was more difficult: base64 encoded perl, that outputted base64 encoded bytecode, that outputted Java source file with a byte array that was byte code for the class file of the solution.</p>
<p>Anyways the 11 iterations gave me this (shot is made from my home computer).<br />
<a href="http://dow.ngra.de/wp-content/uploads/2008/11/scriptkiddies001.png"><img border="0" src="http://dow.ngra.de/wp-content/uploads/2008/11/scriptkiddies001-300x199.png" alt="Wow!" title="Script Kiddie Console" width="300" height="199" class="size-medium wp-image-419" /></a></p>
<p>Lets see the functionality that it has to offer:</p>
<ul>
<li>Full blown file manager</li>
<li>Quick menu for</li>
<ul>
<li>Finding all suid files</li>
<li>Finding all sgid files</li>
<li>Finding all htaccess files</li>
<li>Finding all writeable folders</li>
<li>&#8230;</li>
</ul>
<li>Interface for the UNIX tool find</li>
<li>Input field for executing commands as webserver user</li>
<li>Tools for installing a backdoor</li>
<ul>
<li>Perl/C flavoured programs that are downloaded from a Singapore server</li>
<li>Compiled/Interpreted &#8211; depending what is available</li>
</ul>
<li>Processes viewer</li>
<li>FTP brute force cracker using users from <em>/etc/passwd</em></li>
<li>System info (CPU, Memory, installed binaries, passwd file, configuration files)</li>
<li>SQL dump utility</li>
<li>Interface for executing PHP code</li>
<li>Self removal</li>
<li>Adding a password for the script</li>
<li>Fancy design!</li>
</ul>
<p>I&#8217;m just amazed. This is way too eazy. So this is how it works:</p>
<ul>
<li>Lets scan the internet for Wordpress installation (automated)</li>
<li>Look for vulnerable versions (automated)</li>
<li>Exploit (in this case themes were filled with hidden links &#8211; semi automated)</li>
<li>PROFIT! (automated)</li>
</ul>
<p>How to avoid being hacked:</p>
<ul>
<li>Keep an eye on your Wordpress installations</li>
<li>Subscribe to WordPress release emails/RSS and upgrade when needed</li>
<li>Monitor for changed files (for example <a href="http://www.geocities.com/fcheck2000/fcheck.html">fcheck</a>)</li>
<li>Run Apache in chroot to minimize the available software for the Apache user</li>
<li>Any other ideas?</li>
</ul>
<p>PS. The script is 2500 lines of code, supports Windows and Linux and looks great :)</p>
<p>
<a href='http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/scriptkiddies001/' title='Script Kiddie Console'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/11/scriptkiddies001-150x150.png" class="attachment-thumbnail" alt="" title="Script Kiddie Console" /></a>
<a href='http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/scriptkiddies003/' title='Find Wrapper'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/11/scriptkiddies003-150x150.png" class="attachment-thumbnail" alt="" title="Find Wrapper" /></a>
<a href='http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/scriptkiddies004/' title='Backdoor installation'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/11/scriptkiddies004-150x150.png" class="attachment-thumbnail" alt="" title="Backdoor installation" /></a>
<a href='http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/scriptkiddies005/' title='FTP Brute Force'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/11/scriptkiddies005-150x150.png" class="attachment-thumbnail" alt="" title="FTP Brute Force" /></a>
<a href='http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/scriptkiddies006/' title='System Information'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/11/scriptkiddies006-150x150.png" class="attachment-thumbnail" alt="" title="System Information" /></a>
<a href='http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/scriptkiddies007/' title='Process viewer'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/11/scriptkiddies007-150x150.png" class="attachment-thumbnail" alt="" title="Process viewer" /></a>
<a href='http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/scriptkiddies008/' title='Self Removal'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/11/scriptkiddies008-150x150.png" class="attachment-thumbnail" alt="" title="Self Removal" /></a>
</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>When protected isn&#8217;t protected</title>
		<link>http://dow.ngra.de/2008/11/03/when-protected-isnt-protected/</link>
		<comments>http://dow.ngra.de/2008/11/03/when-protected-isnt-protected/#comments</comments>
		<pubDate>Mon, 03 Nov 2008 10:41:45 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[creative]]></category>
		<category><![CDATA[bytecode]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=360</guid>
		<description><![CDATA[For all those spec believers out there: what would you think if I told you that all of the JVM implementations consistently violate the spec for at least one particular instance? 
Our story begins on a cold and lonely evening when I discover a bug in JavaRebel in conjunction with Xerces XMLEntityManager. The problem was [...]]]></description>
			<content:encoded><![CDATA[<p>For all those spec believers out there: what would you think if I told you that all of the JVM implementations consistently violate the spec for at least one particular instance? </p>
<p>Our story begins on a cold and lonely evening when I discover a bug in <a href="http://www.zeroturnaround.com/javarebel/">JavaRebel</a> in conjunction with Xerces <a href="http://xerces.apache.org/xerces2-j/javadocs/xerces2/org/apache/xerces/impl/XMLEntityManager.html">XMLEntityManager</a>. The problem was with the following method:</p>
<div class="igBar"><span id="ljava-65"><a href="#" onclick="javascript:showPlainTxt('java-65'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-65">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> getRecognizedProperties<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>RPROPERTIES.<span style="color: #006600;">clone</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
However when I looked at the class bytecode I saw a different picture:</p>
<div class="igBar"><span id="ljava-66"><a href="#" onclick="javascript:showPlainTxt('java-66'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-66">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; GETSTATIC XMLEntityManager.<span style="color: #006600;">RPROPERTIES</span> : <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; INVOKEVIRTUAL <a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Object</span></a>.<span style="color: #006600;">clone</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Object</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; CHECKCAST <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; ARETURN</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
For those of you who do not spend hours staring at the Java bytecode, it does the following:</p>
<ol>
<li>Retrieve the <code>RPROPERTIES</code> static field value (which is of type  <code>String[]</code>) and put it on the top of the stack.</li>
<li>Call <code>Object.clone()</code> using the top of the stack as target object (in our case the array). The target is popped off the stack and the result is placed on top of the stack instead of it. </li>
<li>Cast the top of the stack to a <code>String[]</code></li>
<li>Return the top of the stack as method result.</li>
</ol>
<p>Whooph. Now that is all fine except for one tiny thing. This is not legal. <code>Object.clone()</code> is a protected method and the JVM spec (see <a href="http://java.sun.com/docs/books/jvms/second_edition/html/Concepts.doc.html#18914">2.7.4</a>, <a href="http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.doc6.html">INVOKEVIRTUAL</a>) allows calls to protected method only if the following is true:</p>
<ol>
<li>The accessing class is a subclass of the target class or is the same class (check, <code>XMLEntityManager</code> is a subclass of <code>Object</code>, so&#8217;s everything else).</li>
<li>The class of the target object (the actual runtime class of the object, not the target class we use in the signature) is either a subclass of the accessing class or is the same class. </li>
</ol>
<p>The second check may sound crazy, but it restricts the calls to protected methods only to the classes inheriting from the target class, as you would expect. It can also only be enforced during runtime and is the cause of the <code>IllegalAccessError</code> you may see once in a while.</p>
<p>And obviously in our case <code>String[]</code> that is the actual type of the target object is in no way a subclass of the <code>XMLEntityManager</code>. This call should have cause an error in the JVM, but for some inexplicable reason it doesn&#8217;t. Further tests show that this is only limited (AFAIK) to the <code>Object.clone()</code> call, which is <em>public</em> as far as JVM cares.</p>
<p>A little googling turned up two JVM bugs that were discussing the same issue (<a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4750641">bug 1</a>, <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4329886">bug 2</a>). Turns out the Sun <code>javac</code> compiler has been emitting wrong code at least until 1.4.2 and the JVMs have accommodated accordingly right until today. </p>
<p>Does this have any practical importance? Not for most of you, but if you handle Java bytecode in any way be prepared to handle this exception as well. To me it proves that there is more to Java than just the spec.</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2008/11/03/when-protected-isnt-protected/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>When System.currentTimeMillis() is too slow&#8230;</title>
		<link>http://dow.ngra.de/2008/10/27/when-systemcurrenttimemillis-is-too-slow/</link>
		<comments>http://dow.ngra.de/2008/10/27/when-systemcurrenttimemillis-is-too-slow/#comments</comments>
		<pubDate>Mon, 27 Oct 2008 11:09:14 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[creative]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javarebel]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=347</guid>
		<description><![CDATA[At the moment I am working on reducing the performance overhead of JavaRebel. Once I got rid of all the obvious bottlenecks and optimizations I fired up the profiler and started searching and destroying ad-hoc hotspots. After some time I got to the point, when the bottleneck was in System.currentTimeMillis().
This deserves some explanation. Since JavaRebel [...]]]></description>
			<content:encoded><![CDATA[<p>At the moment I am working on reducing the performance overhead of <a href="http://www.zeroturnaround.com/javarebel/">JavaRebel</a>. Once I got rid of all the obvious bottlenecks and optimizations I fired up the profiler and started searching and destroying ad-hoc hotspots. After some time I got to the point, when the bottleneck was in <code>System.currentTimeMillis()</code>.</p>
<p>This deserves some explanation. Since JavaRebel reloads code after the class changes, it needs to poll the actual class file for changes. The way we do it is quite lazy and the class itself causes the resource to be polled. However often there will be a lot of polling involved (more or less any method call on the class will cause a check), so we need to filter those calls. What we did is to check if some amount of time (e.g. 500 ms) has passed since the last check:</p>
<div class="igBar"><span id="ljava-72"><a href="#" onclick="javascript:showPlainTxt('java-72'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-72">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>lastCheck + <span style="color: #cc66cc;color:#800000;">500</span> &gt; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">currentTimeMillis</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">return</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">lastCheck = <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">currentTimeMillis</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">// Resource check code </span></div>
</li>
</ol>
</div>
</div>
</div>
<p>
After some optimization everything else got fast enough that this call started to weigh the performance down. <code>System.currentTimeMillis()</code> causes a system call and often a hardware poll, which is comparatively expensive. I needed some way to eliminate it.</p>
<p>After some thought I decided to cache the call. It may sound crazy to cache the current time, but I only needed the resolution of about half a second, so it was OK for me if the time was lagging behind about that much. What I did is put up a separate thread that would cache the current time and update it every half a sec.</p>
<div class="igBar"><span id="ljava-73"><a href="#" onclick="javascript:showPlainTxt('java-73'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-73">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TimeCacheThread <span style="color: #000000; font-weight: bold;">extends</span> <a href="http://www.google.com/search?q=allinurl%3AThread+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Thread</span></a> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> volatile <span style="color: #993333;">long</span> currentTimeMillis = </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">currentTimeMillis</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> TimeCacheThread<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; setDaemon<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #993333;">static</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> TimeCacheThread<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> run<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>&nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; currentTimeMillis = <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">currentTimeMillis</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AThread+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Thread</span></a>.<span style="color: #006600;">sleep</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;color:#800000;">500</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AInterruptedException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">InterruptedException</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &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?q=allinurl%3ARuntimeException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">RuntimeException</span></a><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
Then the check became:</p>
<div class="igBar"><span id="ljava-74"><a href="#" onclick="javascript:showPlainTxt('java-74'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-74">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>lastCheck + <span style="color: #cc66cc;color:#800000;">500</span> &gt; TimeCacheThread.<span style="color: #006600;">currentTimeMillis</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">return</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">lastCheck = TimeCacheThread.<span style="color: #006600;">currentTimeMillis</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">// Resource check code </span></div>
</li>
</ol>
</div>
</div>
</div>
<p>
This check is amazingly fast, because instead of doing a system call we are just making a memory read.  </p>
<p>[edit] The original code did not have the field declared volatile with the following explanation: </p>
<blockquote><p>You may also notice the complete lack of any synchronization (or volatile keyword) on the <code>currentTimeMillis</code> static field. Since the type is primitive we can only deal with stale data here and I cared about performance more than I cared about missing a couple of checks.</p></blockquote>
<p>There is a follow-up post (<a href="http://dow.ngra.de/2008/10/28/what-do-we-really-know-about-non-blocking-concurrency-in-java/">What do we really know about non-blocking concurrency in Java?</a>) that explains some more of the reasoning behind initially omitting the volatile and then still putting it in. [/edit]</p>
<p>However once I got that far I decided I could do even better. The <code>currentTimeMillis</code> is of type <code>long</code> and we are still doing some arithmetic on it. In fact the only thing we want to know is if a quantum of time has passed since the last check. And we can do that by using a very simple heartbeat counter:</p>
<div class="igBar"><span id="ljava-75"><a href="#" onclick="javascript:showPlainTxt('java-75'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-75">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HeartBeatThread <span style="color: #000000; font-weight: bold;">extends</span> <a href="http://www.google.com/search?q=allinurl%3AThread+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Thread</span></a> <span style="color: #66cc66;">&#123;</span>&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> volatile <span style="color: #993333;">int</span> counter = <span style="color: #cc66cc;color:#800000;">0</span>;&nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> HeartBeatThread<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; setDaemon<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #993333;">static</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> HeartBeatThread<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> run<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>&nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AThread+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Thread</span></a>.<span style="color: #006600;">sleep</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;color:#800000;">500</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AInterruptedException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">InterruptedException</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &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?q=allinurl%3ARuntimeException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">RuntimeException</span></a><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; counter++;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
Now the checking code becomes just:</p>
<div class="igBar"><span id="ljava-76"><a href="#" onclick="javascript:showPlainTxt('java-76'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-76">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>counter == HeartBeatThread.<span style="color: #006600;">counter</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">return</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">counter = HeartBeatThread.<span style="color: #006600;">counter</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
The <code>counter</code> field is now of <code>int</code> type and we only do an equals check on a DWORD, which should translate directly to 3 very fast hardware instructions (two memory reads and one conditional jump). Hopefully if you ever find yourself polling something a lot this post will come of help. As for me, it improved JavaRebel performance by 70% on some benchmarks.</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2008/10/27/when-systemcurrenttimemillis-is-too-slow/feed/</wfw:commentRss>
		<slash:comments>54</slash:comments>
		</item>
		<item>
		<title>Case study: Is PHP embarrasingly slower than Java?</title>
		<link>http://dow.ngra.de/2008/08/04/optimizing-ip2c-php-implementation/</link>
		<comments>http://dow.ngra.de/2008/08/04/optimizing-ip2c-php-implementation/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 08:50:03 +0000</pubDate>
		<dc:creator>Toomas Römer</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[report]]></category>
		<category><![CDATA[review]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=140</guid>
		<description><![CDATA[IP2C is a small library that provides IP to country resolution. It uses the free ip-to-country database. IP2C takes the database CSV file that is about 4mb and converts it into a ~600kb binary format and provides PHP and Java frontend to query the database.
The library is great, easy to convert an ip to a [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://firestats.cc/wiki/ip2c">IP2C</a> is a small library that provides IP to country resolution. It uses the free <a href="http://ip-to-country.webhosting.info/">ip-to-country</a> database. IP2C takes the database CSV file that is about 4mb and converts it into a ~600kb binary format and provides PHP and Java frontend to query the database.</p>
<p>The library is great, easy to convert an ip to a country and when using the <a href="http://firestats.cc/browser/trunk/easy_ip2country/flags">country flags</a> from it&#8217;s side project you could spice up your statistics with the country information. This a lot faster than using reverse DNS lookup.</p>
<p>The problem. The PHP implementation is a lot slower. Embarrassingly slower. Without any caching the Java version is able to do ~6000 queries per second. The PHP counterpart can push through ~850 queries. The implementations are the same. The stats provided by the author of the library are 8000 vs 1200. So about the same as my measurements.</p>
<p>I like PHP, I don&#8217;t use it that much anymore but I still care when I see such embarrassing numbers. I took the implementation and started profiling it. Spent the night running different tests and trying to optimize.</p>
<p>General outline of the algorithm is as follows. We take the dotted string IP and convert it to an IPv4 Internet network address (e.g. 69.55.232.153 becomes 1161291929). The DB holds sorted ranges of these addresses. A binary search will happen on these addresses and we have a country for the ip. Take a look at the <a href="http://firestats.cc/browser/trunk/ip2c/php/ip2c.php">implementation</a>.</p>
<p><div id="attachment_159" class="wp-caption alignleft" style="width: 160px"><a href="http://dow.ngra.de/wp-content/uploads/2008/08/vanilla1.png"><img src="http://dow.ngra.de/wp-content/uploads/2008/08/vanilla-150x150.png" alt=" " title="Vanilla Profiling Results" width="150" height="150" class="size-thumbnail wp-image-159" /></a><p class="wp-caption-text"> </p></div><br />
Lets see where the vanilla version of IP2C spends its time at. The results are based on 1000 iterations with <a href="http://xdebug.org/">Xdebug</a> enabled and visualized by <a href="http://kcachegrind.sourceforge.net/cgi-bin/show.cgi">KCacheGrind</a>. It processed about 210 IP addresses during this time.</p>
<p>IO part is surprisingly low. The internal <a href="http://php.net/fseek">fseek</a>, <a href="http://www.php.net/fread">fread</a> constitute to 2% of the execution time. On the other hand the user level fseek which is just a wrapper alone uses 5%. readShort and readInt take 20% of the execution time.</p>
<div class="igBar"><span id="lphp-79"><a href="#" onclick="javascript:showPlainTxt('php-79'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-79">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">function</span> readShort<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$a</span> = <a href="http://www.php.net/unpack"><span style="color:#000066;">unpack</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">&#8216;n&#8217;</span>, <a href="http://www.php.net/fread"><span style="color:#000066;">fread</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">m_file</span>, <span style="color:#CC66CC;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$a</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>;<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">function</span> readInt<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF;">$a</span> =<a href="http://www.php.net/unpack"><span style="color:#000066;">unpack</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">&#8216;N&#8217;</span>, <a href="http://www.php.net/fread"><span style="color:#000066;">fread</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">m_file</span>, <span style="color:#CC66CC;color:#800000;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#0000FF;">$a</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>;<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">function</span> seek<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$offset</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.php.net/fseek"><span style="color:#000066;">fseek</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">m_file</span>, <span style="color:#0000FF;">$offset</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><div id="attachment_166" class="wp-caption alignleft" style="width: 160px"><a href="http://dow.ngra.de/wp-content/uploads/2008/08/inlinedfunctions2.png"><img src="http://dow.ngra.de/wp-content/uploads/2008/08/inlinedfunctions2-150x150.png" alt=" " title="Inlined Functions" width="150" height="150" class="size-thumbnail wp-image-166" /></a><p class="wp-caption-text"> </p></div> Functions calls are expensive. Lets eliminate them. readInt, readShort, fseek are now inlined. Recursion changed to iteration (e.g. 14 000 less function calls). Able to process 400 queries per second compared to the previous 210.</p>
<p>We see that the latest profiling results have twice the number of freads and unpacks than fseeks. It seems that fseek is used to seek out the right position, read two numbers with <a href="http://www.php.net/unpack">unpacking</a> them. The implementation confirms that. Luckily we could just read once (2 bytes more) and unpack once (2 unpackings with one invocation).</p>
<div class="igBar"><span id="lphp-80"><a href="#" onclick="javascript:showPlainTxt('php-80'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-80">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$a</span> =<a href="http://www.php.net/unpack"><span style="color:#000066;">unpack</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">&#8216;N&#8217;</span>, <a href="http://www.php.net/fread"><span style="color:#000066;">fread</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">m_file</span>, <span style="color:#CC66CC;color:#800000;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$np</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">&#8216;ip&#8217;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF;">$a</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$a</span> =<a href="http://www.php.net/unpack"><span style="color:#000066;">unpack</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">&#8216;n&#8217;</span>, <a href="http://www.php.net/fread"><span style="color:#000066;">fread</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">m_file</span>, <span style="color:#CC66CC;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$np</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">&#8216;key&#8217;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF;">$a</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// this can be changed to</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$np</span> =<a href="http://www.php.net/unpack"><span style="color:#000066;">unpack</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">&#8216;Nip/nkey&#8217;</span>, <a href="http://www.php.net/fread"><span style="color:#000066;">fread</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$this</span>-&gt;<span style="color:#006600;">m_file</span>, <span style="color:#CC66CC;color:#800000;">6</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>How does this version stack up to the Java version? Lets disable profiling and run 100 000 iterations. Vanilla version processes ~850 IPs, when functions are inlined the number is around 1400. Java version can still do 6000.</p>
<p>Lets try caching. Peeking at the Java <a href="http://firestats.cc/browser/trunk/ip2c/java/src/net/firefang/ip2c/input/RandomAccessBuffer.java">implementation</a> shows that Java caching version (whopping 141 242 IPs per second &#8211; yup 141k) uses just a byte[] array and makes lookups from there instead of seeking and reading from file. Easy, lets do the same in PHP.</p>
<p>We read everything into a string and instead of fread with access the string elements with the offset. For fseek with just set the offset. We are using 600kb more memory but can increase the throughput to ~2800.</p>
<p>As it seems I&#8217;ve just wasted a night, I just should have checked the <a href="http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&#038;lang=php&#038;lang2=javaclient">Computer Language Benchmarks</a>. PHP in the sense of execution speed is uncomparable to Java.</p>
<p>The upside, we can still take the library, eliminate recursions, double unpacks and add caching. A small gain is still a gain.</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2008/08/04/optimizing-ip2c-php-implementation/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Typesafe DSLs in Java: Part 1 &#8212; Typesafe Bytecode</title>
		<link>http://dow.ngra.de/2008/03/24/typesafe-dsls-in-java-part-1-typesafe-bytecode/</link>
		<comments>http://dow.ngra.de/2008/03/24/typesafe-dsls-in-java-part-1-typesafe-bytecode/#comments</comments>
		<pubDate>Mon, 24 Mar 2008 08:25:29 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[creative]]></category>
		<category><![CDATA[asm]]></category>
		<category><![CDATA[bytecode]]></category>
		<category><![CDATA[dsl]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/2008/03/24/typesafe-dsls-in-java-part-1-typesafe-bytecode/</guid>
		<description><![CDATA[Domain Specific Languages (DSLs) have been brought to Java under the name of <a href="http://en.wikipedia.org/wiki/Fluent_interface">Fluent Interface</a>. However most of them utilize a lot of strings and untyped behavior to make the interface fluent enough. It turns out that using Java 5 and a bag of tricks we can have the compiler to check a lot more. In this post we'll check out how to write Java bytecode using ASM in a typesafe way.]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript">var dzone_url = "http://dow.ngra.de/2008/03/24/typesafe-dsls-in-java-part-1-typesafe-bytecode/";</script></p>
<div id="dzone_vote_widget" style="float: left; margin-right: 20px;"><script language="javascript" src="http://www.dzone.com/widgets/zoneit.js"></script></div>
<p><em>Domain Specific Languages (DSLs) have been brought to Java under the name of <a href="http://en.wikipedia.org/wiki/Fluent_interface">Fluent Interface</a>. However most of them utilize a lot of strings and untyped behavior to make the interface fluent enough. It turns out that using Java 5 and a bag of tricks we can have the compiler to check a lot more. In this post we'll check out how to write Java bytecode using ASM in a typesafe way.</em></p>
<p><span id="more-50"></span></p>
<h3>Domain Specific Languages</h3>
<p><a href="http://en.wikipedia.org/wiki/Domain-specific_programming_language">Domain specific language</a> usually refers to a small sublangauge that has very little overhead when expressing domain specific data and behaviour.  DSL is a broad term and can refer both to a fully implemented language and a specialized API that looks like a sublanguage, but still written using some general-purpose language. Such DSLs in the latter meaning have been introduced (perhaps independently?) by both the functional and dynamic languages community. Both these communities (and especially functional) took advantage of function composition and operator overloading to build combinator-based languages that look nothing like the original. The functional communities also strongly support the notion of type-safety, therefore the DSLs they create are usually statically typed.</p>
<p>In Java the DSLs are also becoming more popular. The first examples like jMock and Hibernate Criteria were coined as <a href="http://en.wikipedia.org/wiki/Fluent_interface">Fluent Interface</a>. However most of these DSLs are not typesafe and will allow wrong statements to be compiled. In this post we introduce some patterns that help making Java DSLs safer.</p>
<h3>Java bytecode engineering</h3>
<p>Java bytecode is a relatively simple stack-based language. All the code is contained in methods, class structure is pretty much preserved from source (fields and methods, both can be static, constructors and static initializers are turned into methods named "&lt;init&gt;" and "&lt;clinit&gt;" correspondingly). Inside the method we have the variables, referred by an index with 0 being "this", 1 being the first parameter and so on, local variables starting after parameters. We can load and store variables. We also have the stack, where we can push, pop and duplicate values. We have a number of basic operations on the stack (a la add and multiply) as well as method invocation. When invoking the methods parameters are gathered from the stack with last parameter being on top of the stack. Finally we have some flow control, namely conditional (and unconditional) jumps. That pretty much <a href="http://java.sun.com/docs/books/jvms/second_edition/html/VMSpecTOC.doc.html">sums it up</a>.</p>
<p>One of the best libraries for working with Java bytecode is <a href="http://asm.objectweb.org/">ASM</a>. It provides both a lightweight fast visitor-based interface and a more comfortable tree-based object-oriented interface. Unfortunately both of them (and especially visitor-based one) are completely untyped and debugging the wrong bytecode created using them is a huge pain in the neck. 'Nuff to say that Java bytecode verifier will tell you there's a problem, but will not tell you where exactly it is.</p>
<p>Further we will examine in details the bytecode of the following example:</p>
<div class="igBar"><span id="ljava-81"><a href="#" onclick="javascript:showPlainTxt('java-81'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-81">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorld <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> <span style="color: #993333;">void</span> main<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"Hello, World!"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>If it looks simple to you -- it should. However the Java bytecode is a bit more complex, we have a default constructor that compiler will generate for you and we can do only one operation per line (more correctly per instruction). Next you see the <code>HelloWorld</code> example in Java bytecode (you can get a similar using "javah -c ClassName", although <a href="http://asm.objectweb.org/eclipse/index.html">Bytecode outline</a> Eclipse plugin is more comfortable).</p>
<div class="igBar"><span id="ljava-82"><a href="#" onclick="javascript:showPlainTxt('java-82'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-82">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorld <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">// Default constructor</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> &lt;init&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>V</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; ALOAD <span style="color: #cc66cc;color:#800000;">0</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; INVOKESPECIAL java/lang/<a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Object</span></a>.&lt;init&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>V</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">RETURN</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">// System.out.println(&quot;Hello, World!&quot;);</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">static</span> main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>Ljava/lang/<a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a>;<span style="color: #66cc66;">&#41;</span>V</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; GETSTATIC java/lang/<a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span> : Ljava/io/<a href="http://www.google.com/search?q=allinurl%3APrintStream+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">PrintStream</span></a>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; LDC <span style="color: #ff0000;">"Hello, World!"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; INVOKEVIRTUAL java/io/<a href="http://www.google.com/search?q=allinurl%3APrintStream+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">PrintStream</span></a>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span>Ljava/lang/<a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a>;<span style="color: #66cc66;">&#41;</span>V</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">RETURN</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Here's a summary of the instructions in the example:</p>
<ul>
<li>ALOAD loads the local variables to the top of the stack, index 0 is "this"</li>
<li>INVOKESPECIAL in this case invokes super method, it also consumes and Object from the stack.</li>
<li>GETSTATIC retrieves the value of the static fields and puts it on the stack (in this case a PrintStream)</li>
<li>LDC pushes a constant value to the stack</li>
<li>INVOKEVIRTUAL invokes a usual (virtual) method, consuming the parameters and returning the result</li>
<li>RETURN returns from the method</li>
</ul>
<p>If we want to write the same code from Java using ASM it would be almost the same:</p>
<div class="igBar"><span id="ljava-83"><a href="#" onclick="javascript:showPlainTxt('java-83'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-83">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ClassWriter cw = <span style="color: #000000; font-weight: bold;">new</span> ClassWriter<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;color:#800000;">0</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MethodVisitor mv;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">cw.<span style="color: #006600;">visit</span><span style="color: #66cc66;">&#40;</span>V1_6, ACC_PUBLIC + ACC_SUPER, <span style="color: #ff0000;">"HelloWorld"</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #ff0000;">"java/lang/Object"</span>, <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mv = cw.<span style="color: #006600;">visitMethod</span><span style="color: #66cc66;">&#40;</span>ACC_PUBLIC, <span style="color: #ff0000;">"&lt;init&gt;"</span>, <span style="color: #ff0000;">"()V"</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mv.<span style="color: #006600;">visitCode</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mv.<span style="color: #006600;">visitVarInsn</span><span style="color: #66cc66;">&#40;</span>ALOAD, <span style="color: #cc66cc;color:#800000;">0</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mv.<span style="color: #006600;">visitMethodInsn</span><span style="color: #66cc66;">&#40;</span>INVOKESPECIAL, <span style="color: #ff0000;">"java/lang/Object"</span>, <span style="color: #ff0000;">"&lt;init&gt;"</span>, <span style="color: #ff0000;">"()V"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mv.<span style="color: #006600;">visitInsn</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">RETURN</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mv.<span style="color: #006600;">visitMaxs</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;color:#800000;">1</span>, <span style="color: #cc66cc;color:#800000;">1</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mv.<span style="color: #006600;">visitEnd</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mv = cw.<span style="color: #006600;">visitMethod</span><span style="color: #66cc66;">&#40;</span>ACC_PUBLIC + ACC_STATIC, <span style="color: #ff0000;">"main"</span>, <span style="color: #ff0000;">"([Ljava/lang/String;)V"</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mv.<span style="color: #006600;">visitCode</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mv.<span style="color: #006600;">visitFieldInsn</span><span style="color: #66cc66;">&#40;</span>GETSTATIC, <span style="color: #ff0000;">"java/lang/System"</span>, <span style="color: #ff0000;">"out"</span>, <span style="color: #ff0000;">"Ljava/io/PrintStream;"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mv.<span style="color: #006600;">visitLdcInsn</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"Hello, World!"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mv.<span style="color: #006600;">visitMethodInsn</span><span style="color: #66cc66;">&#40;</span>INVOKEVIRTUAL, <span style="color: #ff0000;">"java/io/PrintStream"</span>, <span style="color: #ff0000;">"println"</span>, <span style="color: #ff0000;">"(Ljava/lang/String;)V"</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mv.<span style="color: #006600;">visitInsn</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">RETURN</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mv.<span style="color: #006600;">visitMaxs</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;color:#800000;">2</span>, <span style="color: #cc66cc;color:#800000;">1</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mv.<span style="color: #006600;">visitEnd</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">cw.<span style="color: #006600;">visitEnd</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3>Typesafe ASM frontend</h3>
<p>Take a look at the <code>mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");</code>. It requires two elements on the stack -- an instance of <code>PrintStream</code> on which the method is called and a <code>String</code>. What we want to do is for compiler to issue an error when the stack in fact does not contain such elements. We propose the following DSL as the basis:</p>
<div class="igBar"><span id="ljava-84"><a href="#" onclick="javascript:showPlainTxt('java-84'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-84">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">new</span> ClassBuilder<span style="color: #66cc66;">&#40;</span>cw, V1_4, ACC_PUBLIC, <span style="color: #ff0000;">"HelloWorld"</span>, <span style="color: #ff0000;">"java/lang/Object"</span>, <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>&nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; .<span style="color: #006600;">beginStaticMethod</span><span style="color: #66cc66;">&#40;</span>ACC_PUBLIC | ACC_STATIC, <span style="color: #ff0000;">"main"</span>, <span style="color: #993333;">void</span>.<span style="color: #000000; font-weight: bold;">class</span>, <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; .<span style="color: #006600;">getStatic</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #ff0000;">"out"</span>, <a href="http://www.google.com/search?q=allinurl%3APrintStream+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">PrintStream</span></a>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; .<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"Hello, World!"</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">//Here String.class refers to the type of the first parameter</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; .<span style="color: #006600;">invokeVirtualVoid</span><span style="color: #66cc66;">&#40;</span>INVOKEVIRTUAL, <a href="http://www.google.com/search?q=allinurl%3APrintStream+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">PrintStream</span></a>.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #ff0000;">"println"</span>, <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; .<span style="color: #006600;">returnVoid</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; .<span style="color: #006600;">endMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
Note that now all the types are written as class literals instead of strings. In Java 5, class literals are generified to <code>Class&lt;C&gt;</code>, where <code>C</code> refers to the actual underlying type. This already allows for less mistakes, since the classes are no longer written as strings. However we want more!</p>
<p>The first thing we want to do is to track the stack size. We wouldn't want to allow to <code>pop()</code> off an empty stack. And we would want to ensure that when you invoke a method all the parameters are there. To do that we introduce a simple idiom:</p>
<blockquote><p><strong>The type you return from your DSL method should allow exactly those operations that are possible with the current state.</strong></p></blockquote>
<p>What does it mean in our context? It means that when we build a method we don't have just a <code>MethodBuilder</code> class, but <code>MethodBuilderS0</code>, <code>MethodBuilderS1</code>, <code>MethodBuilderS2</code> and so on, where the <code>S*</code> refers to the stack size. And each of them has only the methods possible with the current stack size. Now the method for pop() will not even show up in autocompletion if the stack is not large enough.</p>
<p>We can apply the same trick to tracking variables, allowing to add only one variable at a time and providing methods <code>loadVar*</code>/<code>stroreVar*</code>. This way our class becomes <code>MethodBuilderS*V*</code> where <code>S</code> stands for stack size and <code>V</code> stands for variable count.</p>
<p>Of course we want more. We want to actually track the stack and variable types. To do that we introduce the following idiom:</p>
<blockquote><p><strong>The DSL type should be parametrized by all of the accumulated types as should be it's methods.</strong></p></blockquote>
<p>What does that mean? Let's return to our example and see what happens if I replace the string "Hello, World!" with an integer 11?</p>
<p><img src='http://dow.ngra.de/wp-content/uploads/2008/03/asm-error-1.PNG' alt='asm-error-1.PNG' /</p>
<p>As we can see from the screenshot the compiler inferred that the argument <code>invokeVirtualVoid()</code> will receive is an Integer (generics do not work with primitive types), whereas we have stated that it should receive a String as a parameter. Another thing we can see is that <code>invokeVirtualVoid()</code> method belongs to the <code>MethodBuilderS2V1&lt;Self, PrintStream, Integer, String[]&gt;</code> class.</p>
<p>As we know the name <code>MethodBuilderS2V1</code> refers to a class that tracks two stack slots and one variable. The types are encoded accordingly in <code>&lt;Self, PrintStream, Integer, String[]&gt;</code> -- the two stack types are <code>PrintStream, Integer</code> and the one variable type us <code>String[]</code>. <code>Self</code> is not important for now.</p>
<p>To understand how the tracking is implemented let's see the <code>push()</code> and <code>pop()</code> implementation (the rest are omitted). </p>
<div class="igBar"><span id="ljava-85"><a href="#" onclick="javascript:showPlainTxt('java-85'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-85">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MethodBuilderS2V1 &lt;O, S0, S1, V0&gt; <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> &lt;S&gt; MethodBuilderS3V1&lt;O, S0, S1, S, V0&gt; push<span style="color: #66cc66;">&#40;</span>S value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; mv.<span style="color: #006600;">visitLdcInsn</span><span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> MethodBuilderS3V1&lt;O, S0, S1, S, V0&gt;<span style="color: #66cc66;">&#40;</span>cb, mv<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> MethodBuilderS1V1&lt;O, S0, V0&gt; pop<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; mv.<span style="color: #006600;">visitInsn</span><span style="color: #66cc66;">&#40;</span>POP<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> MethodBuilderS1V1&lt;O, S0, V0&gt;<span style="color: #66cc66;">&#40;</span>cb, mv<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
So as you can see the class itself is parametrized by all of the accumulated type parameters (which were one by one added by previously invoked DSL methods). We can see that <code>push()</code> returns an instance of <code>MethodBuilderS3V1</code>. That class tracks exactly one more stack slot, which is passed to it as inferred from the argument (<code>S</code>). <code>pop()</code> on the other hand just discards one stack slot and returns an instance of <code>MethodBuilderS1V1</code>.</p>
<p>So what does the <code>invokeVirtualVoid()</code> look like? First of all, it needs to consume two stack variables, therefore we need at least <code>MethodBuilderS2V*</code> class to call it. Therefore all classes with less stack variables will not have this method. Secondly we need to check that the types in the stack are fitting, an must also allow for some leniency due to subtyping:</p>
<div class="igBar"><span id="ljava-86"><a href="#" onclick="javascript:showPlainTxt('java-86'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-86">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MethodBuilderS2V1&lt;O, S0, S1, V0&gt; <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> MethodBuilderS0V1&lt;O, V0&gt; invokeVirtualVoid<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> kind, Class&lt;? super S0&gt; owner, <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> name, Class&lt;? super S1&gt; parameter1<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; mv.<span style="color: #006600;">visitMethodInsn</span><span style="color: #66cc66;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; kind, </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; owner.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'.'</span>, <span style="color: #ff0000;">'/'</span><span style="color: #66cc66;">&#41;</span>, </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; name, </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Type.<span style="color: #006600;">getMethodDescriptor</span><span style="color: #66cc66;">&#40;</span>Type.<span style="color: #006600;">VOID_TYPE</span>, <span style="color: #000000; font-weight: bold;">new</span> Type<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>Type.<span style="color: #006600;">getType</span><span style="color: #66cc66;">&#40;</span>parameter1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> MethodBuilderS0V1&lt;O, V0&gt;<span style="color: #66cc66;">&#40;</span>cb, mv<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
As you can see it indeed consumes two stack values returning <code>MethodBuilderS0V1</code>. If our method would also return a result we would need to take the result type as well, and return a class with stack depth only one less. The expression "? super S0" means that we require the actual parameter type to be a superclass of the stack type.</p>
<h4>Problem 1: The world isn't perfect</h4>
<p>One of the problems with the DSL we proposed here is that it assumes all the types exist. However it is very often the case that some of the types (most prominently the class currently being created) do not. You can somewhat alleviate the problem by introducing a special placeholder <code>Self</code> type and use it instead of the current class name, but it won't solve the problem of other classes still awaiting construction.</p>
<p>A different (but connected) problem is that you are not always constructing the full method, instead you could be just creating a prelude for a particular method or replacing one instruction with a series of your own. To solve we add this idiom:</p>
<blockquote><p><strong>We should allow escaping from the rigid typesafe world by having unsafe operations, which issue compiler warnings</strong></p></blockquote>
<p>This means that instead of missing <code>pop()</code> from a type with no stack slots we should just deprecate it or otherwise issue a warning. This also means that we should have invoke* methods that take strings as parameter types, similarly deprecated.</p>
<p>However, since we know it's not a perfect world we'd like to at least protect ourselves a bit better. Therefore we add another idiom:</p>
<blockquote><p><strong>Allow users to document their assumptions.</strong></p></blockquote>
<p>This means that when we need to write just a fragment of bytecode we want to document what stack values and variables will it need. For that we introduce methods <code>assumePush()</code>/<code>assumePop()</code> and <code>assumeVar*()</code>, which do not push any values, but just add the corresponding type variables:</p>
<div class="igBar"><span id="ljava-87"><a href="#" onclick="javascript:showPlainTxt('java-87'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-87">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MethodBuilderS2V1 &lt;O, S0, S1, V0&gt; <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> &lt;S&gt; MethodBuilderS3V1&lt;O, S0, S1, S, V0&gt; assumePush<span style="color: #66cc66;">&#40;</span>Class&lt;S&gt; type<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> MethodBuilderS3V1&lt;O, S0, S1, S, V0&gt;<span style="color: #66cc66;">&#40;</span>cb, mv<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> MethodBuilderS1V1&lt;O, S0, V0&gt; assumePop<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> MethodBuilderS1V1&lt;O, S0, V0&gt;<span style="color: #66cc66;">&#40;</span>cb, mv<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> &lt;V&gt; MethodBuilderS2V2&lt;O, S0, S1, V0, V&gt; assumeVar1<span style="color: #66cc66;">&#40;</span>Class&lt;V&gt; type<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> MethodBuilderS2V2&lt;O, S0, S1, V0, V&gt;<span style="color: #66cc66;">&#40;</span>cb, mv<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Using them we can document our expectations and let the compiler validate them. The following is an example of how we can use the assumptions to document that we expect <code>PrintStream</code> to be on stack and <code>String[]</code> to be the variable 0.</p>
<div class="igBar"><span id="ljava-88"><a href="#" onclick="javascript:showPlainTxt('java-88'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-88">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">static</span> &lt;O&gt; <span style="color: #993333;">void</span> genSayHello<span style="color: #66cc66;">&#40;</span>MethodBuilderS0V0&lt;O&gt; mb<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; mb.<span style="color: #006600;">assumeVar0</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; .<span style="color: #006600;">assumePush</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3APrintStream+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">PrintStream</span></a>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; .<span style="color: #006600;">loadVar0</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; .<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;color:#800000;">0</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; .<span style="color: #006600;">arrayLoad</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #000000; font-weight: bold;">class</span>, <a href="http://www.google.com/search?q=allinurl%3AInteger+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Integer</span></a>.<span style="color: #000000; font-weight: bold;">class</span>, <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; .<span style="color: #006600;">invokeVirtualVoid</span><span style="color: #66cc66;">&#40;</span>INVOKEVIRTUAL, <a href="http://www.google.com/search?q=allinurl%3APrintStream+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">PrintStream</span></a>.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #ff0000;">"println"</span>, <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h4>Problem 2: Control flow and reuse</h4>
<p>The next problem is how to mix the DSL with the general-purpose control flow and method calls. The problem here is that (although it is hidden from the user) we return a different type every time. Therefore if we add a conditional operation, we would need to save the type in a variable, which would be clumsy, since it contains a lot of inferred types. It also wouldn't cope with changes, since the inferred types would change every time. To solve this we invoke the next idiom:</p>
<blockquote><p><strong>Hide control flow in closures.</strong></p></blockquote>
<p>In fact we introduce two types of closures -- the strongly typed and weakly typed. The weakly typed one loses all accumulated type information and is meant first of all for reusable methods, since they need to take one based type as an argument:</p>
<div class="igBar"><span id="ljava-89"><a href="#" onclick="javascript:showPlainTxt('java-89'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-89">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Closure <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> &lt;O&gt; <span style="color: #993333;">void</span> apply<span style="color: #66cc66;">&#40;</span>MethodBuilderS0V0&lt;O&gt; mb<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The strongly typed closures are generated with the class and look like that:</p>
<div class="igBar"><span id="ljava-90"><a href="#" onclick="javascript:showPlainTxt('java-90'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-90">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MethodBuilderS2V1 &lt;O, S0, S1, V0&gt; <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> MethodBuilderS2V1&lt;O, S0, S1, V0&gt; closure<span style="color: #66cc66;">&#40;</span>ClosureS2V1 closure<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; closure.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">public</span> MethodBuilderS2V1&lt;O, S0, S1, V0&gt; closure<span style="color: #66cc66;">&#40;</span>Closure closure<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; closure.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MethodBuilderS0V0&lt;O&gt;<span style="color: #66cc66;">&#40;</span>cb, mv<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000000; font-weight: bold;">interface</span> ClosureS2V1 <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &lt;O, S0, S1, V0&gt; <span style="color: #993333;">void</span> apply<span style="color: #66cc66;">&#40;</span>MethodBuilderS2V1&lt;O, S0, S1, V0&gt; mb<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>We illustrating using the weakly typed closures to call the method <code>genSayHello()</code> introduced previously:</p>
<div class="igBar"><span id="ljava-91"><a href="#" onclick="javascript:showPlainTxt('java-91'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-91">
<div class="java">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">.<span style="color: #006600;">beginStaticMethod</span><span style="color: #66cc66;">&#40;</span>ACC_PUBLIC | ACC_STATIC, <span style="color: #ff0000;">"main"</span>, <span style="color: #993333;">void</span>.<span style="color: #000000; font-weight: bold;">class</span>, <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; .<span style="color: #006600;">getStatic</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #ff0000;">"out"</span>, <a href="http://www.google.com/search?q=allinurl%3APrintStream+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">PrintStream</span></a>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; .<span style="color: #006600;">closure</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Closure<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> &lt;O&gt; <span style="color: #993333;">void</span> apply<span style="color: #66cc66;">&#40;</span>MethodBuilderS0V0&lt;O&gt; mb<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; genSayHello<span style="color: #66cc66;">&#40;</span>mb<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; .<span style="color: #006600;">returnVoid</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; .<span style="color: #006600;">endMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Of course with the introduction of closures in Java 7 this idiom would be much shorter</p>
<h4>Problem 3: Productivity and performance</h4>
<p>One of the problems with having classes for each stack depth and variable size is that we have a lot of them ((max stack depth + 1) times (max variable count + 1)). And we need to have a reasonably big number as maximum before deprecating the methods and losing track of some stack slots and variables. Writing them by hand would be exteremely unefficient,  therefore we propose to use a generator. </p>
<p>The other problem is that having such a great number of classes and instances at runtime could cause performance problems. The way we could solve it is providing also a source-compatible unsafe library that would provide exactly the same interface, but always return "this" and track no types to allow good perfromance. It cannot be made binary compatible, since every method has a different signature (even though inference hides it from the user), but since those signatures are not present in source it would be a matter of recompiling the application for production, after insuring in development that the code is typesafe.</p>
<h4>Problem 4: Primitive types and operations</h4>
<p>The final problem is one of the most annoying and the one we tackled the least. Since generic types cannot be parametrized by primitive types we have to track boxed types instead of primitives (Integer v/s int). This means that we cannot distinguish among them and they can still cause a runtime error. What's worse we need to somehow indicate to a method that the parameter is a primitive type, but still need to pass to it a boxed one to satisfy the inference. There are several possibilities here, but all of them are relatively ugly. </p>
<p>A similar problem, which is yet to find an elegant solution is that <code>double</code> and <code>long</code> take two stack slots, whereas everything else takes one. And of course some coercions will not work with the current prototype.</p>
<p>If you have any ideas, please let us know.</p>
<h4>But is it real?</h4>
<p>The code is available  <a href="http://code.google.com/p/typed-asm/">in a Google Code repository</a>. The code is just a prototype to show off the typesafe DSLs for our upcoming paper and is yet nowhere ready to even consider its usage. </p>
<p>The generator is written in PHP and driven by a BASH script. It may sound crazy, but it allowed me to prototype the code very quickly.</p>
<p>UPDATE: I also uploaded a <a href="http://typed-asm.googlecode.com/files/TypedAsm.zip">downloadable distribution</a> with 10x20 classes pregenerated for your pleasure. Check out <code>src/Test.java</code> for the example and <code>gen/*</code> for the generated classes. The <code>asm-3.0.jar</code> must be in the classpath.</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2008/03/24/typesafe-dsls-in-java-part-1-typesafe-bytecode/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->