I am trying to create a logging system that will take a series of methods. Some of the methods that I am intercepting have 0 arguments, others many. My question is: can I create a generic @Advice.OnMethodEnter
that could capture the arguments using @Advice.AllArguments
with ByteBuddy?
The method looks like that:
@Advice.OnMethodEnter
public static void intercept(@Port int port,
@Host String host,
@Advice.Origin("#t:#m") String signature,
@Advice.AllArguments Object[] arguments)
{
AgentHelper.log(host, port, signature, Arrays.toString(arguments));
}
Everything works fine when the annotation @Advice.AllArguments
is missing, but I soon as I put it, all the methods with no arguments are ignored.
I also tried to to see if there is an explicit error raised using:
new AgentBuilder.Default()
...
.with(AgentBuilder.Listener.StreamWriting.toSystemOut().withErrorsOnly())
.with(AgentBuilder.InstallationListener.StreamWriting.toSystemOut())
...
but this didn't show any error. Do I have to create interceptor case by case or is there a way to acquire this behavior in a way I don't understand?
Yes, this should work without a problem:
public class Example {
public static void main(String[] args) {
new AgentBuilder.Default()
.disableClassFormatChanges()
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.type(is(Target.class))
.transform((builder, typeDescription, classLoader, module) ->
builder.visit(Advice.to(Example.class).on(isMethod())))
.installOn(ByteBuddyAgent.install());
new Target().method();
}
@Advice.OnMethodEnter
public static void intercept(@Advice.AllArguments Object[] arguments) {
System.out.println("Intercepted: " + Arrays.asList(arguments));
}
public static class Target {
public void method() { }
}
}
This will print an empty list upon invocation.