简体   繁体   English

调用方法时参数数量错误

[英]Wrong number of arguments error when invoking a method

I have class AClass and a method someMethod that gets an Object array as parameter.我有类AClass和方法someMethod ,它获取Object数组作为参数。

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在 main 中,当我尝试在我创建的对象上调用此方法并将对象数组作为此方法的参数时

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?我错过了什么?

That will be all right.那会没事的。

Object[] parameters = {new Object()}; // lets say this object array is null
Class clas = Class.forName("AClass");
Object anObject = clas.newInstance();

Object[] param = {parameters};

Method someMethod = clas.getDeclaredMethod("someMethod", parameters.getClass());
someMethod.invoke(anObject, param);

Be careful about the second parameter of the invoke method.注意 invoke 方法的第二个参数。 It's Object[] itself, and the argument type of your method is Object[] too.它是Object[]本身,你的方法的参数类型也是Object[]

To expand a bit on what orien and biaobiaoqi are saying .稍微扩展一下orien和biaobiaoqi的意思。 . . . .

What's probably confusing you here is that Method.invoke(Object, Object...) can usually just take the arguments "inline", so to speak;这里可能让您感到困惑的是Method.invoke(Object, Object...)通常可以只采用“内联”参数,可以这么说; when the compiler sees something like someMethod.invoke(someObject, arg1, arg2) , it implicitly creates an array new Object[]{arg1, arg2} and then passes that array to Method.invoke .当编译器看到类似someMethod.invoke(someObject, arg1, arg2) ,它会隐式地创建一个数组new Object[]{arg1, arg2} ,然后将该数组传递给Method.invoke Method.invoke then passes the elements of that array as arguments to the method you're invoking. Method.invoke然后将该数组的元素作为参数传递给您正在调用的方法。 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;但是当编译器看到someMethod.invoke(someObject, someArray)类的东西时,它假定您已经将参数打包到一个数组中; so it won't repackage them again.所以它不会再次重新包装它们。 So then Method.invoke will try to pass the elements of someArray as arguments to the method you're invoking, rather than passing someArray itself as an argument.那么Method.invoke将尝试将someArray元素作为参数传递给您正在调用的方法,而不是将someArray本身作为参数传递。

(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, you need to rewrap your parameters into an additional array, new Object[] {parameters} , so that parameters itself ends up being passed into your method.因此,正如 orien 和 biaobiaoqi 所说,您需要将parameters重新包装到一个额外的数组new Object[] {parameters} ,以便parameters本身最终被传递到您的方法中。

Does that make sense?那有意义吗?

The Method.invoke method takes the object to receive the method call and an array of the arguments to the method. Method.invoke方法接受接收方法调用的对象和方法的参数数组。 As your method takes one argument, the array given must have a size of 1 .由于您的方法采用一个参数,因此给定的数组大小必须为1

try creating a new array with size 1 :尝试创建一个大小为1的新数组:

someMethod.invoke(anObject, new Object[] {parameters});

Note that the one value in this array can be null .请注意,此数组中的一个值可以为null This would simulate anObject.someMethod(null)这将模拟anObject.someMethod(null)

The parameters to invoke is an array of Object ;invoke的参数是一个Object数组; your parameters should be an Object[] containing the Object[] you're passing to someMethod .您的参数应该是一个Object[]其中包含您传递给someMethodObject[]

You don't need to create an immediate array to do this, since the invoke signature is invoke(Object, Object...) , but in your case you're trying to pass in an empty array.您不需要创建一个直接数组来执行此操作,因为invoke签名是invoke(Object, Object...) ,但在您的情况下,您试图传入一个数组。 If you want to pass in null:如果你想传入null:

Object[] parameters = { null };
...
someMethod.invoke(anObject, parameters);

Ultimately, however, the other answers are correct: you need to pass in an Object[] containing an entry for each of the methods parameters .然而,最终,其他答案是正确的:您需要传入一个Object[]其中包含每个方法 parameters的条目。

try this:试试这个:

    someMethod.invoke(anObject, new .java.lang.Object[1][]{parameters});

I got by automatically-generating a Reflection API version of your code with dp4j:我通过使用 dp4j 自动生成代码的反射 API 版本获得了:

$ javac -cp dp4j-1.2-jar-with-dependencies.jar -Averbose=true AClass.java
AClass.java:10: Note: 
import com.dp4j.*;

public class AClass {

    public AClass() {
        super();
    }

    public void someMethod(Object[] parameters) {
    }

    @Reflect(all = true)
    public static void main(String... args) throws ... {
        Object[] parameters = null;
        ...
        AClass anObject;
        anObject = (.AClass)aClassConstructor.newInstance();
        java.lang.reflect.Method someMethodWithArrayMethod = null;
        someMethodWithArrayMethod = Class.forName("AClass").getDeclaredMethod("someMethod", .java.lang.Object[].class);
        someMethodWithArrayMethod.setAccessible(true);
        someMethodWithArrayMethod.invoke(anObject, new .java.lang.Object[1][]{parameters});
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM