I want to intercept some methods with ByteBuddy.
When i use InvocationHandlerAdapter.of(invocationHandler)
i can't invoke super method. Holding object instance is not suitable for my case. I want exactly as in CGLIB like below.
(MethodInterceptor)(obj, method, args, proxy)->{
// to do some work
Object o = proxy.invokeSuper(obj,args);
// to do some work
return o;
}
How can i achieve intercepting method in ByteBuddy like this?
I tried MethodCall
type of Implemetation
but it not solved my problem. Because i can't manage MethodCall.invokeSuper()
in this case.
.intercept(MethodCall
.run(() -> System.out.println("Before"))
.andThen(MethodCall.invokeSuper())
.andThen(MethodCall
.run((() -> System.out.println("After")))))
Have a look at MethodDelegation
, for example:
public class MyDelegation {
@RuntimeType
public static Object intercept(@SuperCall Callable<?> superCall) throws Exception {
// to do some work
Object o = superCall.call();
// to do some work
return o;
}
}
Then using:
.intercept(MethodDelegation.to(MyDelegation.class))
You can check the javadoc for MethodDelegation
for more annotations that you can use to inject contextual information.