我一直在尝试生成一些动态代码(使用 Javassist),但是当涉及双数组或浮点数组时,程序在某个点失败。代码如下
Class c = Customers.class; // called in main & Customer class just has a double[] Dubs = new double[10]
CreateType(c); // Main
public static Object CreateType(Class genericType)
{
// some preReq declarations
CtMethod writeCode = dyn.getDeclaredMethod("processCode");
generateCode(genericType, Code, "temp"); // Code is a StringBuilder class
System.out.println(Code);
writeCode.insertAt(1, Code.toString()); // Compilation is successful
Class c = dyn.toClass();
Dynamic h;
Constructor[] ctorlist = null;
ctorlist = c.getDeclaredConstructors(); // Problem is here
h = (DynamicSurrogate) ctorlist[0].newInstance(genericType);
return h;
}
生成代码如下
testapp1.Customers temp=(testapp1.Customers)graph;
output.processDouble(temp.Dubs[1]);
但是当调用 getDeclaredConstructors 时会出现问题 c.getDeclaredConstructors() ...它会引发以下错误
线程“主”java.lang.VerifyError 中的异常:(类:testapp1/Dyn,方法:processDouble 签名:(Lsomething/Output;Ljava/lang/Object;)V)opc_invokeinterface 的 args_size 不一致
存在一种解决方法,但没有任何意义,即如果我只是创建双精度数组的副本并将其传递给动态代码中的 processDouble,即如果动态代码是
testapp1.Customers temp=(testapp1.Customers)graph;
double[] d = temp.Dubs;
output.processDouble(d);
简而言之,getDeclaredConstructor 抛出异常 Unhandled 但它实际上与构造函数无关,因为我是否创建一个都没有关系
希望我的问题和代码足够清楚,如果有任何混淆请告诉我,谢谢你提前:)