0

Im trying to get the execution path of an application. To get in touch with ByteBuddy i implemented an agent using the code below but it doesn´t print out anything. Can anyone help me?

public static void premain(String arg, Instrumentation inst) throws Exception {

        new AgentBuilder.Default()
                .type(ElementMatchers.nameContains("Application"))
                .transform(new AgentBuilder.Transformer() {
                    @Override
                    public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule javaModule) {
                        return builder.method(ElementMatchers.any()).intercept(MethodDelegation.to(LogInterceptor.class));
                    }
                })
                .installOn(inst);

}

 public static class LogInterceptor {
 static void log(@Origin Method method) {
        System.out.println(method.toString() + " was called");
    }
}
Hector Lorenzo
  • 141
  • 3
  • 11

1 Answers1

0

I assume that your classes cannot invoke the package-private method. Byte Buddy does therefore refuse the instrumentation. If you install a listener to the agent builder, you can intercept this error.

You probably also want to invoke the original code after the print. You can add an andThen with a SuperMethodCall to do so.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192