I am instrumenting various methods with ByteBuddy's @Advice.OnMethodEnter and @Advice.OnMethodExit and generally do not want any errors/exceptions in the added code to impact on the original code in these methods. However, there are a few specific exceptions that are thrown in the instrumented code that are required for other behaviour and cannot just be suppressed using .withExceptionHandler(Advice.ExceptionHandler.Default.SUPPRESSING) and suppress = Throwable.class.
I am looking for a way to log and suppress most exceptions thrown in the instrumented code whilst allowing/rethrowing a few specified exceptions.
So far I have looking into creating and using a new ExceptionHandler that implements Advice.ExceptionHandler, this is then added using .withExceptionHandler(newExceptionHandler). Pseudocode for this is:
class newExceptionHandler implements Advice.ExceptionHandler {
@Override
public StackManipulation resolve(MethodDescription instrumentedMethod, TypeDescription instrumentedType) {
if (allowedExceptions.contains(currentException)) {
return Throw.INSTANCE;
} else {
// LOG EXCEPTION
return Removal.SINGLE;
}
}
}
However, I could not find a way to access the type of the thrown exception in resolve and this open issue leads me to believe this may not be possible currently. I have a similar issue of being unable to access the exception when attempting to use MethodInvocation.invoke().
Is there a way to access and use the type of exception in the ByteCode generated by resolve/invoke or any other way to have more customisable exception handling with ByteBuddy and avoid wrapping all the instrumented code with try catch blocks?
Thanks for any help.