5

I am playing around with aop and aspectj and discovered an (for me) unexpected behavior.

In the aspectj-docs I found the following example-pointcut:

execution(public void Middle.*())

for the following class-definitions (I slightly changed the original example):

class Super {
    public void m() { ... }
}
class Middle extends Super {
}
class Sub extends Middle {
    @Override
    public void m() { ... }
}

The description of that exmaple states:

[The pointcut] picks out all method executions for public methods returning void and having no arguments that are either declared in, or inherited by, Middle, even if those methods are overridden in a subclass of Middle.

This example is working fine for me but if class Sub is not overriding m(), the method-calls from outside to m on a Sub-instance are not intercepted. Doesn't this violates the doc?

I had another problem with pointcuts in inhertied classes which is caused by the use of proxies. But in this case the use of a proxy cannot cause this behavior because the proxy should provide methods for all of the proxied class. Or did I missed something?

my aspect-definition:

@Aspect
public class MyAspect {
    @Before(value = "execution(* Middle.*(..))", argNames="joinPoint")
    public void myAdvice(JoinPoint joinPoint) {
        System.out.println("adviced: " + joinPoint.getSignature());
    }
}
Community
  • 1
  • 1
thomas
  • 5,637
  • 2
  • 24
  • 35
  • In the sample code in the documentation you linked to `Super`'s method was `protected` and not `public`. Is it the case that in your actual code the equivalent class to `Super` has `protected` methods and not `public` ones? That might explain the behaviour. – Emil L May 28 '12 at 14:03
  • yes, you are right, I modified the example a bit to be able to call the method of `Super` from outside. But the description states that the pointcut will intercept all methods of `Middle` and all inherited ones to which `m()` should belong to, shouldn't it? – thomas May 28 '12 at 14:09
  • Are you calling the method on a variable declared as `Super`, `Middle` or `Sub`. If it is `Super` it might be that it isn't considered to be the correct type. – Emil L May 28 '12 at 14:20
  • I call the method on an instance of 'Sub' – thomas May 28 '12 at 14:34
  • 1
    I stumbled on the very same problem today, read the exact same doc and got puzzled just as you are. (yes ... more than 3 years later) so my question is exactly the same as yours ! Did you come by a satisfying answer ? And if not, would you mind if I bounty your question ? – Ar3s Jun 03 '15 at 16:26
  • Hi @Ar3s, actually I dont remember any more. But I think this is still an interesting question (even if I dont need it right now). Feel free to bounty this question. – thomas Jun 11 '15 at 13:58
  • actually I reformulated (i wanted examples to be differently stated) and bountied mine – Ar3s Jun 11 '15 at 15:09

0 Answers0