<?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; creative</title>
	<atom:link href="http://dow.ngra.de/category/creative/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>Where did my OOM go?</title>
		<link>http://dow.ngra.de/2009/03/24/where-did-my-oom-go/</link>
		<comments>http://dow.ngra.de/2009/03/24/where-did-my-oom-go/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 10:54:45 +0000</pubDate>
		<dc:creator>Ivo Mägi</dc:creator>
				<category><![CDATA[cool]]></category>
		<category><![CDATA[creative]]></category>
		<category><![CDATA[humour]]></category>
		<category><![CDATA[cdo]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=785</guid>
		<description><![CDATA[Editor: We have a guest appearance from a CDO of a Fortune 500 company (actually not Fortune 500, but yes, a development manager for 200 devs). The twist? He has a compiler installed and apparently an editor too.
The other day I was playing around with a tool for memory analysis and wrote a small snippet [...]]]></description>
			<content:encoded><![CDATA[<p><em>Editor: We have a guest appearance from a CDO of a Fortune 500 company (actually not Fortune 500, but yes, a development manager for 200 devs). The twist? He has a compiler installed and apparently an editor too.</em></p>
<p>The other day I was playing around with a tool for memory analysis and wrote a small snippet using which I would be able to test the new tool. Quickly (<em>Editor: yeah right quickly)</em> created the following and executed it:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #a1a100;">import java.util.*;   </span>
<span style="color: #000000; font-weight: bold;">class</span> Leak <span style="color: #66cc66;">&#123;</span>  
  <span style="color: #993333;">static</span> List<span style="color: #66cc66;">&lt;</span>Integer<span style="color: #66cc66;">&gt;</span> list = <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #66cc66;">&lt;</span>Integer<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>  
  <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><span style="color: #aaaadd; font-weight: bold;">String</span><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>  
    <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">;</span> i <span style="color: #66cc66;">&gt;</span>= <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>  
      list.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span>i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>  
    <span style="color: #66cc66;">&#125;</span>  
    <span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;I will either reach here or die trying (with OOM)&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>  
  <span style="color: #66cc66;">&#125;</span>  
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>And what do you think I was displayed by my command prompt <em>(editor: CDOs don't have a command prompt)</em>:</p>
<p>Option A:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #aaaadd; font-weight: bold;">Exception</span> in thread <span style="color: #ff0000;">&quot;main&quot;</span> java.<span style="color: #006600;">lang</span>.<span style="color: #aaaadd; font-weight: bold;">OutOfMemoryError</span>: Java heap space
        at java.<span style="color: #006600;">util</span>.<span style="color: #aaaadd; font-weight: bold;">Arrays</span>.<span style="color: #006600;">copyOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Arrays</span>.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">2760</span><span style="color: #66cc66;">&#41;</span>
        at java.<span style="color: #006600;">util</span>.<span style="color: #aaaadd; font-weight: bold;">Arrays</span>.<span style="color: #006600;">copyOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Arrays</span>.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">2734</span><span style="color: #66cc66;">&#41;</span>
        at java.<span style="color: #006600;">util</span>.<span style="color: #aaaadd; font-weight: bold;">ArrayList</span>.<span style="color: #006600;">ensureCapacity</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">ArrayList</span>.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">167</span><span style="color: #66cc66;">&#41;</span>
        at java.<span style="color: #006600;">util</span>.<span style="color: #aaaadd; font-weight: bold;">ArrayList</span>.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">ArrayList</span>.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">351</span><span style="color: #66cc66;">&#41;</span>
        at Leak.<span style="color: #006600;">main</span><span style="color: #66cc66;">&#40;</span>Leak.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Option B:</p>
<pre>
I will either reach here or die trying (with OOM)
</pre>
<p>Well, as I found out, it doesn’t print out anything. As this is now two years from my last real Java development experience (got demoted to C-level), I went to my hardcore Java hackers. For 10 minutes we all stood with bedazzled faces, before it struck – the memory will be allocated in a way that there is no room for <code>new OutOfMemoryError()</code> to be created.</p>
<p>If you execute the above with 64MB heapsize (which is the default heapsize):</p>
<pre>
C:\work\snippets\leak java -Xmx64m Leak
C:\work\snippets\leak
</pre>
<p>But if you increase (well actually modify) the heap size a bit:</p>
<pre>C:\work\snippets\leak java -Xmx65m Leak
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at java.util.Arrays.copyOf(Arrays.java:2760)
        at java.util.Arrays.copyOf(Arrays.java:2734)
        at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
        at java.util.ArrayList.add(ArrayList.java:351)
        at Leak.main(Leak.java:6)
C:\work\snippets\leak
</pre>
<p>Moral of the story? I definitely would not want to be fixing a production system dying like this <em>(Editor: lucky you, you don't have to) </em>– most likely I would be mad way before I could have grasped anything about the reasons …</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2009/03/24/where-did-my-oom-go/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Mysteries of &#8220;Protected&#8221;</title>
		<link>http://dow.ngra.de/2009/03/13/the-mysteries-of-protected/</link>
		<comments>http://dow.ngra.de/2009/03/13/the-mysteries-of-protected/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 00:08:38 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[creative]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=736</guid>
		<description><![CDATA[This post is a follow up to the Ultimate Java Puzzler. I was going to name it &#8220;The Most Penultimate Java Puzzler Ever to Exist&#8221;, but it just doesn&#8217;t have the same ring to it anymore :)  
The previous post was about the weird relation between default visibility and method overriding, which in the [...]]]></description>
			<content:encoded><![CDATA[<p>This post is a follow up to the <a href="http://dow.ngra.de/2009/02/16/the-ultimate-java-puzzler/">Ultimate Java Puzzler</a>. I was going to name it &#8220;The Most Penultimate Java Puzzler Ever to Exist&#8221;, but it just doesn&#8217;t have the same ring to it anymore :)  </p>
<p>The previous post was about the weird relation between <code>default</code> visibility and method overriding, which in the end meant that <code>@Override</code> is broken in some cases. Today we&#8217;ll take a look at <code>protected</code>.</p>
<p>The main issue with <code>protected</code> in Java is that unlike C++ and most other OO languages it can pose in one of two incarnations &#8212; either as visibility from subtypes or as visibility in the same package (the latter is just like the <code>default</code> visibility). This can sometime lead to some confusion, but I have a specific case of interest to offer you.</p>
<p>In the previous post there already was one example involving <code>protected</code>:</p>
<div class="igBar"><span id="ljava-30"><a href="#" onclick="javascript:showPlainTxt('java-30'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-30">
<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> <span style="color: #808080; font-style: italic;">/*p1.*/</span>Main <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; 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-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>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-family: 'Courier 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>
<li style="font-family: 'Courier 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> <span style="color: #808080; font-style: italic;">/*p1.*/</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> <span style="color: #808080; font-style: italic;">/*p1.*/</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> <span style="color: #808080; font-style: italic;">/*p2.*/</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>The relevant part in this example is that <code>protected</code> method is invoked via <code>default</code> visibility, but overridden via subtyping visibility. In case you don&#8217;t remember anymore the output is &#8220;3&#8243;. This shows that visibilities and overriding can be mixed pretty much freely. It is also a good example as it demonstrates why the original phrasing in the JVM spec was wrong. In the original phrasing for the method <code>C3.m</code> to override a method <code>C1.m</code> the latter had to be accessible from the former. In our example this is obviously not the case.</p>
<p>The corrected phrasing, available in the <a href="http://java.sun.com/docs/books/jvms/second_edition/jvms-clarify.html">addendum</a>, refers to the <a href="http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.8">JLS notion of overriding</a>, which adds transitivity. That is if <code>C3.m</code> overrides <code>C2.m</code> and <code>C2.m</code> overrides <code>C1.m</code> then <code>C3.m</code> overrides <code>C1.m</code>.</p>
<p>But getting back to the topic &#8212; when playing around with <a href="http://www.zeroturnaround.com/javarebel/">JavaRebel</a> I stumbled on an even more interesting example where this dual nature of <code>protected</code> makes it act unexpectedly. The setup starts like this:</p>
<div class="igBar"><span id="ljava-31"><a href="#" onclick="javascript:showPlainTxt('java-31'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-31">
<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> <span style="color: #808080; font-style: italic;">/*p1.*/</span>Main <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; p2.<span style="color: #006600;">C2</span>&nbsp; c = <span style="color: #000000; font-weight: bold;">new</span> p2.<span style="color: #006600;">C2</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; <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-family: 'Courier 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>
<li style="font-family: 'Courier 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> <span style="color: #808080; font-style: italic;">/*p1.*/</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>&nbsp; <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> <span style="color: #808080; font-style: italic;">/*p2.*/</span>C2 <span style="color: #000000; font-weight: bold;">extends</span> p1.<span style="color: #006600;">C1</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;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This compiles and behaves as expected outputting &#8220;1&#8243;. A question I&#8217;d like to ask is: what happens if for some reason later you override the method <code>m()</code> in <code>C2</code>?</p>
<div class="igBar"><span id="ljava-32"><a href="#" onclick="javascript:showPlainTxt('java-32'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-32">
<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> <span style="color: #808080; font-style: italic;">/*p1.*/</span>Main <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; p2.<span style="color: #006600;">C2</span>&nbsp; c = <span style="color: #000000; font-weight: bold;">new</span> p2.<span style="color: #006600;">C2</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; <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-family: 'Courier 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>
<li style="font-family: 'Courier 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> <span style="color: #808080; font-style: italic;">/*p1.*/</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>&nbsp; <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> <span style="color: #808080; font-style: italic;">/*p2.*/</span>C2 <span style="color: #000000; font-weight: bold;">extends</span> p1.<span style="color: #006600;">C1</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;">protected</span>&nbsp; <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>
</ol>
</div>
</div>
</div>
<p></p>
<p>Stop here for a second, take a look at it and hazard a guess&#8230;</p>
<p><center><img src="http://dow.ngra.de/wp-content/uploads/2009/03/roulette-300x207.jpg" alt="roulette" title="roulette" width="300" height="207" class="alignnone size-medium wp-image-749" /></center></p>
<p>The example seems similar to the previous one, so you&#8217;d expect that the output is &#8220;2&#8243;. But is it?</p>
<p>Let&#8217;s evaluate this from the point of compiler and JVM. Initially the call to <code>C2.m</code> from <code>Main</code> resolved to <code>C1.m()</code>, which is in the same package with <code>Main</code>. When we override the method this is no longer the case and actually the method isn&#8217;t accessible anymore. So the correct answer is that the code won&#8217;t compile.</p>
<p>This is strongly counterintuitive to me, mainly because I always assumed that overriding a method should never affect its caller in such a way. This also isn&#8217;t limited to a compile-time error, as it&#8217;s quite possible to compile the <code>C2</code> without recompiling the rest of the classes and get an <code>IllegalAccessError</code> at runtime. So in my book this reads quite unnatural.</p>
<p>There are definitely more creative ways to f**k up with <code>protected</code> you can come up with, by moving from the &#8220;default&#8221; incarnation to the subtype one. If you know a good one do post it in the comments.</p>
<p>P.S. I&#8217;d love to see what weirdness would come if they&#8217;d actually introduce the <code>module</code> keyword as well. Luckily it&#8217;d be limited mostly to compile time, but as you saw even there things can get tricky.</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2009/03/13/the-mysteries-of-protected/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is the Relational Database Not an Option in Cloud Computing?</title>
		<link>http://dow.ngra.de/2009/02/20/is-the-relational-database-not-an-option-in-cloud-computing/</link>
		<comments>http://dow.ngra.de/2009/02/20/is-the-relational-database-not-an-option-in-cloud-computing/#comments</comments>
		<pubDate>Fri, 20 Feb 2009 19:16:35 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[creative]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=729</guid>
		<description><![CDATA[InfoQ: Though scalability is a key factor, cloud computing has other advantages that makes it attractive for vendors that do not need to deliver highly scalable applications or services. &#8212; you know when something is way overhyped, when it starts to be presented as a solution to all of your problems, even the ones it [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.infoq.com/news/2009/02/Relational-DB-Cloud">InfoQ:</a> <em>Though scalability is a key factor, cloud computing has other advantages that makes it attractive for vendors that do not need to deliver highly scalable applications or services.</em> &#8212; you know when something is way overhyped, when it starts to be presented as a solution to all of your problems, even the ones it was never intended for. Also the keywords like &#8220;doomed&#8221; give a strong hint&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2009/02/20/is-the-relational-database-not-an-option-in-cloud-computing/feed/</wfw:commentRss>
		<slash:comments>0</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-35"><a href="#" onclick="javascript:showPlainTxt('java-35'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-35">
<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-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;">@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>The Law of Meetings</title>
		<link>http://dow.ngra.de/2009/01/26/the-law-of-meetings/</link>
		<comments>http://dow.ngra.de/2009/01/26/the-law-of-meetings/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 20:23:21 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[creative]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=617</guid>
		<description><![CDATA[To be henceforth known as Kabanov&#8217;s Law of Meetings: one meeting will turn into two meetings, two meetings will turn into three and a nonsense break and thus they shall multiply until no real work can be done on that day :( 
Corollary: If you want to do some real work keep some days free [...]]]></description>
			<content:encoded><![CDATA[<p>To be henceforth known as <strong>Kabanov&#8217;s Law of Meetings:</strong> one meeting will turn into two meetings, two meetings will turn into three and a nonsense break and thus they shall multiply until no real work can be done on that day :( </p>
<p><strong>Corollary:</strong> If you want to do some real work keep some days free of meetings altogether.</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2009/01/26/the-law-of-meetings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A strike of creativity:</title>
		<link>http://dow.ngra.de/2008/12/29/a-strike-of-creativity/</link>
		<comments>http://dow.ngra.de/2008/12/29/a-strike-of-creativity/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 13:21:39 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[creative]]></category>
		<category><![CDATA[javarebel]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=578</guid>
		<description><![CDATA[I knew once a very nice tool
that made Java programmers drool:
it reloaded their classes
without any passes
and was all around very cool :)
]]></description>
			<content:encoded><![CDATA[<p>I knew once a very nice tool<br />
that made Java programmers drool:<br />
it reloaded their classes<br />
without any passes<br />
and was all around very cool :)</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2008/12/29/a-strike-of-creativity/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Can iPhone help stop the information overload?</title>
		<link>http://dow.ngra.de/2008/12/18/can-iphone-help-stop-the-information-overload/</link>
		<comments>http://dow.ngra.de/2008/12/18/can-iphone-help-stop-the-information-overload/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 08:58:04 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[creative]]></category>
		<category><![CDATA[iphone]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=492</guid>
		<description><![CDATA[iPhone is a tricky device. It was the first mobile device I ever saw that solved the problem of web browsing on a tiny screen in a usable and enjoyable way. That was the main (and basically only) selling point for me when I bought it. 
However the more I used it the more I [...]]]></description>
			<content:encoded><![CDATA[<p><img alt="" src="http://images.apple.com/iphone/features/images/main_overview20081217.jpg" title="iPhone" class="alignleft" width="139" height="156" style="margin-right: 20px"/>iPhone is a tricky device. It was the first mobile device I ever saw that solved the problem of web browsing on a tiny screen in a usable and enjoyable way. That was the main (and basically only) selling point for me when I bought it. </p>
<p>However the more I used it the more I understood one important thing &#8212; iPhone is essentially a reading device. It&#8217;s great to get and read e-mails on it, but it&#8217;s a pain to answer them. Same goes to everything, the on-screen keyboard just doesn&#8217;t cut it for anything bigger than an SMS or Twitter message. Most business users see this as a limitation and choose BlackBerry with it&#8217;s real keyboard. But after some time I understood that for me it&#8217;s a blessing. </p>
<p><img src="http://dow.ngra.de/wp-content/uploads/2008/12/information_overload_226.jpg" alt="information_overload_226" title="information_overload_226" width="226" height="127" class="alignright size-full wp-image-562" style="margin-left: 20px"/>The typical problem of an engineer (and many business users to boot) is information overload. I get e-mails every couple of minutes, RSS and Twitter messages even more often and am usually compelled at least to skim through them. This kind of constant division of attention is very inefficient, but I never could get myself to break this pattern.</p>
<p><img src="http://dow.ngra.de/wp-content/uploads/2008/12/pull30-200x300.jpg" alt="pull30" title="pull30" width="200" height="300" class="alignleft size-medium wp-image-564" style="margin-right: 20px"/>With the iPhone everything changed. I set the e-mail checking to Pull/Hourly. Now at most every hour the phone will buzz and I will scroll through the e-mails and see if I need to answer anything. If I do I&#8217;ll fire up Outlook and go at it. If I like any RSS links, I will read them right there, on my iPhone, protected from the temptation to follow links ad infinitum. And I might answer a couple Twitter messages while I&#8217;m at it. That&#8217;s it, I&#8217;m done in 5 minutes, iPhone is back in my pocket and I go right back to work.</p>
<p>I never measured how much time and energy I spent fighting the information inflow before, but it sure as hell took more than 5 minutes every hour. Plus the constant multitasking took it&#8217;s tall from the quality of my efforts. Of course it wasn&#8217;t really just iPhone that solved my problem, but its limitations made it way easier to keep myself in check, and perhaps the same will work for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2008/12/18/can-iphone-help-stop-the-information-overload/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>IntelliJ build numbers vs version number</title>
		<link>http://dow.ngra.de/2008/12/10/intellij-build-numbers-vs-version-number/</link>
		<comments>http://dow.ngra.de/2008/12/10/intellij-build-numbers-vs-version-number/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 12:06:59 +0000</pubDate>
		<dc:creator>Toomas Römer</dc:creator>
				<category><![CDATA[creative]]></category>
		<category><![CDATA[humour]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=542</guid>
		<description><![CDATA[Visited the JetBrains booth at Devoxx and confirmed why their build numbers are 9xxx but the version of the product is 8.x. They ran out of build numbers :) This means there were 1000 numbered builds before a release.
]]></description>
			<content:encoded><![CDATA[<p>Visited the <a href="http://www.jetbrains.com/">JetBrains</a> booth at <a href="http://www.devoxx.com">Devoxx</a> and confirmed why their build numbers are 9xxx but the version of the product is 8.x. They ran out of build numbers :) This means there were 1000 numbered builds before a release.</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2008/12/10/intellij-build-numbers-vs-version-number/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Random observations from Devoxx 2008</title>
		<link>http://dow.ngra.de/2008/12/10/random-observations-from-devoxx-2008/</link>
		<comments>http://dow.ngra.de/2008/12/10/random-observations-from-devoxx-2008/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 08:42:35 +0000</pubDate>
		<dc:creator>Toomas Römer</dc:creator>
				<category><![CDATA[creative]]></category>
		<category><![CDATA[humour]]></category>
		<category><![CDATA[report]]></category>
		<category><![CDATA[devoxx]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=507</guid>
		<description><![CDATA[Pictorial report from Devoxx 2008. Does Bruce Willis work for IBM? Which app server is evil? How popular has Spring App Platform become?]]></description>
			<content:encoded><![CDATA[<p><em><a href="http://www.devoxx.com">Devoxx</a> is an annual European Java conference with a total of 3200 attendees from 35 countries and again has been completely sold out. The place is packed. Lots of people, lots of movement and too little internet.</em></p>
<p>Second day at Devoxx and it is going well. I&#8217;ve attended three sessions so far, it is not so much me being lazy but more about being at the <a href="http://www.zeroturnaround.com">JavaRebel</a> booth most of the time. Still I&#8217;ve seen many cool things here. Random ramblings will follow.</p>
<p><img src="http://dow.ngra.de/wp-content/uploads/2008/12/wearethesheep-237x300.jpg" alt="Monitoring tools" title="Monitoring tools" width="237" height="300" class="alignleft size-medium wp-image-512"  style="margin-right:5px"/>The conference passes have embedded RFID chip and every movement (hopefully not) is being tracked. From the local paper I read that they will measure session popularity by how many attended the talks, how many left early and so on.</p>
<p>Don&#8217;t know if they&#8217;ll realease any of it to public (probably not) but it will be great input at least for the next devoxx. The information is said to be anonymous, although I would like to see if and which sessions did the speakers themselves visit.</p>
<p>I presume we are being monitored by these devices that are present on any entrance of the session halls.</p>
<p><img src="http://dow.ngra.de/wp-content/uploads/2008/12/coolscreens1-300x225.jpg" alt="Cool Screens" title="Cool Screens" width="300" height="225" class="alignleft size-medium wp-image-515" style="margin-right:5px" />The venue is just excellent, having the cinema as the location means that the seating at the sessions is excellent. You have even cup holders and room for popcorn:). The screens are huuuuge and they have added live video of the speaker to the screen.<br />
<br/></p>
<p><strong>Which application server is the most popular?</strong></p>
<p><img src="http://dow.ngra.de/wp-content/uploads/2008/12/whichappserverisevil-300x207.jpg" alt="Which app server is evil?" title="Which app server is evil?" width="300" height="207" class="alignleft size-medium wp-image-510" style="margin-right:5px"/>Besides <strong>Websphere</strong> being <strong>evil</strong> we see that <strong>Spring App Platform</strong> has now managed to get two users! Depending on the interpretation of the study I would say Tomcat is in the lead!</p>
<p><img src="http://dow.ngra.de/wp-content/uploads/2008/12/tables1-300x209.jpg" alt="Tables to chill at" title="Tables to chill at" width="300" height="209" class="alignleft size-medium wp-image-513" style="margin-right:5px" />Something that I really like compared to all the other conferences I&#8217;ve attended this year are the tables with the power cords. You don&#8217;t have to worry about your laptop&#8217;s battery dying.</p>
<p>But! when compared to the other conferences where your battery actually died you were able to use the internet. Write emails, read news, hack on projects and all the other stuff until you ran out of juice. Here at certain times you have internet, most of the time you don&#8217;t. Even if you have it, some domains don&#8217;t resolve on certain occasions and it is dead slow if it works.</p>
<p><strong>Is Bruce Willis working for IBM?</strong> Sure, he was in a movie <a href="http://www.imdb.com/title/tt0314353/">Tears of the Sun</a>.</p>
<p>
<a href='http://dow.ngra.de/2008/12/10/random-observations-from-devoxx-2008/whichspeakers/' title='Which speakers would be cool to listent to?'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/12/whichspeakers-150x150.jpg" class="attachment-thumbnail" alt="" title="Which speakers would be cool to listent to?" /></a>
<a href='http://dow.ngra.de/2008/12/10/random-observations-from-devoxx-2008/whichappserverisevil/' title='Which app server is evil?'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/12/whichappserverisevil-150x150.jpg" class="attachment-thumbnail" alt="" title="Which app server is evil?" /></a>
<a href='http://dow.ngra.de/2008/12/10/random-observations-from-devoxx-2008/wearethesheep2/' title='Monitoring tools'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/12/wearethesheep2-150x150.jpg" class="attachment-thumbnail" alt="" title="Monitoring tools" /></a>
<a href='http://dow.ngra.de/2008/12/10/random-observations-from-devoxx-2008/wearethesheep/' title='Monitoring tools'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/12/wearethesheep-150x150.jpg" class="attachment-thumbnail" alt="" title="Monitoring tools" /></a>
<a href='http://dow.ngra.de/2008/12/10/random-observations-from-devoxx-2008/tables1/' title='Tables to chill at'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/12/tables1-150x150.jpg" class="attachment-thumbnail" alt="" title="Tables to chill at" /></a>
<a href='http://dow.ngra.de/2008/12/10/random-observations-from-devoxx-2008/coolscreens1/' title='Cool Screens'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/12/coolscreens1-150x150.jpg" class="attachment-thumbnail" alt="" title="Cool Screens" /></a>
<a href='http://dow.ngra.de/2008/12/10/random-observations-from-devoxx-2008/bruceisworkingforibm1/' title='Bruce Is Working for IBM'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/12/bruceisworkingforibm1-150x150.jpg" class="attachment-thumbnail" alt="" title="Bruce Is Working for IBM" /></a>
</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2008/12/10/random-observations-from-devoxx-2008/feed/</wfw:commentRss>
		<slash:comments>0</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! -->