« AspectJ 1.2 and AJDT 1.1.10 released | Main | Person owns Dog... »

May 27, 2004

Load-time weaving with AspectJ 1.2

After breathing a sigh of relief following the release of AspectJ 1.2, today I thought I'd discuss how to exploit one the new features in the 1.2 release - load-time weaving from the command-line. I'm going to use a variation on the same simple Boy/Girl/Teacher example that I discussed last week. Let's get the boring bit out of the way and show you the code for the basic application first:

In Kissable.aj: public interface Kissable { void kiss(); } In Boy.aj public class Boy implements Kissable { public void kiss() { System.out.println("wahay!"); } } In Girl.aj public class Girl { private Kissable k; public Girl(Kissable k) { this.k = k; k.kiss(); } } In Main.aj public class Main { public static void main(String[] args) { Girl g = new Girl(new Boy()); } }

I compiled this with the command:

ajc *.aj

You can run the application by typing "java Main", but I've chosen to add the new "aj" script that ships with AspectJ 1.2 to my path, and I'm going to use that to launch the application instead (it makes no difference at this stage). You will find the "aj" script in the doc/examples/ltw directory of your AspectJ installation.

aj Main
> wahay!

Time to introduce the Teacher. Here's a really simple variation:

In Teacher.aj: public aspect Teacher { after() returning : execution(* Kissable.kiss()) { System.out.println("Ahem..."); } }

I'm going to compile the Teacher aspect directly into an aspect library. Here's the command-line incantation to do it:

ajc Teacher.aj -outjar teacher.jar

At this point, I could run "aj Main" again and it would produce the same output as before, nothing has changed (we didn't link the Teacher with the rest of our application, we compiled it independently into a library).

Finally, we get to set a new environment variable that the "aj" script looks for: ASPECTPATH. (ASPECTPATH is to aspect libraries as CLASSPATH is to class libraries).

set ASPECTPATH=teacher.jar

Now if I run the application again:

aj Main
> wahay!
> Ahem...

you'll see that AspectJ has linked the application classes with the aspect library as they were loaded. It's as simple as that...

Posted by adrian at May 27, 2004 04:05 PM [permalink]

Comments

I have to say that it looks cool, but lets get out of the marketing noise.

This is definitely not a full blown load time weaving implementation, since it is using this -Djava.system.class.loader=... option of java 1.4, that actually inserts a classloader after the system classloader, that will be somehow the new system classloader.


It just can't work in the J2EE world where a hierarchy of classloader exists, and where it makes sense to have load time weaving.


This topic has been part of AOSD 2003 and as a reminder as we explained it at AOSD 2004, AspectWerkz still looks to be the single AOP solution that enables truely cross platform load time weaving (java 1.3, 1.4, HotSpot, JRockit, IBM VMs).

I would not say It's as simple as that..

Posted by: Alex Vasseur at May 28, 2004 03:12 AM

:)


The top of the article explicitly states that I'm discussing load-time weaving from the command-line, and whilst this is something that AspectJ has been capable of since the 1.1 release, it's not something we've especially gone after. If you notice, the aj script is even in the doc/examples directory rather than in the bin directory because we are well aware that this is not a full implementation. Still, for what it does, I think it does it quite cleanly.


For use with application servers etc. AspectJ 1.2 ships a weaving class loader and an adapter that make integrating load-time aspect linking into the kind of class-loading schemes that you find in an app server much easier. Even before AspectJ 1.2, some vendors (including BEA) were writing their own classloader based implementations exploiting the binary weaving capabilities that we introduced in AspectJ 1.1. AspectJ 1.2 just makes this a little bit easier to do.


But AspectWerkz has the most complete load-time weaving story of any aspect implementation I've seen to date - you won't find me arguing with that.

Posted by: Adrian Colyer at May 28, 2004 12:03 PM

Forgot to comment that JSR-163 will standardize an efficient class load time weaving and this is a good step ahead for all AOP solutions. The class load time weaving trick will soon belong to history.
In the meantime it is good that you take time to write articles about this feature in general.
--------

Posted by: Alex Vasseur at May 29, 2004 07:44 AM

Post a comment

Thanks for signing in, . Now you can comment. (sign out)

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)


Remember me?