Maven classpath isolation
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’t look like changes effective at compile time yet.
You still can get this issue using velocity 1.7 with an annotation processor and run this processor through unit test compiling.
The output is :
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
Got from maven 3.0.3 compiling :
┌─[defrancea@~/eXo/wikbook-annotations/template] └─>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: "mac os x", version: "10.6.8", arch: "x86_64", family: "mac"
Actually maven 3.0.3 use velocity 1.5 and some refactoring was done since this version on the API.
You can either use velocity 1.5 or use another template engine. My choice was to use freemarker (sorry velocity guys).
Groovy and me, a long love story.
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’s a cool way to have a good chromattic integration.
But after quick test based on GroovyClassLoader, I have to port all the existing tests to my adaptation, but nothing work !
After several long hours of work in my code and original code, I have just found the real problem : Groovy !
When we say “groovy is dynamic, MOP is beautiful, MOP is cool” it’s not groovy but groovy in specific context.
Groovy called method have the same mechanism of method resolution of Java …
MOP feature seem available only if the call are made by groovy code …
Check this example :
class A {
public Integer m() { return 3 }
def invokeMethod(String m, Object p) { return 42 }
}
and Java unit test :
public class GoofyTestCase extends TestCase {
public void testGroofy() throws Exception {
assertEquals(42, new GroovyShell().evaluate("import org.chromattic.groovy.metamodel.B; new B().m()")); // true
assertEquals((Object) 3, new B().m()); // true
}
}
MOP is used only when the call are made in groovy script.
Do the Dynamic resolution be made by groovy or the groovy shell ?
Groovy setter generation
Today, I have to generate setter to annote it at the compilation time.
I’ve previously generate getter as following :
classNode.addMethod(
GroovyUtils.getsetName(GroovyUtils.GetSet.GET, fieldNode.getName())
, Modifier.PUBLIC
, fieldNode.getType()
, new Parameter[]{}
, new ClassNode[]{}
, new ReturnStatement(new FieldExpression(fieldNode))
);
But If I try to generate setter :
classNode.addMethod(
GroovyUtils.getsetName(GroovyUtils.GetSet.SET, fieldNode.getName())
, Modifier.PUBLIC
, new ClassNode (Void.TYPE)
, new Parameter[]{ new Parameter(fieldNode.getType(), "value") }
, new ClassNode[]{}
, new ExpressionStatement(new BinaryExpression(new PropertyExpression(new VariableExpression("this"), fieldNode.getName()), Token.newSymbol(Types.EQUAL, 0, 0), new VariableExpression("value")))
);
and … nothing, no setter generated …
It’s time to check groovy sources code and I had found :
public MethodNode getSetterMethod(String setterName) {
for (Object o : getDeclaredMethods(setterName)) {
MethodNode method = (MethodNode) o;
if (setterName.equals(method.getName())
&& ClassHelper.VOID_TYPE==method.getReturnType()
&& method.getParameters().length == 1) {
return method;
}
}
ClassNode parent = getSuperClass();
if (parent!=null) return parent.getSetterMethod(setterName);
return null;
}
VOID_TYPE = new ClassNode(Void.TYPE)
… Groovy developers use a new instance of ClassNode and use == operator. they compare memory address (the instance) but not the value.
Just changing “new ClassNode (Void.TYPE)” by “ClassHelper.VOID_TYPE” resolve my problem.
ClosureExpression with Groovy AST 1.6.5
Today I have to create closure with groovy AST, but when I use ClosureExpression with this snippet :
ClosureExpression closureExpression = new ClosureExpression (
new Parameter[] {}
, new ExpressionStatement(new PropertyExpression(new VariableExpression("it"), "class"))
);
I get a very explicit error :
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)
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 :
VariableScope scope = ce.getVariableScope(); Parameter[] ret = new Parameter[scope.getReferencedLocalVariablesCount()];
Why developers don’t have set the scope in ClosureExpression’s constructor of control the value of the scope to avoid the NullPointerException ?
Of course the simple following statement resolve the problem :
closure.setVariableScope(new VariableScope());
If you want to use groovy (even from java code), you must be very optimistic.
Unicode source code
One day I have read that java source code is considered as unicode text.
Today I want to try that :
$ echo "\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" > A.java $ javac A.java $ java A pouet
The unicode string is equivalents to the following code :
public class A {
public static void main(String[] argv) {
System.out.println("pouet");
}
}
The perfect copy
The Prototype pattern allows to initialize a new instance with the state of an other.
It’s easy to implement this pattern when the fields of the instance are are of primitive types (int, …).
However, when the fields are references, It can be more complex to clone all the members recursively.
The JVM can help us do that more easily with serialization.
Thanks to ByteArrayStream you can write an object, and read the copy of the object.
You want to make the both classes cloneable :
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;
}
}
We write two unit tests to check if the cloning is ok :
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);
}
}
testInstanceClone passes but testInstanceMembreClone fails.
The clone seems good but the cloned references are the sames.
To create a perfect clone you can use the JVM and serializable objects as in this snippet :
class Cloner {
static <T> 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;
}
}
You can now use it in the clone method :
@Override
protected A clone() {
return Cloner.clone(this);
}
Now the two tests pass
.
This tip is not the most performant way to clone an instance but it’s the most reliable.
Should I use the template method or the command pattern to create a hook ?
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.
void m() {
System.out.println("my code");
// hook
System.out.println("my code");
}
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 pattern use composition.
If you use the template method your code will be as follows :
abstract class A {
void m() {
System.out.println("my code");
hook();
System.out.println("my code");
}
void abstract hook();
}
class B extends A {
void hook() {
System.out.println("hook code");
}
}
You can also write a stub implementation :
class A {
void m() {
System.out.println("my code");
hook();
System.out.println("my code");
}
void hook() {}
}
class B extends A {
void hook() {
System.out.println("hook code");
}
}
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.
class A {
private Command command;
public A(Command command) {this.command = command}
void m() {
System.out.println("my code");
if (command != null) command.hook();
System.out.println("my code");
}
}
interface Command { void hook(); }
class StubCommand implements Command { public void hook() {} } // Empty hook
class CommandImpl implements Command {
public void hook() { System.out.println("hook code"); }
}
There is no particular solution but generally the most reusable solution uses composition.
Implement State pattern with enum
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 :
public class A {
private int state = State.OPEN;
class State {
public static final int OPEN = 1;
public static final int CLOSE = 2;
}
}
public class A {
private State state;
interface State {}
class OpenedState implements State {}
class CloseState implements State {}
}
But now if you want to compare the state, you must use the instanceof operator.
You can do better with enum :
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("open"); }
}
public static class StateClose implements StateInterface {
public void doSomething() { System.out.println("close"); }
}
}
}
Automatic imports : are they inefficient ?
Both snippets have the same behaviour :
import java.util.ArrayList;
public class A {
public static void main(String[] argv) {
new ArrayList();
}
}
import java.util.*; // automatic import
public class A {
public static void main(String[] argv) {
new ArrayList();
}
}
But we can be curious about efficiency.
In fact the same bytecode will be produced :
[kaz@akina]$ cat A.jad
import java.util.ArrayList;
public class I {
public static void main(String args[]) {
new ArrayList();
}
}
Automatic imports are processed at compile time, thus no difference exists at runtime.
However, compilation will be probably a bit slower.
How to customize dependency injection
Hi,
I often explain particular features of JSE but not JEE possibilities.
Today we’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 to inject.
2/ Write a default interceptor which acts at post construction. It must check if the annotation exists, and thanks to reflexion set the value.
3/ Use the annotation with your EJB.
Annotation definition :
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface InjectedValue { }
Interceptor implementation :
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);
}
}
}
}
Interceptor declaration :
<interceptors>
<interceptor>
<interceptor-class>ejb.CustomInterceptor</interceptor-class>
<post-construct>
<lifecycle-callback-method>
inject
</lifecycle-callback-method>
</post-construct>
</interceptor>
</interceptors>
Using of the interceptor :
@Stateless
public class MyEJBBean implements MyEJBRemote {
@InjectedValue
private int myValue;
}
Dependency injection is the foundation of IOC (Inversion of control) and with interceptors you can write your dependency injections.
Defaults interceptors are applied on all EJBs, but if you want, you can attach interceptor to a particular EJB.
With AOP (Aspect Oriented Programming), and thanks to pointcuts, you can precisely identify a method signature and act on it.
We’ll maybe see that later.