Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Combining AspectJ and MultiJava (multimethods)

Because I tend to like the multimethods implementation in MultiJava over the inter-type declarations in AspectJ and because MultiJava has nothing like pointcuts and advice, I was wondering if I would be able to combine the best of both worlds.

As far as I know, only AspectJ is capable of weaving aspects into bytecode. So it seamed evident that I would create some classes, aspects (aspectj) and top-level methods (multijava), and than let the multijava compiler do its job, and the aspectj compiler after that.

I created a dummy "Person" class:

Public class Person {
	public String name;
	public int age;
	//plus evident getters and setters and a constructor.
}

I added a top-level method to that class with multijava

public void Person.sayMyName () {
	System.out.println ("my name is: " + getName());
}

Then I compile (with multijava), and put the results into a jarfile together with a simple "Main" class. This "Main" class looks like this:

public class Main {
	public static void main(String[] args) {
		Person p = new Person ("Jan",21);
		
		//multijava method
		p.sayMyName();
		
		//java method with aspectj advice on it
		p.getAge();
	}

This all works perfectly,

Now I want some advice on the "getAge()" method:

public aspect AgeCatcher {	
	pointcut age () : execution (public int Person.getAge());
	after () : age () {
		System.out.println ("getAge() was executed");
	}
}

I want to add that aspect to the existing bytecode in the jar file, so I call ajc with the arguments "-injar myjarfile.jar" and "-outjar myoutputjarfile.jar" and the above aspect. This works, besides a warning about being unable to update the manifest file in the jar. After updating that manifest file by hand, en executing the jar file, I receive an error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/aspectj/lang/NoAspectBoundException
        at multijava.Person.getAge(Person.java:28)
        at justJava.Main.main(Main.java:21)

(Person.java:28 is the getAge() method)
(Main.java:21 is the getAge() call on p)

After thinking a very little bit about the way MultiJava compiles top-level methods into classes, I can imagine that my experiment is not so evident to work, because -to my knowledge- the bytecode of the class Person is not changed at all by the multijava compiler. But I was wondering if anyone can think of a way to make it work...

Thanks in advance for your comments,
Jan Van Besien



Back to the top