Open-api: Annotation based interception of arbitrary method execution Follow
Is there an easy way to intercept arbitrary methods annotated with a specific marker annotation and execute code around them?
I know that @org.jetbrains.annotations.NotNull is intercepted on runtime to log contract violations, so obviously this is possible.
Here my specific use case
e.g.
my.annotation.MyAnnotation // marker annotation
my.annotation.MyAnnotationHandler // wrapper for methods annotated with MyAnnotation
// custom annotated method
@MyAnnotation
void doSomething() {...}
// annotation handler
// interception logic should wrap the execution of doSomething()
Object handle() {
try {
return ... //intercepted method invocation
} catch (Exception e) {
... //do some custom handling
throw e;
}
}
Please sign in to leave a comment.
@NotNull is not intercepted at runtime. If you look at the decompiler output, the `if ( != null)` check is compiled into the code, so the annotation has a compiler hook that adds the code.