Class cs = null;
try {
cs = Class.forName(sqlParam2.getClass().getName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Field[] fileds = cs.getDeclaredFields();
// 循环得到所有set方法
for (Field field : fileds) {
PropertyDescriptor pd = null;
try {
pd = new PropertyDescriptor(field.getName(), cs);
} catch (IntrospectionException e) {
e.printStackTrace();
}
//获取set方法
Method method = pd.getWriteMethod();
//利用set方法赋值
try {
method.invoke(sqlParam2, "赋值");
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
反射 java 获得所有 set方法
最新推荐文章于 2024-01-21 01:00:17 发布