实体类:[/color]
public class Hotel {private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
测试方法:public void testReflect() throws Exception{
Object object = Hotel.class.newInstance();
String setMethod = "setId";
Method method = object.getClass().getDeclaredMethod(setMethod, new Class[]{Object.class});
method.invoke(object, new Object[]{1L});
}
出错信息
java.lang.NoSuchMethodException: setId
at java.lang.ClassCache.findMethodByName(ClassCache.java:247)
at java.lang.Class.getDeclaredMethod(Class.java:731)
at java.lang.reflect.Method.invokeNative(Native Method)