Lieven Doclo
Lieven Doclo

Reputation: 101

AspectJ annotation pointcut not triggered for interface annotations

How would one create a pointcut that matches methods that are annotated with an annotation or whose interface method is annotated with an annotation?

public interface IService {
    @SomeAnnotation
    public void someMethod() { ... }
}

public class Service implements IService {
    public void someMethod() { ... }
}

I'm using Spring AOP for AspectJ autoproxying. However, if I define a pointcut like

execution(public * *(..)) && @annotation(SomeAnnotation)

the advice is not applied on the someMethod method on Service.

What would be the correct pointcut to match that method, only using the fact that the interface method has an annotation on it? Is it possible?

If I put the annotation on the Service someMethod method, the above pointcut works, but in my case, this is not a good solution, as the annotation is part of the contract and must therefor be part of the interface, not the implementation.

Upvotes: 2

Views: 4354

Answers (1)

Leonard Brünings
Leonard Brünings

Reputation: 13222

According to the Java 5 specification, non-type annotations are not inherited, and annotations on types are only inherited if they have the @Inherited meta-annotation.

http://www.eclipse.org/aspectj/doc/released/adk15notebook/annotations-pointcuts-and-advice.html#annotation-inheritance-and-pointcut-matching

Upvotes: 1

Related Questions