调用方法时,参数数错误

时间:2021-06-22 18:56:21

I have class AClass and a method someMethod that gets an Object array as parameter.

我有一个类AClass和一个方法someMethod,它获取一个对象数组作为参数。

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?

我得到“错误的参数数”。我缺少什么?

5 个解决方案

#1


21  

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. It's Object[] itself, and the argument type of your method is Object[] too.

请注意调用方法的第二个参数。它是Object[]本身,您的方法的参数类型也是Object[]。

#2


8  

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 of one.

该方法。调用方法接受对象来接收方法调用和方法的参数数组。当您的方法接受一个参数时,给定的数组的大小必须为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. This would simulate anObject.someMethod(null)

注意,这个数组中的一个值可以是null。这将模拟anObject.someMethod(空)

#3


8  

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 like someMethod.invoke(someObject, arg1, arg2), it implicitly creates an array new Object[]{arg1, arg2} and then passes that array to Method.invoke. Method.invoke then passes the elements of that array as arguments to the method you're invoking. So far, so good.

可能让你困惑的是这个方法。调用(对象,对象…)通常只接受“内联”的参数;当编译器看到类似someMethod的东西时。调用(someObject, arg1, arg2),它隐式地创建一个数组新对象[]{arg1, arg2},然后将该数组传递给Method.invoke。方法。然后调用将该数组的元素作为参数传递给要调用的方法。到目前为止还好。

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 then Method.invoke will try to pass the elements of someArray as arguments to the method your invoking, rather than passing someArray itself as an argument.

但是当编译器看到一些像someMethod的东西时。调用(someObject, someArray),它假设您已经将参数打包到一个数组中;所以它不会再重新包装它们。然后方法。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所说,您需要将参数重新包装到一个附加的数组中,即new Object[] {parameters},以便参数本身最终被传递到您的方法中。

Does that make sense?

这说得通吗?

#4


3  

The parameters to invoke is an array of Object; your parameters should be an Object[] containing the Object[] you're passing to someMethod.

调用的参数是一个对象数组;您的参数应该是一个对象[],其中包含您要传递给someMethod的对象[]。

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. If you want to pass in null:

您不需要创建一个即时数组来实现这一点,因为调用签名是invoke(Object, Object…),但是在您的示例中,您正在尝试传入一个空数组。如果要传入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.

但是,最终,其他的答案都是正确的:您需要传入一个对象[],其中包含每个方法参数的条目。

#5


1  

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

#1


21  

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. It's Object[] itself, and the argument type of your method is Object[] too.

请注意调用方法的第二个参数。它是Object[]本身,您的方法的参数类型也是Object[]。

#2


8  

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 of one.

该方法。调用方法接受对象来接收方法调用和方法的参数数组。当您的方法接受一个参数时,给定的数组的大小必须为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. This would simulate anObject.someMethod(null)

注意,这个数组中的一个值可以是null。这将模拟anObject.someMethod(空)

#3


8  

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 like someMethod.invoke(someObject, arg1, arg2), it implicitly creates an array new Object[]{arg1, arg2} and then passes that array to Method.invoke. Method.invoke then passes the elements of that array as arguments to the method you're invoking. So far, so good.

可能让你困惑的是这个方法。调用(对象,对象…)通常只接受“内联”的参数;当编译器看到类似someMethod的东西时。调用(someObject, arg1, arg2),它隐式地创建一个数组新对象[]{arg1, arg2},然后将该数组传递给Method.invoke。方法。然后调用将该数组的元素作为参数传递给要调用的方法。到目前为止还好。

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 then Method.invoke will try to pass the elements of someArray as arguments to the method your invoking, rather than passing someArray itself as an argument.

但是当编译器看到一些像someMethod的东西时。调用(someObject, someArray),它假设您已经将参数打包到一个数组中;所以它不会再重新包装它们。然后方法。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所说,您需要将参数重新包装到一个附加的数组中,即new Object[] {parameters},以便参数本身最终被传递到您的方法中。

Does that make sense?

这说得通吗?

#4


3  

The parameters to invoke is an array of Object; your parameters should be an Object[] containing the Object[] you're passing to someMethod.

调用的参数是一个对象数组;您的参数应该是一个对象[],其中包含您要传递给someMethod的对象[]。

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. If you want to pass in null:

您不需要创建一个即时数组来实现这一点,因为调用签名是invoke(Object, Object…),但是在您的示例中,您正在尝试传入一个空数组。如果要传入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.

但是,最终,其他的答案都是正确的:您需要传入一个对象[],其中包含每个方法参数的条目。

#5


1  

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