« September 2004 | Main | December 2004 »

October 05, 2004

Three paths stand before you...

There's been some confusion lately concerning which classes will be woven by AspectJ, and why "type not exposed to weaver" messages are issued even when the type is on the classpath. In this short entry I want to explain the difference between CLASSPATH, INPATH, and ASPECTPATH.

CLASSPATH

CLASSPATH is, well, a classpath. Types on the classpath will be found (resolved) by AspectJ during compilation and weaving, exactly as a classpath would be used when compiling with javac. Importantly, also just like javac, types on the classpath are unchanged by the compilation and weaving process - they are for lookup only. So if you just put a type on the classpath, it can be found by the compiler/weaver for resolution purposes, but it is not exposed to the weaver for linking with aspects. To translate that into an AJDT scenario, when you create a project dependency between two projects in AJDT the output of the referenced project is placed on the classpath of the dependent project. So the types in the referenced project are visible to the compiler, but will not be affected by the weaving process. If you want types in the referenced project to be linked with aspects in the dependent project, you need another kind of path...

INPATH

Types placed on the INPATH (or specified via the injars option in older versions of ajc) serve a dual purpose. They are both available for type resolution (as types on the classpath), but are also exposed to the weaver for linking with aspects. Thus the output of a compilation/weave with types specified on the inpath will result in new (possibly modified) versions of those types being written to the output destination of the compilation. Using AJDT, you specify inpath entries using the AspectJ Inpath page of the project properties.

ASPECTPATH

What happens when some of the types specified on the classpath or inpath are aspects? These aspects will be woven into (if other aspects affect them), but any advice or inter-type declarations contained in such aspects will not be woven with any other types. If you want an aspect (in class file form) to be woven with types exposed to the weaver, you need to place it on the aspectpath. We often refer to jar files containing aspects that are placed on this path as "aspect libraries". In AJDT, if you had one project defining a collection of aspects, and another project that wanted to use those aspects, you would proceed as follows:

  • In the aspect library project, use the "outjar" option on the AspectJ page of the project properties to have AJDT place the output of the compilation into a jar file.
  • In the project using the library, put the outjar created by the library project onto the aspectpath using the AspectJ Aspectpath page of the project properties.

Source files

It's worth just mentioning that any types passed to the AspectJ compiler in source form (ie., in .java or .aj files) will be compiled, woven into, and used to weave, as necessary. The prior discussion about classpath, inpath, and aspectpath applies to binary .class files.

Three paths stand before you.

The next time three paths stand before you, I hope you'll be a little better informed as to which road(s) to travel :).

Posted by adrian at 08:26 AM [permalink] | Comments (0)