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

Posted on December 20, 2005


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!