Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] after advice with overridden methods

I think the aspect is behaving correctly.

Programming Guide, Semantics appendix:

  At a method execution join point, the signature [is]
  a method signature whose qualifying type is the
  declaring type of the method.

So a method-execution pointcut will match not only the declaring
type, but also any implementation of that method (i.e., any
overriding methods).

Rather than cflow, try

   execution(void init()) && within(A)

This is staticly-determinable (read: more efficient).

You might also prefer to use call(..).

Wes

Lendvai Attila wrote:
hello all,

if i have the following setup: (three classes, extending each other and
overriding a function)

class A
{
        void init()
        {
                System.out.println("A.init()");
        }
}

class B extends A
{
        void init()
        {
                System.out.println("B.init()");
super.init();
        }
}

class C extends B
{
        void init()
        {
                System.out.println("C.init()");
super.init();
        }
}

private static aspect Chain {
        pointcut f() : execution(void A.init());

        after(A a) : f() && this(a)
        {
                System.out.println("After init, this [" + a + "]
thisJoinPoint [" + thisJoinPoint + "]");
        }
}


in the main:

C c = new C();
c.init();

output:

C.init()
B.init()
A.init()
After init, this [com.netvisor.nvsr.client.Factorial$C@1fc2fb]
thisJoinPoint [execution(void
com.netvisor.nvsr.client.Factorial.A.init())]
After init, this [com.netvisor.nvsr.client.Factorial$C@1fc2fb]
thisJoinPoint [execution(void
com.netvisor.nvsr.client.Factorial.B.init())]
After init, this [com.netvisor.nvsr.client.Factorial$C@1fc2fb]
thisJoinPoint [execution(void
com.netvisor.nvsr.client.Factorial.C.init())]



shouldn't be there only one After init message? note that it's A.init(),
not A+.init() in the pointcut! changing to A+ does not have any effect
on the output.

i can workaround the problem by adding && !cflowbelow(f()) to the
advice, but in a much more complex scenario it does not get called for
all the object i would like to... :(

so my question is: is everything ok with the above output?

thanks in advance,

101





Back to the top