Javassist is fast and impressingly easy for byte-code generation, however it is by no way that comfortable (to code, test and debugg) as plain java. Therefore I've implemented an abstract ServiceInterceptorFactory which allows the user to attach MethodInterceptors to the methods of an interface. (A common pattern used by aopalliance, spring, cglib etc). Ie a simple tracing interceptor looks like this:

public class TracingMethodInterceptor implements MethodInterceptor

public Object invoke(Invocation invocation, Object target, Object[] args) throws Throwable
{
System.out.println("method: "+invocation.getMethod()+" is called with args: "+args);
Object ret = invocation.proceed(target, args); //this invokes the next method in the interceptor chain
System.out.println("method: "+invocation.getMethod()+" returned: "+args);
return ret;
}
}


Both the interceptor and the Invocation are created using Javassist, therefore it is in terms of speed (according to my rudimentary testings) between a plain Javaassist interceptor and a dyna-proxy interceptor.

Comparing the existing Hivemind Logging with the same code using the MethodInterceptor and with a DynaProxy for 5.000.000 repetitions the times (ms) are as follows: (The numbers are not stable but the direction is quite clear)

method: Object getObject()
executing MethodLog took: 1000
executing HivemindLog took: 359
executing DynaProxyLog took: 1360

method: void setObject(Object);
executing MethodLog took: 968
executing HivemindLog took: 516
executing DynaProxyLog took: 1766

method (9 prim params): int add(int,int,byte,short,char,double,long,float);
executing MethodLog took: 3063 (more primitives and arguments means bigger args[] and boxing/unboxing)
executing HivemindLog took: 594
executing DynaProxyLog took: 4031


As I see interceptors for general use should be created using javaassist, however other interceptors (which just add some custom AOP for some components) could well be implemented using MethodInterceptors. Here the higher convinience pays off.

--
Christian Essl http://jucas.sourceforge.net

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to