调用方法时出现参数数量错误
我有类 AClass
和方法 someMethod
,它获取 Object
数组作为参数。
public class AClass {
public void someMethod(Object[] parameters) {
}
}
主要是,当我尝试在我创建的对象上调用此方法并将对象数组作为此方法的参数时,
Object[] parameters; // lets say this object array is null
Class class = Class.forName("AClass");
Object anObject = class.newInstance();
Method someMethod = class.getDeclaredMethod("someMethod", parameters.getClass());
someMethod.invoke(anObject, parameters);
我收到“参数数量错误错误”。 我缺少什么?
I have class AClass
and a method someMethod
that gets an Object
array as parameter.
public class AClass {
public void someMethod(Object[] parameters) {
}
}
In main, when I try to invoke this method on the the object that I have created and give an object array as a parameter to this method
Object[] parameters; // lets say this object array is null
Class class = Class.forName("AClass");
Object anObject = class.newInstance();
Method someMethod = class.getDeclaredMethod("someMethod", parameters.getClass());
someMethod.invoke(anObject, parameters);
I get "wrong number of arguments error".
What am i missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这样就可以了。
请注意调用方法的第二个参数。它是
Object[]
本身,并且您的方法的参数类型也是Object[]
。That will be all right.
Be careful about the second parameter of the invoke method. It's
Object[]
itself, and the argument type of your method isObject[]
too.稍微扩展一下 Orien 和 biabieqi 所说的内容。 。 。
这里可能让您感到困惑的是
Method.invoke(Object, Object...)
通常可以只采用“内联”参数,可以这么说;当编译器看到类似someMethod.invoke(someObject, arg1, arg2)
的内容时,它会隐式创建一个数组new Object[]{arg1, arg2}
,然后传递该数组到Method.invoke
。然后,Method.invoke
将该数组的元素作为参数传递给您正在调用的方法。到目前为止,一切都很好。但是,当编译器看到类似
someMethod.invoke(someObject, someArray)
的内容时,它会假设您已经将参数打包到数组中;所以它不会再次重新打包它们。因此,Method.invoke
将尝试将someArray
的元素作为参数传递给您正在调用的方法,而不是传递someArray
本身作为参数。(这始终是
...
表示法的工作方式;它接受包含适当类型元素的数组,或零个或多个参数)因此,正如 orien 和 biaobiaoqi 所说,一种解决方案是将您的
parameters
重新包装到一个附加数组中,new Object[] {parameters}
,因此参数
本身最终被传递到你的方法中。 (另一个解决方案是编写(Object)parameters
,显式从Object[]
向上转换为Object
,这样编译器就看不到它作为一个数组,并将为您包装它。)这有意义吗?
To expand a bit on what orien and biaobiaoqi are saying . . .
What's probably confusing you here is that
Method.invoke(Object, Object...)
can usually just take the arguments "inline", so to speak; when the compiler sees something likesomeMethod.invoke(someObject, arg1, arg2)
, it implicitly creates an arraynew Object[]{arg1, arg2}
and then passes that array toMethod.invoke
.Method.invoke
then passes the elements of that array as arguments to the method you're invoking. So far, so good.But when the compiler sees something like
someMethod.invoke(someObject, someArray)
, it assumes that you've already packaged the arguments into an array; so it won't repackage them again. So thenMethod.invoke
will try to pass the elements ofsomeArray
as arguments to the method you're invoking, rather than passingsomeArray
itself as an argument.(This is always how the
...
notation works; it accepts either an array containing elements of the appropriate type, or zero or more arguments of the appropriate type.)So, as orien and biaobiaoqi have said, one solution is to rewrap your
parameters
into an additional array,new Object[] {parameters}
, so thatparameters
itself ends up being passed into your method. (Another solution is to write(Object)parameters
, explicitly upcasting fromObject[]
toObject
, so that the compiler won't see it as an array, and will wrap it for you.)Does that make sense?
Method.invoke
方法采用接收方法调用的对象以及该方法的参数数组。由于您的方法采用一个参数,因此给定的数组的大小必须为1
。尝试创建一个大小为
1
的新数组:请注意,该数组中的一个值可以为
null
。这将模拟anObject.someMethod(null)
The
Method.invoke
method takes the object to receive the method call and an array of the arguments to the method. As your method takes one argument, the array given must have a size of1
.try creating a new array with size
1
:Note that the one value in this array can be
null
. This would simulateanObject.someMethod(null)
invoke
的参数是一个Object
数组;您的参数应该是一个Object[]
,包含您要传递给someMethod
的Object[]
。您不需要创建一个立即数组来执行此操作,因为
invoke
签名是invoke(Object, Object...)
,但在您的情况下,您是尝试传入一个空数组。如果您想传入 null:但是,最终,其他答案是正确的:您需要传入一个
Object[]
,其中包含每个方法参数的条目 。The parameters to
invoke
is an array ofObject
; your parameters should be anObject[]
containing theObject[]
you're passing tosomeMethod
.You don't need to create an immediate array to do this, since the
invoke
signature isinvoke(Object, Object...)
, but in your case you're trying to pass in an empty array. If you want to pass in null:Ultimately, however, the other answers are correct: you need to pass in an
Object[]
containing an entry for each of the methods parameters.试试这个:
我通过使用 dp4j 自动生成代码的 Reflection API 版本得到:
try this:
I got by automatically-generating a Reflection API version of your code with dp4j: