Skip to content

Multiple Targets

Advice and target annotations are many-to-many: one advice can name several annotations, and one annotation can be named by several advices.

One Advice, Multiple Targets

One @Before method can target several annotation classes:

@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.BINARY)
annotation class Logged

@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.BINARY)
annotation class Traced

@Aspect
object ObservabilityAspect {
    @Before(target = [Logged::class, Traced::class])
    fun observe(joinPoint: JoinPoint) {
        println("Intercepting: ${joinPoint.signature.methodName}")
    }
}

The advice runs for any function annotated with @Logged, @Traced or both.

Multiple Advice, One Target

Several @Before methods, in the same aspect or in different ones, can target the same annotation:

@Aspect
object LoggingAspect {
    @Before(target = [Audited::class])
    fun log(joinPoint: JoinPoint) {
        println("LOG: ${joinPoint.signature.methodName}")
    }
}

@Aspect
object AuditAspect {
    @Before(target = [Audited::class])
    fun audit(joinPoint: JoinPoint) {
        auditService.record(joinPoint.signature.methodName, joinPoint.args)
    }
}

@Audited
fun deleteAccount(userId: String) {
    // Both log() and audit() run before this body
}

Combining Multiple Annotations on a Function

A function can carry several target annotations, and every matching advice applies:

@Logged @Traced @Metered
fun processCheckout(cart: Cart, userId: String) {
    // All three aspects run their advice before this body
}

The advices of the annotation written first come first, so reordering the annotations on the function reorders the advices. See Advice Ordering.

Identifying the Trigger Annotation

Inside advice, JoinPoint.signature.annotations lists the annotations on the intercepted function:

@Aspect
object DispatchAspect {
    @Before(target = [Logged::class, Traced::class])
    fun dispatch(joinPoint: JoinPoint) {
        val annotationNames = joinPoint.signature.annotations.map { it.typeName }
        if ("com.example.Logged" in annotationNames) {
            logger.info(joinPoint.signature.methodName)
        }
        if ("com.example.Traced" in annotationNames) {
            tracer.start(joinPoint.signature.methodName)
        }
    }
}