Using Byte Buddy's advice API, is it possible to return from the instrumented method without actually executing it?
One use case would be to implement a cache and to return the cached value, if present, instead of computing the value again.
@Advice.OnMethodEnter
public static Object returnCachedValue(@Advice.Argument(0) String query) {
if (cache.containsKey(query)) {
// should "abort" method call
return cache.get(query);
}
}
I know that this code sample above just creates a local variable which I can get in a @Advice.OnMethodExit
method. But is there a way to abort the method call on an explicit return
? If yes, is this also possible for void
methods?