public Object autoPackaging(Object t1, Object t2) {
//创建map来存放临时的数据
Map<String, Object> map = new HashMap<String, Object>();
//通过对象.getClass获得反射对象,
Field[] f1 = t1.getClass().getDeclaredFields();
for (int i = 0; i < f1.length; i++) {
f1[i].setAccessible(true);
if (“serialVersionUID”.equals(f1[i].getName())) {
continue;
}
String d1_name = f1[i].getName();
Object d1_num = null;
try {
d1_num = f1[i].get(t1);
} catch (IllegalArgumentException e) {
log.error(“发生IllegalArgumentException 异常:不合法或不正确的参数”);
throw new IllegalArgumentException(e.getMessage());
} catch (IllegalAccessException e) {
log.error(“发生IllegalArgumentException 异常:有pirvate修饰的方法没有设置为public”);
throw new IllegalArgumentException(e.getMessage());
}
if (d1_num != null && !"".equals(d1_num)) {
map.put(d1_name, d1_num.toString());
}
}
// 获取t2的属性名
Map<String, Object> t2_map = new HashMap<String, Object>();
Field[] f2 = t2.getClass().getDeclaredFields();
for (int a = 0; a < f2.length; a++) {
f2[a].setAccessible(true);
if (map.containsKey(f2[a].getName())) {
t2_map.put(f2[a].getName(), map.get(f2[a].getName()));
}
}
return JSONObject.toBean(JSONObject.fromObject(t2_map), t2.getClass());
}