如何使用反射在java中调用带有可变参数的方法?

发布于 2024-12-27 03:34:44 字数 553 浏览 2 评论 0原文

我正在尝试使用 java 反射调用带有变量参数的方法。这是托管该方法的类:

public class TestClass {

public void setParam(N ... n){
    System.out.println("Calling set param...");
}

这是调用代码:

try {
        Class<?> c = Class.forName("com.test.reflection.TestClass");
        Method  method = c.getMethod ("setParam", com.test.reflection.N[].class);
        method.invoke(c, new com.test.reflection.N[]{});

在调用调用的最后一行,我收到了“参数数量错误”形式的 IllegalArgumentException。不确定我做错了什么。

任何指示将不胜感激。

  • 谢谢

I'm trying to invoke a method with variable arguments using java reflection. Here's the class which hosts the method:

public class TestClass {

public void setParam(N ... n){
    System.out.println("Calling set param...");
}

Here's the invoking code :

try {
        Class<?> c = Class.forName("com.test.reflection.TestClass");
        Method  method = c.getMethod ("setParam", com.test.reflection.N[].class);
        method.invoke(c, new com.test.reflection.N[]{});

I'm getting IllegalArgumentException in the form of "wrong number of arguments" at the last line where I'm calling invoke. Not sure what I'm doing wrong.

Any pointers will be appreciated.

  • Thanks

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

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

发布评论

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

评论(2

尘世孤行 2025-01-03 03:34:44
public class Test {

public void setParam(N... n) {
    System.out.println("Calling set param...");
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws Exception {
    Test t=new Test();
    Class<?> c = Class.forName("test.Test");
    Method  method = c.getMethod ("setParam", N[].class);
    method.invoke(t, (Object) new N[]{});
}
}

对我有用。

  1. 将 N[] 转换为
  2. 实例上的对象调用调用,而不是类上的调用
public class Test {

public void setParam(N... n) {
    System.out.println("Calling set param...");
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws Exception {
    Test t=new Test();
    Class<?> c = Class.forName("test.Test");
    Method  method = c.getMethod ("setParam", N[].class);
    method.invoke(t, (Object) new N[]{});
}
}

Works for me.

  1. Cast your N[] to Object
  2. call invoke on instance, not on class
擦肩而过的背影 2025-01-03 03:34:44

您的代码片段中没有调用该方法的 TestClass 实例。您需要 TestClass实例,而不仅仅是 TestClass 本身。在 c 上调用 newInstance(),并使用此调用的结果作为 method.invoke() 的第一个参数。

此外,为了确保您的数组被视为一个参数,而不是可变参数,您需要将其转换为 Object:

m.invoke(testClassInstance, (Object) new com.test.reflection.N[]{});

There is no TestClass instance in your code snippet on which the methd is invoked. You need an instance of the TestClass, and not just the TestClass itself. Call newInstance() on c, and use the result of this call as the first argument of method.invoke().

Moreover, to make sure your array is considered as one argument, and not a varargs, you need to cast it to Object:

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