vach
vach

Reputation: 11377

Aspect pointcut to use annotation property

There is a TraceAspect which should execute some logging on any method or class (all methods of class) annotated with Trace annotation.

@Aspect
public class TraceAspect {    
    @Pointcut("(@annotation(Trace) || @within(Trace)) && execution(* *(..))")
    void allAnnotated(){}

    @Around("allAnnotated()")
    public Object trace(final ProceedingJoinPoint joinPoint) throws Throwable {
        // doing some stuff here
    }
}

and the annotation:

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Trace {
    boolean enabled() default true;
}

I need to modify pointcut so that all methods/classes that have Trace.enabled setted to false to be skipped (not considered as join points).

@Pointcut("allAnnotated() && Trace.enabled")

or (if its not possible) at least have that Annotation and its value in the advise so that I could check for property and skip logging...

Upvotes: 0

Views: 699

Answers (1)

Andy Clement
Andy Clement

Reputation: 2560

Take a look at 'annotation value matching' in https://eclipse.org/aspectj/doc/released/README-160.html

What you can do is:

@Pointcut("execution(@Trace(enabled=true) * *(..)) || execution(* (@Trace(enabled=true) *).*(..))")

The first is method level annotations, the second is type level annotations. The syntax for embedding values in @annotation/@within isn't there yet (so you can't do @annotation(Trace(enabled=true))

Upvotes: 2

Related Questions