0

I am trying to add a method to a class using Java agent.But its gives a error as follows.

java.lang.VerifyError: Local variable table overflow 


Exception Details:
  Location:com/github/shehanperera/example/Method.method1()V @3: aload_0
  Reason: Local index 0 is invalid
  Bytecode: 0x0000000: b800 532a b600 56b1            
    at com.github.shehanperera.example.Sample.main(Sample.java:13)

This is my agent

new AgentBuilder.Default()
            .with(AgentBuilder.Listener.StreamWriting.toSystemError())
            .with(new AgentBuilder.InitializationStrategy.SelfInjection.Eager())
            .type((ElementMatchers.nameContains("Method")))
            .transform((builder, typeDescription, classLoader, module) -> builder
                    .defineMethod("method3", void.class, Visibility.PUBLIC)
                    .intercept(MethodDelegation.to(AddMethod.class))
                    .method(ElementMatchers.nameContains("method1"))
                    .intercept(SuperMethodCall.INSTANCE
                            .andThen(MethodCall.invoke(ElementMatchers.nameContains("method3"))))
            ).installOn(instrumentation);

and this is the method I need to add .

public class AddMethod {

public static void method3() throws Exception {

    System.out.println("This is new method : method 3");

}}

This is the my real Method class where I want to add new method.

public class Method {

Method() {

    System.out.println("This is constructor ");
}

public static void method1() {

    try {
        Thread.sleep(500);
        System.out.println("This is Method 1");
    } catch (InterruptedException e) {
        //Ignore
    }
}}

And this is the main method

public static void main(String[] args) {

    System.out.println("This is Sample main");

   (13) Method method = new Method();
    method.method1();

}

Can you give me any idea what is the problem in this case.And this is a normal class I am trying to add method.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
Shehan Perera
  • 339
  • 1
  • 18

1 Answers1

1

You are trying to invoke a non-static method from a static method and Byte Buddy was missing a check here. I added that check in the latest version but what you attempt to do will not work but of course this should not result in a verifier error.

Either make method1 non-static or method3 static and this will work.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • I just write a simple blog to show what byte-buddy can do with agent . When you filling free please give 5 min to read [this](https://medium.com/@shehanperera.office/java-agents-with-byte-buddy-93185305c9e9) and give me a comment. Thanks for your helps ! – Shehan Perera Mar 26 '18 at 10:25