« ObjectGrid Programming Guide | Main | Maxims WebSphere JTA broken claim debunked... »

August 31, 2005

Aspect or cglib

Wondering about how to implement EJB 3.0 style EntityManagers today. Basically, I need to intercept modifications to attributes and intercept methods.

ASM lets me do this as a low level but it hurts my head thinking in byte codes.

Aspects let me do this through byte weaving but that means modifying the environment I run in to use as aspect class loader which I can't do. Most managed environments do not allow an application to add a class loader to the environment class loader list. This basically kills aspects for me in my stuff as I need my stuff to work in legacy environments where I can't control class loaders etc.

cglib looks most attractive as I can generate a subclass of the class and when the application is using the subclass I can intercept field and method accesses. It works on JDK 1.2 or higher and doesn't require any class loader fudging.

So, for now, cglib is way ahead of aspects for this type of application given cglib isn't invasive in the way aspects are. My blog on invasive middleware is my philosophy for the stuff I'm working on now and it appears the aspect pattern was designed to be invasive and this is to it's detrement in my view not matter how cool it is at least for my purposes.

There are some interesting issues around the JIT and aspects versus cglib and it looks like performance wise, aspects has advantages over cglib style approaches but its invasive nature would still tip me against it despite the performance benefit.

August 31, 2005 | Permalink


Billy, we are thinking about add generic method wrapper/proxy adapter to ASM commons package. Give a shout in ASM mail list if you are interested.

By the way, CGLIB actually does some reflection tricks to inject generated bytecode into the current class loader.

Posted by: eu | Aug 31, 2005 2:26:14 PM

If you want to intercept field access/modification for private fields then you are screwed, the same holds for private methods (using CGLIB).

If I can't change the class loader then I'm screwed anyway with aspects, right :) This may be the best that we can do.

Posted by: Jonas | Sep 1, 2005 2:15:22 AM

First AOP is not bytecode instrumentation. We do AOP in the JRockit VM with no bytecode instrumentation at all f.e.
Second CGLIB won't let you intercept a final method assuming there is no interface f.e.
Third, aspects (AspectJ/ AspectWerkz) are not at all invasive or based on ClassLoader tricks. We support f.e JSR-163, as well as other simple way to do weaving of aspects. Also, you can do the weaving during your build process.
You should talk to your peer Andy Clement in Hursley, AOSD group to get an update on that field.

Posted by: Alex | Sep 27, 2005 7:48:21 AM

Post a comment