Walter
Walter

Reputation: 1470

aspectj not intercepting methods with annotation

I am trying to get aspectj to intercept annotated methods:

@Aspect
    public class InterceptMeAspect {
      @Around("execution(* *(..)) && within(@InterceptMe *)")
      public Object doIntercept(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("intercepted:");
        return proceedingJoinPoint.proceed();
      }
    }

    public class InterceptedExample {
        @InterceptMe
        public void doSomething(){

        }
    }

I removed the !within(InterceptMeAspect) for brevity, but it isn't intercepting too much anyways. If I remove the annotation constraint (within(@InterceptMe *)), it works, but intercepts everything and makes for a big problem.

The output bytecode appears to have the annotations intact, so I would expect the annotation criteria to match. I am doing or attempting to do compile-time weaving. This is important because I have another aspect that does work using the same approach above. I suspect that aspect is messing with this one, but the final bytecode shouldn't have the annotation, right?

EDIT: This is the code for the other aspect:

@Around(
      "execution(protected * *(..)) && !within(com.walterjwhite.logging..*) && !call(*.new(..)) && within(@ContextualLoggable *) && !within(@NonLoggable *)")

I have a generic logging aspect and a special contextual logging aspect. I am guessing this is incorrectly written as well and should be following the format from above.

Upvotes: 0

Views: 288

Answers (2)

Cosmin Constantinescu
Cosmin Constantinescu

Reputation: 305

As you use a custom annotation, you should use @annotation instead of within:

@Aspect
    public class InterceptMeAspect {
      @Around("execution(* *(..)) && @annotation(com.or.org.package.InterceptMe)")

Upvotes: 0

Sukhpal Singh
Sukhpal Singh

Reputation: 2288

Instead of @Around("execution(* *(..)) && within(@InterceptMe *)"). It should be @Around("execution(* *(..)) && @annotation(your.package.InterceptMe )")

Or, if you need to access some properties from annotation:

@Around("execution(* *(..)) && @annotation(interceptMeVar)")
public Object doIntercept(ProceedingJoinPoint proceedingJoinPoint,InterceptMe interceptMeVar)

Upvotes: 1

Related Questions