Reflect Constructor newInstance 报 IllegalArgumentException 异常

Relfect Constructor's newInstance reports IllegalArgumentException exception

我尝试使用 Reflect Constructor::newInstance 为动态生成的 class 生成一个实例。错误和输出是:

public TTryCatch(java.lang.invoke.MethodHandle,java.lang.Throwable,java.lang.invoke.MethodHandle)
MethodHandle(List)void  class java.lang.Throwable   MethodHandle(Throwable,List)void
java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:68)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:529)
    at java.lang.invoke.CatchHandle.getCompiledInstance(CatchHandle.java:188)

我的代码是:

 Class<?> generatedClass = ....;
 try {
     Constructor constr = generatedClass.getConstructor(MethodHandle.class, Throwable.class, MethodHandle.class);
     System.out.println(constr.toString());
     System.out.println(tryTarget.toString()+"  "+exceptionClass.toString()+"   "+ catchTarget.toString());
    Object obj = constr.newInstance(tryTarget, exceptionClass, catchTarget);
    return obj;
} catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} ....

异常显示给定的构造函数的三个参数与构造函数的参数类型不匹配。这让我很困惑,因为调试(也是输出中的 println 结果)信息确实显示参数应该相同。

生成的Constructor方法为:

  public TTryCatch(java.lang.invoke.MethodHandle, java.lang.Throwable, java.lang.invoke.MethodHandle);
    flags: ACC_PUBLIC
    Code:

给出的三个参数分别为:MethodHandlethrowableMethodHandle

有人可以帮助解决我的问题吗?谢谢。

看起来构造函数的第二个参数采用 Throwable 对象,但您提供的是 Throwable.class.