简体   繁体   中英

Is it possible to intercept a method that has no arguments with using @Advice.AllArguments in ByteBuddy?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM