【发布时间】:2015-03-03 23:20:11
【问题描述】:
为什么这段代码 sn-p 失败并显示IllegalArgumentException: wrong number of arguments?演示代码适用于普通成员和静态方法,以及原生声明的方法...
public class Program {
public static void main(String[] args) throws ReflectiveOperationException {
//get native getDeclaredMethods method
Method Class$getDeclaredMethods0 = Class.class.getDeclaredMethod("getDeclaredMethods0", boolean.class);
Class$getDeclaredMethods0.setAccessible(true);
//call main
Method[] methods = (Method[]) Class$getDeclaredMethods0.invoke(Program.class, false);
Method main = methods[0];
main.invoke(null, args); // ← Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments
//at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
//at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
//at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
//at java.lang.reflect.Method.invoke(Method.java:606)
//at Program.main(Program.java:19)
//at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
//at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
//at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
//at java.lang.reflect.Method.invoke(Method.java:606)
//at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
}
}
【问题讨论】:
-
你为什么要通过反射而不是直接调用
getDeclaredMethods?为什么你假设只有一个声明的方法,必须是main? -
作为补充——如何将静态初始化器、实例初始化器和默认构造器作为方法返回?
-
每个帖子一个问题,拜托。 (是什么让您认为这些是可用的 as 方法?)
-
构造函数被实现为方法(在我看来它们是一个糟糕的设计选择),而初始化程序是可执行代码。它们具有需要作为方法处理的各个方面 - 名称、偏移量、可见性、父对象
-
“构造函数被实现为方法”——在什么意义上? (这里有很多可能的含义。)反射允许您直接访问构造函数,那么为什么不这样做呢?至于实例初始值设定项 - 它们没有有名称,它们实际上已融入到每个构造函数中。
标签: java reflection