Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] execution(method) and execution(constructor)

Hi All

I got a question about the difference between execution 
pointcut with methods and constructor, consider the following program:

public class TestSuperCalls {
	public static void main(String[] args) {
		Foo f = new SubFoo();
		f.bar();
	}
}

class Foo {
	public void bar(){}
}

class SubFoo extends Foo{
	public void bar(){super.bar();}
	public SubFoo(){super();}
}

aspect Testing {
	after(Foo f):execution(Foo.new(..))&&target(f){
		System.out.println(f+" created.");
	}
	after(Foo f):execution(* Foo.bar(..))&&target(f){
		System.out.println(f+" bar executed.");
	}
}

The output of the above program is:
SubFoo@35ce36 created.
SubFoo@35ce36 bar executed.
SubFoo@35ce36 bar executed.

Notice that the first advice was executed only once, which is 
what I expected, however, the second advice was executed twice.

If the line "super.bar();" is removed, the second advice will be 
executed only once.

My question is, why the execution pointcut behaves differently 
for methods and constructor?

I understand that when "super.bar();" is not present, the pointcut 
matches because SubFoo is also a Foo, so the execution of the overriden 
method SubFoo.bar() should be also considered the execution of Foo.bar().  

But why after adding a super call, the pointcut matches twice?
And why this is different for constructors?

thanks,
linton



Back to the top