Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@AspectJ pointcut for subclasses of a class with an annotation

Tags:

java

aop

aspectj

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 following 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?

like image 914
Hans-Peter Störr Avatar asked Aug 19 '11 13:08

Hans-Peter Störr


People also ask

What is pointcut annotation?

Pointcut is a set of one or more JoinPoint where an advice should be executed. You can specify Pointcuts using expressions or patterns as we will see in our AOP examples. In Spring, Pointcut helps to use specific JoinPoints to apply the advice.

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)

What methods does the pointcut expression reference?

This pointcut matches any method that starts with find and has only one parameter of type Long. If we want to match a method with any number of parameters, but still having the fist parameter of type Long, we can use the following expression: @Pointcut("execution(* *.. find*(Long,..))")

What is pointcut expression reference?

Pointcut is an expression language of spring AOP which is basically used to match the target methods to apply the advice. It has two parts ,one is the method signature comprising of method name and parameters. Other one is the pointcut expression which determines exactly which method we are applying the advice to.


1 Answers

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());
}
}
like image 88
alehro Avatar answered Sep 23 '22 13:09

alehro