Walter
Walter

Reputation: 1470

aspectj not intercepting constructors where class is annotated

I would like to intercept all constructor invocations where the class is annotated with a specific annotation.

I have an aspect like this:

@Aspect
public class ConstructorClassLevelAspect {
  @Before(
      "execution(*.new(..)) && @annotation(ConstructorClassLevel)")
    public void doConstructorClassLevel(
      JoinPoint point) throws Throwable {
    System.out.println("constructed" + point);
  }
}

And an instance:

@ConstructorClassLevel
public class ConstructorClassLevelExample {

}

Now, if I change the aspect and remove the @annotation filter, then I see aspectj is intercepting the call. Additionally, if I create a default constructor, then annotate it with the annotation, it also works.

But, I want the annotation to live on the class so that if I have 1 constructor or 10, they'll all be intercepted the same (and I only have to put it on the class).

Upvotes: 1

Views: 284

Answers (1)

Walter
Walter

Reputation: 1470

It seems that if I do @within(ConstructorClassLevelExample), it works!

Upvotes: 0

Related Questions