Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] What's the different between call and execution

On 4-jun-08, at 18:32, Dean Wampler wrote:
For calls, the advice is inserted just before transferring control to the method. For execution, the advice is inserted just after transferring control, within the stack frame of the method. (This is my naive way of viewing it.)

Simply put, the call join point occurs before method lookup, while the execution join point occurs after.

As a consequence, if your advice changes the receiving object (with around advice) to some object of a different class, then the choice between call and execution determines which class' method will be executed. In case of call, method lookup will occur with the new object, so the method from the new object's class will be executed. In case of execution, method lookup will occur with the old object, so the method from the old object's class will be executed (with "this" bound to the new object!).

If you would want to, you can use this to force static binding, by temporarily changing the receiving object, as shown below.

public aspect LookupDemo {

	public static class A {
		public String toString() {
			return "Informative: " + super.toString();
		}
	}
	
	public static class B extends A {
		public String toString() {
			return "Not informative";
		}
	}

	public static void print(A value) {
		System.out.println(value.toString());
	}
	
	public static void main(String[] args) {
		print(new A());
		print(new B());
	}
	
// The following declarations will force static binding for A.toString()

	private A target;
	
	String around(A a): call(String A.toString()) && target(a) {
		target = a;
		return proceed(new A());
	}
	
	String around(A a): execution(String A.toString()) && target(a) {
		return proceed(target);
	}
	
}

Regards,
Bruno De Fraine



Back to the top