反射过载.为反射可变参数方法填充反射数组

编程入门 行业动态 更新时间:2024-10-25 00:29:41
本文介绍了反射过载.为反射可变参数方法填充反射数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正处于有史以来最伟大的混血儿的竞争者之中.我需要使用 Spring JDBC 而无需参考它.自定义类加载器提供上下文,我需要使用反射来调用方法.一种这样的方法是 SimpleJdbcCall.declareParameters(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 ...)

我的问题是创建和设置可变参数 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.

为了简洁起见,下面省略了类加载.但是假设 ClasssimpleJdbcCallClass = SimpleJdbcCall.class 等

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);

抛出的异常是:

java.lang.IllegalArgumentException: array element type mismatch at java.lang.reflect.Array.set(Native Method)

该方法采用 SqlParameter ... 并且我有一个子类 SqlOutParameter 的实例.因此我尝试使用 sqlParameterClass.cast(sqlOutParameter) 进行转换.无论是否进行此转换,都会抛出异常.

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.

调试 我可以确认 paramArray 是一个 SqlParameter[] 并且 sqlParameterClass.cast(sqlOutParameter) 是一个 SqlOutParamtercode> (不是 SqlParameter 作为演员).我怀疑这可能是问题所在.

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.

推荐答案

我认为问题出在这一行:

I think the problem is in this line:

Object paramArray = Array.newInstance(sqlParameterArrayClass, 1);

具体来说,您没有告诉我们 sqlParameterArrayClass 是什么,但根据名称我猜测这是数组类型的类.其实需要是数组元素的类;请参阅 newInstance(...) 方法.

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.

更多推荐

反射过载.为反射可变参数方法填充反射数组

本文发布于:2023-11-11 18:37:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579251.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:反射   数组   参数   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!