IllegalArgumentException:参数数量错误

发布于 2024-12-20 09:49:41 字数 925 浏览 1 评论 0原文

我正在尝试运行以下代码:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Reflection {

    /**
     * @param args
     * @throws InvocationTargetException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public static void main(String[] args) throws IllegalAccessException,
            InvocationTargetException, IllegalArgumentException {
        Class<Cls> cls = Cls.class;
        Method[] methods = cls.getMethods();
        for (Method m : methods) {
            m.invoke(cls);
        }
    }

}

class Cls {
    public static void method1() {
        System.out.println("Method1");
    }

    public static void method2() {
        System.out.println("Method2");
    }
}

即使这两个方法不带参数,我仍然收到 IllegalArgumentException: 参数数量错误。

我尝试将 null 传递给 invoke 方法,但这会引发 NPE。

I'm trying to run the following code:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Reflection {

    /**
     * @param args
     * @throws InvocationTargetException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public static void main(String[] args) throws IllegalAccessException,
            InvocationTargetException, IllegalArgumentException {
        Class<Cls> cls = Cls.class;
        Method[] methods = cls.getMethods();
        for (Method m : methods) {
            m.invoke(cls);
        }
    }

}

class Cls {
    public static void method1() {
        System.out.println("Method1");
    }

    public static void method2() {
        System.out.println("Method2");
    }
}

I keep getting an IllegalArgumentException: wrong number of arguments, even though the two methods take no arguments.

I tried passing null to the invoke method, but that throws a NPE.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你在看孤独的风景 2024-12-27 09:49:41

您实际上正确地调用了这些方法,问题是 Class.getMethods() 方法返回类中的所有方法,包括从超类继承的方法,例如本例中的 Object。 文档指出:

返回一个包含 Method 对象的数组,该对象反映了此 Class 对象所表示的类或接口的所有公共成员方法,包括由类或接口声明的方法以及从超类和超接口继承的方法。

例如,在这种情况下,您可能最终会调用不带任何参数的 Object.equals。 Object.wait 方法等也是如此。

You are actually invoking the methods correctly, the problem is that the Class.getMethods() method returns ALL methods in the class, INCLUDING those inherited from super classes, such as Object in this case. The documentation states:

Returns an array containing Method objects reflecting all the public member methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.

In this case you might end up calling Object.equals without any arguments, for example. The same goes for the Object.wait methods etc.

玩物 2024-12-27 09:49:41

根据 文档

如果底层方法所需的形式参数数量为 0,则提供的 args 数组的长度可能为 0 或 null。

你试过这个吗?

m.invoke(cls, null);

According to documentation:

If the number of formal parameters required by the underlying method is 0, the supplied args array may be of length 0 or null.

Have you tried this?

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