<?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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Alain Defrance&#039;s Blog</title>
	<atom:link href="http://alaindefrance.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://alaindefrance.wordpress.com</link>
	<description>Behind Java</description>
	<lastBuildDate>Sun, 20 Nov 2011 11:48:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='alaindefrance.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/f15636bbc4099831608d9ab4eeba5cb2?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Alain Defrance&#039;s Blog</title>
		<link>http://alaindefrance.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://alaindefrance.wordpress.com/osd.xml" title="Alain Defrance&#039;s Blog" />
	<atom:link rel='hub' href='http://alaindefrance.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Maven classpath isolation</title>
		<link>http://alaindefrance.wordpress.com/2011/11/20/maven-classpath-isolation/</link>
		<comments>http://alaindefrance.wordpress.com/2011/11/20/maven-classpath-isolation/#comments</comments>
		<pubDate>Sun, 20 Nov 2011 11:48:41 +0000</pubDate>
		<dc:creator>Alain Defrance</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://alaindefrance.wordpress.com/?p=366</guid>
		<description><![CDATA[I remember some issues I had with maven about classpath due to classpath isolation. For exemple if you try to use stax in unit test, you could be disturbed because maven already use it. Maven 3.x should fix this kind of problems but it doesn&#8217;t look like changes effective at compile time yet. You still [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=366&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I remember some issues I had with maven about classpath due to classpath isolation.<br />
For exemple if you try to use stax in unit test, you could be disturbed because maven already use it.</p>
<p>Maven 3.x should fix this kind of problems but it doesn&#8217;t look like changes effective at compile time yet.<br />
You still can get this issue using velocity 1.7 with an annotation processor and run this processor through unit test compiling.</p>
<p>The output is :<br />
<pre class="brush: plain;">
Caused by: org.apache.velocity.exception.VelocityException: The specified logger class org.apache.velocity.runtime.log.CommonsLogLogChute does not implement the org.apache.velocity.runtime.log.LogChute interface.
	at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:181)
	... 46 more
</pre></p>
<p>Got from maven 3.0.3 compiling :<br />
<pre class="brush: plain;">
┌─[defrancea@~/eXo/wikbook-annotations/template]
└─&gt;mvn --version
Listening for transport dt_socket at address: 5005
Apache Maven 3.0.3 (r1075438; 2011-03-01 00:31:09+0700)
Maven home: /usr/share/maven
Java version: 1.6.0_26, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: &quot;mac os x&quot;, version: &quot;10.6.8&quot;, arch: &quot;x86_64&quot;, family: &quot;mac&quot;
</pre></p>
<p>Actually maven 3.0.3 use velocity 1.5 and some refactoring was done since this version on the API.<br />
You can either use velocity 1.5 or use another template engine. My choice was to use freemarker (sorry velocity guys).</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alaindefrance.wordpress.com/366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alaindefrance.wordpress.com/366/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alaindefrance.wordpress.com/366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alaindefrance.wordpress.com/366/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alaindefrance.wordpress.com/366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alaindefrance.wordpress.com/366/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alaindefrance.wordpress.com/366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alaindefrance.wordpress.com/366/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alaindefrance.wordpress.com/366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alaindefrance.wordpress.com/366/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alaindefrance.wordpress.com/366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alaindefrance.wordpress.com/366/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alaindefrance.wordpress.com/366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alaindefrance.wordpress.com/366/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=366&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alaindefrance.wordpress.com/2011/11/20/maven-classpath-isolation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/893f2d7465d4cd3957640212efe32843?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alaindefrance</media:title>
		</media:content>
	</item>
		<item>
		<title>Groovy and me, a long love story.</title>
		<link>http://alaindefrance.wordpress.com/2010/08/13/groovy-and-me-a-long-love-story/</link>
		<comments>http://alaindefrance.wordpress.com/2010/08/13/groovy-and-me-a-long-love-story/#comments</comments>
		<pubDate>Fri, 13 Aug 2010 15:06:58 +0000</pubDate>
		<dc:creator>Alain Defrance</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://alaindefrance.wordpress.com/?p=332</guid>
		<description><![CDATA[For many days I was trying to adapt some Plain old java test (POJT ?) on my groovy portage of Chromattic Framework (Object mapper to JCR). This adaptation use the MOP, it&#8217;s a cool way to have a good chromattic integration. But after quick test based on GroovyClassLoader, I have to port all the existing [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=332&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>For many days I was trying to adapt some Plain old java test (POJT ?) on my groovy portage of Chromattic Framework (Object mapper to JCR).<br />
This adaptation use the MOP, it&#8217;s a cool way to have a good chromattic integration.<br />
But after quick test based on GroovyClassLoader, I have to port all the existing tests to my adaptation, but nothing work !<br />
After several long hours of work in my code and original code, I have just found the real problem : Groovy !<br />
When we say &#8220;groovy is dynamic, MOP is beautiful, MOP is cool&#8221; it&#8217;s not groovy but groovy in <strong>specific</strong> context.<br />
Groovy called method have the same mechanism of method resolution of Java &#8230;<br />
MOP feature seem available only if the call are made by groovy code &#8230;</p>
<p>Check this example :<br />
<pre class="brush: plain;">class A {
 public Integer m() { return 3 }
 def invokeMethod(String m, Object p) { return 42 }
}</pre></p>
<p>and Java unit test :<br />
<pre class="brush: plain;">public class GoofyTestCase extends TestCase {
 public void testGroofy() throws Exception {
   assertEquals(42, new GroovyShell().evaluate(&quot;import org.chromattic.groovy.metamodel.B; new B().m()&quot;)); // true
   assertEquals((Object) 3, new B().m()); // true
 }
}</pre></p>
<p>MOP is used <strong>only</strong> when the call are made in groovy script.<br />
Do the Dynamic resolution be made by groovy or the groovy shell ?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alaindefrance.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alaindefrance.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alaindefrance.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alaindefrance.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alaindefrance.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alaindefrance.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alaindefrance.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alaindefrance.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alaindefrance.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alaindefrance.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alaindefrance.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alaindefrance.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alaindefrance.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alaindefrance.wordpress.com/332/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=332&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alaindefrance.wordpress.com/2010/08/13/groovy-and-me-a-long-love-story/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/893f2d7465d4cd3957640212efe32843?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alaindefrance</media:title>
		</media:content>
	</item>
		<item>
		<title>Groovy setter generation</title>
		<link>http://alaindefrance.wordpress.com/2010/07/27/groovy-setter-generation/</link>
		<comments>http://alaindefrance.wordpress.com/2010/07/27/groovy-setter-generation/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 16:41:14 +0000</pubDate>
		<dc:creator>Alain Defrance</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[ast]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://alaindefrance.wordpress.com/?p=324</guid>
		<description><![CDATA[Today, I have to generate setter to annote it at the compilation time. I&#8217;ve previously generate getter as following : But If I try to generate setter : and &#8230; nothing, no setter generated &#8230; It&#8217;s time to check groovy sources code and I had found : &#8230; Groovy developers use a new instance of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=324&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today, I have to generate setter to annote it at the compilation time.</p>
<p>I&#8217;ve previously generate getter as following :<br />
<pre class="brush: java;">
classNode.addMethod(
      GroovyUtils.getsetName(GroovyUtils.GetSet.GET, fieldNode.getName())
      , Modifier.PUBLIC
      , fieldNode.getType()
      , new Parameter[]{}
      , new ClassNode[]{}
      , new ReturnStatement(new FieldExpression(fieldNode))
    );
</pre></p>
<p>But If I try to generate setter :<br />
<pre class="brush: java;">
    classNode.addMethod(
      GroovyUtils.getsetName(GroovyUtils.GetSet.SET, fieldNode.getName())
      , Modifier.PUBLIC
      , new ClassNode (Void.TYPE)
      , new Parameter[]{ new Parameter(fieldNode.getType(), &quot;value&quot;) }
      , new ClassNode[]{}
      , new ExpressionStatement(new BinaryExpression(new PropertyExpression(new VariableExpression(&quot;this&quot;), fieldNode.getName()), Token.newSymbol(Types.EQUAL, 0, 0), new VariableExpression(&quot;value&quot;)))
    );
</pre></p>
<p>and &#8230; nothing, no setter generated &#8230;</p>
<p>It&#8217;s time to check groovy sources code and I had found :<br />
<pre class="brush: java;">
    public MethodNode getSetterMethod(String setterName) {
        for (Object o : getDeclaredMethods(setterName)) {
            MethodNode method = (MethodNode) o;
            if (setterName.equals(method.getName())
                    &amp;&amp; ClassHelper.VOID_TYPE==method.getReturnType()
                    &amp;&amp; method.getParameters().length == 1) {
                return method;
            }
        }
        ClassNode parent = getSuperClass();
        if (parent!=null) return parent.getSetterMethod(setterName);
        return null;
    }
</pre><br />
<pre class="brush: java;">
    VOID_TYPE = new ClassNode(Void.TYPE)
</pre></p>
<p>&#8230; Groovy developers use a new instance of ClassNode and use == operator. they compare memory address (the instance) but not the value.</p>
<p>Just changing &#8220;new ClassNode (Void.TYPE)&#8221; by &#8220;ClassHelper.VOID_TYPE&#8221; resolve my problem.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alaindefrance.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alaindefrance.wordpress.com/324/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alaindefrance.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alaindefrance.wordpress.com/324/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alaindefrance.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alaindefrance.wordpress.com/324/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alaindefrance.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alaindefrance.wordpress.com/324/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alaindefrance.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alaindefrance.wordpress.com/324/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alaindefrance.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alaindefrance.wordpress.com/324/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alaindefrance.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alaindefrance.wordpress.com/324/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=324&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alaindefrance.wordpress.com/2010/07/27/groovy-setter-generation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/893f2d7465d4cd3957640212efe32843?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alaindefrance</media:title>
		</media:content>
	</item>
		<item>
		<title>ClosureExpression with Groovy AST 1.6.5</title>
		<link>http://alaindefrance.wordpress.com/2010/07/22/closureexpression-with-groovy-ast-1-6-5/</link>
		<comments>http://alaindefrance.wordpress.com/2010/07/22/closureexpression-with-groovy-ast-1-6-5/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 11:57:42 +0000</pubDate>
		<dc:creator>Alain Defrance</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://alaindefrance.wordpress.com/?p=316</guid>
		<description><![CDATA[Today I have to create closure with groovy AST, but when I use ClosureExpression with this snippet : I get a very explicit error : Thanks to IntelliJ idea I can set a breakpoint on NullPointerException and I come in the AsmClassGenerator.java file at the line 3731 from groovy 1.6.5 sources and I can see [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=316&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today I have to create closure with groovy AST, but when I use ClosureExpression with this snippet :</p>
<p><pre class="brush: java;">ClosureExpression closureExpression = new ClosureExpression (
  new Parameter[] {}
  , new ExpressionStatement(new PropertyExpression(new VariableExpression(&quot;it&quot;), &quot;class&quot;))
);</pre></p>
<p>I get a very explicit error :<br />
<pre class="brush: plain;">java.lang.NullPointerException
at org.codehaus.groovy.classgen.AsmClassGenerator.createClosureClass(AsmClassGenerator.java:3601)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitClosureExpression(AsmClassGenerator.java:1559)
at org.codehaus.groovy.ast.expr.ClosureExpression.visit(ClosureExpression.java:46)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitAndAutoboxBoolean(AsmClassGenerator.java:4037)
at org.codehaus.groovy.classgen.AsmClassGenerator.makeCallSite(AsmClassGenerator.java:1965)
at org.codehaus.groovy.classgen.AsmClassGenerator.makeCall(AsmClassGenerator.java:1799)
at org.codehaus.groovy.classgen.AsmClassGenerator.makeCall(AsmClassGenerator.java:1785)
at org.codehaus.groovy.classgen.AsmClassGenerator.makeInvokeMethodCall(AsmClassGenerator.java:1768)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitMethodCallExpression(AsmClassGenerator.java:2277)
at org.codehaus.groovy.ast.expr.MethodCallExpression.visit(MethodCallExpression.java:63)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitAndAutoboxBoolean(AsmClassGenerator.java:4037)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitCastExpression(AsmClassGenerator.java:1711)
at org.codehaus.groovy.ast.expr.CastExpression.visit(CastExpression.java:66)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitAndAutoboxBoolean(AsmClassGenerator.java:4037)
at org.codehaus.groovy.classgen.AsmClassGenerator.makeCallSite(AsmClassGenerator.java:1965)
at org.codehaus.groovy.classgen.AsmClassGenerator.makeCall(AsmClassGenerator.java:1799)
at org.codehaus.groovy.classgen.AsmClassGenerator.makeCall(AsmClassGenerator.java:1785)
at org.codehaus.groovy.classgen.AsmClassGenerator.makeInvokeMethodCall(AsmClassGenerator.java:1768)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitMethodCallExpression(AsmClassGenerator.java:2277)
at org.codehaus.groovy.ast.expr.MethodCallExpression.visit(MethodCallExpression.java:63)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitAndAutoboxBoolean(AsmClassGenerator.java:4037)
at org.codehaus.groovy.classgen.AsmClassGenerator.makeCallSite(AsmClassGenerator.java:1941)
at org.codehaus.groovy.classgen.AsmClassGenerator.makeCall(AsmClassGenerator.java:1799)
at org.codehaus.groovy.classgen.AsmClassGenerator.makeCall(AsmClassGenerator.java:1785)
at org.codehaus.groovy.classgen.AsmClassGenerator.makeInvokeMethodCall(AsmClassGenerator.java:1768)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitMethodCallExpression(AsmClassGenerator.java:2277)
at org.codehaus.groovy.ast.expr.MethodCallExpression.visit(MethodCallExpression.java:63)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitAndAutoboxBoolean(AsmClassGenerator.java:4037)
at org.codehaus.groovy.classgen.AsmClassGenerator.evaluateExpression(AsmClassGenerator.java:1296)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitReturnStatement(AsmClassGenerator.java:1257)
at org.codehaus.groovy.ast.stmt.ReturnStatement.visit(ReturnStatement.java:47)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:73)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:80)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitStdMethod(AsmClassGenerator.java:552)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitConstructorOrMethod(AsmClassGenerator.java:528)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:88)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitMethod(AsmClassGenerator.java:632)
at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1055)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:48)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitClass(AsmClassGenerator.java:242)
at org.codehaus.groovy.control.CompilationUnit$10.call(CompilationUnit.java:718)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:925)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:462)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:278)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:249)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:244)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:206)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:216)
at org.chromattic.groovy.core.PlopTestCase.testPlop(PlopTestCase.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.intellij.junit3.JUnit3IdeaTestRunner.doRun(JUnit3IdeaTestRunner.java:108)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:64)</pre></p>
<p>Thanks to IntelliJ idea I can set a breakpoint on NullPointerException and I come in the AsmClassGenerator.java file at the line 3731 from groovy 1.6.5 sources and I can see :</p>
<p>        <pre class="brush: java;">VariableScope scope = ce.getVariableScope();
Parameter[] ret = new Parameter[scope.getReferencedLocalVariablesCount()];</pre></p>
<p>Why developers don&#8217;t have set the scope in ClosureExpression&#8217;s constructor of control the value of the scope to avoid the NullPointerException ?</p>
<p>Of course the simple following statement resolve the problem :<br />
        <pre class="brush: java;">closure.setVariableScope(new VariableScope());</pre></p>
<p>If you want to use groovy (even from java code), you must be very optimistic.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alaindefrance.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alaindefrance.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alaindefrance.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alaindefrance.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alaindefrance.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alaindefrance.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alaindefrance.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alaindefrance.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alaindefrance.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alaindefrance.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alaindefrance.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alaindefrance.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alaindefrance.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alaindefrance.wordpress.com/316/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=316&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alaindefrance.wordpress.com/2010/07/22/closureexpression-with-groovy-ast-1-6-5/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/893f2d7465d4cd3957640212efe32843?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alaindefrance</media:title>
		</media:content>
	</item>
		<item>
		<title>Unicode source code</title>
		<link>http://alaindefrance.wordpress.com/2010/06/09/unicode-source-code/</link>
		<comments>http://alaindefrance.wordpress.com/2010/06/09/unicode-source-code/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 16:24:19 +0000</pubDate>
		<dc:creator>Alain Defrance</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[en]]></category>
		<category><![CDATA[Syntax]]></category>
		<category><![CDATA[Tip]]></category>

		<guid isPermaLink="false">http://alaindefrance.wordpress.com/?p=306</guid>
		<description><![CDATA[One day I have read that java source code is considered as unicode text. Today I want to try that : The unicode string is equivalents to the following code :<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=306&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One day I have read that java source code is considered as unicode text.</p>
<p>Today I want to try that :</p>
<p><pre class="brush: plain;">
$ echo &quot;\u0070\u0075\u0062\u006C\u0069\u0063\u0020\u0063\u006C\u0061\u0073\u0073\u0020\u0041\u0020\u007B\u000A\u0020\u0020\u0020\u0020\u0070\u0075\u0062\u006C\u0069\u0063\u0020\u0073\u0074\u0061\u0074\u0069\u0063\u0020\u0076\u006F\u0069\u0064\u0020\u006D\u0061\u0069\u006E\u0028\u0053\u0074\u0072\u0069\u006E\u0067\u005B\u005D\u0020\u0061\u0072\u0067\u0076\u0029\u0020\u007B\u000A\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0053\u0079\u0073\u0074\u0065\u006D\u002E\u006F\u0075\u0074\u002E\u0070\u0072\u0069\u006E\u0074\u006C\u006E\u0028\u0022\u0070\u006F\u0075\u0065\u0074\u0022\u0029\u003B\u000A\u0020\u0020\u0020\u0020\u007D\u000A\u007D&quot; &gt; A.java
$ javac A.java
$ java A
pouet
</pre></p>
<p>The unicode string is equivalents to the following code :</p>
<p><pre class="brush: java;">
public class A {
    public static void main(String[] argv) {
        System.out.println(&quot;pouet&quot;);
    }
}
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alaindefrance.wordpress.com/306/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alaindefrance.wordpress.com/306/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alaindefrance.wordpress.com/306/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alaindefrance.wordpress.com/306/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alaindefrance.wordpress.com/306/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alaindefrance.wordpress.com/306/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alaindefrance.wordpress.com/306/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alaindefrance.wordpress.com/306/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alaindefrance.wordpress.com/306/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alaindefrance.wordpress.com/306/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alaindefrance.wordpress.com/306/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alaindefrance.wordpress.com/306/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alaindefrance.wordpress.com/306/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alaindefrance.wordpress.com/306/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=306&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alaindefrance.wordpress.com/2010/06/09/unicode-source-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/893f2d7465d4cd3957640212efe32843?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alaindefrance</media:title>
		</media:content>
	</item>
		<item>
		<title>The perfect copy</title>
		<link>http://alaindefrance.wordpress.com/2010/06/01/the-perfect-copy/</link>
		<comments>http://alaindefrance.wordpress.com/2010/06/01/the-perfect-copy/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 15:01:11 +0000</pubDate>
		<dc:creator>Alain Defrance</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[en]]></category>
		<category><![CDATA[Tip]]></category>

		<guid isPermaLink="false">http://alaindefrance.wordpress.com/?p=284</guid>
		<description><![CDATA[The Prototype pattern allows to initialize a new instance with the state of an other. It&#8217;s easy to implement this pattern when the fields of the instance are are of primitive types (int, &#8230;). However, when the fields are references, It can be more complex to clone all the members recursively. The JVM can help [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=284&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The Prototype pattern allows to initialize a new instance with the state of an other.<br />
It&#8217;s easy to implement this pattern when the fields of the instance are are of primitive types (int, &#8230;).<br />
However, when the fields are references, It can be more complex to clone all the members recursively.</p>
<p>The JVM can help us do that more easily with serialization.</p>
<p>Thanks to ByteArrayStream you can write an object, and read the copy of the object.</p>
<p>You want to make the both classes cloneable :<br />
<pre class="brush: java;">class B {}
class A implements Cloneable {
    public B b = new B();

    @Override
    protected A clone() {
        try {
            return (A) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }
}</pre></p>
<p>We write two unit tests to check if the cloning is ok : </p>
<p><pre class="brush: java;">class CloneTest extends TestCase {
    private A a = new A();

   @Test
   public void testInstanceClone() {
       assertTrue(a != a.clone());
   }

   @Test
   public void testInstanceMembreClone() {
       assertTrue(a.b != a.clone().b);
   }
}</pre></p>
<p>testInstanceClone passes but testInstanceMembreClone fails.</p>
<p>The clone seems good but the cloned references are the sames.</p>
<p>To create a perfect clone you can use the JVM and serializable objects as in this snippet :</p>
<p><pre class="brush: java;">class Cloner {
     static &lt;T&gt; T clone(T t) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(t);
            oos.flush();
            return (T) new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())).readObject();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}</pre></p>
<p>You can now use it in the clone method :<br />
<pre class="brush: java;">
    @Override
    protected A clone() {
        return Cloner.clone(this);
    }</pre></p>
<p>Now the two tests pass <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>This tip is not the most performant way to clone an instance  but it&#8217;s the most reliable.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alaindefrance.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alaindefrance.wordpress.com/284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alaindefrance.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alaindefrance.wordpress.com/284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alaindefrance.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alaindefrance.wordpress.com/284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alaindefrance.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alaindefrance.wordpress.com/284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alaindefrance.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alaindefrance.wordpress.com/284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alaindefrance.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alaindefrance.wordpress.com/284/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alaindefrance.wordpress.com/284/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alaindefrance.wordpress.com/284/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=284&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alaindefrance.wordpress.com/2010/06/01/the-perfect-copy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/893f2d7465d4cd3957640212efe32843?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alaindefrance</media:title>
		</media:content>
	</item>
		<item>
		<title>Should I use the template method or the command pattern to create a hook ?</title>
		<link>http://alaindefrance.wordpress.com/2010/04/01/may-i-use-template-method-or-command-to-create-hook/</link>
		<comments>http://alaindefrance.wordpress.com/2010/04/01/may-i-use-template-method-or-command-to-create-hook/#comments</comments>
		<pubDate>Thu, 01 Apr 2010 11:22:51 +0000</pubDate>
		<dc:creator>Alain Defrance</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Design pattern]]></category>
		<category><![CDATA[en]]></category>

		<guid isPermaLink="false">http://alaindefrance.wordpress.com/?p=262</guid>
		<description><![CDATA[Sometimes you need to have a hook in your code. This hook allows other developers to make some of their code be executed within yours. You have two ways to do that. The first way is the template method pattern and the second is the command pattern. The template method uses inheritance while the command [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=262&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Sometimes you need to have a hook in your code. This hook allows other developers  to make some of their code be executed within yours.</p>
<p><pre class="brush: java;">
void m() {
    System.out.println(&quot;my code&quot;);
    // hook
    System.out.println(&quot;my code&quot;);
}
</pre></p>
<p>You have two ways to do that. The first way is the template method pattern and the second is the command pattern.<br />
The template method uses inheritance while the command pattern use composition.</p>
<p>If you use the template method your code will be as follows :</p>
<p><pre class="brush: java;">
abstract class A {
    void m() {
        System.out.println(&quot;my code&quot;);
        hook();
        System.out.println(&quot;my code&quot;);
    }
    void abstract hook();
}

class B extends A {
    void hook() {
        System.out.println(&quot;hook code&quot;);
    }
}
</pre></p>
<p>You can also write a stub implementation :</p>
<p><pre class="brush: java;">
class A {
    void m() {
        System.out.println(&quot;my code&quot;);
        hook();
        System.out.println(&quot;my code&quot;);
    }
    void hook() {}
}

class B extends A {
    void hook() {
        System.out.println(&quot;hook code&quot;);
    }
}
</pre></p>
<p>But Inheritance is static, thus solved at compile-time. If you want a dynamically (run-time) modifiable hook, you have to use composition. You can do that thanks to the Command pattern.</p>
<p><pre class="brush: java;">
class A {
    private Command command;
    public A(Command command) {this.command = command}
    void m() {
        System.out.println(&quot;my code&quot;);
        if (command != null) command.hook();
        System.out.println(&quot;my code&quot;);
    }
}

interface Command { void hook(); }
class StubCommand implements Command { public void hook() {} } // Empty hook
class CommandImpl implements Command {
    public void hook() { System.out.println(&quot;hook code&quot;); }
}
</pre></p>
<p>There is no particular solution but generally the most reusable solution uses composition.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alaindefrance.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alaindefrance.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alaindefrance.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alaindefrance.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alaindefrance.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alaindefrance.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alaindefrance.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alaindefrance.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alaindefrance.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alaindefrance.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alaindefrance.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alaindefrance.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alaindefrance.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alaindefrance.wordpress.com/262/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=262&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alaindefrance.wordpress.com/2010/04/01/may-i-use-template-method-or-command-to-create-hook/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/893f2d7465d4cd3957640212efe32843?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alaindefrance</media:title>
		</media:content>
	</item>
		<item>
		<title>Implement State pattern with enum</title>
		<link>http://alaindefrance.wordpress.com/2010/03/31/implement-state-pattern-with-enum/</link>
		<comments>http://alaindefrance.wordpress.com/2010/03/31/implement-state-pattern-with-enum/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 11:57:39 +0000</pubDate>
		<dc:creator>Alain Defrance</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Design pattern]]></category>
		<category><![CDATA[en]]></category>

		<guid isPermaLink="false">http://alaindefrance.wordpress.com/?p=252</guid>
		<description><![CDATA[State pattern allow to choose the correct action related to the state of an object. The goal is to get rid of if/elseif and switch blocks. You can replace constants which represents state by implementing the pattern as follows : But now if you want to compare the state, you must use the instanceof operator. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=252&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>State pattern allow to choose the correct action related to the state of an object. The goal is to get rid of if/elseif and switch blocks.</p>
<p>You can replace constants which represents state by implementing the pattern as follows :</p>
<p><pre class="brush: java;">
public class A {
    private int state = State.OPEN;

    class State {
        public static final int OPEN = 1;
        public static final int CLOSE = 2;
    }
}
</pre></p>
<p><pre class="brush: java;">
public class A {
    private State state;

    interface State {}
    class OpenedState implements State {}
    class CloseState implements State {}
}
</pre></p>
<p>But now if you want to compare the state, you must use the instanceof operator.</p>
<p>You can do better with enum :<br />
<pre class="brush: java;">
public class A {
    private State state;
    public A(State state) { this.state = state; }
    public State getState() { return state; }
    public void setState(State state) { this.state = state; }

    public void doSomething() { state.doSomething(); }

    public static void main(String[] argv) {
        A a = new A(State.CLOSE);
        a.doSomething();
        a.setState(State.OPEN);
        a.doSomething();
    }
    
    enum State {
        OPEN(new StateOpen()), CLOSE(new StateClose());
    
        private StateInterface stateInterface;

        private State(StateInterface stateInterface) { this.stateInterface = stateInterface; }
        
        public void doSomething() { stateInterface.doSomething(); }
    
        public static interface StateInterface { void doSomething(); }
        public static class StateOpen implements StateInterface {
            public void doSomething() { System.out.println(&quot;open&quot;); }
        }
        public static class StateClose implements StateInterface {
            public void doSomething() { System.out.println(&quot;close&quot;); }
        }
    }
}</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alaindefrance.wordpress.com/252/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alaindefrance.wordpress.com/252/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alaindefrance.wordpress.com/252/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alaindefrance.wordpress.com/252/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alaindefrance.wordpress.com/252/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alaindefrance.wordpress.com/252/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alaindefrance.wordpress.com/252/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alaindefrance.wordpress.com/252/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alaindefrance.wordpress.com/252/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alaindefrance.wordpress.com/252/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alaindefrance.wordpress.com/252/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alaindefrance.wordpress.com/252/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alaindefrance.wordpress.com/252/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alaindefrance.wordpress.com/252/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=252&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alaindefrance.wordpress.com/2010/03/31/implement-state-pattern-with-enum/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/893f2d7465d4cd3957640212efe32843?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alaindefrance</media:title>
		</media:content>
	</item>
		<item>
		<title>Automatic imports : are they inefficient ?</title>
		<link>http://alaindefrance.wordpress.com/2009/12/28/automatic-imports-are-they-inefficient/</link>
		<comments>http://alaindefrance.wordpress.com/2009/12/28/automatic-imports-are-they-inefficient/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 20:41:11 +0000</pubDate>
		<dc:creator>Alain Defrance</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://alaindefrance.wordpress.com/?p=241</guid>
		<description><![CDATA[Both snippets have the same behaviour : But we can be curious about efficiency. In fact the same bytecode will be produced : Automatic imports are processed at compile time, thus no difference exists at runtime. However, compilation will be probably a bit slower.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=241&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Both snippets have the same behaviour :<br />
<pre class="brush: java;">
import java.util.ArrayList;
public class A {
    public static void main(String[] argv) {
        new ArrayList();
    }
}
</pre><br />
<pre class="brush: java;">
import java.util.*; // automatic import
public class A {
    public static void main(String[] argv) {
        new ArrayList();
    }
}
</pre></p>
<p>But we can be curious about efficiency.<br />
In fact the same bytecode will be produced :<br />
<pre class="brush: plain;">
[kaz@akina]$ cat A.jad
import java.util.ArrayList;
public class I {
    public static void main(String args[]) {
        new ArrayList();
    }
}
</pre></p>
<p>Automatic imports are processed at compile time, thus no difference exists at runtime.<br />
However, compilation will be probably a bit slower.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alaindefrance.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alaindefrance.wordpress.com/241/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alaindefrance.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alaindefrance.wordpress.com/241/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alaindefrance.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alaindefrance.wordpress.com/241/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alaindefrance.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alaindefrance.wordpress.com/241/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alaindefrance.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alaindefrance.wordpress.com/241/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alaindefrance.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alaindefrance.wordpress.com/241/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alaindefrance.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alaindefrance.wordpress.com/241/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=241&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alaindefrance.wordpress.com/2009/12/28/automatic-imports-are-they-inefficient/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/893f2d7465d4cd3957640212efe32843?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alaindefrance</media:title>
		</media:content>
	</item>
		<item>
		<title>How to customize dependency injection</title>
		<link>http://alaindefrance.wordpress.com/2009/12/25/how-to-customize-dependency-injection/</link>
		<comments>http://alaindefrance.wordpress.com/2009/12/25/how-to-customize-dependency-injection/#comments</comments>
		<pubDate>Fri, 25 Dec 2009 22:12:29 +0000</pubDate>
		<dc:creator>Alain Defrance</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[en]]></category>
		<category><![CDATA[Interceptors]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://alaindefrance.wordpress.com/?p=219</guid>
		<description><![CDATA[Hi, I often explain particular features of JSE but not JEE possibilities. Today we&#8217;ll see how to make a custom injection with JEE. This feature is not directly supported by JEE but with defaults interceptors it becomes possible. The Steps are : 1/ Create the annotation which will be used to tag a member instance [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=219&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hi,</p>
<p>I often explain particular features of JSE but not JEE possibilities.<br />
Today we&#8217;ll see how to make a custom injection with JEE.<br />
This feature is not directly supported by JEE but with defaults interceptors it becomes possible.<br />
The Steps are :<br />
1/ Create the annotation which will be used to tag a member instance to inject.<br />
2/ Write a default interceptor which acts at post construction. It must check if the annotation exists, and thanks to reflexion set the value.<br />
3/ Use the annotation with your EJB.</p>
<p>Annotation definition :<br />
<pre class="brush: java;">
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface InjectedValue { }
</pre></p>
<p>Interceptor implementation :<br />
<pre class="brush: java;">
public class CustomInterceptor {
    public void inject(InvocationContext invocation) throws IllegalAccessException {
        Object o = invocation.getTarget();
        for (Field field : o.getClass().getFields()) {
            if (field.isAnnotationPresent(InjectedValue.class)) {
                boolean previousAccessibility = field.isAccessible();
                field.setAccessible(true);
                try {
                    field.set(o, 42);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                field.setAccessible(previousAccessibility);
            }
        }
    }
}
</pre></p>
<p>Interceptor declaration :<br />
<pre class="brush: xml;">
&lt;interceptors&gt;
    &lt;interceptor&gt;
        &lt;interceptor-class&gt;ejb.CustomInterceptor&lt;/interceptor-class&gt;
        &lt;post-construct&gt;
            &lt;lifecycle-callback-method&gt;
                inject
            &lt;/lifecycle-callback-method&gt;
        &lt;/post-construct&gt;
    &lt;/interceptor&gt;
&lt;/interceptors&gt;
</pre></p>
<p>Using of the interceptor :<br />
<pre class="brush: java;">
@Stateless
public class MyEJBBean implements MyEJBRemote {
    @InjectedValue
    private int myValue;
}
</pre></p>
<p>Dependency injection is the foundation of IOC (Inversion of control) and with interceptors you can write your dependency injections.<br />
Defaults interceptors are applied on all EJBs, but if you want, you can attach interceptor to a particular EJB.<br />
With AOP (Aspect Oriented Programming), and thanks to pointcuts, you can precisely identify a method signature and act on it.<br />
We&#8217;ll maybe see that later.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alaindefrance.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alaindefrance.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alaindefrance.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alaindefrance.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/alaindefrance.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/alaindefrance.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/alaindefrance.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/alaindefrance.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alaindefrance.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alaindefrance.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alaindefrance.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alaindefrance.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alaindefrance.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alaindefrance.wordpress.com/219/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alaindefrance.wordpress.com&amp;blog=9922379&amp;post=219&amp;subd=alaindefrance&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alaindefrance.wordpress.com/2009/12/25/how-to-customize-dependency-injection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/893f2d7465d4cd3957640212efe32843?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alaindefrance</media:title>
		</media:content>
	</item>
	</channel>
</rss>
