[英]Add a method to a class using byte-buddy
我正在嘗試使用Java agent將方法添加到類中,但是它給出了如下錯誤。
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)
這是我的經紀人
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);
這是我需要添加的方法。
public class AddMethod {
public static void method3() throws Exception {
System.out.println("This is new method : method 3");
}}
這是我想要添加新方法的真正的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
}
}}
這是主要方法
public static void main(String[] args) {
System.out.println("This is Sample main");
(13) Method method = new Method();
method.method1();
}
您能告訴我這種情況下的問題是什么嗎,這是我嘗試添加方法的普通類。
您正在嘗試從靜態方法調用非靜態方法,並且Byte Buddy在此處缺少檢查。 我在最新版本中添加了該檢查功能,但是您嘗試執行的操作將不起作用,但是當然這不會導致驗證程序錯誤。
使method1
為非靜態或method3
靜態,這將起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.