通过反射重新给当前对象赋值
/* 通过反射重新set值
* @param hashMap 存储 字段名 -- 字段对应值
* @return void
*/
public <T> void getField(T data) {
Class<?> aClass = data.getClass();
Field[] fields = aClass.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
// 得到字段属性名
String name = field.getName();
try {
// 得到字段值
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(name, aClass);
Method readMethod = propertyDescriptor.getReadMethod();
Object value = readMethod.invoke(data);
ZxdDictConversionColumn annotation = field.getAnnotation(ZxdDictConversionColumn.class);
if (annotation != null && StringUtil.isNotBlank(annotation.key()) && value != null) {
//重新赋值数据
String value = "重新赋值数据"
setPropertyVal(field.getName(), data,value );
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//反射set方法
private static void setPropertyVal(String propertyName, Object s, Object newVal)
throws IntrospectionException, IllegalAccessException, InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(propertyName, s.getClass());
Method setMethod = pd.getWriteMethod();
setMethod.invoke(s, newVal);
}