Is it possible to pass annotation as an argument of a function?
I wanted to invoke every method annotated with annotation XY and in other situation every method annotated with YZ.
I have a following function:
fun process(annotation: Annotation, eyeTrackingContext: EyeTrackingContext) {
subscriberSet.forEach { instance ->
run {
instance::class.declaredMemberFunctions.forEach { f ->
run {
when (annotation) {
is OnFaceAppeared -> {
f.findAnnotation<OnFaceAppeared>()?.let {
f.call(instance, eyeTrackingContext)
}
}
is OnFaceLost -> {
f.findAnnotation<OnFaceLost>()?.let {
f.call(instance, eyeTrackingContext)
}
}
is OnContextUpdate -> {
f.findAnnotation<OnContextUpdate>()?.let {
f.call(instance, eyeTrackingContext)
}
}
else -> {
Log.v(TAG, "Method does not specify any allowed annotation.")
}
}
}
}
}
}
}
and I would like to perform different operation based on which annotation is passed as an argument. Is there any change to do that in Kotlin? Or I’d have to create N functions with a slightly different name that would call desired function?
To explain the function a bit:
- it goes through every class that subscribed for notifications
- then it checks if there are any methods annotated with custom annotations (OnFaceAppeared, OnFaceLost, OnContextUpdate)
- if it finds desired annotation (is just not null), then it calls function
- otherwise does nothing