AspectJ pointcut on a method variable, is this possible?

I have been using AspectJ for a while, and it works great with object area fields containing annotations. I just came across a situation where I want to annotate a method area variable that will work with my pointcut, but I am having problems with it.

Here is the item that I am using. It works fine if my variable is a field for an object, but if I reduce the scope to a method (a variable declared inside the method), then it no longer works, and I don't know 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 example of what I want to work with. This System.out.println above should fire when a trigger instance is created:

 public void foo() { @Triggereable private Trigger trigger = new Trigger(); } 
+4
source share
2 answers

AspectJ does not currently support pointcuts for local variables (read frequently asked questions ).

It seems that I recall a recent discussion about such an opportunity that may be added in the near future, but I could not find it in AspectJ problem tracking and in the mailing list archive

+1
source

If you come to this situation, you are probably trying to change the implementation, instead of applying the actual cross-cutting problems. Basically, this is not what AOP and AspectJ should be used for.

As a job, you can either extract the corresponding functionality into a separate method and then apply your aspects to this method or, alternatively, you can replace the entire method with this local variable using tips.

Moreover, in your specific example, the pointcut can be applied to the execution of the constructor within the framework of this method, so you can do almost the same without binding to a local variable.

+5
source

Source: https://habr.com/ru/post/1332786/


All Articles