Reflection overload. Populating reflected array for reflected varargs method
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开发者_如何学Python paramArray
is an SqlParameter[]
and sqlParameterClass.cast(sqlOutParameter)
is an SqlOutParamter
(not SqlParameter
as cast). I suspect this may be the problem.
I think the problem is in this line:
Object paramArray = Array.newInstance(sqlParameterArrayClass, 1);
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 the newInstance(...)
methods.
精彩评论