这是程序:
package com.hebrf.cla;
import java.lang.reflect.Method;
public class TestReplect{
public static void main(String[] args) {
try {
Class cls = Class.forName("com.hebrf.cla.method");
method c= new method();
Method methlist[] = cls.getMethods();
for(int i=0;i<methlist.length;i++){
Class<?>[] types = methlist[i].getParameterTypes();
System.out.println("mrthod:"+methlist[i].toString());
for(int j=0;j<types.length;j++){
System.out.println("type:"+types[j]);
}
if(types.length == 0){
methlist[i].invoke(c);
}else{
methlist[i].invoke(c, 3);
}
}
}
catch (Throwable e) {
System.err.println(e);
}
}
}
为什么最后会报:mrthod:public final void java.lang.Object.wait() throws java.lang.InterruptedException
java.lang.reflect.InvocationTargetException这种错误,求指教
------解决方案--------------------
Method methlist[] = cls.getMethods();
改成
Method methlist[] = cls.getDeclaredMethods();