packagecom.network.utils;importjava.lang.reflect.Field;importjava.lang.reflect.Modifier;/*** @Description: 对象合并的工具类
*@authorvictor*/
public classCombineBeansUtil {/**** 该方法是用于相同对象不同属性值的合并
** 如果两个相同对象中同一属性都有值,那么sourceBean中的值会覆盖tagetBean重点的值
** 如果sourceBean有值,targetBean没有,则采用sourceBean的值
** 如果sourceBean没有值,targetBean有,则保留targetBean的值
*
*@paramsourceBean 被提取的对象bean
*@paramtargetBean 用于合并的对象bean
*@returntargetBean,合并后的对象*/
public static T combineSydwCore(T sourceBean, T targetBean){
Class extends Object> sourceBeanClass =sourceBean.getClass();
Class extends Object> targetBeanClass =targetBean.getClass();
Field[] sourceFields=sourceBeanClass.getDeclaredFields();
Field[] targetFields=targetBeanClass.getDeclaredFields();for(int i=0; i
Field sourceField=sourceFields[i];if(Modifier.isStatic(sourceField.getModifiers())){continue;
}
Field targetField=targetFields[i];if(Modifier.isStatic(targetField.getModifiers())){continue;
}
sourceField.setAccessible(true);
targetField.setAccessible(true);try{if( !(sourceField.get(sourceBean) == null) && !"serialVersionUID".equals(sourceField.getName().toString())){
targetField.set(targetBean,sourceField.get(sourceBean));
}
}catch (IllegalArgumentException |IllegalAccessException e) {
e.printStackTrace();
}
}returntargetBean;
}
}