<?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>Ahti Kitsik / AhtiK &#187; Java</title>
	<atom:link href="http://ahtik.com/blog/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://ahtik.com/blog</link>
	<description>blog</description>
	<lastBuildDate>Wed, 12 Oct 2011 23:10:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>New Eclipse Word Wrap plugin adds keyboard shortcut</title>
		<link>http://ahtik.com/blog/2011/03/08/new-eclipse-word-wrap-plugin-adds-keyboard-shortcut/</link>
		<comments>http://ahtik.com/blog/2011/03/08/new-eclipse-word-wrap-plugin-adds-keyboard-shortcut/#comments</comments>
		<pubDate>Tue, 08 Mar 2011 11:37:58 +0000</pubDate>
		<dc:creator>Ahti Kitsik</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://ahtik.com/blog/?p=371</guid>
		<description><![CDATA[Had a few spare minutes yesterday to move the eclipse word wrap plugin from sourceforge to github. While on it also added keyboard shortcut so you can switch on-off the wrapping using Ctrl+Alt+W (M1,M3+W in &#8220;Eclipse Language&#8221;). Trust me, it&#8217;s a useful plugin : ) Latest version can be installed from the update site: http://ahtik.com/eclipse-update/ [...]]]></description>
			<content:encoded><![CDATA[<p><b>Had a few spare minutes yesterday to move the <a href="https://github.com/ahtik/eclipse-wordwrap">eclipse word wrap plugin from sourceforge to github</a>.</b></p>
<p>While on it also added keyboard shortcut so you can switch on-off the wrapping using Ctrl+Alt+W (M1,M3+W in &#8220;Eclipse Language&#8221;).</p>
<p><img src="http://ahtik.com/img/eclipse-word-wrap.png" alt="Eclipse Word Wrap"/></p>
<p>Trust me, it&#8217;s a useful plugin : )<br/><br />
Latest version can be installed from the update site: <a href="http://ahtik.com/eclipse-update/">http://ahtik.com/eclipse-update/</a></p>
<p><a href="http://ahtik.com/blog/projects/eclipse-word-wrap/">Word Wrap project site</a></p>
<p>Thanks to the shortcut I&#8217;m not even waiting anymore to have it fixed in the very deep core of the Eclipse platform as the changes would be massive and simply switching it off before launching a debugger is good enough for now.</p>
]]></content:encoded>
			<wfw:commentRss>http://ahtik.com/blog/2011/03/08/new-eclipse-word-wrap-plugin-adds-keyboard-shortcut/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>BPMN2.0 Editor for Eclipse now available</title>
		<link>http://ahtik.com/blog/2011/03/07/bpmn2-0-editor-for-eclipse-now-available/</link>
		<comments>http://ahtik.com/blog/2011/03/07/bpmn2-0-editor-for-eclipse-now-available/#comments</comments>
		<pubDate>Mon, 07 Mar 2011 13:24:04 +0000</pubDate>
		<dc:creator>Ahti Kitsik</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://ahtik.com/blog/?p=351</guid>
		<description><![CDATA[Soon to be released jBPM5.1 is getting a new addition to its product suite &#8211; visual editor for the BPMN2 language. This was somewhat inevitable as the jBPM workflow engine moves to BPMN2.0 with the version 5 and hacking together xml files without visual guidance can be.. hmm.. less fun. For a quick background, BPMN [...]]]></description>
			<content:encoded><![CDATA[<p><b>Soon to  be released jBPM5.1 is getting a new addition to its product suite &#8211; visual editor for the BPMN2 language. This was somewhat inevitable as the jBPM workflow engine moves to BPMN2.0 with the version 5 and hacking together xml files without visual guidance can be.. hmm.. less fun.</b></p>
<p>For a quick background, <a href="http://www.bpmn.org/">BPMN (Business Process Modeling Notation)</a> is the leading standard for business process modeling managed by the <a href="http://www.omg.org/">OMG</a>. A new version called <a href="http://www.omg.org/spec/BPMN/2.0/">BPMN2.0</a> (released January 2011) brings numerous changes to the table, most importantly increasing the consistency and integrating orchestration and choreography in a way that makes BPMN 2.0 a great choice for business process engines.</p>
<p>Over the past few months <a href="http://codehoop.com">we</a>&#8216;ve been very excited to work on a new <a href="https://github.com/imeikas/BPMN2-Editor-for-Eclipse/wiki">BPMN2.0 Visual Editor for Eclipse</a>. It is free and open source, <a href="https://github.com/imeikas/BPMN2-Editor-for-Eclipse">hosted at github</a>. This github repo is a temporary place and will find a new home soon.</p>
<p>BPMN2 Editor is built on top of the awesome <a href="http://www.eclipse.org/graphiti/">Graphiti modeling framework</a> and behind the scenes uses <a href="http://wiki.eclipse.org/MDT-BPMN2">BPMN2 EMF metamodel</a>.</p>
<p>For more details check out the <a href="http://kverlaen.blogspot.com/2011/02/new-bpmn-20-eclipse-editor.html">more detailed post about the BPMN2.0 visual editor</a> by Kris from jBPM.</p>
<p>Check it out, have fun, contribute, report issues and bear in mind that it&#8217;s still beta and actively developed ; )</p>
]]></content:encoded>
			<wfw:commentRss>http://ahtik.com/blog/2011/03/07/bpmn2-0-editor-for-eclipse-now-available/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fighting with the SSL &#8220;unexpected_message&#8221; while using HTTP proxy and your own socket tunnel</title>
		<link>http://ahtik.com/blog/2011/02/28/fighting-with-the-ssl-unexpected_message-while-using-http-proxy-and-your-own-socket-tunnel/</link>
		<comments>http://ahtik.com/blog/2011/02/28/fighting-with-the-ssl-unexpected_message-while-using-http-proxy-and-your-own-socket-tunnel/#comments</comments>
		<pubDate>Mon, 28 Feb 2011 15:13:52 +0000</pubDate>
		<dc:creator>Ahti Kitsik</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://ahtik.com/blog/?p=340</guid>
		<description><![CDATA[Whenever you write an application that should support http proxies with your own custom SSLSocketFactory, you MIGHT run into a problem&#8230; Java JRE in itself supports proxies for HTTPS but it does NOT support SSL connections over proxy. For that you&#8217;ll need to create a separate proxy tunneling socket and layer it on top of [...]]]></description>
			<content:encoded><![CDATA[<p><b>Whenever you write an application that should support http proxies with your own custom SSLSocketFactory, you MIGHT run into a problem&#8230;</b></p>
<p>Java JRE in itself supports proxies for HTTPS but it does <strong>NOT</strong> support SSL connections over proxy. For that you&#8217;ll need to create a separate proxy tunneling socket and layer it on top of the actual SSL connection. So far so good but there&#8217;s a nasty exception that can take you ages to figure out:
</p>
<p><pre name="code" class="java">
Caused by: javax.net.ssl.SSLException: Received fatal alert: unexpected_message
	at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:190)
	at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:136)
	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1682)
	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:932)
	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1112)
	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1139)
	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1123)
</pre>
</p>
<p>There can be many reasons but one of the unexpected ones &#8212; make sure to add<br/><br />
Pragma: no-cache<br/><br />
to your http proxy CONNECT request header! Otherwise you&#8217;ll get this exception.</p>
]]></content:encoded>
			<wfw:commentRss>http://ahtik.com/blog/2011/02/28/fighting-with-the-ssl-unexpected_message-while-using-http-proxy-and-your-own-socket-tunnel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Glassfish Eclipse Bundle for JavaDB, JPA and JSP</title>
		<link>http://ahtik.com/blog/2009/04/09/using-glassfish-eclipse-bundle-for-javadb-jpa-and-jsp/</link>
		<comments>http://ahtik.com/blog/2009/04/09/using-glassfish-eclipse-bundle-for-javadb-jpa-and-jsp/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 13:49:59 +0000</pubDate>
		<dc:creator>Ahti Kitsik</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://ahtik.com/blog/?p=139</guid>
		<description><![CDATA[Have you ever wondered how quickly one could get from installing a J2EE server to running a JSP page that fetches data from DB using modern persistency technology like JPA? To find out we (me and Ivar) did a little test-drive using recently announced Glassfish Eclipse Bundle that contains Eclipse IDE with bundled Glassfish J2EE [...]]]></description>
			<content:encoded><![CDATA[<p><b>Have you ever wondered how quickly one could get from installing a J2EE server to running a JSP page that fetches data from DB using modern persistency technology like JPA?</b></p>
<p>To find out we (me and <a href="http://meikas.com">Ivar</a>) did a little test-drive using recently announced <a href="http://download.java.net/glassfish/eclipse/">Glassfish Eclipse Bundle</a> that contains Eclipse IDE with bundled Glassfish J2EE server, optionally JDK and a lot of integrated plugins to get you started quickly.</p>
<p>After cutting out all the downloading, startup etc delays we ended up with a surprisingly short 10min demonstration! It was interesting that we barely wrote any code or XML &#8212; see for yourself! Tricky part was initial setup to get all the jars and configurations right &#8212; must be followed pretty much the same sequence as in the video!</p>
<p>Ok, here it comes, have fun <img src='http://ahtik.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Probably adding a few annotations and audio would help?</p>
<p>&nbsp;</p>
<p><center><br />
<object width="640" height="480"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=4075401&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=4075401&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="640" height="480"></embed></object><br />
</center></p>
<p>&nbsp;</p>
<p><b>Any feedback is more than welcome!</b></p>
]]></content:encoded>
			<wfw:commentRss>http://ahtik.com/blog/2009/04/09/using-glassfish-eclipse-bundle-for-javadb-jpa-and-jsp/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Can you guess the output? junit and initialization</title>
		<link>http://ahtik.com/blog/2008/12/15/can-you-guess-the-output-junit-and-initialization/</link>
		<comments>http://ahtik.com/blog/2008/12/15/can-you-guess-the-output-junit-and-initialization/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 17:04:38 +0000</pubDate>
		<dc:creator>Ahti Kitsik</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://ahtik.com/blog/2008/12/15/can-you-guess-the-output-junit-and-initialization/</guid>
		<description><![CDATA[Can you guess the output without running the code? The relation to Eclipse is simply the fact that big part of eclipse.org is a great example of good test coverage built on top of jUnit. Plus, it was literally pulling my hair out in one of the eclipse-related testing-suites. I stepped into this a few [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Can you guess the output without running the code?</strong></p>
<p>The relation to Eclipse is simply the fact that big part of eclipse.org is a great example of good test coverage built on top of jUnit. Plus, it was literally pulling my hair out in one of the eclipse-related testing-suites.</p>
<p>I stepped into this a few years ago while bug-fighting a test class that had unexpected initialization.</p>
<p>Pretty sure that many of you know the answer but definitely fun outcome!</p>
<p>As everyone can simply run this snippet yourself I won&#8217;t delay approving comments, I&#8217;ll just accept them whenever I get a free moment. This comment system here has captcha but additionally all comments must be approved manually.</p>
<pre name="code" class="java">
import junit.framework.TestCase;
public class MyTest extends TestCase {
private static int count = 0;

{ count++; }

public MyTest() {count++;}

public void test1() { System.out.print(count); }
public void test2() { System.out.print(count); }
public void test3() { System.out.print(count); }

}
</pre>
<p>I&#8217;m sure some of us don&#8217;t always take this behavior into account <img src='http://ahtik.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>After figuring this out, SPECIAL fun is a bit modified case:</p>
<pre name="code" class="java">
import junit.framework.TestCase;
public class MyTest extends TestCase {
private static int count = 0;

{ count=count*2; }

public MyTest() { count++; }

public void test1() { System.out.print(count); }
public void test2() { System.out.print(count); }
public void test3() { System.out.print(count); }
}
</pre>
<p>For this last snippet I think without running it you won&#8217;t figure it out <img src='http://ahtik.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  At least I didn&#8217;t..</p>
<p>UPDATE: Decent in-depth hi-tech doc about java init <a href="http://www.artima.com/designtechniques/initialization7.html">http://www.artima.com/designtechniques/initialization7.html</a></p>
<p>UPDATE2: Correct answers were 666 and 777.</p>
]]></content:encoded>
			<wfw:commentRss>http://ahtik.com/blog/2008/12/15/can-you-guess-the-output-junit-and-initialization/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Java Quiz of The Day &#8211; same private field instance for two classes</title>
		<link>http://ahtik.com/blog/2008/12/01/java-quiz-of-the-day-same-private-field-instance-for-two-classes/</link>
		<comments>http://ahtik.com/blog/2008/12/01/java-quiz-of-the-day-same-private-field-instance-for-two-classes/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 10:14:28 +0000</pubDate>
		<dc:creator>Ahti Kitsik</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://ahtik.com/blog/2008/12/01/java-quiz-of-the-day-same-private-field-instance-for-two-classes/</guid>
		<description><![CDATA[What is the smallest change to main method that makes it sysout &#8220;true&#8221;? You are allowed to change ONLY the main method! Of course changing sysout line is out of question. You can leave your answer in comments and I&#8217;ll publish/approve them together with the solution. [java] public class InstanceDemo { public static void main(String[] [...]]]></description>
			<content:encoded><![CDATA[<p><strong>What is the smallest change to <i>main</i> method that makes it sysout &#8220;true&#8221;?</strong></p>
<p><strong>You are allowed to change ONLY the main method! Of course changing sysout line is out of question.</strong></p>
<p>You can leave your answer in comments and I&#8217;ll publish/approve them together with the solution.</p>
<p>[java]<br />
public class InstanceDemo {</p>
<p>	public static void main(String[] args){<br />
		MyClass cl1 = new MyClass();<br />
		MyClass cl2 = new MyClass();<br />
		System.out.println(cl1.ocl==cl2.ocl &#038;&#038; cl1!=cl2);<br />
	}</p>
<p>	private static class MyClass extends java.util.ArrayList {<br />
		private final Object ocl = new Object();<br />
	}</p>
<p>}<br />
[/java]</p>
<p>I&#8217;ll post the correct answer in 24h <img src='http://ahtik.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>UPDATE:</strong> Was not that hard afterall! <img src='http://ahtik.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ~45 correct answers.</p>
<p>Yes, clone() was the answer I was looking for:<br />
[java]MyClass cl2 = (MyClass) cl1.clone();[/java]</p>
<p><a href="http://ed-merks.blogspot.com/">Ed Merks</a> surprised with a nice solution:<br />
[java]<br />
//Add this line as the first line of main.<br />
class MyClass extends InstanceDemo.MyClass {<br />
Object ocl = null;<br />
}<br />
[/java]</p>
<p>And a lot of people took the hard-core way:<br />
[java]<br />
Field field = cl1.getClass().getDeclaredField(&#8220;ocl&#8221;);<br />
field.setAccessible(true);<br />
field.set(cl2,cl1.ocl);<br />
[/java] <img src='http://ahtik.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://ahtik.com/blog/2008/12/01/java-quiz-of-the-day-same-private-field-instance-for-two-classes/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
		<item>
		<title>SimpleDateFormat is not thread-safe</title>
		<link>http://ahtik.com/blog/2008/11/07/simpledateformat-is-not-thread-safe/</link>
		<comments>http://ahtik.com/blog/2008/11/07/simpledateformat-is-not-thread-safe/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 10:35:20 +0000</pubDate>
		<dc:creator>Ahti Kitsik</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://ahtik.com/blog/2008/11/07/simpledateformat-is-not-thread-safe/</guid>
		<description><![CDATA[How many of you use java.text.SimpleDateFormat as static field? I have seen this to be pretty standard practice (and have done this myself too). Be aware that SimpleDateFormat#format(..) is not thread-safe and thereby for most of the cases you should not use this as a static field in server, Eclipse RPC or any other multi-threaded [...]]]></description>
			<content:encoded><![CDATA[<p><b>How many of you use java.text.SimpleDateFormat as static field?</b></p>
<p>I have seen this to be pretty standard practice (and have done this myself too).</p>
<p>Be aware that SimpleDateFormat#format(..) <b>is not thread-safe</b> and thereby for most of the cases you should not use this as a static field in server, Eclipse RPC or any other multi-threaded environment.</p>
<p>SimpleDateFormat is internally using field &#8220;calendar&#8221; that is set with each #format(&#8230;) method call so that pretty much breaks the thread-safety.</p>
]]></content:encoded>
			<wfw:commentRss>http://ahtik.com/blog/2008/11/07/simpledateformat-is-not-thread-safe/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Getting unsigned bytes in java</title>
		<link>http://ahtik.com/blog/2008/08/15/getting-unsigned-bytes-in-java/</link>
		<comments>http://ahtik.com/blog/2008/08/15/getting-unsigned-bytes-in-java/#comments</comments>
		<pubDate>Thu, 14 Aug 2008 22:15:54 +0000</pubDate>
		<dc:creator>Ahti Kitsik</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://ahtik.com/blog/2008/08/15/getting-unsigned-bytes-in-java/</guid>
		<description><![CDATA[Welcome back after long overwork and vacation period Let&#8217;s start small, it&#8217;s still almost summer. Bytes in java are always signed and are quite horrible to use because of the casting and weird side-effects (or I&#8217;m just stupid enough to call it weird). But we need unsigned bytes quite a lot. Most of the time [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome back after long overwork and vacation period <img src='http://ahtik.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Let&#8217;s start small, it&#8217;s still almost summer.</p>
<p>Bytes in java are always signed and are quite horrible to use because of the casting and weird side-effects (or I&#8217;m just stupid enough to call it weird).</p>
<p>But we need unsigned bytes quite a lot. Most of the time for reading various streams (networks, 8bit strings etc) some text stream you still end up with byte streams.</p>
<p>There is a nice trick converting them from signed to unsigned in case you want to get normal 0..255 range for the chars.</p>
<blockquote><p>
byte mybyte = -104;<br />
long mynewint = mybyte &#038; 0xFF;
</p></blockquote>
<p>Hope it helps!</p>
<p>I had quite a nice puzzling while trying to find some easier way. Is there?</p>
]]></content:encoded>
			<wfw:commentRss>http://ahtik.com/blog/2008/08/15/getting-unsigned-bytes-in-java/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Avoid useless Plugin Initialization and/or Class Loading</title>
		<link>http://ahtik.com/blog/2007/12/19/avoiding-useless-plugin-initialization-andor-class-loading/</link>
		<comments>http://ahtik.com/blog/2007/12/19/avoiding-useless-plugin-initialization-andor-class-loading/#comments</comments>
		<pubDate>Wed, 19 Dec 2007 12:08:49 +0000</pubDate>
		<dc:creator>Ahti Kitsik</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://ahtik.com/blog/2007/12/19/avoiding-useless-plugin-initialization-andor-class-loading/</guid>
		<description><![CDATA[Here&#8217;s a tip for initializing your plugins or any other singleton-like repository only when needed. Consider a code like this: if (isConfigurable()) { Bundle bundleCopy = bundle; Preferences[] preferencesCopy = new Preferences[1]; preferencesCopy[0] = new org.eclipse.core.internal. preferences.legacy.PreferenceForwarder( this, bundleCopy.getSymbolicName()); return preferencesCopy; } return null; Do you see what&#8217;s wrong there? The problem is that if [...]]]></description>
			<content:encoded><![CDATA[<p><b>Here&#8217;s a tip for initializing your plugins or any other singleton-like repository only when needed.</b></p>
<p>Consider a code like this:</p>
<pre style="background:#F6F6F6">
if (isConfigurable()) {
  Bundle bundleCopy = bundle;
  Preferences[] preferencesCopy = new Preferences[1];
  preferencesCopy[0] = new org.eclipse.core.internal.
			preferences.legacy.PreferenceForwarder(
			this, bundleCopy.getSymbolicName());
  return preferencesCopy;
}
return null;
</pre>
</p>
<p><b>Do you see what&#8217;s wrong there?</b></p>
<p><b>The problem is that if your #isConfigurable is always FALSE then your code still:</b></p>
<ol>
<li>loads PreferencesForwarder class
<li>initializes all PreferencesForwarder&#8217;s static variables
<li>forces Preferences plugin to load
</ol>
</p>
<p>Always, if possible and makes sense, isolate your class loading into a simple Runnable inner class if there is some static init or plugin activation trigger involved!</p>
<p>Then the code would become:</p>
<pre style="background:#F6F6F6">
if (isConfigurable()) {
  final Bundle bundleCopy = bundle;
  final Preferences[] preferencesCopy = new Preferences[1];
  Runnable innerCall = new Runnable() {
    public void run() {
      preferencesCopy[0] = new org.eclipse.core.internal.
			       preferences.legacy.PreferenceForwarder(
				 this, bundleCopy.getSymbolicName());
    }
  };
  innerCall.run();
}
return null;
</pre>
</p>
<p>When you think of it &#8211; this pattern is somewhat similar to the singleton pattern implementation &#8211; the one of the very few thread-safe and fast singleton patterns that is bugfree and truly lazy!</p>
<p>Quoting <a href="http://en.wikipedia.org/wiki/Singleton_pattern#Java">wonderful singleton impl from wikipedia</a>:</p>
<pre style="background:#F6F6F6">
public class Singleton {
   private Singleton() {}
   private static class SingletonHolder {
     private final static Singleton INSTANCE = new Singleton();
   }
   public static Singleton getInstance() {
     return SingletonHolder.INSTANCE;
   }
 }
</pre>
<p>(OK, to be fully honest, since java1.5 you can also trust volatile flags but &#8211; trust noone, use the old stuff:P)
</p>
<p>For the reference, the PreferencesForwarder example was taken from the org.eclipse.core.runtime.Plugin#getPluginPreferences class and modified a bit.</p>
<p><b>UPDATE: This post is not fully correct! Never assume something to be true even if it comes from a trustworthy source like eclipse core runtime source. Please see the comments for explanation!</b></p>
]]></content:encoded>
			<wfw:commentRss>http://ahtik.com/blog/2007/12/19/avoiding-useless-plugin-initialization-andor-class-loading/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Reason to serialVersionUID your serializable classes</title>
		<link>http://ahtik.com/blog/2006/08/23/reason-to-serialversionuid-your-serializable-classes/</link>
		<comments>http://ahtik.com/blog/2006/08/23/reason-to-serialversionuid-your-serializable-classes/#comments</comments>
		<pubDate>Wed, 23 Aug 2006 14:18:29 +0000</pubDate>
		<dc:creator>Ahti Kitsik</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://ahtik.com/blog/2006/08/23/reason-to-serialversionuid-your-serializable-classes/</guid>
		<description><![CDATA[Quoting Eclipse FAQ: &#8220;When classes compiled in Eclipse are written to an object stream, you may not be able to read them back in a program that was compiled elsewhere. Many people blame this on the Eclipse compiler, assuming that it is somehow not conforming properly to spec. In fact, this can be a problem [...]]]></description>
			<content:encoded><![CDATA[<p>Quoting <a target="_blank" href="http://wiki.eclipse.org/index.php/FAQ_Why_does_the_Eclipse_compiler_create_a_different_serialVersionUID_from_javac%3F">Eclipse FAQ</a>: &#8220;When classes compiled in Eclipse are written to an object stream, you may not be able to read them back in a program  that was compiled elsewhere.  Many people blame this on the Eclipse compiler,  assuming that it is somehow not conforming properly to spec.  In fact, this can be a problem between any two compilers or even two versions of a compiler provided by the same vendor.   <strong>If you need object serialization, the <em>only</em> way to be safe is to explicitly define the <tt>serialVersionUID</tt> in your code!</strong> /&#8230;/ Bottom line: Always define the <tt>serialVersionUID</tt> explicitly in your source files.&#8221;</p>
<p>My question is: can there be an audit to automatically detect source files where you explicitly defined serialVersionUID that is out of date? A nice-to-have addition would be a quick-fix for repairing serialVersionUID. Current eclipse quick-fix supports only generating serialVersionUID from scratch.</p>
]]></content:encoded>
			<wfw:commentRss>http://ahtik.com/blog/2006/08/23/reason-to-serialversionuid-your-serializable-classes/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

