调用方法时出现参数数量错误

发布于 2024-12-17 01:52:32 字数 560 浏览 2 评论 0原文

我有类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冷月断魂刀 2024-12-24 01:52:32

这样就可以了。

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

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

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.

执笏见 2024-12-24 01:52:32

稍微扩展一下 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 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.

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 you're invoking, rather than passing someArray 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 that parameters itself ends up being passed into your method. (Another solution is to write (Object)parameters, explicitly upcasting from Object[] to Object, so that the compiler won't see it as an array, and will wrap it for you.)

Does that make sense?

心奴独伤 2024-12-24 01:52:32

Method.invoke 方法采用接收方法调用的对象以及该方法的参数数组。由于您的方法采用一个参数,因此给定的数组的大小必须为 1

尝试创建一个大小为 1 的新数组:

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

请注意,该数组中的一个值可以为 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 of 1.

try creating a new array with size 1:

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

Note that the one value in this array can be null. This would simulate anObject.someMethod(null)

故事↓在人 2024-12-24 01:52:32

invoke 的参数是一个 Object 数组;您的参数应该是一个Object[]包含您要传递给someMethodObject[]

您不需要创建一个立即数组来执行此操作,因为 invoke 签名是 invoke(Object, Object...),但在您的情况下,您是尝试传入一个数组。如果您想传入 null:

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

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

The parameters to invoke is an array of Object; your parameters should be an Object[] containing the Object[] you're passing to 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:

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.

不…忘初心 2024-12-24 01:52:32

试试这个:

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

我通过使用 dp4j 自动生成代码的 Reflection 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});
    }
}

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:

$ 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});
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文