0

I have this PointCut

@Pointcut("execution(@com.foo.bar.aspect.annotation.MyAnnotation* * (..))"
          + "&& @annotation(annot)")
public void anyFoo(MyAnnotation annot)
{

}

MyAnnotation looks like this:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation
{
   boolean isFoo();

   String name;
}

Let's say I annotate a method with this annotation with isFoo set to true

@MyAnnotation(isFoo = true, name = "hello")
public void doThis()
{
   System.out.println("Hello, World");
}

How do I write my pointcut so that it matches only method annotated with MyAnnotaion AND isFoo = true?

I tried this but it doesn't seem to work

@Pointcut("execution(@com.foo.bar.aspect.annotation.MyAnnotation(isFoo = true, *) * * (..))"
          + "&& @annotation(annot)")
public void anyFoo(MyAnnotation annot)
{

}
0x56794E
  • 20,883
  • 13
  • 42
  • 58
  • This question is still listed as unanswered. Would you please accept and upvote my answer if it seems appropriate? Thanks. – kriegaex Jun 09 '14 at 12:51

1 Answers1

2

You cannot write a pointcut like this because AspectJ does not support it. You need to use something like

@Pointcut("execution(@com.foo.bar.aspect.annotation.MyAnnotation* * (..))"
          + "&& @annotation(annot)")
public void anyFoo(MyAnnotation annot) {
    if (!annot.isFoo())
        return;
    // Only continue here if the annotation has the right parameter value
    // ...
}
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • +1 for the same thought :D Yeah, I thought of it, but I was just wondering if there's any better way to do it. – 0x56794E May 19 '14 at 14:27