如题
public class SetClassUtil {
@SuppressWarnings("unchecked")
public static Object setClassFileValue(Class c, List<String> list) {
try {
Object object = Class.forName(c.getName()).newInstance();
Class<?> obj = object.getClass();
Field[] fields = obj.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
fields[i].setAccessible(true);
for (int j = 0; j < list.size(); j++) {
String str = list.get(j);
String strs[] = str.split(";");
if (strs[0].equals(fields[i].getName())) {
fields[i].set(object, strs[1].equals("备注") || strs[1].equals("其他") ? "" : strs[1]);
break;
}
}
}
return object;
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
return null;
}
}
应用
Vo vo = new Vo();
Vo newVo = (Vo) SetClassUtil.setClassFileValue(vo.getClass(), lableCodeList);