1
votes

I am trying to write an advice to intercept the calls to constructors of a class with my custom annotation:

@MyCustomAnnotation
public class SomeClass {

   public SomeClass(Foo a, Bar b){
      ...
   }

   public SomeClass(Foo a){
      this(a, null);
   }

}

I see an example of how to intercept constructor calls, in general:

@Before("execution(*.new(..))")

How do I update this to only execute for classes that are annotated with my @MyCustomAnnotation annotation

1

1 Answers

1
votes

I use this for method invocation:

within(@MyCustomAnnotation *)

So, the resulting aspect code would be:

@Before("execution(*.new(..)) && within(@MyCustomAnnotation *)")

Alternatively, try this:

@Pointcut("execution(@MyCustomAnnotation *.new(..))")

I am referring to the documentation here: https://blog.espenberntsen.net/2010/03/20/aspectj-cheat-sheet/

and, it should work in theory, but I am not having any luck.