java简单反射调用get,set方法

public Object setDate(ResultSet rs,Object o) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IntrospectionException, IllegalArgumentException, InvocationTargetException, SQLException{
Class cs = Class.forName(o.getClass().getName());
Field [] fileds = cs.getDeclaredFields();
for (Field field : fileds) {


PropertyDescriptor pd = new PropertyDescriptor(field.getName(),cs);

//获取所有set方法
Method method = pd.getWriteMethod();
//利用set方法赋值
method.invoke(o,rs.getString(field.getName()));
}
return o;
}

posted @ 2015-04-01 11:50  skeek  阅读(519)  评论(0)    收藏  举报