gpt4 book ai didi

java - 为什么 Javassist ProxyFactory 的 create 方法没有根据 args 参数调用正确的构造函数?

转载 作者:行者123 更新时间:2023-12-01 08:13:28 24 4
gpt4 key购买 nike

考虑以下类声明:

class A{
private String x;
public A(String x) {
this.x = x;
}
}

当我尝试使用以下代码使用 javassist 为 A 类创建代理时:

ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(A.class);
MethodHandler mh = new MethodHandler() {...};
A a = (A) factory.create(new Class<?>[0], new String(){"hello"}, mh);

然后我得到了java.lang.RuntimeException: java.lang.NoSuchMethodException: app.test.A_$$_javassist_0.<init>()

为什么 javassist 不根据传递给 create 方法的第二个参数的参数类型使用正确的构造函数来实例化类 A?

最佳答案

通过new Class<?>[0] ,您告诉工厂调用无参数构造函数。尝试:

factory.create(new Class<?>[] { String.class }, new String[]{ "hello" }, mh);

关于java - 为什么 Javassist ProxyFactory 的 create 方法没有根据 args 参数调用正确的构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15482534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com