My Photo

August 2007

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

« October 2005 | Main | January 2006 »

December 20, 2005

A Hidden Treasure: Weaving with References to Missing Third Party Libraries, and Weaving on Installation

One of the nice little features of AspectJ 5 that isn't documented (at least no where that I can find) is the valuable new Xlint warning cantFindType;  This solves a major problem AspectJ users have faced until now when working with third party libraries. Previously when weaving into a jar file, you would to have to track down all the dependent jars  of that library and put them on the classpath so AspectJ could resolve all the types. This could be quite difficult when weaving into libraries like Spring or Hibernate, where there are many adapter classes that support a large variety of, e.g., cache managers.  It's also a problem in load-time weaving. In that context, a smaller example I ran into was when weaving into the Struts jar, where Action that has a type reference to a legacy data source that is not normally shipped with Struts. The Java code will not even invoke the method that refers to that data source, but by default AspectJ tries to weave everything at once and when it fails to resolve it, it will error out.

However, now you can fix the problem, telling AspectJ to simply ignore (or warn you) for missing types.  Simply create an Xlint.properties file and either copy one (like this one from the Glassbox Inspector project) or create a blank one. You can then add or edit two entries:

cantFindType = ignore

cantFindTypeAffectingJPMatch = warning

You can then use the Xlint properties file to weave with AJDT in Eclipse, with an ant task, with maven or the ajc compiler (or even a smaller command-line weaving utility I talk about below). You can also use it by adding a reference to it in the META-INF/aop.xml file for load-time weaving.

Put together, this allows you to weave and run with incomplete dependencies. I was reminded of how nice this feature was tonight when I ran into a problem weaving into a project jar because there is another configuration of the code that uses a 3rd party jar. On this project, we are doing a command line weave on installation for a GUI application, to allow us to minimize the time for weaving and to minimize change sizes. whereby an aspect was choking on a jar that had a reference to an unused 3rd party library in one, even with smaller configurations of your own application. So we don't want to include all these optional libraries in our distribution! Instead, I was able to just create an Xlint.properties file and fix the problem. By the way, I also created a tiny 20k contribution to support command-line weaving with just the aspectjweaver.jar instead of pulling along a few megabytes of additional AspectJ compiler code. If you want to use that please let me know and I can upload a few small updates I had to make and even a jar. Hopefully we'll get this integrated into AspectJ 1.5.1

It is worth noting that if you do this, you should make sure you won't be running the woven jar in an environment where the missing dependency is present... I think this is so useful that it is probably worth making the case of missing types a warning rather than an error. I also think it's a little surprising that it is an Xlint property. But I'm sure glad the feature is available!

before() : christmas() { release(); }

This morning some great news arrived in my in box. AspectJ 5 final shipped today. It's been over a year in the making and I'm really excited we've hit this milestone.


I for one want to say thank you to the AspectJ committers for working so long and hard to release AspectJ 5! Adding generics support to AspectJ was certainly a major undertaking and it’s great that the end result has been so stable and, in fact, greatly improved even for older Java VM’s. This is a real milestone in the history of aspect-oriented programming and a great Christmas present for all of us.


If you are using AspectJ, I'd encourage you to download it and the Eclipse tools . If you aren't yet using AspectJ, this is a great time to try it and see for yourself what aspects can do!