考慮下面的類聲明:爲什麼Javassist ProxyFactory的創建方法不會根據args參數調用正確的構造函數?
class A{
private String x;
public A(String x) {
this.x = x;
}
}
當我試圖創建A級代理與下面的代碼Javassist是:
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?
Class proxyKlass = factory.createClass();
Constructor<T> ctor = proxyKlass.getConstructor(String.class);
T res = ctor.newInstance(new String(){"hello"});
((Proxy) res).setHandler(handler);
是的我明白你的方法,但爲什麼ProxyFactory的'create'方法不遵循相同的解決方案? – rollaeriu360 2013-03-19 10:55:23