Java反射调用方法
我定义了这样一个类,当执行时,它会恢复类中作为参数传递给主方法的所有方法,并执行它们:Java反射调用方法,java,reflection,Java,Reflection,我定义了这样一个类,当执行时,它会恢复类中作为参数传递给主方法的所有方法,并执行它们: package tec; import java.lang.reflect.Method; public class LancerTests { static private void lancer(Class c) throws Exception{ Method methods[] = c.getMethods(); int nbTest = 0; for(Method m
package tec;
import java.lang.reflect.Method;
public class LancerTests {
static private void lancer(Class c) throws Exception{
Method methods[] = c.getMethods();
int nbTest = 0;
for(Method m : methods){
m.invoke(c.newInstance());
System.out.println(".");
nbTest++;
}
System.out.println("(" + nbTest + "):OK: " + c.getName());
}
static public void main(String[] args) throws Exception {
boolean estMisAssertion = false;
assert estMisAssertion = true;
if (!estMisAssertion) {
System.out.println("Execution impossible sans l'option -ea");
return;
}
for(String arg : args){
Class cls = Class.forName(arg);
lancer(cls);
}
}
}
但是,我得到了以下错误:
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:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at tec.LancerTests.lancer(LancerTests.java:10)
at tec.LancerTests.main(LancerTests.java:28)
我不知道代码出了什么问题
编辑:这是一个我想执行的类的示例,但是错误仍然存在,尽管不需要参数:
package tec;
class TestMonteeFatigue{
public void testChoixPlaceMontee(){
FauxArret a = new FauxArret();
MonteeFatigue p = new MonteeFatigue("Jean",5,null);
FauxVehicule v = new FauxVehicule(FauxVehicule.ASSIS);
p.choixPlaceMontee(v);
//assert "monteeDemanderAssis"==getLastLog(v) : "assis";
v = new FauxVehicule(FauxVehicule.PLEIN);
p = new MonteeFatigue("Jean",5,null);
p.choixPlaceMontee(v);
assert p.estDehors();
}
/*protected String getLastLog(FauxVehicule f) {
return f.logs.get(f.logs.size() -1);
}*/
}
您尝试以反射方式调用的方法不是不带参数的方法
// snip...
Method methods[] = c.getMethods();
for(Method m : methods){
System.out.println(m.getName() + " requires " + m.getParameterTypes().length + " arguments");
}
有些人会带你经历这个过程,还有一些东西
编辑以添加: 要运行该方法,可以将调用包装在一个条件中,以检查该方法是否不需要任何参数
if (m.getParameterTypes().length == 0) {
m.invoke(c.newInstance());
}
在方法上,调用必须具有以下参数:
method.invoke(class, arg1, arg2, arg3, ...)
method1.invoke(class.newInstance()); //Executes fine
method2.invoke(class.newInstance()); //Exception as it lacks the parameter
method2.invoke(class.newInstance(),"The parameter"); //Executes fine
其中class是方法的类,args是方法的参数
// snip...
Method methods[] = c.getMethods();
for(Method m : methods){
System.out.println(m.getName() + " requires " + m.getParameterTypes().length + " arguments");
}
问题是,您可能正在调用一个需要传递一些参数的方法,而您没有传递任何参数+
例如:
public void method1() {
//DO STUFF
}
public void mehtod2(String parameter) {
//DO OTHER STUFF
}
如果不指定参数,则会发生这种情况:
method.invoke(class, arg1, arg2, arg3, ...)
method1.invoke(class.newInstance()); //Executes fine
method2.invoke(class.newInstance()); //Exception as it lacks the parameter
method2.invoke(class.newInstance(),"The parameter"); //Executes fine
当您调用Method::invoke()方法而不提供任何参数时,您所引用的类可能需要发送一些缺少的输入参数
// snip...
Method methods[] = c.getMethods();
for(Method m : methods){
System.out.println(m.getName() + " requires " + m.getParameterTypes().length + " arguments");
}
方法签名如下:
公共对象调用(对象obj、对象…args)
由于此方法接受varargs
(三个点),因此它允许您根本不发送任何参数,或者发送任意数量的参数
在您的情况下,您没有发送任何信息。我用一个类示例编辑了我的问题。它可能是在抱怨方法
getLastLog(FauxVehicleue f)
,该方法显然需要参数。如果我删除它,错误仍然存在。我编辑我的问题并添加条件。您可能还被类Object
中的内置方法绊倒了(例如:booleanequals(objecto)
)。如果您从c.getMethods()
更改为c.getDeclaredMethods()
,您将获得类中声明的方法列表,而不是继承的方法列表。不确定这是否是您在编辑问题后的结果stacktrace(异常)是否仍然相同?你只调用这个方法吗?如果调用多个类,那么错误可能在另一个类的方法上。