开发者

AspectJ pointcut on method variable, is it possible?

I have 开发者_Go百科been using AspectJ for a while and it works great on object scope fields containing annotations. I just ran into a situation where I want to annotate a variable of method scope that will work with my pointcut but I am having trouble with it.

Here is the pointcut that I am using. It works fine if my variable is a field for the object, but if I reduce the scope to a method (variable declared inside the method), then it doesn't work anymore and I am not sure why. Let me know what I can do, thanks.

 after(final Trigger trigger): set(@Triggereable * *) && args(trigger)
 {
  System.out.println("trigger flush");
 }

Also, here is an exmaple of what I want to work. That System.out.println above should fire when the Trigger is instantiated:

public void foo()
{
   @Triggereable
   private Trigger trigger = new Trigger();
}


If you came to such situation, you probably trying to change implementation instead of applying actual cross cutting concerns. Basically, it is not what AOP and AspectJ is supposed to be used for.

As a work around, you can either extract relevant functionality into a separate method and then apply your aspects to that method or alternatively, you can replace an entire method with that local variable, using around advice.

More over, in your particular example, the pointcut can be applied to the constructor execution within scope of a given method, so you can do practically the same thing without binding to a local variable.


AspectJ does not currently support pointcuts on local variables (read the FAQ entry).

I seem to recall a recent discussion about such a feature possibly added soon, but I could not find it in the AspectJ issue tracker nor in the Mailing List archives

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜