|
|||||
|
|||||
Dimple Tips
Tips for Java 1.3 and above
GenericsWhen using dimple for Java 5, generics is used so that you do not need to down-cast. The above non-closeable connection example can become: ... private static final Implementor<NonCloseable> closeSuppresser = Implementor.instance(NonCloseable.class); Connection realConn = ...;//wherever you get it from Connection intercepted = closeSuppresser.implement(Connection.class, new NonCloseable(), realConn); ... Stronger typeWhen using dimple to implement or override, the compiler doesn't check the method signature for you. When you have a typo or something so that the impl method does not match the method you are trying to implement, (parameters are not even contravariant), your method will not be used. For example: Connection nonCloseable = Implementor.proxy(Connection.class, new Object(){ public void cloze(){}//Oops, Typo! }, realConn); The close method will not be intercepted at all. Normally such error is not hard to find as your unit test case would fail anyway. However, when you do want to make sure that the method is used, use the Implementor.implementedBy() or Implementor.willImplement() methods to perform additional type checking. For example: Connection nonCloseable = Implementor.proxy(Implementor.implementedBy(Connection.class, NonCloseable.class), new NonCloseable(), realConn);
or Implementor closeSuppresser = new Implementor(Implementor.willImplement(NonCloseable.class, Connection.class)); Connection nonCloseable = (Connection)closeSuppresser.implement(Connection.class, new NonCloseable(), realConn); In Java 5, the @Implement annotation can be used to annotate the method or the entire class to ask dimple runtime to check against such error. Using @Implement, the above code will become: Connection nonCloseable = Implementor.proxy(Connection.class, new Object(){ @Implement public void cloze(){}//fail fast for this typo }, realConn); And you will get an UnusedMethodException right away. |
|||||
|
Copyright 2003-2006 - The Codehaus. All rights reserved unless otherwise noted.
Powered by Atlassian Confluence
|
|||||