Pointcut to match all calls to public methods, except calls to self
I am attempting to write an aspect which monitors calls to public methods on a variety of objects, but ignore calls to self. For this, I have an aspect like this:
abstract aspect MonitorPublicMe开发者_开发技巧thodCalls { abstract pointcut matchingType(); pointcut matchingCall(): call(public * *(..)); pointcut notSelf(); Object around(Object t): matchingType() && matchingCall() && notSelf() { // do something useful with the call if(/* allow call? */) { return proceed(); } else { throw new IllegalStateException("Badness!"); } } }
This will monitor all calls to all public methods, but in order to ignore calls to self I would really like to define notSelf
as something like
pointcut notSelf(Object o): target(o) && !this(o)
However for this I get an error: "negation does not allow binding" on the negation of this(o)
. Right now as a workaround I check within the advice body whether the target is equal to this and if so bypass the other logic, however this means the aspect is being weaved into more places than are actually necessary. Is it possible to define a constraint to avoid calls to self?
Try this:
pointcut selfAndTarget(Object tgt, Object thiz) : target(tgt) && this(thiz)
pointcut notSelf(): selfAndTarget(tgt, thiz) && if(tgt != thiz)
精彩评论