Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspectJ pointcut on method variable, is it possible?

I have 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();
}
like image 425
smuggledPancakes Avatar asked Dec 20 '10 16:12

smuggledPancakes


People also ask

What is pointcut what is advice?

Pointcut: Pointcut is expressions that are matched with join points to determine whether advice needs to be executed or not. Pointcut uses different kinds of expressions that are matched with the join points and Spring framework uses the AspectJ pointcut expression language.

What is Pointcut in AspectJ?

AspectJ provides primitive pointcuts that capture join points at these times. These pointcuts use the dynamic types of their objects to pick out join points. They may also be used to expose the objects used for discrimination. this(Type or Id) target(Type or Id)

Which of the following AspectJ Pointcut designators are supported in Spring AOP?

Spring AOP supports the following Pointcut Designators (PCD). execution – for matching method execution join points. This is the most widely used PCD. within – for matching methods of classes within certain types e.g. classes within a package.

Which of the following AOP advice can access and modify arguments of a Joinpoint 1 @AfterReturning 2 @around?

After returning is an advice in Spring AOP that invokes after the execution of join point complete (execute) normally. It does not invoke if an exception is thrown. We can implement after returning advice in an application by using @AfterReturning annotation.


1 Answers

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.

like image 65
Eugene Kuleshov Avatar answered Sep 28 '22 03:09

Eugene Kuleshov