开发者

@AspectJ pointcut for subclasses of a class with an annotation

开发者 https://www.devze.com 2023-03-29 12:56 出处:网络
I\'m looking for a pointcut that matches method executions in classes that subclass a class with a specific annotation. The excellent AspectJ cheat sheet helped me to create the follo开发者_JAVA百科wi

I'm looking for a pointcut that matches method executions in classes that subclass a class with a specific annotation. The excellent AspectJ cheat sheet helped me to create the follo开发者_JAVA百科wing pointcut:

within(@my.own.annotations.AnnotationToMatch *) && execution(* *(..))

This matches all method calls of a class A that carries the @AnnotationToMatch, but not method of a class B that extends A. How can I match both?


public aspect AnnotatedParentPointcutAspect {   

//introducing empty marker interface
declare parents : (@MyAnnotation *) implements TrackedParentMarker;

public pointcut p1() : execution(* TrackedParentMarker+.*(..));

before(): p1(){
    System.out.println("Crosscutted method: "
            +thisJoinPointStaticPart.getSignature().getDeclaringTypeName()
            +"." 
            +thisJoinPointStaticPart.getSignature().getName());
}
}


Another and simpler possibility is to declare the annotation as @Inherited - thus it applies to the subclasses as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号