<?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>Rich Internet Applications (RIA) &#187; General</title>
	<atom:link href="http://www.canoo.com/blog/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.canoo.com/blog</link>
	<description></description>
	<lastBuildDate>Wed, 18 Jan 2012 14:30:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>JavaOne 2011 Monday</title>
		<link>http://www.canoo.com/blog/2011/10/04/javaone-2011-monday/</link>
		<comments>http://www.canoo.com/blog/2011/10/04/javaone-2011-monday/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 06:40:38 +0000</pubDate>
		<dc:creator>Dierk</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[JavaOne]]></category>
		<category><![CDATA[Dierk König]]></category>

		<guid isPermaLink="false">http://www.canoo.com/blog/?p=2273</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/10/04/javaone-2011-monday/";</script>The technical keynote started with a weird JavaZone-style video featuring a Java programmer as a rapper. It was certainly intended to be funny but as far as I can tell, it didn&#8217;t catch on. The keynote was packed but the somehow reduced ballroom layout added to this impression. Attendance was said to be twice that [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/10/04/javaone-2011-monday/";</script><p>The technical keynote started with a weird JavaZone-style video featuring a Java programmer as a rapper. It was certainly intended to be funny but as far as I can tell, it didn&#8217;t catch on.</p>
<p>The keynote was packed but the somehow reduced ballroom layout added to this impression. Attendance was said to be twice that of last year (so probably around 10&#8217;000). Even though there are certainly more people than last year, a doubled number seems a bit exaggerated to me. Throughout the day, all talks were well attended, though, but nothing like in the days when JavaOne had 10&#8217;000 attendees in Moscone Center.</p>
<p>The technical content was not surprising, beside that Oracle now advertises its NoSQL solution, which is based on the former Berkeley DB. As expected JavaFX 2.0 GA has been announced along with the respective tooling and covered by 50 (!) talks on JavaFX at JavaOne. The JavaFX presentation started very conventionally but in the end showed some really cool lab projects with a dancing duke steered by gesture recognition.</p>
<p>The best presenter was Mark Reinhold on Java 7/8/9. Good style, nice slides, perfect pace, interesting (but not really surprising) content. New to me was project Nashorn: new JavaScript implementation for the JVM expected for Java 8. Project Lambda is planned to contain &#8220;defender methods&#8221;, default implementations for interface methods. That sounds like traits and actually I expect some issues when doing this in Java.</p>
<p>Overall, the keynote was missing the JavaOne &#8220;feeling&#8221; from the olden Sun times. There was no host that led through the event, welcomed the attendees, and encouraged everybody to network. No big names on stage, no overwhelming achievements. The crowd left the room unexcited.</p>
<p>For the rest of the day I was mainly concerned with preparing and delivering my own talks on &#8220;Extending Java&#8217;s reach with Groovy&#8221; and &#8220;Pro Groovy&#8221;. They were well attended and received.</p>
<p>Andres delivered his Griffon talk in parallel.</p>
<p>Afterwards, I was a tired but still listened to Charles Nutter on JVM bytecode, Dan Sline on Griffon, and Jim Discroll on Groovy DSLs. Quote to take away: &#8220;Oracle ADFm makes <strong>heavy</strong> use of Groovy!&#8221;</p>
<p>That&#8217;s it for today.</p>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2011/10/04/javaone-2011-monday/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2011/10/04/javaone-2011-monday/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testdata generation</title>
		<link>http://www.canoo.com/blog/2011/09/26/testdata-generation/</link>
		<comments>http://www.canoo.com/blog/2011/09/26/testdata-generation/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 12:01:53 +0000</pubDate>
		<dc:creator>Erik Jan</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.canoo.com/blog/?p=2234</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/09/26/testdata-generation/";</script>code { font-size: 12px; overflow: auto; } We&#8217;ve all being there, we&#8217;ve all had this on a project once or maybe even more times. The assignment is to build an application, but there is no data for you to work with. There could be any number of reasons this could be the case to name [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/09/26/testdata-generation/";</script><style>
code {
font-size: 12px;
overflow: auto;
}
</style>
<p>We&#8217;ve all being there, we&#8217;ve all had this on a project once or maybe even more times. The assignment is to build an application, but there is no data for you to work with. There could be any number of reasons this could be the case to name a few, the web-service that should be connected is not done in time, the database migration is postponed. Then someone has to create database scripts with test data, or implement a test web-services. This is all a waste of time.</p>
<p>But lucky for you now there is a solution.</p>
<p>I&#8217;ve created an little <a href="https://github.com/edewit/testdata-generator">test data framework</a> that is easy to setup and will create test data automatically. So how does it work? Simple, its entry point is a method interceptor that you can wrap around anything you want, it doesn&#8217;t even have to be an implementation. So that DAO you want to use, but the database is still empty, just let the framework step into place and all the methods return the data that you want.</p>
<p>It can also be handy to create test data for you unit test. You need to test if you bean serialization works, or want to put an entity into a in memory database with <a href="http://www.dbunit.org/">dbunit</a>. Just call the framework with you bean class and the framework will return an instance filled with test data. It will read the existing annotations that you have on your entity bean an will only generate data that is valid for you bean.</p>
<p>Here is an example of how it works:<br />
<pre><code>
 //Create an instance of the Employee class and fill it with test data.
 Employee employee = TestData.createBeanInstance(Employee.class);

 //Annotations that restrict the data for first name are recognized.
 assertNotNull(employee.getFirstName());
</code></pre></p>
<p>To create a service that returns test data on it&#8217;s service methods:<br />
<pre><code>
 //create a instance of the service class/interface that will return test data
 Service exampleService = TestData.createService(Service.class);

 //this now returns a list of employee instances that are filled with test data
 List&lt;Employee&gt; employees = exampleService.findByName(&quot;name&quot;);
</code></pre></p>
<p>Create a dbunit xml file:<br />
<pre><code>
 //create a file
 File fileLocation = File.createTempFile(&quot;file&quot;, &quot;.xml&quot;);

 //populate the file with test data for the employee
 TestData.createDBUnitDataSet(Employee.class, fileLocation);
</code></pre><br />
If employee has relations with other classes, like employees have managers these objects will also be in the generated xml file.</p>
<p>To give it a spin add the following into you pom.xml<br />
<pre><code>
&nbsp;&nbsp;&nbsp;&nbsp; &lt;dependency&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;groupId&gt;ch.nerdin&lt;/groupId&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;artifactId&gt;testdata-framework&lt;/artifactId&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;version&gt;0.10&lt;/version&gt;
&nbsp;&nbsp;&nbsp;&nbsp; &lt;/dependency&gt;
</code></pre><br />
or if you use ivy, gradle or something else you properly know how to use this information as well.</p>
<p>So nothing holding you back to make your data driven test easier to setup, or to stub out you migration points. I&#8217;m hosting this code on <a href="https://github.com/edewit/testdata-generator" title="testdata-generator on github">github</a> so if you have issues, comments or even better contribute please do so.</p>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2011/09/26/testdata-generation/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2011/09/26/testdata-generation/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>GWT and HTML5 Canvas, the future of the web?</title>
		<link>http://www.canoo.com/blog/2011/08/25/gwt-and-html5-canvas-the-future-of-the-web/</link>
		<comments>http://www.canoo.com/blog/2011/08/25/gwt-and-html5-canvas-the-future-of-the-web/#comments</comments>
		<pubDate>Thu, 25 Aug 2011 11:35:52 +0000</pubDate>
		<dc:creator>Erik Jan</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[GWT]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Java RIA]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://www.canoo.com/blog/?p=2217</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/08/25/gwt-and-html5-canvas-the-future-of-the-web/";</script>The future of computing lies in mobile computing, more and more devices come on the market, tablets and phones that will connect through the Internet as technology is getting cheaper and wifi and 3g more common. That means our software will also need to run on these handheld devices, because our clients will want their [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/08/25/gwt-and-html5-canvas-the-future-of-the-web/";</script><p>The future of computing lies in mobile computing, more and more devices come on the market, tablets and phones that will connect through the Internet as technology is getting cheaper and wifi and 3g more common. That means our software will also need to run on these handheld devices, because our clients will want their services on all platforms. This is what Java set out to do 15 years ago, so that we have the ability to write software once and run it everywhere. The problem is these new platforms, that I believe will make up a large part of the future have, until now, no Java. It would be great if we can find something that will give us the ability to write our software only once.</p>
<p>I believe that the technology that is going to deliver us this promise is called HTML5. More and more applications will run inside you browser. I&#8217;m typing this article in google docs, which is a perfect example of how applications will evolve. No need to install anything or maintain up to date versions, even an operating system could be very minimalistic (<a href="http://chrome.blogspot.com/search/label/chrome%20os">google chrome</a>). So Google is obviously sharing this vision, but also Microsoft is building it&#8217;s next version of office on something that is based on their web-browser.</p>
<p>Sadly the development language of HTML5 Javascript and not Java. Java has been around a long time and there are a lot of libraries that people have made. Would be cool if we can still use these, the answer is <a href="http://code.google.com/webtoolkit/">Googles Web Toolkit</a>. GWT provides a way to code in Java and then translate that code into Javascript. This way we can still use all that great stuff that others made.</p>
<p>A good example of this future is <a href="http://chrome.angrybirds.com/">Angry Birds</a>, you have probably heard of this game. To create this game they have taken the java implementation of <a href="http://box2d.org/">box2d</a> called <a href="http://www.jbox2d.org/">jbox2d</a> and created a javascript version of that with the help of GWT. Once that is done you can use <a href="http://en.wikipedia.org/wiki/Canvas_element">HTML5 Canvas</a> to draw the Birds and calculate their positions if they are falling. Now because they have build it this way with HTML5 this game could also work on an iPhone, but even better because android is based on java you could create from this source a android version with only one line of code.</p>
<p>Have a look at some of the great things people are building with the HTML5, be sure that your browser supports it.</p>
<ol>
<li>Drawing program <a href="http://muro.deviantart.com/">deviantART</a></li>
<li>Old style game with new technology <a href="http://www.kevs3d.co.uk/dev/arena5/">area5</a></li>
<li><a href="http://www.youtube.com/watch?v=XhMN0wlITLk">Quake GWT HTML5 port</a></li>
</ol>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2011/08/25/gwt-and-html5-canvas-the-future-of-the-web/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2011/08/25/gwt-and-html5-canvas-the-future-of-the-web/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Code generation in GWT with Deferred Binding (CDI-like events)</title>
		<link>http://www.canoo.com/blog/2011/07/04/code-generation-in-gwt-with-deferred-binding-cdi-like-events/</link>
		<comments>http://www.canoo.com/blog/2011/07/04/code-generation-in-gwt-with-deferred-binding-cdi-like-events/#comments</comments>
		<pubDate>Mon, 04 Jul 2011 11:22:30 +0000</pubDate>
		<dc:creator>alberto</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Sample Apps]]></category>
		<category><![CDATA[alberto]]></category>
		<category><![CDATA[code generation]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[event bus]]></category>
		<category><![CDATA[GIN]]></category>

		<guid isPermaLink="false">http://www.canoo.com/blog/?p=2176</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/07/04/code-generation-in-gwt-with-deferred-binding-cdi-like-events/";</script>code { font-size: 12px; overflow: auto; } If you read my series on GWT dependency injection (parts: first, second and third), maybe you remember that, in part 3, I mentioned how convenient would be to reduce the boilerplate required to define events in GWT. I also mentioned how elegant I find the event definition in [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/07/04/code-generation-in-gwt-with-deferred-binding-cdi-like-events/";</script><style type="text/css">
    code {
        font-size: 12px;
        overflow: auto;
    }
</style>
<p>If you read my series on GWT dependency injection (parts: <a href='http://www.canoo.com/blog/2011/04/05/gwt-dependency-injection-recipes-using-gin'>first</a>, <a href='http://www.canoo.com/blog/2011/06/14/gwt-dependency-injection-recipes-using-gin-ii'>second</a> and <a href='http://www.canoo.com/blog/2011/06/20/gwt-dependency-injection-recipes-using-gin-iii'>third</a>), maybe you remember that, in part 3, I mentioned how convenient would be to reduce the boilerplate required to define events in GWT. I also mentioned how elegant I find the event definition in CDI. Wouldn&#8217;t it be nice to have a lightweight event model like that in GWT?</p>
<p>Let&#8217;s see first how an event is defined and handled in CDI:<br />
<pre><code>
public class CDISample {
&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;private Event&lt;String&gt; event;

&nbsp;&nbsp;&nbsp;&nbsp;public void handler(@Observes String payload) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(payload);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String[] args) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event.fire(&quot;Hello event world!&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
As you can grasp from the code, to define a new type of event in CDI it is enough to define an injected field of the parametrized class Event and the CDI container will do all the wiring for us. The parametrized type of the event defines its payload type by mean of the type parameter. To listen to the event, we just need to define a method that receives a parameter of the event payload type  and annotate it with &#8220;@Observes&#8221;.</p>
<p>While exactly this is maybe not doable in GWT (at least not yet), what about something like this?:<br />
<pre><code>
public class GWTSample {
&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;private SampleEvent event;

&nbsp;&nbsp;&nbsp;&nbsp;public void execute() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event.dispatchTo(new Handler&lt;String&gt;() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void onEvent(String payload) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Window.alert(payload);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event.fire(&quot;Hello event world!&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;public interface SampleEvent extends Event&lt;String&gt; {
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
While I can agree with you that it is not as elegant as the events approach in CDI, it is (IMO) a big improvement compared with, for instance, the ping / pong event definition in the demo application of my GIN series (source code <a href='http://www.canoo.com/blog/wp-content/uploads/2011/06/Part0.zip'>here</a>):<br />
<pre><code>
public class PingEvent extends GwtEvent&lt;Handler&gt; {
&nbsp;&nbsp;&nbsp;&nbsp;public static Type&lt;Handler&gt; TYPE = new Type&lt;Handler&gt;();

&nbsp;&nbsp;&nbsp;&nbsp;@Override
&nbsp;&nbsp;&nbsp;&nbsp;public Type&lt;Handler&gt; getAssociatedType() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return TYPE;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;@Override
&nbsp;&nbsp;&nbsp;&nbsp;protected void dispatch(Handler handler) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handler.onEvent(this);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;public interface Handler extends EventHandler {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void onEvent(PingEvent event);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
With the new approach, the resulting &#8220;PingEvent&#8221; would look like this:<br />
<pre><code>
public interface PingEvent extends Event&lt;Void&gt; {}
</code></pre><br />
If you are wondering why not to avoid defining the &#8220;PingEvent&#8221; class itself, two are the reasons:</p>
<ul>
<li>First, the events have the same payload type (Void in this case), and therefore we need explicit types to distinguish the ping event from the pong event. This also happens in CDI where we would define explicit payload types instead.</li>
<li>Second, in this implementation we will be using GWT&#8217;s deferred binding and, during the code generation phase, we need to know the parametrized type. Because of Java generics limitations, for this last it is necessary to define a new parametrized type which captures the payload type in its type parameter (wrapper).</li>
</ul>
<p>Did I convince you? If I did, let&#8217;s go on and see how to use deferred binding and dependency injection to achieve it.</p>
<h3>Letting GWT generate the boilerplate</h3>
<p>Deferred binding in GWT is a really powerful feature implemented in the GWT compiler to substitute class implementations depending in environment properties like the user agent. This way, while the developer has the illusion of using the same classes independently of the browser, the deferred binding mechanism allows to select the adequate implementation for each browser.</p>
<p>Far from being static, deferred binding allows to generate code on the fly by mean of a generator class. This happens when the developer invokes the &#8220;GWT.create()&#8221; method on an interface. For the operation to succeed, a generator for the interface must exist. This will be invoked during GWT compilation time and its result (one or more Java classes) will be compiled and linked into the resulting JS application.</p>
<p>To configure which generator should be used for which interface, it is necessary to add a new &#8220;generate-with&#8221; entry in the GWT module descriptor. In the case of the demo application, the following code has been added to the &#8220;PingPong.gwt.xml&#8221; file:<br />
<pre><code>
...
&nbsp;&nbsp;&nbsp;&nbsp;&lt;generate-with class=&quot;com.canoo.gwt.events.server.eventsfwk.EventGenerator&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;when-type-assignable class=&quot;com.canoo.gwt.events.client.eventsfwk.Event&quot;/&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/generate-with&gt;
...
</code></pre></p>
<p>The contents of the &#8220;Event&#8221; and &#8220;EventGenerator&#8221; classes are the following:<br />
<pre><code>
public interface Event&lt;T&gt; {
&nbsp;&nbsp;&nbsp;&nbsp;public void fire(T payload);

&nbsp;&nbsp;&nbsp;&nbsp;public void dispatchTo(Handler&lt;T&gt; callback);

&nbsp;&nbsp;&nbsp;&nbsp;public interface Handler&lt;T&gt; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void onEvent(T payload);
&nbsp;&nbsp;&nbsp;&nbsp;}
}

public class EventGenerator extends Generator {
&nbsp;&nbsp;&nbsp;&nbsp;private static final String EVENT_IMPL = &quot;EventImpl&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;private static final CodeTemplate GWT_SIMPLE_EVENT_TEMPLATE = new GwtSimpleEventTemplate();
&nbsp;&nbsp;&nbsp;&nbsp;private static final CodeTemplate EVENT_IMPL_TEMPLATE = new EventImplTemplate();

&nbsp;&nbsp;&nbsp;&nbsp;@Override
&nbsp;&nbsp;&nbsp;&nbsp;public String generate(TreeLogger treeLogger, GeneratorContext generatorContext, String typeName) throws UnableToCompleteException {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TypeOracle typeOracle = generatorContext.getTypeOracle();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JClassType parentEventType = typeOracle.findType(Event.class.getName());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JClassType eventType = typeOracle.findType(typeName);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JClassType payloadType = findPayloadType(eventType, parentEventType);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String gwtSimpleEventImplName = generateGwtSimpleEvent(treeLogger, generatorContext, eventType, payloadType);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String eventImplTypeName = generateEventImpl(treeLogger, generatorContext, eventType, payloadType, gwtSimpleEventImplName);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return qualifyInGwtEvents(eventImplTypeName);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private static JClassType findPayloadType(JClassType eventType, JClassType parentEventType) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JClassType interfaces[] = eventType.getImplementedInterfaces();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (JClassType anInterface : interfaces) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ((anInterface instanceof JParameterizedType) &amp;&amp; (anInterface.getErasedType() == parentEventType.getErasedType())) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JParameterizedType parametrizedType = (JParameterizedType) anInterface;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return parametrizedType.getTypeArgs()[0];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new IllegalStateException(&quot;Payload type not found for: &#039;&quot; + eventType + &quot;&#039;.&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private String generateGwtSimpleEvent(TreeLogger treeLogger, GeneratorContext generatorContext, JClassType eventType, JClassType payloadType) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String gwtSimpleEventImplName = getGwtSimpleEventImplName(eventType);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintWriter gwtSimpleEventPrintWriter = generatorContext.tryCreate(treeLogger, getEventsPackageName(), gwtSimpleEventImplName);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GWT_SIMPLE_EVENT_TEMPLATE.generate(gwtSimpleEventPrintWriter, getEventsPackageName(), gwtSimpleEventImplName, payloadType.getName());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;generatorContext.commit(treeLogger, gwtSimpleEventPrintWriter);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return gwtSimpleEventImplName;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private String generateEventImpl(TreeLogger treeLogger, GeneratorContext generatorContext, JClassType eventType, JClassType payloadType, String gwtSimpleEventImplName) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String eventImplTypeName = getEventImplTypeName(eventType);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String eventTypeName = getEventTypeName(eventType);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintWriter eventPrintWriter = generatorContext.tryCreate(treeLogger, getEventsPackageName(), eventImplTypeName);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EVENT_IMPL_TEMPLATE.generate(eventPrintWriter, getEventsPackageName(), eventType.getQualifiedSourceName(), eventImplTypeName, eventTypeName, gwtSimpleEventImplName, payloadType.getName());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;generatorContext.commit(treeLogger, eventPrintWriter);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return eventImplTypeName;
&nbsp;&nbsp;&nbsp;&nbsp;}

... (some methods omitted)
</code></pre></p>
<p>While the &#8220;Event&#8221; class contains the interface definition for our events and lives in the &#8220;client&#8221; package of our GWT application, the &#8220;EventGenerator&#8221; is a GWT specific class that resides in the &#8220;server&#8221; package of our GWT application. In order to compile the code, we will need to add a new dependency to our project: &#8220;gwt-dev-&lt;version&gt;.jar&#8221;.</p>
<p>This generator class, by mean of some templates included in the source code, generates the boilerplate classes and saves us the tedious work of writing these classes. If you want to see this in detail, please refer to the &#8220;eventsfwk&#8221; packages in the client and server part of the application (source code <a href='http://www.canoo.com/blog/wp-content/uploads/2011/06/Part1.zip'>here</a>).</p>
<h3>Using GIN to bring everything together</h3>
<p>Following what I mentioned until now, it seems that if we define an interface extending the &#8220;Event&#8221; interface (ex: &#8220;SampleEvent&#8221;) and we use the generator to generate and wire the code by mean of &#8220;GWT.create(SampleEvent.class)&#8221;, we should be able to use the result as shown in the &#8220;GWTSample&#8221; execute method. Not really.</p>
<p>One piece that is missing in the puzzle is which event bus should the underlying GWT events use. We could create an own one without loosing functionality but a better thing that we can do is to provide an injection point and provide a mechanism to make our events participate in the application&#8217;s dependency injection context and use the application&#8217;s event bus instead of an own instance.</p>
<p>To achieve this, we have created a GIN module in the &#8220;eventsfwk&#8221; package:<br />
<pre><code>
public class EventsModule extends AbstractGinModule {
&nbsp;&nbsp;&nbsp;&nbsp;@Override
&nbsp;&nbsp;&nbsp;&nbsp;protected void configure() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;requestStaticInjection(GwtSimpleEvent.class);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre></p>
<p>This module should be installed in the application&#8217;s dependency context and it will inject an static field in the &#8220;GwtSimpleEvent&#8221; class which is the event bus used for our improved events. To install it in our application, the injection module has been modified like this (note the line &#8220;install(EventsModule.class);&#8221;):<br />
<pre><code>
@GinModules(Module.class)
public interface Injector extends Ginjector {
&nbsp;&nbsp;&nbsp;&nbsp;public static class Module extends AbstractGinModule {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Override
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;protected void configure() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;install(new EventsModule());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;install(new GinFactoryModuleBuilder().build(Factory.class));

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bindConstant().annotatedWith(named(&quot;PingText&quot;)).to(&quot;Ping&quot;);

... (code omitted)
}
</code></pre></p>
<p>This means that, when the dependency injection module is initialized at the beginning of the application execution, the previously configured event bus will be statically injected in the &#8220;GwtSimpleEvent&#8221; class used by all the generated event classes.</p>
<p>I can not read minds, but maybe you are also wondering why the event fields annotated with &#8220;@Inject&#8221; get generated and injected. All in all, we don&#8217;t call anywhere &#8220;GWT.create()&#8221; to use the generator class.</p>
<p>Well, we are not doing it but GIN does. Whenever GIN finds a dependency (something annotated with &#8220;@Inject&#8221;) and that dependency has not explicitly been configured (bound in the GIN modules), GIN invokes &#8220;GWT.create()&#8221; as fallback strategy. This simple mechanism allows us to generate and inject our event in only one step.</p>
<p>Wow! This post has been &#8220;hardcore&#8221; GWT, don&#8217;t you think? Maybe even too &#8220;hardcore&#8221;. Please, just let me know if you like it and what do you think about the approach.</p>
<p>See you in a future post!</p>
<p>The source code for the initial version of the application can be downloaded <a href='http://www.canoo.com/blog/wp-content/uploads/2011/06/Part0.zip'>here</a>. The final version <a href='http://www.canoo.com/blog/wp-content/uploads/2011/06/Part1.zip'>here</a>. To see any of the applications working, unzip the corresponding file, change to the folder where the Maven pom file is stored and type the command: &#8220;mvn clean gwt:run&#8221;. After the GWT &#8220;Development mode&#8221; application starts, click on the &#8220;Launch Default Browser&#8221; button.</p>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2011/07/04/code-generation-in-gwt-with-deferred-binding-cdi-like-events/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2011/07/04/code-generation-in-gwt-with-deferred-binding-cdi-like-events/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>GWT Dependency Injection recipes using GIN (III)</title>
		<link>http://www.canoo.com/blog/2011/06/20/gwt-dependency-injection-recipes-using-gin-iii/</link>
		<comments>http://www.canoo.com/blog/2011/06/20/gwt-dependency-injection-recipes-using-gin-iii/#comments</comments>
		<pubDate>Mon, 20 Jun 2011 10:25:32 +0000</pubDate>
		<dc:creator>alberto</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Sample Apps]]></category>
		<category><![CDATA[alberto]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[GIN]]></category>

		<guid isPermaLink="false">http://www.canoo.com/blog/?p=2161</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/06/20/gwt-dependency-injection-recipes-using-gin-iii/";</script>code { font-size: 12px; overflow: auto; } This is the third part of a series about Dependency Injection in Google Web Toolkit using GIN. If you have not yet read the first and second parts, you maybe should do it before reading this third and last article. In this article, we will introduce two GIN [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/06/20/gwt-dependency-injection-recipes-using-gin-iii/";</script><style type="text/css">
    code {
        font-size: 12px;
        overflow: auto;
    }
</style>
<p>This is the third part of a series about Dependency Injection in Google Web Toolkit using GIN. If you have not yet read the <a href='http://www.canoo.com/blog/2011/04/05/gwt-dependency-injection-recipes-using-gin/'>first</a> and <a href='http://www.canoo.com/blog/2011/06/14/gwt-dependency-injection-recipes-using-gin-ii/'>second</a> parts, you maybe should do it before reading this third and last article.</p>
<p>In this article, we will introduce two GIN features: &#8220;constants binding&#8221; and &#8220;static injection&#8221;.<br />
To do this with an example, let&#8217;s refactor the &#8220;serve button&#8221; in the &#8220;Simulator&#8221; main class of the second part into an own class:<br />
<pre><code>
...
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final Button button = new Button(&quot;Serve!&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;button.addClickHandler(new ClickHandler() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void onClick(ClickEvent event) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;button.setVisible(false);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;injector.getRacket().serve();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
...
</code></pre></p>
<p>Here the new extracted &#8220;ServeView&#8221; class:<br />
<pre><code>
public class ServeView extends Button {
&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;public ServeView(final Racket racket, @Named(&quot;ServeText&quot;) String serveText) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(serveText);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addClickHandler(new ClickHandler() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void onClick(ClickEvent event) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setVisible(false);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;racket.serve();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre></p>
<p>As you can see, the constructor of this new class is annotated with the &#8220;@Inject&#8221; annotation and receives two parameters. One of them is the racket and the other is the button&#8217;s label. In the case of the racket, it is already defined as an injectable dependency in the injector interface and in the case of the button&#8217;s label, it should be a configurable constant.<br />
GIN, by mean of &#8220;constants binding&#8221;, performs the binding of injectable dependencies to constants.<br />
Because the type of the &#8220;serveText&#8221; is String, we need to be more specific here and therefore we have annotated this dependency with the &#8220;@Named&#8221; annotation. To configure the constant value to bind to, we need to add the following line into our &#8220;InjectorModule&#8221; class:<br />
<pre><code>
...
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bindConstant().annotatedWith(named(&quot;ServeText&quot;)).to(&quot;Serve!&quot;);
...
</code></pre></p>
<p>To let the other constants in the application be configured in the same way and to introduce the last GIN feature (&#8220;static injection&#8221;), let&#8217;s now modify the &#8220;Simulator&#8221; main class as follows:<br />
<pre><code>
public class Simulator implements EntryPoint {
&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;static ServeView SERVE_VIEW;

&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;static AssistedInjectionFactory FACTORY;

&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;@Named(&quot;PingText&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;static String PING_TEXT;

&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;@Named(&quot;PongText&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;static String PONG_TEXT;

&nbsp;&nbsp;&nbsp;&nbsp;public void onModuleLoad() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;initInjection();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;buttonSlot&quot;).add(SERVE_VIEW);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;pingSlot&quot;).add(FACTORY.createPingPongView(PING_TEXT, PingEvent.class));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;pongSlot&quot;).add(FACTORY.createPingPongView(PONG_TEXT, PongEvent.class));
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private void initInjection() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GWT.create(Injector.class);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre></p>
<p>And our &#8220;InjectorModule&#8221; like this:<br />
<pre><code>
public class InjectorModule extends AbstractGinModule {
&nbsp;&nbsp;&nbsp;&nbsp;@Override
&nbsp;&nbsp;&nbsp;&nbsp;protected void configure() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;install(new GinFactoryModuleBuilder().build(AssistedInjectionFactory.class));

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bindConstant().annotatedWith(named(&quot;PingText&quot;)).to(&quot;Ping&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bindConstant().annotatedWith(named(&quot;PongText&quot;)).to(&quot;Pong&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bindConstant().annotatedWith(named(&quot;ServeText&quot;)).to(&quot;Serve!&quot;);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;requestStaticInjection(Simulator.class);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre></p>
<p>The &#8220;static injection&#8221; allows us to inject the static fields of a class. This happens by mean of the method &#8220;requestStaticInjection&#8221; of the GIN module class and, in our application, we use it to inject the static dependencies of the &#8220;Simulator&#8221; class.</p>
<p>This way, even when the &#8220;Simulator&#8221; class does not participate in the dependency injection context, its static members will be injected making the explicit references to the injector not required.</p>
<p>This has the side-effect of making the &#8220;Injector&#8221; getters no more necessary leaving our injector interface &#8220;empty&#8221;:<br />
<pre><code>
@GinModules(InjectorModule.class)
public interface Injector extends Ginjector {
}
</code></pre></p>
<p>Please note that, in order to get the dependency injection context configured and initialized, it is required to instantiate the &#8220;Injector&#8221; interface by mean of the &#8220;GWT.create()&#8221; method as part of the application initialization. This is done in our case within the &#8220;initInjection&#8221; method in the &#8220;Simulator&#8221; main class.</p>
<h3>Conclusions</h3>
<p>If you have read the 3 articles of this series, maybe you can remember how our demo application looked like at the beginning. While the functionality and appearance has not changed at all, the intern structure of the application is completely different.</p>
<p>In my opinion, dependency injection allows a much cleaner structure, enables configuring the application in an elegant and easy way and, when used together with an event bus, produces low-coupled high-modular applications.</p>
<p>Of course, &#8220;there is no free lunch&#8221; and using GIN means also that the development team has to learn new concepts and introduce a new framework in the application. Anyway,  dependency injection is an already proven concept and GIN is being intensively used in most of the new GWT projects and GWT frameworks.</p>
<p>While some minimal application structure optimizations could still be applied to the application, the thing that I still find improvable in it is the verbosity of the GWT events definitions. This requires writing some boilerplate classes to ensure the type safety (event handlers), and also the definition of the own event classes is too verbose, being possible to generate them automatically in most of the cases by using either GWT&#8217;s deferred binding or a JDK 6 annotation processor. If you know how the events in CDI are defined, very probably you will agree with me that such an approach would be much more elegant and concise.</p>
<p>A solution for that, even when it requires dependency injection, is more related to other topics and therefore a subject for a different thread. If you got curious, stay tuned for a future post on code generation in GWT! <img src='http://www.canoo.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   </p>
<p>The source code of the application can be downloaded <a href='http://www.canoo.com/blog/wp-content/uploads/2011/06/Part3.zip'>here</a>. To see the application working, unzip the file, change to the folder where the Maven pom file is stored and type the command: &#8220;mvn clean gwt:run&#8221;. After the GWT &#8220;Development mode&#8221; application starts, click on the &#8220;Launch Default Browser&#8221; button.</p>
<p>As always, I hope that you enjoyed reading this series of articles as much as I did writing it and hope to see you soon in a future post!</p>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2011/06/20/gwt-dependency-injection-recipes-using-gin-iii/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2011/06/20/gwt-dependency-injection-recipes-using-gin-iii/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>GWT Dependency Injection recipes using GIN (II)</title>
		<link>http://www.canoo.com/blog/2011/06/14/gwt-dependency-injection-recipes-using-gin-ii/</link>
		<comments>http://www.canoo.com/blog/2011/06/14/gwt-dependency-injection-recipes-using-gin-ii/#comments</comments>
		<pubDate>Tue, 14 Jun 2011 10:43:52 +0000</pubDate>
		<dc:creator>alberto</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Sample Apps]]></category>
		<category><![CDATA[alberto]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[GIN]]></category>

		<guid isPermaLink="false">http://www.canoo.com/blog/?p=2101</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/06/14/gwt-dependency-injection-recipes-using-gin-ii/";</script>code { font-size: 12px; overflow: auto; } This is the second part of a series about Dependency Injection in Google Web Toolkit using GIN. If you have not yet read the first part, there we explained how to integrate GIN in an existing GWT sample application. In this second part, we will continue enhancing the [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/06/14/gwt-dependency-injection-recipes-using-gin-ii/";</script><style type="text/css">
    code {
        font-size: 12px;
        overflow: auto;
    }
</style>
<p>This is the second part of a series about Dependency Injection in Google Web Toolkit using GIN. If you have not yet read the <a href='http://www.canoo.com/blog/2011/04/05/gwt-dependency-injection-recipes-using-gin/'>first part</a>, there we explained how to integrate GIN in an existing GWT sample application. In this second part, we will continue enhancing the sample application while explaining other types of injection supported by GIN.</p>
<h3>Encapsulating the event bus</h3>
<p>In the first part of this series, we configured the GWT event bus used in the original application to be injected in some of the application elements. Using an event bus is a &#8220;best practice&#8221; that helps communicating the different components in the application while keeping a low coupling between them. The point here was to show how to declare and configure a first dependency using the &#8220;injector&#8221; and the &#8220;module&#8221;. Because the class &#8220;SimpleEventBus&#8221; is provided by GWT, it was not possible to annotate the code of the class with &#8220;@Singleton&#8221; to set the &#8220;injection scope&#8221;. Instead, we use the Java DSL provided by GIN.<br />
In order to see how to do the same with annotations, let&#8217;s use now an application class (instead of a framework class) where we can use the annotations.<br />
As you probably noticed, we are using the event bus to emulate the &#8220;ping-pong&#8221; strokes. Instead of such a technical artifact, let&#8217;s create a &#8220;racket&#8221;:<br />
<pre><code>
@Singleton
public class Racket {
&nbsp;&nbsp;&nbsp;&nbsp;private final EventBus fEventBus;

&nbsp;&nbsp;&nbsp;&nbsp;private GwtEvent&lt;?&gt; fLastEvent;

&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;public Racket(EventBus eventBus) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fEventBus = eventBus;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;public void serve() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fLastEvent = null;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hit();
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;public void hit() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fEventBus.fireEvent(getNextEvent());
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private GwtEvent&lt;?&gt; getNextEvent() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (fLastEvent == null || fLastEvent instanceof PongEvent) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fLastEvent = new PingEvent();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fLastEvent = new PongEvent();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return fLastEvent;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;public void onStroke(Class&lt;?&gt; pingPongEvent, final Command command) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (pingPongEvent == PingEvent.class) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fEventBus.addHandler(PingEvent.TYPE, new PingEventHandler() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void onEvent(PingEvent event) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command.execute();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fEventBus.addHandler(PongEvent.TYPE, new PongEventHandler() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void onEvent(PongEvent event) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command.execute();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
As you can see at the code, our class is annotated with &#8220;@Singleton&#8221; to indicate the injection scope and &#8220;@Inject&#8221; to get the event bus injected in the constructor. The class is a more adequate wrapper for the event handling and offers three methods: &#8220;serve&#8221;, &#8220;hit()&#8221; and &#8220;onStroke()&#8221;.<br />
To configure the injection please adjust the &#8220;injector&#8221; class like this:<br />
<pre><code>
@GinModules(InjectorModule.class)
public interface Injector extends Ginjector {
&nbsp;&nbsp;&nbsp;&nbsp;public static final Injector INSTANCE = GWT.create(Injector.class);

&nbsp;&nbsp;&nbsp;&nbsp;public Racket getRacket();

&nbsp;&nbsp;&nbsp;&nbsp;public PingView getPingView();

&nbsp;&nbsp;&nbsp;&nbsp;public PongView getPongView();
}
</code></pre><br />
This time, we only need to declare the dependency in the &#8220;Injector&#8221; interface (the method will be invoked from the &#8220;Simulator&#8221; class) but no additional configuration is required and therefore no changes should be made in the &#8220;InjectorModule&#8221; class.<br />
Because now the event bus will not be directly accessed from the &#8220;Simulator&#8221; class, we have removed it from the &#8220;Injector&#8221; interface. But, let&#8217;s see the rest of the code changes:<br />
<pre><code>
public class PingView extends Label {
&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;public PingView(final Racket racket) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;racket.onStroke(PongEvent.class, new Command() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void execute() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setText(&quot;Pong&quot;);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new Timer() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void run() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setText(&quot;&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;racket.hit();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}.schedule(1000);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
The &#8220;PongView&#8221; class is almost identical and will not be shown here.<br />
<pre><code>
public class Simulator implements EntryPoint {
&nbsp;&nbsp;&nbsp;&nbsp;public void onModuleLoad() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final Injector injector = Injector.INSTANCE;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;pingSlot&quot;).add(injector.getPingView());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;pongSlot&quot;).add(injector.getPongView());

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final Button button = new Button(&quot;Serve!&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;button.addClickHandler(new ClickHandler() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void onClick(ClickEvent event) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;button.setVisible(false);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;injector.getRacket().serve();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;buttonSlot&quot;).add(button);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
Recapitulating what we have seen so far:</p>
<ul>
<li>GIN allows to define &#8220;injectors&#8221; by mean of interfaces (&#8220;Injector&#8221; interface) that can be configured using one or more &#8220;modules&#8221; each (&#8220;InjectorModule&#8221; class).</li>
<li>If we need to configure an injection aspect we can use annotations in the class code or the Java DSL in the module.</li>
<li>In at least one point of the application we will need to access the &#8220;injector&#8221; to retrieve a top-level injected object (ex: &#8220;Racket&#8221;, &#8220;PingView&#8221;, &#8220;PongView&#8221; ). For these objects to be accessible through the injector, we need to declare them in the &#8220;injector&#8221; interface.</li>
<li>Any other dependency that is declared to be injected (annotated with @Inject) will be resolved by GIN using the algorithm described in the first part of this series and does not need to be declared in the &#8220;injector&#8221; interface (ex: &#8220;EventBus&#8221;).</li>
</ul>
<p>If you are familiar with GUICE, you probably already know other ways of configuring dependencies and injection. Let&#8217;s see in the next point how to avoid the repetition in the views by using &#8220;assisted injection&#8221;.</p>
<h3>Using assisted injection to avoid code repetition</h3>
<p>One of the main applications of dependency injection is to avoid coding factory classes and singletons and also centralizing the application&#8217;s configuration values. This is a very powerful tool to configure application-wide objects like services, which typically are either &#8220;singletons&#8221; or &#8220;prototypes&#8221; and scarce.</p>
<p>When working with data, persistency tools like JPA assist us with the task of generating Java objects for our numerous domain model entities. These objects have normally no behavior or just a little which is typically coded in the &#8220;entity&#8221; classes. But, what happens if we want to inject our services in some of these entities to have a richer domain model? Normally, we have to tackle this task ourselves and inject the services by hand. This happens because such objects do not by default participate in the injection context (unless we are using JEE or weaving).</p>
<p>While this not a difficult task, it is a purely programmatic one and breaks one of the principles of dependency injection: declarative dependencies definition.<br />
In GUICE (and therefore in GIN) there is a concept called &#8220;assisted injection&#8221; that helps us to create objects which require dependencies from the injection context but also instance specific property values. Basically, it consists of using a factory interface with methods to instantiate the objects as managed beans (injected objects) receiving each method only the object&#8217;s specific properties as parameters. The object constructors receive of course not only the object properties but also the dependencies who should be retrieved from the dependency injection context.</p>
<p>Because an image is worth a thousand words, let&#8217;s see this with an example. What we would like to achieve here is to have only one view class (&#8220;PingPongView&#8221;) instead of two specific ones that have almost the same code. If we inspect both view classes, the differences are: to which event each reacts and which text each shows. If we refactor these values as constructor parameters, we have the following resulting class:<br />
<pre><code>
public class PingPongView extends Label {
&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;public PingPongView(final Racket racket, @Assisted final String text, @Assisted Class&lt;? extends GwtEvent&gt; eventClass) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;racket.onStroke(eventClass, new Command() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void execute() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setText(text);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new Timer() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void run() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setText(&quot;&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;racket.hit();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}.schedule(1000);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
Probably you already noticed the @Assisted annotation in the new extracted parameters. It just indicates GIN that the non-annotated parameters should be resolved as dependencies and that the annotated ones should be provided though parameters in a factory method of the assisted injection factory:<br />
<pre><code>
public interface AssistedInjectionFactory {
&nbsp;&nbsp;&nbsp;&nbsp;public PingPongView createPingPongView(String text, Class&lt;? extends GwtEvent&gt; eventClass);
}
</code></pre><br />
To install this factory in our dependency injection context we need to modify our GIN module class like this:<br />
<pre><code>
public class InjectorModule extends AbstractGinModule {
&nbsp;&nbsp;&nbsp;&nbsp;@Override
&nbsp;&nbsp;&nbsp;&nbsp;protected void configure() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;install(new GinFactoryModuleBuilder().build(AssistedInjectionFactory.class));
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
And in the injector class, we have to expose the factory as any other dependency:<br />
<pre><code>
@GinModules(InjectorModule.class)
public interface Injector extends Ginjector {
&nbsp;&nbsp;&nbsp;&nbsp;public static final Injector INSTANCE = GWT.create(Injector.class);

&nbsp;&nbsp;&nbsp;&nbsp;public Racket getRacket();
&nbsp;&nbsp;&nbsp;&nbsp;public AssistedInjectionFactory getFactory();
}
</code></pre><br />
And last but not least, let&#8217;s use the assisted injection in our application:<br />
<pre><code>
...
&nbsp;&nbsp;&nbsp;&nbsp;public void onModuleLoad() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final Injector injector = Injector.INSTANCE;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final AssistedInjectionFactory factory = injector.getFactory();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;pingSlot&quot;).add(factory.createPingPongView(&quot;Ping&quot;, PingEvent.class));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;pongSlot&quot;).add(factory.createPingPongView(&quot;Pong&quot;, PongEvent.class));
...
</code></pre></p>
<p>In this article, we have applied new dependency injection recipes to our GWT demo application. I hope that they can help you give a better structure to your GWT applications and also learn dependency injection features and its &#8220;best practices&#8221;.</p>
<p>The source code of the application can be downloaded <a href='http://www.canoo.com/blog/wp-content/uploads/2011/06/Part2.zip'>here</a>. To see the application working, unzip the file, change to the folder where the Maven pom file is stored and type the command: &#8220;mvn clean gwt:run&#8221;. After the GWT &#8220;Development mode&#8221; application starts, click on the &#8220;Launch Default Browser&#8221; button.</p>
<p>The other parts of this series can be found <a href='http://www.canoo.com/blog/2011/04/05/gwt-dependency-injection-recipes-using-gin'>here</a> and <a href='http://www.canoo.com/blog/2011/06/20/gwt-dependency-injection-recipes-using-gin-iii'>here</a>.</p>
<p>I hope that you enjoyed reading this article as much as I did writing it.<br />
See you soon!</p>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2011/06/14/gwt-dependency-injection-recipes-using-gin-ii/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2011/06/14/gwt-dependency-injection-recipes-using-gin-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hackergarten at Canoo 27.05.2011</title>
		<link>http://www.canoo.com/blog/2011/05/26/hackergarten-at-canoo-27-05-2011/</link>
		<comments>http://www.canoo.com/blog/2011/05/26/hackergarten-at-canoo-27-05-2011/#comments</comments>
		<pubDate>Thu, 26 May 2011 13:23:23 +0000</pubDate>
		<dc:creator>Hamlet</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.canoo.com/blog/?p=2142</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/05/26/hackergarten-at-canoo-27-05-2011/";</script>Don&#8217;t forget! Tomorrow night is Hackergarten. On the last Friday of the month Canoo opens our doors for an open source programming group called Hackergarten. Our goal is to contribute in some way to open source software, to meet new friends, and to generally have a good time. In the past we have made contributions [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/05/26/hackergarten-at-canoo-27-05-2011/";</script><p>Don&#8217;t forget! Tomorrow night is <a href="http://www.hackergarten.net">Hackergarten</a>.</p>
<p>On the last Friday of the month Canoo opens our doors for an open source programming group called Hackergarten. Our goal is to contribute in some way to open source software, to meet new friends, and to generally have a good time. In the past we have made contributions to Groovy, Gradle, NetBeans, Griffon, GPars&#8230; and the list goes on and on. Canoo provides food, drinks, and wifi. Please note, there is a Hackergarten tomorrow! If you&#8217;re coming tomorrow then please let me know because I am cooking American hamburgers for everyone and need to know how much food to buy. To learn more about Hackergarten you can see our main site at <a href="http://hackergarten.net/">http://hackergarten.net/</a> or join the low-volume mailing list at <a href="http://groups.google.com/group/hackergarten">http://groups.google.com/group/hackergarten</a>. </p>
<p>This month we have an open topic. There is always interest in hacking on Gradle and other Groovy technologies, and contributing to IntelliJ IDEA was also proposed. The topic can be anything you&#8217;d like&#8230; so please just show up with ideas. As always, Canoo provides food and drink, and the party starts at 5 PM. We are grilling hamburgers tomorrow, so if you&#8217;d like one please respond to the active <a href="http://groups.google.com/group/hackergarten/browse_frm/thread/dcacf4c88ed21409">mailing list thread</a>. </p>
<p>I hope to see you tomorrow night! </p>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2011/05/26/hackergarten-at-canoo-27-05-2011/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2011/05/26/hackergarten-at-canoo-27-05-2011/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Git training at Canoo with Matthew McCullough</title>
		<link>http://www.canoo.com/blog/2011/05/06/git-training-at-canoo-with-matthew-mccullough/</link>
		<comments>http://www.canoo.com/blog/2011/05/06/git-training-at-canoo-with-matthew-mccullough/#comments</comments>
		<pubDate>Fri, 06 May 2011 14:55:48 +0000</pubDate>
		<dc:creator>Andreas Hölzl</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[training]]></category>

		<guid isPermaLink="false">http://www.canoo.com/blog/?p=2118</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/05/06/git-training-at-canoo-with-matthew-mccullough/";</script>Yesterday I was lucky to attend an excellent git training at Canoo headquarters in Basel. Matthew McCullough of Ambient Ideas, Denver USA was invited by Canoo to share his expert Git knowledge with the people of Basel and nearby. Matthew is a first class trainer, speaking about Git at many conferences and also providing the [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/05/06/git-training-at-canoo-with-matthew-mccullough/";</script><p>Yesterday I was lucky to attend an excellent <a href="http://git-scm.com/">git</a> training at Canoo headquarters in Basel. Matthew McCullough of <a href="http://ambientideas.com/">Ambient Ideas</a>, Denver USA was invited by Canoo to share his expert Git knowledge with the people of Basel and nearby. Matthew is a first class trainer, speaking about Git at many conferences and also providing the online git training (<a href="https://github.com/training/online">https://github.com/training/online</a>).</p>
<p/>
The training was open to everyone and very well attended, probably because it was very affordable. Over the one-day workshop &#8220;fast-forward&#8221; became a new meaning to me. Starting at the very beginning, such as where to get, install and host git repositories, Matthew quickly passed over all the base concepts and commands including comprehensive exercises. The workshop was rounded off by more advanced concepts like how to handle your revision graph in every imaginable way, including highlights like &#8220;fast-forward&#8221; and &#8220;octopus&#8221; <a href="http://www.kernel.org/pub/software/scm/git/docs/git-merge.html">merges</a>, &#8220;<a href="http://www.kernel.org/pub/software/scm/git/docs/git-cherry-pick.html">cherry-picking</a>&#8220;, &#8220;<a href="http://www.kernel.org/pub/software/scm/git/docs/git-rebase.html">rebasing</a>&#8221; and &#8220;<a href="http://www.kernel.org/pub/software/scm/git/docs/git-bisect.html">bisecting</a>&#8220;. The hours flew by and over the full day Matthew never lowered his fast pace not in the slightest.</p>
<p/>
<a href="http://www.canoo.com/blog/wp-content/uploads/2011/05/IMG_0031.jpg"><img src="http://www.canoo.com/blog/wp-content/uploads/2011/05/IMG_0031.jpg" alt="" title="git training @Canoo with Matthew McCullough" width="600" height="450" class="alignnone size-full wp-image-2124" /></a></p>
<p/>
Before attending the training my perception of git was merely of &#8220;yet another version control system, but distributed&#8221;, but this changed significantly afterwards. Now I believe the distributed nature of Git is not the most important aspect. Git&#8217;s value derives from its most fundamental concept: make the handling of branches easy and treat all branches equally. This opens up the door for a much more fine grained revision management, having branches literally for single features or experiments. Treating branches equally opens up the door for distribution on a larger scale &#8230; </p>
<p/>
Liberating branching wouldn&#8217;t work if you didn&#8217;t have tools to bring back together you and your colleagues&#8217; work. This is another aspect where git excels and impresses. Similarity detection, multi-branch merges and rebasing techniques, together with sophisticated revision graph inspection tools, help you with this normally non-trivial task. As an interesting side aspect Matthew also talked about the design principals behind git, which are very cunning. Git actually tracks content, not files and is designed to be very reliable and fast.</p>
<p/>
If possible I&#8217;ll switch to git for my next software projects. Thank you Matthew for the excellent training and multiple insights.</p>
<p/>
Matthew can be reached at twitter: <a href="http://twitter.com/#!/matthewmccull">@matthewmccull</a> and maintains a blog under: <a href="http://ambientideas.com/blog/">http://ambientideas.com/blog/</a>. He is the author of a huge amount of git tutorials, documentation and related links: <a href="http://bit.ly/gitlinks">http://bit.ly/gitlinks</a>. </p>
<p/>
As a suggestion to programmers: if you want to have a famous speaker at your office then just send them an email and ask. At a minimum they will be thankful; at best they will come and speak at your office! That&#8217;s what we did, and the result was great. </p>
<p/>
Did you not know that Canoo was offering a Git training? Canoo is offering a lot of free (or very affordable) public events. Basel does not have a permanent Java User Group, but we hold &#8220;Reading Groups&#8221; every few months. The best way to be kept informed is to follow <a href="http://twitter.com/#!/canoo">@Canoo</a> on Twitter or send an email to info@canoo.com. Also, we host Hackergarten (<a href="http://www.hackergarten.net">http://www.hackergarten.net</a>) every month, which is your chance to come and hack on open source software with some great friends. We have pizza and drinks on a Friday and then try to make a patch or contribution to an open source project. Our goal is to make Basel a great technology community. Care to help us make it so?</pre>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2011/05/06/git-training-at-canoo-with-matthew-mccullough/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2011/05/06/git-training-at-canoo-with-matthew-mccullough/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Command Line Usage for Diff Viewer from IntelliJ IDEA, RubyMine, and PyCharm</title>
		<link>http://www.canoo.com/blog/2011/05/04/command-line-usage-for-diff-viewer-from-intellij-idea-rubymine-and-pycharm/</link>
		<comments>http://www.canoo.com/blog/2011/05/04/command-line-usage-for-diff-viewer-from-intellij-idea-rubymine-and-pycharm/#comments</comments>
		<pubDate>Wed, 04 May 2011 13:23:27 +0000</pubDate>
		<dc:creator>Hamlet</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.canoo.com/blog/?p=2107</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/05/04/command-line-usage-for-diff-viewer-from-intellij-idea-rubymine-and-pycharm/";</script>code { font-size: 12px; overflow: auto; } The Diff Viewer from JetBrains is by far my favorite file diff and merge tool, and I&#8217;ve used a ton of different diff tools over the years. I finally figured out how to run the tool reliably from the command line, so now you can easily use it [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/05/04/command-line-usage-for-diff-viewer-from-intellij-idea-rubymine-and-pycharm/";</script><style type="text/css">
    code {
        font-size: 12px;
        overflow: auto;
    }
</style>
<p>
    The <a href="http://www.jetbrains.com/idea/features/local_history.html#link6">Diff Viewer</a> from <a href="http://www.jetbrains.com/">JetBrains</a> is by far my favorite file diff and merge tool, and I&#8217;ve used a ton of different diff tools over the years. I finally figured out how to run the tool reliably from the command line, so now you can easily use it as a standalone tool. The diff tool was first featured in <a href="http://www.jetbrains.com/idea/">IntelliJ IDEA</a>, is the exact same tool now seen in <a href="http://www.jetbrains.com/ruby/">RubyMine</a> and <a href="http://www.jetbrains.com/pycharm/">PyCharm</a>, and is part of the IDEA Community Edition. If you use these products then you already have it installed. If you&#8217;re on <a href="http://www.jetbrains.com/webstorm/">WebStorm</a> or <a href="http://www.jetbrains.com/phpstorm/">PHPStorm</a> (or anything else), you can still install the IDEA community edition and use the diff viewer as an external diff tool.
</p>
<p>
  The diff tool shows the lines and blocks of a file that have been added, removed or changed. And it does a good job of it, allowing you to easily move, edit, and remove lines between files. Within the IDE there are more options as well, like code completion, syntax highlighting, intentions, and more. From the command line it is a little more basic because it doesn&#8217;t have a project file sitting behind it. But it does give you the nice &#8220;accept changes&#8221; interface and you won&#8217;t have to learn and use a second, inferior diff tool.
</p>
<p>
  <strong>PLEASE NOTE</strong>: This is tested on the IDEA 10.5 EAP. If you are on an earlier version, then it works correctly as long as IDEA is <em>not</em> already open. If IDEA is open, then you&#8217;ll simply open the two files in the editor. Bear with me while I find a solution&#8230;
</p>
<p>
    So here is what I do&#8230; I wrote a shell script so that it compares two files:
</p>
<p><code>diffx file1 file2</code></p>
<p>
Which opens up the Diff Viewer. If IDEA is already opened, then it uses the running IDEA process and the window opens very quickly. If no IDEA is running then there is a few second startup time; but there are no files to parse so the startup time is only a few seconds and nothing like the startup of a full Java project.
</p>
<p>
    <a href="http://hackergarten.net/images/2011/ideadiffview.png"><img width="600px" src="http://hackergarten.net/images/2011/ideadiffview.png"  alt="screenshot"/></a></p>
<p>
    Without my shell script it is a little difficult to run. You need to change to the $IDEA_HOME/bin directory and then invoke:
</p>
<p>    <pre><code>idea.sh diff file1 file2
or
idea.exe diff file1 file2</code></pre></p>
<p>
There is also an inspect_diff.sh script you can run, but I found my method just as easy. The problem with the IDEA provided shell scripts is that file1 and file2 must be absolute paths because you&#8217;re in the IDEA/bin directory. If you&#8217;re on Windows (without cygwin) then perhaps this is the best you can do. On any system with Bash you can use my bash script (<a href="http://dl.dropbox.com/u/138156/diffx">available here</a>). Basically, the script is called &#8220;diffx&#8221;, and it handles the directories and paths correctly so that you can invoke diffx from anywhere with parameters of either relative or absolute paths. If anyone has improvements then please let me know! You&#8217;ll need to have a symlink that points ~/bin/idea/bin to the IDEA/bin directory, do a &#8220;chmod +x diffx&#8221; to give the file execute permissions, then drop it in your path (you have ~/Dropbox/bin on the path, right?). Otherwise it should all work.
</p>
<p><pre><code>#!/bin/bash

# makes sure file exists in current dir or as absolute path
getFile() {
&nbsp;&nbsp;&nbsp;&nbsp;local FILENAME=$1; local CURRENT_DIR=$2; local RESULTVAR=$3

&nbsp;&nbsp;&nbsp;&nbsp;if [ -e $FILENAME ]
&nbsp;&nbsp;&nbsp;&nbsp;then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval $RESULTVAR=$FILENAME&nbsp;&nbsp;# if file exists then use it
&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# otherwise try to discover the correct path
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local FULLFILENAME=$CURRENT_DIR&quot;/&quot;$FILENAME
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if [ ! -e $FULLFILENAME ]
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;Couldn&#039;t read $FILENAME or $FULLFILENAME.&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit 1
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fi
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval $RESULTVAR=$FULLFILENAME
&nbsp;&nbsp;&nbsp;&nbsp;fi
}

# save the current directory and then move to the IDEA dir
CURRENT_DIR=`pwd`
pushd .&nbsp;&nbsp;&gt; /dev/null

cd ~/bin/idea/bin&nbsp;&nbsp; # you may want to change this or make an environment variable

getFile $1 $CURRENT_DIR FILE1
getFile $2 $CURRENT_DIR FILE2

./idea.sh diff $FILE1 $FILE2

popd &gt; /dev/null</code></pre></p>
<p>
There are a lot of tools that allow you to configure an external diff viewer, and you can always point those tools to this diffx script to use IDEA as the tool. Also, coming in IDEA 10.5 is the ability to <a href="http://blogs.jetbrains.com/idea/2011/04/new-diff-tools/">compare directories</a> using the Diff Viewer, and this script should work for directories at that point, however I don&#8217;t have it working yet. You can read about the directory diffs over at the <a href="http://blogs.jetbrains.com/idea/2011/04/new-diff-tools/">idea blog</a> if you&#8217;d like.
</p>
<p>
    Thanks everyone, and I look forward to seeing some Bash script refactorings. This is the first bash function I&#8217;ve ever written.</p>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2011/05/04/command-line-usage-for-diff-viewer-from-intellij-idea-rubymine-and-pycharm/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2011/05/04/command-line-usage-for-diff-viewer-from-intellij-idea-rubymine-and-pycharm/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Getting Started with Spock and Groovy</title>
		<link>http://www.canoo.com/blog/2011/04/15/getting-started-with-spock-and-groovy/</link>
		<comments>http://www.canoo.com/blog/2011/04/15/getting-started-with-spock-and-groovy/#comments</comments>
		<pubDate>Fri, 15 Apr 2011 16:10:01 +0000</pubDate>
		<dc:creator>Hamlet</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.canoo.com/blog/?p=2091</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/04/15/getting-started-with-spock-and-groovy/";</script>This screencast demonstrates how to create Spock testing specifications. It covers creating basic when/then blocks, given/when/then blocks, expect/where blocks, and data tables. It explains how to create Spock file templates and Spock Live Templates for IDEA. If there are any playback issues then you might try watching it from the JetBrains.tv site, and you&#8217;re always [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/04/15/getting-started-with-spock-and-groovy/";</script><p>This screencast demonstrates how to create Spock testing specifications. It covers creating basic when/then blocks, given/when/then blocks, expect/where blocks, and data tables. It explains how to create Spock file templates and Spock Live Templates for IDEA.</p>
<p>If there are any playback issues then you might try watching it from the <a href="http://tv.jetbrains.net/videocontent/getting-started-with-spock-and-groovy">JetBrains.tv site</a>, and you&#8217;re always welcome to upvote at <a href="http://www.dzone.com/links/getting_started_with_spock_and_groovy_screencast.html">DZone</a>. </p>
<p><object width="400" height="300" id="_player" name="_player" data="http://tv.jetbrains.net/sites/default/files/flowplayer3/flowplayer-3.2.2.swf" type="application/x-shockwave-flash"><param name="movie" value="http://tv.jetbrains.net/sites/default/files/flowplayer3/flowplayer-3.2.2.swf" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="flashvars" value='config={"clip":{"baseUrl":"http://tv.jetbrains.net","scaling":"orig","autoPlay":false,"autoBuffering":true,"url":"sites/default/files/videos/converted/spock.flv"},"plugins":{"controls":{"stop":true}},"playlist":[{"baseUrl":"http://tv.jetbrains.net","scaling":"orig","autoPlay":false,"autoBuffering":true,"url":"http://tv.jetbrains.net/sites/default/files/videos/converted/spock.flv"}]}' /></object></p>
<p>Here are some useful links to read for this webcast:</p>
<ul>
<li>Spock Framework &#8211; http://code.google.com/p/spock/</li>
<li>IDEA File Templates &#8211; http://www.jetbrains.com/idea/webhelp/file-templates.html</li>
<li>IDEA Live Templates &#8211; http://www.jetbrains.com/idea/webhelp/live-templates.html</li>
</ul>
<p>To work with Spock, I use one file template and three live templates.&nbsp;</p>
<p>My file template creates a Spock Specification with the correct java package and javadoc:&nbsp;</p>
<p><a href="http://hackergarten.net/images/2011/filetemplate.png"><img alt="idea file template" width="450" height="253" src="http://hackergarten.net/images/2011/filetemplate.png" /></a></p>
<p>The text of the template is:</p>
<p><pre><code style="font-size: 12px;">#if (${PACKAGE_NAME} != &quot;&quot;)package ${PACKAGE_NAME};#end

import spock.lang.Specification

#parse(&quot;File Header.java&quot;)
class ${NAME} extends Specification {
&nbsp;&nbsp;&nbsp;&nbsp;
}</code></pre></p>
<p>My first Live Template is the when-then template:&nbsp;</p>
<p><a href="http://hackergarten.net/images/2011/whenthentemplate.png"><img alt="when then" width="350" height="401" src="http://hackergarten.net/images/2011/whenthentemplate.png" /></a></p>
<p>The text of the template is:</p>
<p><pre><code style="font-size: 12px;">def &quot;$NAME$&quot;() {
&nbsp;&nbsp;&nbsp;&nbsp;when:
&nbsp;&nbsp;&nbsp;&nbsp;$END$
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;then:
&nbsp;&nbsp;&nbsp;&nbsp;true
}</code></pre></p>
<p>The second template is the given/when/then template:&nbsp;</p>
<p><a href="http://hackergarten.net/images/2011/givenwhenthentemplate.png"><img alt="given when then template" width="350" height="438" src="http://hackergarten.net/images/2011/givenwhenthentemplate.png" /></a>&nbsp;</p>
<p>The text of the template is:</p>
<p><pre><code style="font-size: 12px;">def &quot;$NAME$&quot;() {
&nbsp;&nbsp;&nbsp;&nbsp;given:
&nbsp;&nbsp;&nbsp;&nbsp;$END$
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;when:
&nbsp;&nbsp;&nbsp;&nbsp;// TODO: add when
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;then:
&nbsp;&nbsp;&nbsp;&nbsp;true
}</code></pre></p>
<p>The last template is the expect/where template:&nbsp;</p>
<p><a href="http://hackergarten.net/images/2011/expectwheretemplate.png"><img width="350" height="401" alt="" src="http://hackergarten.net/images/2011/expectwheretemplate.png" /></a>&nbsp;</p>
<p>The text of the template is:</p>
<p><pre><code style="font-size: 12px;">def &quot;$NAME$&quot;() {
&nbsp;&nbsp;&nbsp;&nbsp;expect: 
&nbsp;&nbsp;&nbsp;&nbsp;$END$
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;where: 
&nbsp;&nbsp;&nbsp;&nbsp;// TODO add where block
}</code></pre></p>
<p>I&#8217;ve made a lot of screencasts and blog posts over the years. If you like this, then there are many ways to see the other stuff I&#8217;ve done:&nbsp;</p>
<ul>
<li>My main blog:&nbsp;<a href="http://hamletdarcy.blogspot.com">http://hamletdarcy.blogspot.com</a></li>
<li>My other JetBrains.tv posts:&nbsp;<a href="http://tv.jetbrains.net/tags/hamlet">http://tv.jetbrains.net/tags/hamlet</a></li>
<li>IDEA&nbsp;related posts on my blog:&nbsp;<a href="http://hamletdarcy.blogspot.com/search/label/IDEA">http://hamletdarcy.blogspot.com/search/label/IDEA</a></li>
<li>My screencasts on YouTube:&nbsp;<a href="http://www.youtube.com/user/HamletDRC">http://www.youtube.com/user/HamletDRC</a></li>
<li>IDEA related Posts on my work blog:&nbsp;<a href="http://www.canoo.com/blog/tag/idea/">http://www.canoo.com/blog/tag/idea/</a></li>
<li>Or follow me on Twitter:&nbsp;<a href="http://twitter.com/hamletdrc">@HamletDRC</a></li>
</ul>
<p>Phew, that&#8217;s a lot of self-promotion <img src='http://www.canoo.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Thanks for watching, and leave a comment!&nbsp;</p>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2011/04/15/getting-started-with-spock-and-groovy/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2011/04/15/getting-started-with-spock-and-groovy/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
<enclosure url="http://tv.jetbrains.net/sites/default/files/videos/converted/spock.flv" length="6292987" type="video/x-flv" />
		</item>
	</channel>
</rss>

