Load Time Weaving with AspectJ 5

Posted on May 5, 2005


I’ve been working with the new AspectJ 5 Load Time Weaving implementation that Alex and Jonas have created, and am really excited at the opportunity this technology brings. I have tried it with the Sun Java 1.5 VM and the JRockIt 1.5 VM too (*).  I’m working on creating application monitoring tools, so it’s really natural to weave into the whole VM that a container is running in. Using LTW, we are already able to track the operations of any deployed application inside Tomcat 5.5.x, which is very cool!

I have used build-time weaving with third party libraries in past and found it very painful because you needed to either find any libraries that these libraries used (e.g., to weave into Spring.jar you need all the various O/R tools, JMX, and a bevy of obscure cache management libraries). With load-time weaving, you only weave the classes that are loaded, so you only need the libraries that are referenced by these. That’s a big improvement.

LTW is still "pre-alpha", so naturally I faced a few gotchas (I was working off a branch in CVS and they just merged it into head in the last couple of days). Thanks to Alex for helping me with these and to Andy Clement for bearing with my questions about mixing 1.5.0 M2 output with the fairly old branch code!

One of the things I’d like to change is the need to specify all the aspects that are woven in an aop.xml file. Alex and I traded thoughts on this and my idea of an aspectpath like mechanism on bugzilla (where else)? I would really like to have some way to generate the list of aspects, rather than having to maintain it by hand, hopefully stored separately (in something like META-INF/aspects.mf).

I also ran into an interesting problem when I was weaving into a MySQL JDBC driver’s debug version:

error at com\mysql\jdbc\NonRegisteringDriver.java::0 class ‘com.mysql.jdbc.NonRegisteringDriver’ is already woven and has not been built with -Xreweavable

That’s right. This error means that the code was already woven with AspectJ! The MYSQL debug JDBC driver includes a tracing aspect: it’s great to see this kind of increased use & popularity of AspectJ. For the curious, you can see the source code for this aspect in the source distribution of the Mysql Connector/J. In this case, the normal, non-debug, library doesn’t use AspectJ so it’s not a major problem.  There is already consensus that reweavable should be the default mode in future. I even question why there’s an option to not allow reweaving. (**)

Now the next challenges I see on this front are:

  • Shipping 1.5.0
  • Optimizing the weaving performance, especially for simple cases. I’d really like to see AspectJ’s load-time weaving fairly close in performance to hand-written bytecode manipulation. Not necessarily the same, but at least close enough.
  • Support for older VMs (which I know Alex and Jonas are working on)


(*) I found JRockIt to be about twice as fast, but to not be quite as good at debugging my aspects inside Eclipse. Apparently BEA has a patch for Eclipse debugging but is only providing it to paying customers…

(**) Amusingly, I only discovered this because I had an error in the first zip distribution I downloaded so I could only extract the debug jar file.