packagecom.datad.dream.utils;importjava.beans.PropertyDescriptor;importjava.lang.reflect.InvocationTargetException;importjava.util.Iterator;importjava.util.Map;importorg.apache.commons.beanutils.DynaBean;importorg.apache.commons.beanutils.DynaProperty;importorg.apache.commons.beanutils.PropertyUtils;importorg.apache.commons.beanutils.PropertyUtilsBean;importorg.apache.commons.collections.map.LinkedMap;/***
* Title:JavaBean对象转换工具类
*
*
* Description:复制两个不同Bean的相同属性值;
*
*
*@authorLion
*@version2.0*/
public class MyBeanUtils extendsPropertyUtilsBean {publicMyBeanUtils() {super();
}private static void convert(Object dest, Object orig) throwsIllegalAccessException, InvocationTargetException {if (dest == null) {throw new IllegalArgumentException("No destination bean specified");
}if (orig == null) {throw new IllegalArgumentException("No origin bean specified");
}if (orig instanceofDynaBean) {
DynaProperty origDescriptors[]=((DynaBean) orig).getDynaClass()
.getDynaProperties();for (int i = 0; i < origDescriptors.length; i++) {
String name=origDescriptors[i].getName();if(PropertyUtils.isWriteable(dest, name)) {
Object value=((DynaBean) orig).get(name);try{
getInstance().setSimpleProperty(dest, name, value);
}catch(Exception e) {
;//Should not happen
}
}
}
}else if (orig instanceofMap) {
Iterator names=((Map) orig).keySet().iterator();while(names.hasNext()) {
String name=(String) names.next();if(PropertyUtils.isWriteable(dest, name)) {
Object value=((Map) orig).get(name);try{
getInstance().setSimpleProperty(dest, name, value);
}catch(Exception e) {
;//Should not happen
}
}
}
}else{
PropertyDescriptor origDescriptors[]=PropertyUtils
.getPropertyDescriptors(orig);for (int i = 0; i < origDescriptors.length; i++) {
String name=origDescriptors[i].getName();if ("class".equals(name)) {continue;
}if(PropertyUtils.isReadable(orig, name)&&PropertyUtils.isWriteable(dest, name)) {try{
Object value=PropertyUtils.getSimpleProperty(orig,
name);
getInstance().setSimpleProperty(dest, name, value);
}catch(IllegalArgumentException ie) {
;//Should not happen
} catch(Exception e) {
}
}
}
}
}/*** 对象拷贝 数据对象空值不拷贝到目标对象
*
*@paramdataObject
*@paramtoObject
*@throwsNoSuchMethodException copy*/
public static voidcopyBeanNotNull2Bean(Object databean, Object tobean)throwsException {
PropertyDescriptor origDescriptors[]=PropertyUtils
.getPropertyDescriptors(databean);for (int i = 0; i < origDescriptors.length; i++) {
String name=origDescriptors[i].getName();if ("class".equals(name)) {continue;
}if(PropertyUtils.isReadable(databean, name)&&PropertyUtils.isWriteable(tobean, name)) {try{
Object value=PropertyUtils.getSimpleProperty(databean,
name);if (value != null) {
getInstance().setSimpleProperty(tobean, name, value);
}
}catch(IllegalArgumentException ie) {
;//Should not happen
} catch(Exception e) {
;//Should not happen
}
}
}
}/*** 把orig和dest相同属性的value复制到dest中
*
*@paramdest
*@paramorig
*@throwsIllegalAccessException
*@throwsInvocationTargetException*/
public static void copyBean2Bean(Object dest, Object orig) throwsException {if (orig == null) {
dest= null;return;
}
convert(dest, orig);
}public static voidcopyBean2Map(Map map, Object bean) {
PropertyDescriptor[] pds=PropertyUtils.getPropertyDescriptors(bean);for (int i = 0; i < pds.length; i++) {
PropertyDescriptor pd=pds[i];
String propname=pd.getName();try{
Object propvalue=PropertyUtils.getSimpleProperty(bean,
propname);
map.put(propname, propvalue);
}catch(IllegalAccessException e) {//e.printStackTrace();
} catch(InvocationTargetException e) {//e.printStackTrace();
} catch(NoSuchMethodException e) {//e.printStackTrace();
}
}
}/*** 将Map内的key与Bean中属性相同的内容复制到BEAN中
*
*@parambean Object
*@paramproperties Map
*@throwsIllegalAccessException
*@throwsInvocationTargetException*/
public static void copyMap2Bean(Object bean, Map properties) throwsIllegalAccessException, InvocationTargetException {if ((bean == null) || (properties == null)) {return;
}
Iterator names=properties.keySet().iterator();while(names.hasNext()) {
String name=(String) names.next();if (name == null) {continue;
}
Object value=properties.get(name);try{
Class clazz=PropertyUtils.getPropertyType(bean, name);if (null ==clazz) {continue;
}
String className=clazz.getName();if (className.equalsIgnoreCase("java.sql.Timestamp")) {if (value == null || value.equals("")) {continue;
}
}
getInstance().setSimpleProperty(bean, name, value);
}catch(NoSuchMethodException e) {continue;
}
}
}/*** 自动转Map key值大写 将Map内的key与Bean中属性相同的内容复制到BEAN中
*
*@parambean Object
*@paramproperties Map
*@throwsIllegalAccessException
*@throwsInvocationTargetException*/
public static voidcopyMap2Bean_Nobig(Object bean, Map properties)throwsIllegalAccessException, InvocationTargetException {if ((bean == null) || (properties == null)) {return;
}
Iterator names=properties.keySet().iterator();while(names.hasNext()) {
String name=(String) names.next();if (name == null) {continue;
}
Object value=properties.get(name);//命名应该大小写应该敏感(否则取不到对象的属性)
try{if (value == null) { //不光Date类型,好多类型在null时会出错
continue; //如果为null不用设 (对象如果有特殊初始值也可以保留?)
}
Class clazz=PropertyUtils.getPropertyType(bean, name);if (null == clazz) { //在bean中这个属性不存在
continue;
}
String className=clazz.getName();//临时对策(如果不处理默认的类型转换时会出错)
if (className.equalsIgnoreCase("java.util.Date")) {
value= newjava.util.Date(
((java.sql.Timestamp) value).getTime());//wait to//do:貌似有时区问题,//待进一步确认
}
getInstance().setSimpleProperty(bean, name, value);
}catch(NoSuchMethodException e) {continue;
}
}
}/*** Map内的key与Bean中属性相同的内容复制到BEAN中 对于存在空值的取默认值
*
*@parambean Object
*@paramproperties Map
*@paramdefaultValue String
*@throwsIllegalAccessException
*@throwsInvocationTargetException*/
public static void copyMap2Bean(Object bean, Map properties, String defaultValue) throwsIllegalAccessException,
InvocationTargetException {if ((bean == null) || (properties == null)) {return;
}
Iterator names=properties.keySet().iterator();while(names.hasNext()) {
String name=(String) names.next();if (name == null) {continue;
}
Object value=properties.get(name);try{
Class clazz=PropertyUtils.getPropertyType(bean, name);if (null ==clazz) {continue;
}
String className=clazz.getName();if (className.equalsIgnoreCase("java.sql.Timestamp")) {if (value == null || value.equals("")) {continue;
}
}if (className.equalsIgnoreCase("java.lang.String")) {if (value == null) {
value=defaultValue;
}
}
getInstance().setSimpleProperty(bean, name, value);
}catch(NoSuchMethodException e) {continue;
}
}
}/*** 对象属性深度拷贝(JACKSON转换)
*
*@paramdest
*@paramsource
*@return*@throwsException*/@SuppressWarnings("unchecked")public static void copyDeepBean2Bean(Object dest, Object source) throwsException {if (source == null) {return;
}
Map map = newLinkedMap();
MyBeanUtils.copyBean2Map(map, source);
MyBeanUtils.copyMap2Bean(dest, map);
}
}