<?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/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">

<channel>
	<title>Thinking Inside a Bigger Box &#187; Java</title>
	<atom:link href="http://johannesbrodwall.com/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://johannesbrodwall.com</link>
	<description>Johannes Brodwall&#039;s Musings on Software Architecture and Programming</description>
	<lastBuildDate>Mon, 19 Mar 2012 12:44:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>		<item>
		<title>The Architecture Spike Kata</title>
		<link>http://johannesbrodwall.com/2011/12/22/the-architecture-spike-kata/</link>
		<comments>http://johannesbrodwall.com/2011/12/22/the-architecture-spike-kata/#comments</comments>
		<pubDate>Thu, 22 Dec 2011 13:28:09 +0000</pubDate>
		<dc:creator>Johannes Brodwall</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Extreme Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://johannesbrodwall.com/?p=842</guid>
		<description><![CDATA[Do you know how to apply coding practices the technology stack that you use on a daily basis? Do you know how the technology stack works? For many programmers, it&#8217;s easy enough to use test-driven development with a trivial example, but it can be very hard to know how to apply it to the problems [...]]]></description>
			<content:encoded><![CDATA[<p>Do you know how to apply coding practices the technology stack that you use on a daily basis? Do you know how the technology stack works? For many programmers, it&#8217;s easy enough to use test-driven development with a trivial example, but it can be very hard to know how to apply it to the problems you face every day in your job.</p>
<p>Java web+database applications are usually filled to the brim with technologies. Many of these are hard to test and many of these may not add value. In order to explore TDD and Java applications, I practiced the Java EE Spike Kata in 2010. <a href="http://streaming.java.no/tcs/?id=9BE4BB66-1FD9-4497-966C-91FFAC728CC1">Here&#8217;s a video of me and Anders Karlsen doing this kata at JavaZone 2010</a>.</p>
<p>A similar approach is likely useful for programmers using any technology. Therefore, I give you: The rules of the Architecture Spike Kata.</p>
<h3>The problem</h3>
<p>Create a web application that lets users register a Person with names and search for people. The Person objects should be saved in a data store that is similar to the technology you use daily (probably a relational database). The goal is to get a spike working as quickly as possible, so in the first iteration, the Person entity should probably only contain one field. You can add more fields and refactor the application later.</p>
<h3>The rules</h3>
<p>The most important rules are <a href="http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd">Robert Martin</a>&#8216;s three rules of Test-driven development:</p>
<ul>
<li>No code without test (that is, the code should never do something that isn&#8217;t required in order to get a test to pass)</li>
<li>Only enough test to get to red (that is, the tests should <i>run</i>, give an error message and that error message should correct)</li>
<li>Only enough code to get to green (that is, the tests should run and not give an error)</li>
<li>(My addition: Refactor on green without adding functionality)</li>
</ul>
<p>Secondly, application should be driven from the outside in. That is, your first test should be a top-level acceptance test that tests through http and html. It&#8217;s okay to comment out or @Ignore this test after it has run red for the first time.</p>
<p>Lastly, you should not introduce any technology before the pain of not doing so is blinding. The first time you do the kata in a language, don&#8217;t use a web framework beyond the language minimum (in Java, this means Servlets, in node.js it&#8217;s <code>require('http')</code>, in Ruby it means Rack). Don&#8217;t use a Object-Relational Mapping framework. Don&#8217;t use a dependency injection framework. Most definitely don&#8217;t use an application generator like Rails scaffold, Spring Roo or Lift. These frameworks can be real time savers, but this kata is about understanding how the underlying technology works.</p>
<p>As a second iteration, use the technologies you use on a daily basis, but this time set up from scratch. For example, if your project uses Hibernate, try configuring the session factory by hand. By using frameworks in simplest way possible, you&#8217;ll both learn more about what they bring to the table and how to use them properly. For complex technology like Hibernate, there&#8217;s no substitute for deeper understanding.</p>
<h3>What to expect</h3>
<p>So far, I&#8217;ve only done the Architecture Spike Kata in Java. But on the other hand, I&#8217;ve done it around 50 times together with more than ten other developers. <a href="http://johannesbrodwall.com/2009/12/02/trene-pa-java-ee/">I&#8217;ve written about how to get started with the Java EE Spike Kata (in Norwegian) on my blog before</a>.</p>
<p>This is what I&#8217;ve learned about working with web applications in Java:</p>
<ul>
<li>Most Java web frameworks seem to harm more than they help</li>
<li>Hibernate is a bitch to set up, but once it&#8217;s working, it saves a lot of hassle</li>
<li>Using TDD with Hibernate helped me understand how to use Hibernate more effectively</li>
<li>I&#8217;ve stopped using dependency injection frameworks (but kept on using dependency injection as a pattern)</li>
<li>I have learned several ways to test web applications and database access independently and integrated</li>
<li>I no longer have to expend mental energy to write tests for full stack application</li>
</ul>
<p>The first time I write this kata with another developer, it takes around 3 to 5 hours, depending on the experience level of my pair. After running through it a few times, most developers can complete the task in less than an hour.</p>
<p>We get better through practice, and the Architecture Spike Kata is a way to practice TDD with the technologies that you use daily and get a better understanding of what&#8217;s going on.</p>
]]></content:encoded>
			<wfw:commentRss>http://johannesbrodwall.com/2011/12/22/the-architecture-spike-kata/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Can we replace requirement specification with better understanding?</title>
		<link>http://johannesbrodwall.com/2011/09/23/better-understanding/</link>
		<comments>http://johannesbrodwall.com/2011/09/23/better-understanding/#comments</comments>
		<pubDate>Fri, 23 Sep 2011 16:39:30 +0000</pubDate>
		<dc:creator>Johannes Brodwall</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Extreme Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://johannesbrodwall.com/?p=820</guid>
		<description><![CDATA[On larger projects, I&#8217;ve always ended up resorting to writing down a lot of detailed specifications, many of which are wrong, irrelevant or we might not be ready to answer them yet. On small projects, the dialogue between the customer and the developers can flow easy, and good things happen. The quick analysis Developer: &#8230; [...]]]></description>
			<content:encoded><![CDATA[<p>On larger projects, I&#8217;ve always ended up resorting to writing down a lot of detailed specifications, many of which are wrong, irrelevant or we might not be ready to answer them yet. On small projects, the dialogue between the customer and the developers can flow easy, and good things happen.</p>
<h3>The quick analysis</h3>
<blockquote>
<p><strong>Developer:</strong> &#8230; so we&#8217;re going to complete the current task tomorrow or the day after. Could we discuss what to do next before you&#8217;re off to your next meeting?</p>
<p><strong>Customer:</strong> Sure. The next task on the backlog is to send the payment request to the accounting system</p>
<p><strong>Developer:</strong> Yeah. Um&#8230; well, we&#8217;re not really ready for the integration tasks yet. I was thinking that we might perhaps pick another user interface task. Would that be okay?</p>
<p><strong>Customer:</strong> No problem. Let&#8217;s see&#8230;. how about &#8220;road project submits funding request&#8221;?</p>
<p><strong>Developer:</strong> Great. What&#8217;s that?</p>
<p><strong>Customer:</strong> Well, today, the owner of the road project sends in a paper form. Let&#8217;s see, the form is available as a PDF on our web site. Let&#8217;s take a look.</p>
<p><strong>Developer:</strong> Let&#8217;s see&#8230;. We know how to deal with social security number, names, addresses. This field &#8220;municipality,&#8221; that&#8217;s the usual stuff, right? (customer nods) What about &#8220;request id&#8221;? What&#8217;s that?</p>
<p><strong>Customer:</strong> It&#8217;s just a sequence number per municipality. It would be really nice if you could suggest a request number and then let the user override.</p>
<p><strong>Developer:</strong> That should be doable.</p>
<p>They work out a few more details and get on their way. Total time, 30 minutes</p>
</blockquote>
<h3>The low ambition implementation</h3>
<blockquote>
<p><strong>Developer:</strong> So, we worked on the funding request UI for the last two days. I think we&#8217;ve got something that looks pretty good.</p>
<p><strong>Customer:</strong> Great, let&#8217;s see it! Let&#8217;s see&#8230; There&#8217;s a new &#8220;apply for funding request&#8221; menu item. Nice. But it should be &#8220;Fund road project&#8221; (developer notes feedback). Let me try and press it&#8230; Hmm&#8230; &#8220;select municipality&#8221;? Why do I get that?</p>
<p><strong>Developer:</strong> Well, we&#8217;re still a bit shaky on AJAX. So in order to find the next sequence number, we would like to have the user select municipality first.</p>
<p><strong>Customer:</strong> Oh, AJAX. That&#8217;s interactive web pages, right? Yeah, I guess that&#8217;s fine, but we need to fix up the text a little. So, what happens when I select one&#8230;? Nice, it sort of looks like the paper form. That will help us get learn it. But let&#8217;s move the fields around a little (he sketches a few change on a piece of paper).</p>
<p><strong>Developer:</strong> We were a bit uncertain about this &#8220;sum&#8221; field. Does the user have to fill in the sum himself? He&#8217;s already filled in all the numbers.</p>
<p><strong>Customer:</strong> That doesn&#8217;t make too much sense. How about if the web pages updates the sum field dynamically? Oh, you&#8217;ve got that &#8216;don&#8217;t make me do dynamic webpages&#8217; face again.</p>
<p><strong>Developer:</strong> Yeah, sorry. The last time it was a big mess. Can we hold off on that for now? Do you really need it?</p>
<p><strong>Customer:</strong> (laughs) No, that&#8217;s okay. I guess we can just remove the field. That should be fine.</p>
<p><strong>Developer:</strong> Yay! What about validating the bank account number? We left that out, because the documentation is&#8230;. well, insane. That alone will take at least another day.</p>
<p><strong>Customer:</strong> No, we really need that. People often type it wrong, and then it&#8217;s a nightmare to fix all the errors that follow. You just have to fight though it.</p>
<p><strong>Developer:</strong> (sigh) I guess it can&#8217;t be helped. We&#8217;re on it.</p>
<p><strong>Customer:</strong> All in all, it looks pretty good. I&#8217;ll go over it by myself after lunch and bring you back some issues tomorrow.</p>
</blockquote>
<h3>The quick completion</h3>
<blockquote>
<p><strong>Customer:</strong> Here&#8217;s a list of corrections. Nothing major, just a few misplaced fields and a few extra validation rules.</p>
<p><strong>Developer:</strong> Great. We spent all of yesterday doing the bank account number validation. Man, that&#8217;s some bad doc! But we should be done before lunch.</p>
<p><strong>Customer:</strong> I have an opening in my calendar at the end of the day, let&#8217;s take a look then and call it done. Great work, guys!</p>
</blockquote>
<h3>The contrast</h3>
<p>Have you ever been on a small project where everyone has a pretty good understand of what&#8217;s going on and the communication works well? Developers can make a pretty good guess at the details and develop a pretty good first version. There may be some more work that&#8217;s needed, but these can be negotiated when the developers have a better idea of what&#8217;s going to be easy and what&#8217;s going to be hard.</p>
<p>When I&#8217;ve been on larger projects, this communication seems to break down. Instead the developers require a detailed specification in writing. And if something is missing from the specification, they reject it and ask for further details. And they won&#8217;t show the product to the customer before all the kinks are worked out.</p>
<p>When we do it well it&#8217;s not so bad, getting a detailed specification and finishing the task before we get real feedback. But it requires the customer to decide on things that may not be important, but that may impact cost substantially. It requires back and forth to get the specification written, reviewed, amended etc.</p>
<p>On a small project, a developer can understand the purpose, build something quickly and adjust it based on feedback. Is the reality of larger projects that this will just remain a pipe dream? If so, why? If not, how do you get the quick and informal feedback?</p>
]]></content:encoded>
			<wfw:commentRss>http://johannesbrodwall.com/2011/09/23/better-understanding/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>What will Java 7 mean for you?</title>
		<link>http://johannesbrodwall.com/2011/08/08/what-will-java-7-mean-for-you/</link>
		<comments>http://johannesbrodwall.com/2011/08/08/what-will-java-7-mean-for-you/#comments</comments>
		<pubDate>Sun, 07 Aug 2011 22:35:38 +0000</pubDate>
		<dc:creator>Johannes Brodwall</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://johannesbrodwall.com/?p=813</guid>
		<description><![CDATA[Oracle released Java 7 on July 28, 2011. This is nearly 5 years after the release of Java 6 in December 2006. The release received a lot of bad press, both because it is very meager on features, and because it shipped with a severe bug. Nevertheless, once the most serious bugs have been fixed, [...]]]></description>
			<content:encoded><![CDATA[<p>Oracle released Java 7 on July 28, 2011. This is nearly 5 years after the release of Java 6 in December 2006. The release received a lot of bad press, both because it is <a href="http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html">very meager on features</a>, and because <a href="http://developers.slashdot.org/story/11/07/29/1639233/Java-7-Ships-With-Severe-Bug">it shipped with a severe bug</a>. Nevertheless, once the most serious bugs have been fixed, you might think about starting to use Java 7. What will this mean?</p>
<h3>New language features</h3>
<p>Java 7 has a few new language features. Sadly, the most exciting ones have been postponed until Java 8. The following 3 features may show up in your pretty quickly, though:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#40;</span> <span style="color: #003399;">BufferedReader</span> reader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileReader</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: #003399;">String</span> line <span style="color: #339933;">=</span> reader.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  process<span style="color: #009900;">&#40;</span>line<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is the try-with-resources or Automatic Resource Management block. If you declare a variable in the <code>try()</code> statement, Java automatically calls <code>close</code> on it, like you would in a finally block. This is a small improvement, but nice. You can use try-with-resources on your own object by implementing the new interface <code>java.lang.AutoCloseable</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    clazz.<span style="color: #006633;">getConstructor</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">List</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span>list<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;">InstantiationException</span><span style="color: #339933;">|</span>IllegalAccessException<span style="color: #339933;">|</span>InvocationTargetException<span style="color: #339933;">|</span>NoSuchMethodException e<span style="color: #009900;">&#41;</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><span style="color: #0000ff;">&quot;I really don't care&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is the multi-catch statement. It&#8217;s useful because of the load of checked exceptions on the sanity of your average Java-developer. It&#8217;s nice, but hardly revolutionary. It makes me really wish we got rid of checked exceptions, though.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Map<span style="color: #339933;">&lt;&gt;</span> ordersPerCustomer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>customer ,List<span style="color: #339933;">&lt;</span>Order<span style="color: #339933;">&gt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>customer<span style="color: #339933;">&gt;</span></pre></div></div>

<p>This is type inference for Generic Instance Creation. Saves a few keystrokes without removing any type safety. Again, a nice, but very small improvement.</p>
<p>There are a few more language features, but I expect they will see very little use.</p>
<h3>JVM changes</h3>
<p>The Java virtual machine gets a new instruction: <code>invokedynamic</code>. Using invokedynamic, the JVM can invoke a method on an object without having to know on which class or interface the method is declared. If it walks like a duck and talks like a duck&#8230;</p>
<p>Invokedynamic will be very helpful for implementors of dynamic languages in the JVM, so it&#8217;s great. But the average developer will never encounter it in the wild.</p>
<h3>Library changes</h3>
<p>Looking at the <a href="http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html">release notes</a> for Java 7, you may first suspect that there are some interesting library changes here. However, when examining the list more thoroughly, I couldn&#8217;t find a single change that I expect I will actually use. The library changes are mostly low-level, behind the scenes fixes of small problems.</p>
<h3>Conclusions</h3>
<p>So there it is: try-with-resources, multi-catch and a very limited type inference. Hopefully, Java 8 will be released as planned in late 2012 with all the stuff we&#8217;ve been waiting for. If so, I expect most shops will skip Java 7. But if Java 8 follows the pattern of delays from Java 7, these slim pickings may be all the crumbs the Java community gets for another five years.</p>
]]></content:encoded>
			<wfw:commentRss>http://johannesbrodwall.com/2011/08/08/what-will-java-7-mean-for-you/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>This dependency injection madness must end!</title>
		<link>http://johannesbrodwall.com/2010/11/10/this-dependency-injection-madness-must-end/</link>
		<comments>http://johannesbrodwall.com/2010/11/10/this-dependency-injection-madness-must-end/#comments</comments>
		<pubDate>Wed, 10 Nov 2010 19:14:50 +0000</pubDate>
		<dc:creator>Johannes Brodwall</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://johannesbrodwall.com/?p=717</guid>
		<description><![CDATA[Or: Poor man&#8217;s dependency injection: Singleton-initialized field As dependency injection frameworks like Spring have become central in software applications in the last few years, the conventional view is now that the Singleton pattern is evil and should never be used. Instead, relationships between objects in your system should be managed by a dependency injection container. [...]]]></description>
			<content:encoded><![CDATA[<p><em>Or: Poor man&#8217;s dependency injection: Singleton-initialized field</em></p>
<p>As dependency injection frameworks like Spring have become central in software applications in the last few years, the conventional view is now that the Singleton pattern is <em>evil</em> and should never be used. Instead, relationships between objects in your system should be <em>managed</em> by a dependency injection container. I have started disliking the consequence of this strategy very much: All coupling in my system becomes implicit and harder to understand. I have instead reverted to using design patterns like the Singleton pattern, but with a slight twist.</p>
<h3>The Singleton-initialized field</h3>
<p>Here is an example of a Singleton-initialized field:</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;">class</span> PersonService <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> PersonDao personDao<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> PersonService<span style="color: #009900;">&#40;</span>PersonDao personDao<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;">personDao</span> <span style="color: #339933;">=</span> personDao<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> PersonService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        SessionFactory sessionFactory <span style="color: #339933;">=</span> 
              MyHibernateContext.<span style="color: #006633;">getSessionFactoryInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">personDao</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HibernatePersonDao<span style="color: #009900;">&#40;</span>sessionFactory<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> someServiceMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #666666; font-style: italic;">// Do something with personDao</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When I write a test for this class, the test code overrides the <code>PersonDao</code> by passing it to the non-default constructor.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Test
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> shouldDoSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   PersonService service <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PersonService<span style="color: #009900;">&#40;</span>mockPersonDao<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   service.<span style="color: #006633;">someServiceMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   verify<span style="color: #009900;">&#40;</span>mockPersonDao<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findAllPeople</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>

<h3>Compared: The dependency injection way</h3>
<p>The now-conventional way of doing the same thing is with dependency injection:</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;">class</span> PersonService <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> PersonDao personDao<span style="color: #339933;">;</span>
&nbsp;
    @Inject
    <span style="color: #000000; font-weight: bold;">public</span> PersonService<span style="color: #009900;">&#40;</span>PersonDao personDao<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;">personDao</span> <span style="color: #339933;">=</span> personDao<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> someServiceMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #666666; font-style: italic;">// Do something with personDao</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And the test will set up the dependencies using a container:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@RunWith<span style="color: #009900;">&#40;</span>SpringTestRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
@Configuration<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;src/test/resources/test-context.xml&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PersonServiceTest <span style="color: #009900;">&#123;</span>
&nbsp;
   @Resource
   <span style="color: #000000; font-weight: bold;">private</span> PersonService service<span style="color: #339933;">;</span>
&nbsp;
   @Resource
   <span style="color: #000000; font-weight: bold;">private</span> PersonDao mockPersonDao<span style="color: #339933;">;</span>
&nbsp;
   @Test
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> shouldDoSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      service.<span style="color: #006633;">someServiceMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      verify<span style="color: #009900;">&#40;</span>mockPersonDao<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findAllPeople</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></pre></div></div>

<p>Somewhere in the realms of <code>test-context.xml</code> we will configure the fact that <code>PersonDao</code> should be provided by a mock:</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;">class</span> MyDependencyConfigTest <span style="color: #000000; font-weight: bold;">extends</span> MyDependencyConfig <span style="color: #009900;">&#123;</span>
    @Bean
    <span style="color: #000000; font-weight: bold;">public</span> PersonDao personService<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> MockPersonDao<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></pre></div></div>

<h3>The problem: Declarative programming</h3>
<p>With a dependency injection container, the components in my code are loosely coupled. Indeed, this is the main selling point of dependency injection. But sometimes, loosely coupled code is the problem. </p>
<p>First, understanding which dependency is actually used (and why!) can require a lot of brain power: It will require you to consider all possible configurations when you look at a dependency and it will not work well with the normal code navigation functionality of your IDE. Second, the configuration will tend to deteriorate: When you no longer use a dependency, will you check whether you can remove it, or will you just leave it there to be safe?</p>
<p>Lastly, it can be hard to spot errors in the configuration:</p>
<p>I once had a service that needed had a <code>@Resource TransactionManager</code> and a <code>@Resource DataSource</code>. However, the system had a pair of each. The test code was correct, but in the production code, I had by accident configured the wrong <code>TransactionManager</code>. The effect was that the Service didn&#8217;t run in a transaction for the correct data source. That is: It didn&#8217;t really run in a transaction. The problem with this is that you only discover the problem if you scrutinize the contents of the database after the transaction was supposed to be rolled back.</p>
<p>Dependency injection in specific and declarative programming in general mean More Magic. More Magic is seldom a good thing, at least not when there are simple, time-tested strategies that work. Even if said strategies have fallen out of fashion.</p>
<p>This dependency injection madness must end!</p>
]]></content:encoded>
			<wfw:commentRss>http://johannesbrodwall.com/2010/11/10/this-dependency-injection-madness-must-end/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Video: No-red refactoring</title>
		<link>http://johannesbrodwall.com/2010/11/05/no-red-refactoring/</link>
		<comments>http://johannesbrodwall.com/2010/11/05/no-red-refactoring/#comments</comments>
		<pubDate>Fri, 05 Nov 2010 05:54:20 +0000</pubDate>
		<dc:creator>Johannes Brodwall</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Extreme Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://johannesbrodwall.com/?p=714</guid>
		<description><![CDATA[The more I code, the more I&#8217;ve learned to appreciate keeping the code clean even during complex refactorings. By &#8220;clean&#8221;, I mean that the code always compiles and the test always run. I often find myself in a situation where I have a method call that&#8217;s starting to accumulate parameters. Something like this: showPersonCreateForm&#40;writer, firstName, [...]]]></description>
			<content:encoded><![CDATA[<p>The more I code, the more I&#8217;ve learned to appreciate keeping the code clean even during complex refactorings. By &#8220;clean&#8221;, I mean that the code always compiles and the test always run.</p>
<p>I often find myself in a situation where I have a method call that&#8217;s starting to accumulate parameters. Something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">showPersonCreateForm<span style="color: #009900;">&#40;</span>writer, firstName, firstNameErrorMessage, lastName, lastNameErrorMessage,....<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>After three or four parameters, the need to refactor is starting to become evident. I would rather have something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">CreatePersonForm form <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CreatePersonForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
form.<span style="color: #006633;">setFirstName</span><span style="color: #009900;">&#40;</span>firstName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
form.<span style="color: #006633;">setFirstNameErrorMessage</span><span style="color: #009900;">&#40;</span>firstName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
form.<span style="color: #006633;">setLastName</span><span style="color: #009900;">&#40;</span>firstName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
form.<span style="color: #006633;">setLastNameErrorMessage</span><span style="color: #009900;">&#40;</span>firstName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
form.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span>writer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is one of the more complex simple refactorings you can make, and it requires several steps. In this five minute video, I show how to perform such a refactoring without any steps that break my code:</p>
<p><embed src="http://blip.tv/play/AYKKglwC" type="application/x-shockwave-flash" width="480" height="306" allowscriptaccess="always" allowfullscreen="true"></embed></p>
<p>The screencast was created using the free BB FlashBack Express on Windows. All the magic you see happening while I program is either <code>ctrl-space</code> (complete) or <code>ctrl-1</code> (quick fix).</p>
<p>Can you modify your code without going thought long stages of nothing working? I think you can!</p>
]]></content:encoded>
			<wfw:commentRss>http://johannesbrodwall.com/2010/11/05/no-red-refactoring/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cross-cutting code, the homemade way</title>
		<link>http://johannesbrodwall.com/2010/10/26/cross-cutting-code/</link>
		<comments>http://johannesbrodwall.com/2010/10/26/cross-cutting-code/#comments</comments>
		<pubDate>Tue, 26 Oct 2010 12:22:26 +0000</pubDate>
		<dc:creator>Johannes Brodwall</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://johannesbrodwall.com/?p=706</guid>
		<description><![CDATA[I though I&#8217;d do something different this time. Instead of describing something technical, I have recorded a five minute video that shows a very neat trick in Java: How to create a bit of code that wraps existing method calls to an object with any behavior you may desire. This is nothing new, but I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>I though I&#8217;d do something different this time. Instead of describing something technical, I have recorded a five minute video that shows a very neat trick in Java: How to create a bit of code that wraps existing method calls to an object with any behavior you may desire.</p>
<p>This is nothing new, but I&#8217;ve noticed that not many developers know how to use it, so I hope this video may be useful:</p>
<p><embed src="http://blip.tv/play/AYKG4EoC" type="application/x-shockwave-flash" width="480" height="390" allowscriptaccess="always" allowfullscreen="true"></embed></p>
<p>If you would rather not include a library like <a href="http://cglib.sourceforge.net/">cglib</a> in your code and you don&#8217;t mind having to create an interface to create this sort of code, you can do the same with <code>java.lang.reflect.Proxy</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://johannesbrodwall.com/2010/10/26/cross-cutting-code/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Eclipse telepathy: Your IDE can guess what you want</title>
		<link>http://johannesbrodwall.com/2010/10/14/eclipse-telepathy/</link>
		<comments>http://johannesbrodwall.com/2010/10/14/eclipse-telepathy/#comments</comments>
		<pubDate>Thu, 14 Oct 2010 09:32:12 +0000</pubDate>
		<dc:creator>Johannes Brodwall</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Extreme Programming]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://johannesbrodwall.com/?p=663</guid>
		<description><![CDATA[Ctrl-1 is the magic &#8220;do what I think&#8221; button in Eclipse. Whenever I press it, Eclipse seems to come up with something that&#8217;s helpful in the current context. In this blog post, I illustrate 10 things that Eclipse hide under the ctrl-1 keypress. This is a follow up on my post on Eclipse stenography. I [...]]]></description>
			<content:encoded><![CDATA[<p>Ctrl-1 is the magic &#8220;do what I think&#8221; button in Eclipse. Whenever I press it, Eclipse seems to come up with something that&#8217;s helpful in the current context. In this blog post, I illustrate 10 things that Eclipse hide under the ctrl-1 keypress. This is a follow up on my post on <a href="http://johannesbrodwall.com/2010/09/28/eclipse-stenography/">Eclipse stenography</a>.</p>
<p>I got some comments on my last post about Eclipse stenography about the animated gifs. I know this can be annoying, but I considered the alternative: To have you mouse-over or click a picture to animate it. I&#8217;m afraid that the pictures will sort of disappear from your attention if I do this. So I&#8217;ll try again. But this time, I&#8217;ll put the images below the fold. Click on to see the ten tips.</p>
<h3>1: Create a class that you refer to in existing code</h3>
<p><img src="http://johannesbrodwall.com/wp-content/uploads/2010/10/1-create-class-anim.gif" alt="Create class" title="Create class" class="alignnone size-full wp-image-664" /></p>
<h3>2: If you create a class and Eclipse detects that it must implement an interface, Eclipse will generate stub implementations of the interface for you</h3>
<p><img src="http://johannesbrodwall.com/wp-content/uploads/2010/10/2-create-class-with-interface-anim.gif" alt="Create class with interface" title="Create class with interface" class="alignnone size-full wp-image-664" /></p>
<h3>3: As above: If you code references a non-existing constructor, Eclipse can create it for you</h3>
<p><img src="http://johannesbrodwall.com/wp-content/uploads/2010/10/3-create-constructor-anim.gif" alt="Create constructor" title="Create constructor" class="alignnone size-full wp-image-664" /></p>
<h3>4: And if a parameter to a constructor or method is unused, Eclipse can create a new field and assign the parameter to it</h3>
<p><img src="http://johannesbrodwall.com/wp-content/uploads/2010/10/4-create-field-anim.gif" alt="Create field" title="Create field" class="alignnone size-full wp-image-664" /></p>
<h3>5: When the cursor is on an expression, Eclipse can assign the expression to a local variable</h3>
<p><img src="http://johannesbrodwall.com/wp-content/uploads/2010/10/5-create-local-var-anim.gif" alt="Create local variable" title="Create local variable" class="alignnone size-full wp-image-664" /></p>
<h3>6: If you&#8217;re comparing two values, and only one can be null, Eclipse can invert the objects to avoid the null</h3>
<p><img src="http://johannesbrodwall.com/wp-content/uploads/2010/10/6-invert-equals-anim.gif" alt="Invert equals" title="Invert equals" class="alignnone size-full wp-image-664" /></p>
<h3>7: Eclipse can help you massage a conditional expression into shape</h3>
<p><img src="http://johannesbrodwall.com/wp-content/uploads/2010/10/7-massage-conditional-anim.gif" alt="Massage conditional" title="Massage conditional" class="alignnone size-full wp-image-664" /></p>
<h3>8: Convert old style loops to fancy new loops, where possible</h3>
<p><img src="http://johannesbrodwall.com/wp-content/uploads/2010/10/8-enhanced-for-loop-anim.gif" alt="Enhanced for-loop" title="Enhanced for-loop" class="alignnone size-full wp-image-664" /></p>
<h3>9: Take care of those annoying checked exceptions</h3>
<p><img src="http://johannesbrodwall.com/wp-content/uploads/2010/10/9-catch-exceptions-anim.gif" alt="Catch exceptions" title="Catch exceptions" class="alignnone size-full wp-image-664" /></p>
<p>On a related note, there&#8217;s only one thing I really hate about the Java language: Checked exceptions. They get in the way of code coverage, they trick novice developers to deal with problems locally instead of at the top level, and ultimately, they introduce more bugs. And that&#8217;s a fact!</p>
<h3>10: When you have a field that&#8217;s just not being used, why not have Eclipse help you violate encapsulation with less work</h3>
<p><img src="http://johannesbrodwall.com/wp-content/uploads/2010/10/10-generate-getters-and-setters-anim.gif" alt="Create getter and setter" title="Create getter and setter" class="alignnone size-full wp-image-664" /></p>
<h3>What&#8217;s your favorite?</h3>
<p>Eclipse (and other IDEs) have a myriad of smart tricks. For Eclipse, ctrl-space and ctrl-1 give you access to a surprising amount of functionality. Do you know any other keyboard shortcut that&#8217;s as versatile? Or as useful? Do share! :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://johannesbrodwall.com/2010/10/14/eclipse-telepathy/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How pair programming and test-driven development looks in real life</title>
		<link>http://johannesbrodwall.com/2010/09/30/java-ee-spike-kata/</link>
		<comments>http://johannesbrodwall.com/2010/09/30/java-ee-spike-kata/#comments</comments>
		<pubDate>Thu, 30 Sep 2010 14:17:18 +0000</pubDate>
		<dc:creator>Johannes Brodwall</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Extreme Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://johannesbrodwall.com/?p=638</guid>
		<description><![CDATA[Pair programming and test-driven development are some of the practices that are most often talked about and least often actually understood. So I&#8217;ve decided to undertake the task to teach myself to program a simple, yet realistic problem with a pair programming partner. The goal is to create an entertaining and realistic performance that portrays [...]]]></description>
			<content:encoded><![CDATA[<p>Pair programming and test-driven development are some of the practices that are most often talked about and least often actually understood. So I&#8217;ve decided to undertake the task to teach myself to program a simple, yet realistic problem with a pair programming partner. The goal is to create an entertaining and realistic performance that portrays what it feels like to work like this.</p>
<p>I&#8217;ve been extremely lucky. I&#8217;ve found not one, but two programmers that have been willing to train enough with me to make a smooth performance of a pair programming session. The result is the <em>Java EE Spike Kata</em>. My colleagues in Steria, Anders Karlsen and Ivar Nilsen have been great to work with. Between us, we&#8217;ve showed the kata at three conferences, and more are coming.</p>
<p><a href="http://streaming.java.no/tcs/?id=9BE4BB66-1FD9-4497-966C-91FFAC728CC1">Here is the performance at JavaZone 2010 by Anders Karlsen and myself.</a></p>
<p>Make sure to watch the video in full screen mode as there will be LOTS of code.</p>
<p>Some things to watch for when you&#8217;re watching a (paired) code kata:</p>
<ul>
<li>How are we working together? How often are we changing who&#8217;s at the keyboard? What will the person who&#8217;s not at the keyboard be doing?</li>
<li>How do we progress from one test to the next? When do we decide to modify an existing test and when do we decide to add a new one? How much of the test do we write before we start modifying the production code?</li>
<li>How are we writing the production code? How often are we refactoring existing code?</li>
<li>How are we using the IDE? What tricks do we use to take advantage of the code completion facilities? How often do we use the mouse and how often do we use the keyboard?</li>
</ul>
<p>I also hope to be putting up the video of me and Ivar Nilsen at the TelecomCity conference in Sweden. As we&#8217;d both practiced even more for that performance, I think it&#8217;s even smoother than the JavaZone performance.</p>
<p>Enjoy!</p>
<p>(Kudos to Cisco Norway, formerly Tandberg, for excellent filming services at JavaZone. And thank you to Robert &#8220;Uncle Bob&#8221; Martin for turning me on to code katas in the first place)</p>
]]></content:encoded>
			<wfw:commentRss>http://johannesbrodwall.com/2010/09/30/java-ee-spike-kata/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Eclipse stenography: Create code faster</title>
		<link>http://johannesbrodwall.com/2010/09/28/eclipse-stenography/</link>
		<comments>http://johannesbrodwall.com/2010/09/28/eclipse-stenography/#comments</comments>
		<pubDate>Tue, 28 Sep 2010 05:28:03 +0000</pubDate>
		<dc:creator>Johannes Brodwall</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Extreme Programming]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://johannesbrodwall.com/?p=625</guid>
		<description><![CDATA[<a href="http://en.wikipedia.org/wiki/Shorthand">According to Wikipedia</a>, stenography or shorthand is "is an abbreviated symbolic writing method that increases speed or brevity of writing as compared to a normal method of writing a language".

Just as a stenographer learns to take down information really fast, a good programmer can learn to write code really fast by taking advantage of his or her tools. In this post I'll show you my secret code stenography tricks.

The basic way completion works is: You write something and press ctrl-space. Eclipse will respond with a list of option. Use the arrow keys to select an option and press enter. Eclipse will replace what you typed with the option.

<h3>Name completion</h3>

The most well-known form of completion: Start writing the name of a class or method, press-ctrl space, and Eclipse will complete the typing for you.

<img src="http://johannesbrodwall.com/wp-content/uploads/2010/09/complete-method-anim.gif" alt="Complete method name" title="complete-method-anim" width="319" height="135" class="alignnone size-full wp-image-627" />

Notice the blue squares around "expected" and "actual". These are "fields". Use tab to jump between them.

New to many is the fact that Eclipse support CamelCase syntax when completing names:

<img src="http://johannesbrodwall.com/wp-content/uploads/2010/09/completion-anim.gif" alt="Class name completion" title="completion-anim" class="alignnone size-medium wp-image-626" />

When I type "TPH", Eclipse finds to classes with names that match ("TaggedProfileHelper" and "TaggedProfileHolder") and offers them to me as a list. When I type "TPHe", Eclipse finds only one class that matches ("TaggedProfileHelper"), and expands it automatically.

Pro tip: Remove classes you don't use but that often make completion ambiguous by going to Window->Preferences and adding the classes to Java->Appearance->Type filter.


<h3>Expand code template</h3>

Another fairly basic one: Type the name of a template (from Window->Preferences, Java->Editor->Templates) and press ctrl-space to insert it.

<img src="http://johannesbrodwall.com/wp-content/uploads/2010/09/template-anim.gif" alt="Expand code template" title="template-anim" width="327" height="123" class="alignnone size-full wp-image-630" />

Notice that Eclipse brings up the type list on the first ctrl-space. Press ctrl-space again to show Template completions.

Pro tip: Edit and remove editor templates to make your coding faster. You may also add template, but I seldom find this as useful.


<h3>Override method</h3>

Now, we're getting into new territory for most of you: In the class body, type the name of a method in the super class and press ctrl-space:

<img src="http://johannesbrodwall.com/wp-content/uploads/2010/09/override-anim.gif" alt="Override method" title="override-anim" width="501" height="161" class="alignnone size-full wp-image-628" />

Eclipse offers to override the method for you. Notice that CamelCase completion works here.


<h3>Generate getters and setters</h3>

Yet another little know super trick: In the class body of a class with a field "name", type "getN" and press ctrl-space to generate a getter, or "setN" to generate a setter:

<img src="http://johannesbrodwall.com/wp-content/uploads/2010/09/getter-setter-anim.gif" alt="Generate getter and setter" title="getter-setter-anim" width="373" height="174" class="alignnone size-full wp-image-629" />

Notice that CamelCasing doesn't work perfectly here; you can't type "gNa" to create a getter for "name".

(It's possible to generate getters and setters for a large number of fields with "Source->Generate getters and setters". But when I only add one new field, I find code completion to be a faster option)



As I've been working on code katas, I've found many ways to speed up my programming in my IDE of choice. I hope Eclipse stenography or code completion can help you write code faster as well.
]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Shorthand">According to Wikipedia</a>, stenography or shorthand is &#8220;is an abbreviated symbolic writing method that increases speed or brevity of writing as compared to a normal method of writing a language&#8221;.</p>
<p>Just as a stenographer learns to take down information really fast, a good programmer can learn to write code really fast by taking advantage of his or her tools. In this post I&#8217;ll show you my secret code stenography tricks.</p>
<p>The basic way completion works is: You write something and press ctrl-space. Eclipse will respond with a list of option. Use the arrow keys to select an option and press enter. Eclipse will replace what you typed with the option.</p>
<h3>Name completion</h3>
<p>The most well-known form of completion: Start writing the name of a class or method, press-ctrl space, and Eclipse will complete the typing for you.</p>
<p><img src="http://johannesbrodwall.com/wp-content/uploads/2010/09/complete-method-anim.gif" alt="Complete method name" title="complete-method-anim" width="319" height="135" class="alignnone size-full wp-image-627" /></p>
<p>Notice the blue squares around &#8220;expected&#8221; and &#8220;actual&#8221;. These are &#8220;fields&#8221;. Use tab to jump between them.</p>
<p>New to many is the fact that Eclipse support CamelCase syntax when completing names:</p>
<p><img src="http://johannesbrodwall.com/wp-content/uploads/2010/09/completion-anim.gif" alt="Class name completion" title="completion-anim" class="alignnone size-medium wp-image-626" /></p>
<p>When I type &#8220;TPH&#8221;, Eclipse finds to classes with names that match (&#8220;TaggedProfileHelper&#8221; and &#8220;TaggedProfileHolder&#8221;) and offers them to me as a list. When I type &#8220;TPHe&#8221;, Eclipse finds only one class that matches (&#8220;TaggedProfileHelper&#8221;), and expands it automatically.</p>
<p>Pro tip: Remove classes you don&#8217;t use but that often make completion ambiguous by going to Window->Preferences and adding the classes to Java->Appearance->Type filter.</p>
<h3>Expand code template</h3>
<p>Another fairly basic one: Type the name of a template (from Window->Preferences, Java->Editor->Templates) and press ctrl-space to insert it.</p>
<p><img src="http://johannesbrodwall.com/wp-content/uploads/2010/09/template-anim.gif" alt="Expand code template" title="template-anim" width="327" height="123" class="alignnone size-full wp-image-630" /></p>
<p>Notice that Eclipse brings up the type list on the first ctrl-space. Press ctrl-space again to show Template completions.</p>
<p>Pro tip: Edit and remove editor templates to make your coding faster. You may also add template, but I seldom find this as useful.</p>
<h3>Override method</h3>
<p>Now, we&#8217;re getting into new territory for most of you: In the class body, type the name of a method in the super class and press ctrl-space:</p>
<p><img src="http://johannesbrodwall.com/wp-content/uploads/2010/09/override-anim.gif" alt="Override method" title="override-anim" width="501" height="161" class="alignnone size-full wp-image-628" /></p>
<p>Eclipse offers to override the method for you. Notice that CamelCase completion works here.</p>
<h3>Generate getters and setters</h3>
<p>Yet another little know super trick: In the class body of a class with a field &#8220;name&#8221;, type &#8220;getN&#8221; and press ctrl-space to generate a getter, or &#8220;setN&#8221; to generate a setter:</p>
<p><img src="http://johannesbrodwall.com/wp-content/uploads/2010/09/getter-setter-anim.gif" alt="Generate getter and setter" title="getter-setter-anim" width="373" height="174" class="alignnone size-full wp-image-629" /></p>
<p>Notice that CamelCasing doesn&#8217;t work perfectly here; you can&#8217;t type &#8220;gNa&#8221; to create a getter for &#8220;name&#8221;.</p>
<p>(It&#8217;s possible to generate getters and setters for a large number of fields with &#8220;Source->Generate getters and setters&#8221;. But when I only add one new field, I find code completion to be a faster option)</p>
<p>As I&#8217;ve been working on code katas, I&#8217;ve found many ways to speed up my programming in my IDE of choice. I hope Eclipse stenography or code completion can help you write code faster as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://johannesbrodwall.com/2010/09/28/eclipse-stenography/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Dynamic subclass APIs make Java seem young again</title>
		<link>http://johannesbrodwall.com/2010/09/07/dynamic-subclass-apis-make-java-seem-young-again/</link>
		<comments>http://johannesbrodwall.com/2010/09/07/dynamic-subclass-apis-make-java-seem-young-again/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 13:45:27 +0000</pubDate>
		<dc:creator>Johannes Brodwall</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://johannesbrodwall.com/?p=585</guid>
		<description><![CDATA[At JavaZone 2010 I will be giving a lightning talk on APIs that use dynamic subclasses. These APIs make it possible to do things in Java that seem like pure magic. Here are some ideas of what you can get from these APIs and a look under the hood, so you really understand what&#8217;s going [...]]]></description>
			<content:encoded><![CDATA[<p>At <a href="http://javazone.no/incogito10/events/JavaZone%202010/sessions#ae4aa326-60ae-43a4-85b1-aa906d7f1fe5">JavaZone 2010</a> I will be giving a lightning talk on APIs that use dynamic subclasses. These APIs make it possible to do things in Java that seem like pure magic. Here are some ideas of what you can get from these APIs and a look under the hood, so you really understand what&#8217;s going on.</p>
<h3>Mockito &#8211; &#8220;the best Java library named after a summery drink&#8221;</h3>
<p>Mocking is a hotly debated subject within testing camps. I think a lot of the arguments come against mocking (beyond overuse&#8230;) come down to the fact that the last generation of mocking libraries left a lot to be desired. A desire that has been fulfilled by <a href="http://code.google.com/p/mockito/">Mockito</a>.</p>
<p>I will leave the examples at a minimum, but here&#8217;s an example from code I actually use a lot. Mocking the servlet API:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">HttpServletRequest req <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>HttpServletRequest.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
HttpServletResponse resp <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>HttpServletResponse.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
when<span style="color: #009900;">&#40;</span>req.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">thenReturn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;GET&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
servletUnderTest.<span style="color: #006633;">service</span><span style="color: #009900;">&#40;</span>req, resp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
verify<span style="color: #009900;">&#40;</span>resp<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>For those of you who&#8217;ve struggled with jMock or it&#8217;s ilk, you may notice the concise syntax used to specify behavior (&#8220;when(&#8230;).thenReturn()&#8221;) and the fact that you don&#8217;t have to set up expectations before exercising the code.</p>
<h3>LambdaJ &#8211; &#8220;making Java beautiful and incomprehensible&#8221;</h3>
<p><a href="http://code.google.com/p/lambdaj/">LambdaJ</a> pretty much starts with the realizations that waiting for new language features like closures may prove to be a very long wait indeed. Furthermore, anonymous inner classes may technically be used to simulate closures, but they are so ugly that not even a mother could love them.</p>
<p>Let&#8217;s look at an example: Finding the name of every member of the Simpsons family who&#8217;s older than 8, in order of age.</p>
<p>In plain Java:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">List</span><span style="color: #009900;">&#91;</span>Person<span style="color: #009900;">&#93;</span> tmp <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#91;</span>Person<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</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>Person p <span style="color: #339933;">:</span> theSimpsons<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>p.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span> tmp.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003399;">Collections</span>.<span style="color: #006633;">sort</span><span style="color: #009900;">&#40;</span>tmp, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Comparator</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> <span style="color: #000066; font-weight: bold;">int</span> compare<span style="color: #009900;">&#40;</span>Person a, Person b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">return</span> a.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> b.<span style="color: #006633;">getAge</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: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">List</span><span style="color: #009900;">&#91;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#93;</span> names <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#91;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</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>Person p <span style="color: #339933;">:</span> tmp<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  names.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>p.<span style="color: #006633;">getName</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;">&#125;</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>names<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>More modern languages are way ahead of Java. Here&#8217;s Ruby:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span> theSimpsons.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">age</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">8</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">sort_by</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:age<span style="color:#006600; font-weight:bold;">&#41;</span>.
     <span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">name</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>The rather simple request is simple to express. Yay!</p>
<p>Java 7 may make this better.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>theSimpsons.<span style="color: #006633;">select</span><span style="color: #009900;">&#40;</span>#<span style="color: #009900;">&#40;</span>Person p<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">return</span> p.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">sort_by</span><span style="color: #009900;">&#40;</span>#<span style="color: #009900;">&#40;</span>Person p<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">return</span> p.<span style="color: #006633;">getAge</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;">&#41;</span>.<span style="color: #006633;">collect</span><span style="color: #009900;">&#40;</span>#<span style="color: #009900;">&#40;</span>Person p<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">return</span> p.<span style="color: #006633;">getName</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;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>All in all, not a bad contender. What about LambdaJ:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>with<span style="color: #009900;">&#40;</span>theSimpsons<span style="color: #009900;">&#41;</span>
    .<span style="color: #006633;">retain</span><span style="color: #009900;">&#40;</span>having<span style="color: #009900;">&#40;</span>on<span style="color: #009900;">&#40;</span>Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, gt<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #006633;">sort</span><span style="color: #009900;">&#40;</span>on<span style="color: #009900;">&#40;</span>Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #006633;">extract</span><span style="color: #009900;">&#40;</span>on<span style="color: #009900;">&#40;</span>Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</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: #339933;">;</span></pre></div></div>

<p>It&#8217;s not perfect, but it has one thing to recommend it: This works with standard, everyday Java, today! There&#8217;s no special compilation crap. No other language. Nothin. Just plain old Java.</p>
<p><em>(Thanks to Mario Fusco for improving the example with LamdbaJ 2.3 syntax)</em></p>
<h3>What&#8217;s going on</h3>
<p>Both LambdaJ and Mockito use a tricky sleight-of-hand: Generate a dynamic subclass which records its method invocations for later use. Creating such an API is in principle easy, although my implementation leave <i>a lot!</i> of details unimplemented.</p>
<p>So: We want invocations on the generated class to be template/mock objects to be saved:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Test
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> shouldRecordInvocations<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
    Person template <span style="color: #339933;">=</span> DynSubclassDemo.<span style="color: #006633;">on</span><span style="color: #009900;">&#40;</span>Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertNull<span style="color: #009900;">&#40;</span>DynSubclassDemo.<span style="color: #006633;">lastMethodCall</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    template.<span style="color: #006633;">getAge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;getAge&quot;</span>,
        DynSubclassDemo.<span style="color: #006633;">lastMethodCall</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</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;">&#125;</span></pre></div></div>

<p>This magic is surprisingly easy to implement with <a href="http://cglib.sourceforge.net/">CgLib</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@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: #003399;">Object</span> on<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span> templateClass<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> Enhancer.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span>templateClass, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InvocationHandler</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> invoke<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> o, <span style="color: #003399;">Method</span> m, <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            lastMethodCall <span style="color: #339933;">=</span> m<span style="color: #339933;">;</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><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This creates a subclass of the template class (in this case: Person). But every method in Person is overridden by calling the invocation handler. Which simply saves what method was called.</p>
<p>This makes it possible to implement a prototype of LambdaJ&#8217;s <code>collect</code> method:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Test
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> shouldCallRecordedMethodForEveryObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    names <span style="color: #339933;">=</span> DynSubclassDemo.<span style="color: #006633;">collect</span><span style="color: #009900;">&#40;</span>theSimpsons, 
            DynSubclassDemo.<span style="color: #006633;">on</span><span style="color: #009900;">&#40;</span>Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertEquals<span style="color: #009900;">&#40;</span>names, asList<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Lisa&quot;</span>, <span style="color: #0000ff;">&quot;Homer&quot;</span>, <span style="color: #0000ff;">&quot;Marge&quot;</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: #000000; font-weight: bold;">static</span><span style="color: #009900;">&#91;</span>T ,U<span style="color: #009900;">&#93;</span> <span style="color: #003399;">List</span><span style="color: #009900;">&#91;</span>U<span style="color: #009900;">&#93;</span> collect<span style="color: #009900;">&#40;</span>Iterable<span style="color: #009900;">&#91;</span>T<span style="color: #009900;">&#93;</span> c, U property<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#91;</span>U<span style="color: #009900;">&#93;</span> result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#91;</span>U<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</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>T t <span style="color: #339933;">:</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// An insane amount of exception handling removed</span>
        result.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>U<span style="color: #009900;">&#41;</span> lastMethodCall.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</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;">return</span> result<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is a gross simplification. LambdaJ handles many aspects that my prototype simple ignores: Chained method calls, multithreading, method arguments, and multiple invocations on the same line.</p>
<h3>The future of Java</h3>
<p>Maybe the future of Java isn&#8217;t extending the language. Maybe the future of Java isn&#8217;t replacing the language with Scala. Maybe the future of Java is smart people discovering smarter ways of using what&#8217;s already there. My hat off to Mario Fusco, Szczepan Faber and everybody who&#8217;s contributed to LambdaJ or Mockito.</p>
<p>Come to my talk at JavaZone to learn more!</p>
]]></content:encoded>
			<wfw:commentRss>http://johannesbrodwall.com/2010/09/07/dynamic-subclass-apis-make-java-seem-young-again/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

