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.