I try to intercept calls to methods and calls to Java 8 lambda expressions using a Byte Buddy AgentBuilder
as follows:
static {
final Instrumentation inst = ByteBuddyAgent.install();
new AgentBuilder.Default()
.type(ElementMatchers.nameContainsIgnoreCase("foo"))
.transform((builder, typeDescription) ->
builder.method(ElementMatchers.any())
.intercept(MethodDelegation.to(LogInterceptor.class)))
.installOn(inst);
}
public static class LogInterceptor {
@RuntimeType
public static Object log(@SuperCall Callable<?> superCall) throws Exception {
System.out.println("yeah...");
return superCall.call();
}
}
I'm using Byte Buddy v0.7.1.
It can intercept the following Runnable
(anonymous class):
FunnyFramework.callMeLater(new Runnable() {
@Override
public void run() {
System.out.println("Hello from inner class");
}
});
and of course any calls to objects defined as normal (non-anonymous) classes. But the interception does not work for lambda expression's like:
FunnyFramework.callMeLater(() -> {
System.out.println("Hello from lambda");
});
How can I intercept also the lambda expression calls? There's no such thing as a LambdaInterceptor in Byte Buddy, as far as I know.