我正处于有史以来最伟大的混战的竞争者之中。我需要使用 Spring JDBC 而无需引用它。自定义类加载器提供上下文,我需要使用反射来调用方法。一种这样的方法是 SimpleJdbcCall.declareParameters(SqlParameter ...)
我的问题是创建和设置可变参数 SqlParameter
(这些实例也必须反映出来)。我需要将单个参数硬塞到数组中以满足 varargs 签名。
为了简洁起见,下面省略了类加载。但假设 Class simpleJdbcCallClass = SimpleJdbcCall.class
等。
Constructor sqlOutParameterConstructor =
sqlOutParameterClass.getConstructor(String.class, int.class);
Object sqlOutParameter = sqlOutParameterConstructor.newInstance(param, type);
Object paramArray = Array.newInstance(sqlParameterArrayClass, 1);
Array.set(paramArray, 0, sqlParameterClass.cast(sqlOutParameter));
// IllegalArgumentException thrown above.
// It is thrown without the call to .cast too.
Method declareParametersMethod =
simpleJdbcCallClass.getMethod("declareParameters", sqlParameterArrayClass);
declareParametersMethod.invoke(procedure, paramArray);
抛出的异常是:
java.lang.IllegalArgumentException: array element type mismatch
at java.lang.reflect.Array.set(Native Method)
该方法采用 SqlParameter ...
并且我有一个子类 SqlOutParameter
的实例。因此我尝试使用 sqlParameterClass.cast(sqlOutParameter) 进行转换。无论是否进行此转换,都会引发异常。
调试我可以确认 paramArray
是一个 SqlParameter[]
并且 sqlParameterClass.cast(sqlOutParameter)
是一个 SqlOutParamter
(不是 SqlParameter
作为强制转换)。我怀疑这可能是问题所在。
I'm in the middle of a contender for the greatest kludge of all time. I need to use Spring JDBC without ever making reference to it. A custom classloader is providing the context and I need to use reflection to invoke the methods. One such method is SimpleJdbcCall.declareParameters(SqlParameter ...)
My problem is with creating and setting the varargs SqlParameter
(these instances must be reflected also). I need to shoehorn a single parameter into an array to satisfy the varargs signature.
In the following, class loading is omitted for brevity. But assume Class<?> simpleJdbcCallClass = SimpleJdbcCall.class
, etc.
Constructor sqlOutParameterConstructor =
sqlOutParameterClass.getConstructor(String.class, int.class);
Object sqlOutParameter = sqlOutParameterConstructor.newInstance(param, type);
Object paramArray = Array.newInstance(sqlParameterArrayClass, 1);
Array.set(paramArray, 0, sqlParameterClass.cast(sqlOutParameter));
// IllegalArgumentException thrown above.
// It is thrown without the call to .cast too.
Method declareParametersMethod =
simpleJdbcCallClass.getMethod("declareParameters", sqlParameterArrayClass);
declareParametersMethod.invoke(procedure, paramArray);
The exception thrown is:
java.lang.IllegalArgumentException: array element type mismatch
at java.lang.reflect.Array.set(Native Method)
The method takes SqlParameter ...
and I have an instance of a subclass SqlOutParameter
. Hence I try to cast with sqlParameterClass.cast(sqlOutParameter)
. The exception is thrown with or without this casting.
Debugging I can confirm that paramArray
is an SqlParameter[]
and sqlParameterClass.cast(sqlOutParameter)
is an SqlOutParamter
(not SqlParameter
as cast). I suspect this may be the problem.
发布评论
评论(1)
我认为问题出在这一行:
具体来说,您没有告诉我们 sqlParameterArrayClass 是什么,但根据名称我猜测它是数组类型的类。事实上,它需要是数组元素的类;请参阅
newInstance(...)
方法。I think the problem is in this line:
Specifically, you don't tell us what
sqlParameterArrayClass
is, but based on the name I'm guessing that is the class of the array type. In fact, it needs to be the class of the array element; see the javadoc for thenewInstance(...)
methods.