<?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>Benji's Blog &#187; Uncategorized</title>
	<atom:link href="http://benjiweber.co.uk/blog/posts/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://benjiweber.co.uk/blog</link>
	<description></description>
	<lastBuildDate>Thu, 19 May 2011 23:08:57 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Java Abuse: Inline instanceof</title>
		<link>http://benjiweber.co.uk/blog/2010/09/16/java-abuse-inline-instanceof/</link>
		<comments>http://benjiweber.co.uk/blog/2010/09/16/java-abuse-inline-instanceof/#comments</comments>
		<pubDate>Thu, 16 Sep 2010 20:23:40 +0000</pubDate>
		<dc:creator>benji</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://benjiweber.co.uk/blog/?p=125</guid>
		<description><![CDATA[One annoyance in Java is having to do instanceof checks on multiple lines. e.g.

if &#40;object instanceof Foo&#41; &#123;
    Foo foo = &#40;Foo&#41;object;
    foo.foo&#40;&#41;;
&#125;

While this is often a sign of a design failure, there are times when instanceof checks are required often due to framework constraints etc. The above is [...]]]></description>
			<content:encoded><![CDATA[<p>One annoyance in Java is having to do instanceof checks on multiple lines. e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>object <span style="color: #000000; font-weight: bold;">instanceof</span> Foo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Foo foo <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Foo<span style="color: #009900;">&#41;</span>object<span style="color: #339933;">;</span>
    foo.<span style="color: #006633;">foo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>While this is often a sign of a design failure, there are times when instanceof checks are required often due to framework constraints etc. The above is quite ugly and involves using 3 lines instead of 1 for a single method call.  If foo() is a void method we can&#8217;t even use the ternary ? : operator to make it more concise. </p>
<p>In c# with Lambdas and Extension methods one can create something like :</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #FF0000;">object</span>.<span style="color: #0000FF;">When</span><span style="color: #008000;">&lt;</span>Foo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>foo <span style="color: #008000;">=&gt;</span> foo.<span style="color: #0000FF;">Foo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Where &#8220;When&#8221; is an extension method added to all objects which takes a type parameter and a lambda to execute if the type parameter matches. (Implementation available if anyone cares).</p>
<p>Since Java doesn&#8217;t have Extension methods or Lambdas yet (Oracle minus minus) we can&#8217;t do this. We can however always use reflection and get something like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">scratchpad</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.Method</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">net.sf.cglib.proxy.Enhancer</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">net.sf.cglib.proxy.MethodInterceptor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">net.sf.cglib.proxy.MethodProxy</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> scratchpad.<span style="color: #006633;">When</span>.<span style="color: #006633;">when</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Let <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Object</span> a <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">Object</span> b <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;hello world&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		when<span style="color: #009900;">&#40;</span>Foo.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isTypeOf</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">foo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Prints &quot;foo&quot;</span>
		when<span style="color: #009900;">&#40;</span>Foo.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isTypeOf</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">foo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Does nothing at all</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> When <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> Is<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> when<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> Class<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> cls<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Is<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">public</span> T isTypeOf<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">return</span> o <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> cls.<span style="color: #006633;">isAssignableFrom</span><span style="color: #009900;">&#40;</span>o.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
					<span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span>o
					<span style="color: #339933;">:</span> NullObject.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span>cls<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Is<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">public</span> T isTypeOf<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> o<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> NullObject <span style="color: #000000; font-weight: bold;">implements</span> MethodInterceptor  <span style="color: #009900;">&#123;</span>
&nbsp;
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> T create<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> cls<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		Enhancer enhancer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Enhancer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		enhancer.<span style="color: #006633;">setSuperclass</span><span style="color: #009900;">&#40;</span>cls<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		enhancer.<span style="color: #006633;">setCallback</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> NullObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span>enhancer.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> intercept<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> o, <span style="color: #003399;">Method</span> method, <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> os, MethodProxy mp<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://benjiweber.co.uk/blog/2010/09/16/java-abuse-inline-instanceof/feed/</wfw:commentRss>
		<slash:comments>4399</slash:comments>
		</item>
		<item>
		<title>Netbeans on n900</title>
		<link>http://benjiweber.co.uk/blog/2010/08/23/netbeans-on-n900/</link>
		<comments>http://benjiweber.co.uk/blog/2010/08/23/netbeans-on-n900/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 20:47:52 +0000</pubDate>
		<dc:creator>benji</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://benjiweber.co.uk/blog/?p=120</guid>
		<description><![CDATA[I meant to post some time ago about how great the n900 is for java development. Not only can one install OpenJDK, but Netbeans is even usable. It&#8217;s pretty easy to get running. Just install openjdk6 in the application manager. Then download and run the netbeans installer.


It even runs at quite a reasonable speed if [...]]]></description>
			<content:encoded><![CDATA[<p>I meant to post some time ago about how great the <a href="http://maemo.nokia.com/n900/" onclick="javascript:pageTracker._trackPageview('/outbound/article/maemo.nokia.com');">n900</a> is for java development. Not only can one install OpenJDK, but Netbeans is even usable. It&#8217;s pretty easy to get running. Just install openjdk6 in the application manager. Then download and run the netbeans installer.</p>
<p><img src="http://files.benjiweber.co.uk/nbn900/Screenshot-20100527-141307.png" alt="installation"/></p>
<p><img src="http://files.benjiweber.co.uk/nbn900/Screenshot-20100527-150754.png" alt="running"/></p>
<p>It even runs at quite a reasonable speed if you&#8217;re not running much else.</p>
]]></content:encoded>
			<wfw:commentRss>http://benjiweber.co.uk/blog/2010/08/23/netbeans-on-n900/feed/</wfw:commentRss>
		<slash:comments>3712</slash:comments>
		</item>
		<item>
		<title>Java Abuse: public static void main was not invented here.</title>
		<link>http://benjiweber.co.uk/blog/2010/08/23/java-abuse-public-static-void-main-was-not-invented-here/</link>
		<comments>http://benjiweber.co.uk/blog/2010/08/23/java-abuse-public-static-void-main-was-not-invented-here/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 20:42:48 +0000</pubDate>
		<dc:creator>benji</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://benjiweber.co.uk/blog/?p=107</guid>
		<description><![CDATA[Normally a command-line Java application will have an entry method that looks something like:

public static void main&#40;String... args&#41; &#123;
    /* Code that launches the application/deals with command line params here */
&#125;

This is far too easy, there must be a way to NIH it ¬_¬. There are a few annoyances with the public [...]]]></description>
			<content:encoded><![CDATA[<p>Normally a command-line Java application will have an entry method that looks something like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/* Code that launches the application/deals with command line params here */</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is far too easy, there must be a way to NIH it ¬_¬. There are a few annoyances with the public static void main method.</p>
<ul>
<li>Args are passed in as an Array, rather than something like a List&lt;String&gt;</li>
<li>We can&#8217;t choose to call the main method something other than main.</li>
<li>It&#8217;s a bit obscure how the startup process works.</li>
</ul>
<p>There is a common way to execute code on app startup without using a static void main method. You can use a static initialization block:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">static</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/* Code here run on class load. */</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So we could launch our application from within a static initialization block. However this presents a number of problems:</p>
<ol>
<li>We get a nasty error when the application finishes (Exception in thread &#8220;main&#8221; java.lang.NoSuchMethodError: main)</li>
<li>We have no access to the command line arguments</li>
<li>We can&#8217;t have multiple &#8220;Main&#8221; classes with this method (Both static initialization blocks will be run, if both classes are loaded)</li>
<li>It doesn&#8217;t really provide any benefits over static void main.</li>
</ol>
<p>These problems can, however, all be overcome. </p>
<h3>Suppressing NoSuchMethodError</h3>
<p>We can just call System.err.close(); at the end of our initialization method.</p>
<h3>Access to the command line arguments</h3>
<p>This one is probably the most difficult. I&#8217;ve not found any &#8220;good&#8221; way to do this, but there is a hack that works at present on Sun&#8217;s Java, though it may stop working at any point.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">sun.<span style="color: #006633;">misc</span>.<span style="color: #006633;">VMSupport</span>.<span style="color: #006633;">getAgentProperties</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sun.java.command&quot;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>This will give the command line string used to start the application. There are alternatives involving attaching to the currently running VM with the Agent API or shelling out to external processes and using platform-specific commands to work out the command used to start the application. If anyone knows a proper way to access the command line arguments using the public API then please let me know.</p>
<h3>Multiple Main Classes</h3>
<p>We need to get our initialization system to ignore invocations from classes other than the one used to start our application. This can be done by checking the length of the StackTrace. e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Throwable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">fillInStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">length</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Reflection Examples</h3>
<p>With these 3 issues solved it&#8217;s possible to do things like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">MainlessDemo</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> uk.<span style="color: #006633;">co</span>.<span style="color: #006633;">benjiweber</span>.<span style="color: #006633;">realjava</span>.<span style="color: #006633;">mainless</span>.<span style="color: #006633;">Bootstrap</span>.<span style="color: #006633;">init</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ReflectionMainless <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #009900;">&#123;</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;From std err&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> arg <span style="color: #339933;">:</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>arg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">/* Output:
$ javac -cp .:./RealJava.jar ./MainlessDemo/*.java &amp;&amp; java -cp .:./RealJava.jar MainlessDemo.ReflectionMainless Foo Bar Baz
Hello World
From std err
Foo
Bar
Baz
*/</span></pre></div></div>

<p>Here a statically imported &#8220;init&#8221; method instantiates our main class, invokes our non-static main method, and passes it the command line arguments. The class to instantiate can be determined by walking back up the stack trace again.</p>
<p>We can also inject constructor arguments if we wish:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">MainlessDemo</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> uk.<span style="color: #006633;">co</span>.<span style="color: #006633;">benjiweber</span>.<span style="color: #006633;">realjava</span>.<span style="color: #006633;">mainless</span>.<span style="color: #006633;">Bootstrap</span>.<span style="color: #006633;">init</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ReflectionMainlessWithArgs <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// This time we pass in the constructor argument</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #009900;">&#123;</span> init<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> message<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Even though we load another class that is Launchable it doesn't get launched.</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> ReflectionMainless test <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ReflectionMainless<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> ReflectionMainlessWithArgs<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">message</span> <span style="color: #339933;">=</span> message<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>message <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; (Passed in through constructor)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> arg <span style="color: #339933;">:</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>arg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">/* Output:
$ javac -cp .:./RealJava.jar ./MainlessDemo/*.java &amp;&amp; java -cp .:./RealJava.jar MainlessDemo.ReflectionMainlessWithArgs Foo Bar Baz
Hello World (Passed in through constructor)
Foo
Bar
Baz
&nbsp;
*/</span></pre></div></div>

<h3>Interface Examples</h3>
<p>Unfortunately, apart from substituting an argument Array for a List, we &#8216;ve not really improved anything. However, now that we&#8217;re in control of the initialization process we can do more interesting things, like use an interface:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Launchable <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">MainlessDemo</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">uk.co.benjiweber.realjava.mainless.Launchable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> uk.<span style="color: #006633;">co</span>.<span style="color: #006633;">benjiweber</span>.<span style="color: #006633;">realjava</span>.<span style="color: #006633;">mainless</span>.<span style="color: #006633;">Bootstrap</span>.<span style="color: #006633;">init</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SaferMainless <span style="color: #000000; font-weight: bold;">implements</span> Launchable <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #009900;">&#123;</span> init<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SaferMainless<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello from a Launchable&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> arg <span style="color: #339933;">:</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>arg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
Output:
$ javac -cp .:./RealJava.jar ./MainlessDemo/*.java &amp;&amp; java -cp .:./RealJava.jar MainlessDemo.SaferMainless Foo Bar Baz
Hello from a Launchable
Foo
Bar
Baz
*/</span></pre></div></div>

<p>Now it&#8217;s clearer how the initialization process works, you can use your IDE to follow execution through from the init block to the main method. This also gives us the freedom to call our main method something different, and inject dependencies before reaching the main method.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">MainlessDemo</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">uk.co.benjiweber.realjava.mainless.Launchable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> uk.<span style="color: #006633;">co</span>.<span style="color: #006633;">benjiweber</span>.<span style="color: #006633;">realjava</span>.<span style="color: #006633;">mainless</span>.<span style="color: #006633;">Bootstrap</span>.<span style="color: #006633;">init</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SaferAnonymousMainless <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #009900;">&#123;</span> init<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Launchable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			SaferAnonymousMainless mainless <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SaferAnonymousMainless<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			mainless.<span style="color: #006633;">setMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I was injected manually&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			mainless.<span style="color: #006633;">someMethodNotCalledMain</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> message<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setMessage<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">message</span> <span style="color: #339933;">=</span> message<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> someMethodNotCalledMain<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> arg <span style="color: #339933;">:</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>arg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">/* 
Output:
$ javac -cp .:./RealJava.jar ./MainlessDemo/*.java &amp;&amp; java -cp .:./RealJava.jar MainlessDemo.SaferAnonymousMainless Foo Bar Baz
I was injected manually
Foo
Bar
Baz
&nbsp;
*/</span></pre></div></div>

<p><a href="http://github.com/benjiman/realjava/blob/master/src/main/java/uk/co/benjiweber/realjava/mainless/Bootstrap.java" onclick="javascript:pageTracker._trackPageview('/outbound/article/github.com');">Source for the BootStrap.init method is here</a></p>
<p>Thanks to <a href="http://blog.prelode.com/" onclick="javascript:pageTracker._trackPageview('/outbound/article/blog.prelode.com');">Faux</a> for some of the ideas.</p>
<p>Yes I know this isn&#8217;t remotely a good idea, no need to tell me <img src='http://benjiweber.co.uk/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
]]></content:encoded>
			<wfw:commentRss>http://benjiweber.co.uk/blog/2010/08/23/java-abuse-public-static-void-main-was-not-invented-here/feed/</wfw:commentRss>
		<slash:comments>3920</slash:comments>
		</item>
		<item>
		<title>Java Abuse &#8211; Ternary Try/Catch</title>
		<link>http://benjiweber.co.uk/blog/2009/06/14/java-abuse-ternary-trycatch/</link>
		<comments>http://benjiweber.co.uk/blog/2009/06/14/java-abuse-ternary-trycatch/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 13:03:27 +0000</pubDate>
		<dc:creator>benji</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://benjiweber.co.uk/blog/?p=46</guid>
		<description><![CDATA[We often discuss Java limitations on IRC and try to come up with (sometimes silly) workarounds. Unfortunately after time passes it&#8217;s often easy to forget the outcome, and lose code snippets. So I thought I&#8217;d start blogging some of them so I don&#8217;t lose them, and other people might suggest other ways of doing things [...]]]></description>
			<content:encoded><![CDATA[<p>We often discuss Java limitations on IRC and try to come up with (sometimes silly) workarounds. Unfortunately after time passes it&#8217;s often easy to forget the outcome, and lose code snippets. So I thought I&#8217;d start blogging some of them so I don&#8217;t lose them, and other people might suggest other ways of doing things that we&#8217;ve overlooked.</p>
<p>This particular problem occurs when you want to assign the result of a method that can throw an exception to a final variable. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">final</span> Customer c<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">try</span>
<span style="color: #009900;">&#123;</span>
	c <span style="color: #339933;">=</span> getCustomer<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>CustomerNotFoundException e<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	c <span style="color: #339933;">=</span> createNewCustomer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This will fail to compile with &#8220;variable c might already have been assigned&#8221;. Of course making c not final would solve the problem, but that&#8217;s no fun.</p>
<p>If we were not using Exceptions, Java provides a useful ternary operator &#8220;?:&#8221; that lets us do things like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">final</span> Customer c <span style="color: #339933;">=</span> customerExists<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> getCustomer<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> createNewCustomer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Which is nice and clean, but means that getCustomer is going to have to return null or throw a RuntimeException in the case that there is no matching customer, which is undesirable. Also  customerExists() may be expensive.</p>
<p>We could also possibly use something along the lines of</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">final</span> Option<span style="color: #339933;">&lt;</span>Customer<span style="color: #339933;">&gt;</span> c <span style="color: #339933;">=</span> getCustomer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Both of these alternatives, however, require changing the API you&#8217;re consuming, and avoiding Exceptions. It would be nice if there was an equivalent of &#8220;?:&#8221; for try/catch so that you could assign the result to a final variable. The best I can manage in Java is below, can anyone do better?</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.ParameterizedType</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> TryCatch<span style="color: #339933;">&lt;</span>T, U <span style="color: #000000; font-weight: bold;">extends</span> Exception<span style="color: #339933;">&gt;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> T value<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">Try</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>getTypeOfU<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isAssignableFrom</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">Catch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Class<span style="color: #339933;">&lt;</span>U<span style="color: #339933;">&gt;</span> getTypeOfU<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>U<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ParameterizedType<span style="color: #009900;">&#41;</span> getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getGenericSuperclass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getActualTypeArguments</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>1<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> T <span style="color: #000000; font-weight: bold;">Try</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> U<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> T <span style="color: #000000; font-weight: bold;">Catch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Example</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> CustomerRepo repo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CustomerRepo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> Customer c <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TryCatch<span style="color: #339933;">&lt;</span>Customer, CustomerNotFoundException<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">public</span> Customer <span style="color: #000000; font-weight: bold;">Try</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> CustomerNotFoundException
			<span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;in try&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">return</span> repo.<span style="color: #006633;">getCustomer</span><span style="color: #009900;">&#40;</span>1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">public</span> Customer <span style="color: #000000; font-weight: bold;">Catch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;in catch&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">return</span> repo.<span style="color: #006633;">createCustomer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>.<span style="color: #006633;">value</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> CustomerRepo
<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Customer getCustomer<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> id<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> CustomerNotFoundException
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> CustomerNotFoundException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Customer createCustomer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Customer
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> CustomerNotFoundException <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Exception</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In C# we don&#8217;t run into the same problem since <a href="http://msdn.microsoft.com/en-us/library/acdd6hb7.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/msdn.microsoft.com');">readonly</a> is much less useful than Java&#8217;s <a href="http://mindprod.com/jgloss/final.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/mindprod.com');">final</a>. However, if we wanted to try/catch at the same time we can do a bit better. Here&#8217;s an alternative in C#:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Example
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		Example t <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Example<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		t.<span style="color: #0000FF;">Foo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Foo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #FF0000;">String</span> result1 <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0600FF;">Try</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> GetBar<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0600FF;">Catch</span><span style="color: #008000;">&lt;</span>BarException<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #666666;">&quot;Caught a BarException&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #FF0000;">String</span> result2 <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0600FF;">Try</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> GetBar<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0600FF;">Catch</span><span style="color: #008000;">&lt;</span>BarException<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #666666;">&quot;Caught a BarException&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>result1<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>result2<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> GetBar<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">bool</span> succeed<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>succeed<span style="color: #000000;">&#41;</span>
			<span style="color: #0600FF;">return</span> <span style="color: #666666;">&quot;Success!&quot;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">else</span>
			<span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> BarException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> BarException <span style="color: #008000;">:</span> Exception <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Tryer<span style="color: #008000;">&lt;</span>TResult<span style="color: #008000;">&gt;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> Func<span style="color: #008000;">&lt;</span>TResult<span style="color: #008000;">&gt;</span> toTry<span style="color: #008000;">;</span>
	<span style="color: #0600FF;">internal</span> Tryer<span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>TResult<span style="color: #008000;">&gt;</span> toTry<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">toTry</span> <span style="color: #008000;">=</span> toTry<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> TResult <span style="color: #0600FF;">Catch</span><span style="color: #008000;">&lt;</span>TException<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;</span>TResult<span style="color: #008000;">&gt;</span> whenCaught<span style="color: #000000;">&#41;</span>
		where TException <span style="color: #008000;">:</span> Exception
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">try</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">return</span> toTry<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>TException<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">return</span> whenCaught<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> <span style="color: #000000;">System</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> ProvidesTry
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Tryer<span style="color: #008000;">&lt;</span>TResult<span style="color: #008000;">&gt;</span> <span style="color: #0600FF;">Try</span><span style="color: #008000;">&lt;</span>T,TResult<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> T other, Func<span style="color: #008000;">&lt;</span>TResult<span style="color: #008000;">&gt;</span> toTry<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> Tryer<span style="color: #008000;">&lt;</span>TResult<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>toTry<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span> 
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://benjiweber.co.uk/blog/2009/06/14/java-abuse-ternary-trycatch/feed/</wfw:commentRss>
		<slash:comments>3479</slash:comments>
		</item>
		<item>
		<title>Software Portal Preview Available.</title>
		<link>http://benjiweber.co.uk/blog/2009/02/02/software-portal-preview-available/</link>
		<comments>http://benjiweber.co.uk/blog/2009/02/02/software-portal-preview-available/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 22:59:35 +0000</pubDate>
		<dc:creator>benji</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://benjiweber.co.uk/blog/?p=34</guid>
		<description><![CDATA[In the last couple of weeks I&#8217;ve had some time to work on the software portal project for the first time for a while.
There is now a test snapshot that you can play with if you wish, and help find bugs.
Please do have a play and leave comments here or file a bug report under [...]]]></description>
			<content:encoded><![CDATA[<p>In the last couple of weeks I&#8217;ve had some time to work on the <a href="http://en.opensuse.org/Software_Portal" onclick="javascript:pageTracker._trackPageview('/outbound/article/en.opensuse.org');">software portal</a> project for the first time for a while.</p>
<p>There is now a <a href="http://softwareportal-test.opensuse-community.org" onclick="javascript:pageTracker._trackPageview('/outbound/article/softwareportal-test.opensuse-community.org');">test snapshot</a> that you can play with if you wish, and help find bugs.</p>
<p>Please do <a href="http://softwareportal-test.opensuse-community.org" onclick="javascript:pageTracker._trackPageview('/outbound/article/softwareportal-test.opensuse-community.org');">have a play</a> and leave comments here or <a href="http://bugzilla.novell.com" onclick="javascript:pageTracker._trackPageview('/outbound/article/bugzilla.novell.com');">file a bug report</a> under the <a href="http://bw.uwcs.co.uk/b/swp/swp_bug_report.png" >Software Portal</a> component.</p>
<p>You&#8217;ll need to <a href="http://softwareportal-test.opensuse-community.org/web/user/register" onclick="javascript:pageTracker._trackPageview('/outbound/article/softwareportal-test.opensuse-community.org');">register</a> to add comments/screenshots/tags etc. Unfortunately since the account you use to login to other openSUSE.org services is controlled by Novell and they provide no openid/saml facility you cannot use the same user account. If you want permissions to edit application details just send me an email (benji at opensuse dot org).</p>
<p>Most of the recent work has been under the hood. Significant speed improvements to the repository indexer and a Webpin compatible webservice that will enable the <a href="http://dev-loki.blogspot.com/2007/07/webpin-command-line-client.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/dev-loki.blogspot.com');">various</a> <a href="http://hedgehogpainter.livejournal.com/4686.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/hedgehogpainter.livejournal.com');">clients</a> to continue to work against software portal as they did with webpin until they can be ported to the new api.</p>
<p>There are also some visible changes. The homepage now displays recent comments &amp; top rated applications, and includes the tag cloud.</p>
<p><a href="http://bw.uwcs.co.uk/b/swp/swp_home.png" ><img src="http://bw.uwcs.co.uk/b/swp/swp_home_small.png" alt="Software Portal Home Page" /></a></p>
<p>Users can now choose a specific vendor for an application for their distribution, as well as installing from the preferred vendor.</p>
<p><a href="http://bw.uwcs.co.uk/b/swp/swp_view_vendors.png" ><img src="http://bw.uwcs.co.uk/b/swp/swp_view_vendors_small.png" alt="" /></a></p>
<p>Editors can now customise which source packages map to which application.</p>
<p><a href="http://bw.uwcs.co.uk/b/swp/swp_edit_source_packages.png" ><img src="http://bw.uwcs.co.uk/b/swp/swp_edit_source_packages_small.png" alt="" /></a></p>
<p>and which binary packages should be installed by default when a user clicks install.</p>
<p><a href="http://bw.uwcs.co.uk/b/swp/swp_edit_ymp.png" ><img src="http://bw.uwcs.co.uk/b/swp/swp_edit_ymp_small.png" alt="" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://benjiweber.co.uk/blog/2009/02/02/software-portal-preview-available/feed/</wfw:commentRss>
		<slash:comments>690</slash:comments>
		</item>
		<item>
		<title>Snow</title>
		<link>http://benjiweber.co.uk/blog/2009/02/02/snow/</link>
		<comments>http://benjiweber.co.uk/blog/2009/02/02/snow/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 22:04:42 +0000</pubDate>
		<dc:creator>benji</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://benjiweber.co.uk/blog/?p=27</guid>
		<description><![CDATA[This morning we had an unusual amount of snow here, more than we&#8217;ve had in my lifetime. Most public transport was suspended and the people attempting to drive were quickly getting stuck and sliding all over the place.
Motorists were making the classic motorist error. They&#8217;ve got too much power. I&#8217;ve got half a horsepower on [...]]]></description>
			<content:encoded><![CDATA[<p>This morning we had an unusual amount of snow here, more than we&#8217;ve had in my lifetime. Most public transport was suspended and the people attempting to drive were quickly getting stuck and sliding all over the place.</p>
<p>Motorists were making the classic motorist error. They&#8217;ve got too much power. I&#8217;ve got half a horsepower on this &#8211; you don&#8217;t want any more than that on snow.</p>
<p><a href="http://files.benjiweber.co.uk/b/snow1.jpg" ><img src="http://files.benjiweber.co.uk/b/snow1_small.jpg" alt="" /></a></p>
<p><a href="http://files.benjiweber.co.uk/b/snow3.jpg" ><img src="http://files.benjiweber.co.uk/b/snow3_small.jpg" alt="" /></a></p>
<p>Even Boris Johnson mayor of London <a href="http://news.bbc.co.uk/1/hi/uk/7865169.stm" >apparently cycled to work today</a>.</p>
<p>Also see <a href="http://www.yehudamoon.com/index.php?date=2009-01-21" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.yehudamoon.com');">this comic.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://benjiweber.co.uk/blog/2009/02/02/snow/feed/</wfw:commentRss>
		<slash:comments>4296</slash:comments>
		</item>
	</channel>
</rss>

