[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Aspects and overridden methods

Hi,

I have a problem with the following code example:

 class Parent
 {
	public void foo()
	{
		System.out.println("Parent::foo()");
	}
 }

class Child extends Parent
{
    public void bar()
    {
		System.out.println("Child::bar()");
    }

	public void foo()
	{
		System.out.println("Child::foo()");
		super.foo();
		bar();
	}
}

aspect ParentAspect
{
    pointcut to_foo() : execution(void Parent.foo());

	after() : to_foo()
	{
    	System.out.println("after calling: " + thisJoinPoint.getSignature());
	}
}

When compling this example and executing the following main:

public static void main(String[] args)
{
	Child c = new Child();
	c.foo();
}

... I get this output:

Child::foo()
Parent::foo()
after calling: void Parent.foo()
Child::bar()
after calling: void Child.foo()

Obviously the pointcut matches both foo() methods, although I wanted to
match only Parent.foo(). When trying a call instead the execution pointcut
the result is that Child.foo() is matched only (which is clearly not
intended):

Child::foo()
Parent::foo()
Child::bar()
after calling: void Child.foo()


For both cases (execution and called) I had expected that only the
Parent.foo() method was matched. Am I thinking wrong? To my opinion there
must be a possibility to match only the Parent.foo() method without using
the within pointcut, etc. (Imaging the Child class is added subsequently,
in a later development step.)

thanks in advance

Sven