Simon Guo
Simon Guo

Reputation: 2926

AspectJ Pointcut on Methods with Multiple Annotations

Use load-time weaving, pure AspectJ.

We have 2 annotations @Time and @Count, and a few annotated methods.

@Time (name="myMethod1Time")
@Count (name="myMethod1Count")
public void myMethod1(){..};

@Time (name="myMethod2Time")
public void myMethod2(){..};

@Count (name="myMethod3Count")
public void myMethod3(){..};

Now I am defining my own around aspect for myMethod1 which has multiple annotations:

// multiple annotations, not working
@Around("@annotation(time) && @annotation(count))
public Object myAspect(Time time, Count count) {..}

This doesn't work. However, capture method myMethod2 works fine with a single annotation:

// single annotation, is working
@Around("@annotation(time))
public Object myAnotherAspect(Time time) {..}

I want to capture only methods with both Time and Count annotations present in their signature, and I want to use the annotation value. Anyone know how to achieve this?

Upvotes: 3

Views: 3278

Answers (1)

Tokazio
Tokazio

Reputation: 530

Maybe combine 2 pointcuts like:

@Around("call(@Time * *(..)) && call(@Count * *(..))");

Upvotes: 1

Related Questions