java 对象之间属性值复制

来源:互联网 发布:算法 第四版 中文 pdf 编辑:程序博客网 时间:2024/05/18 18:44

Java对象之间属性值复制在许多开源框架中也有实现,在这里介绍下自己的实现、apache commons-beanutils、spring三种方式实现。

1 自己实现。采用反射,通过源对象getter 方法获取属性值,并通过目标对象的setter方法设置到目标对象中去。没有考虑太多性能,如果需要批量或者经常性执行该操作,建议不采用此种实现。下面给出源码:

[java] view plain copy
print?
  1. /** 
  2.      * 利用反射实现对象之间属性复制 
  3.      * @param from 
  4.      * @param to 
  5.      */  
  6.     public static void copyProperties(Object from, Object to) throws Exception {  
  7.         copyPropertiesExclude(from, to, null);  
  8.     }  
  9.       
  10.     /** 
  11.      * 复制对象属性 
  12.      * @param from 
  13.      * @param to 
  14.      * @param excludsArray 排除属性列表 
  15.      * @throws Exception 
  16.      */  
  17.     @SuppressWarnings("unchecked")  
  18.     public static void copyPropertiesExclude(Object from, Object to, String[] excludsArray) throws Exception {  
  19.         List<String> excludesList = null;  
  20.         if(excludsArray != null && excludsArray.length > 0) {  
  21.             excludesList = Arrays.asList(excludsArray); //构造列表对象  
  22.         }  
  23.         Method[] fromMethods = from.getClass().getDeclaredMethods();  
  24.         Method[] toMethods = to.getClass().getDeclaredMethods();  
  25.         Method fromMethod = null, toMethod = null;  
  26.         String fromMethodName = null, toMethodName = null;  
  27.         for (int i = 0; i < fromMethods.length; i++) {  
  28.             fromMethod = fromMethods[i];  
  29.             fromMethodName = fromMethod.getName();  
  30.             if (!fromMethodName.contains("get"))  
  31.                 continue;  
  32.             //排除列表检测  
  33.             if(excludesList != null && excludesList.contains(fromMethodName.substring(3).toLowerCase())) {  
  34.                 continue;  
  35.             }  
  36.             toMethodName = "set" + fromMethodName.substring(3);  
  37.             toMethod = findMethodByName(toMethods, toMethodName);  
  38.             if (toMethod == null)  
  39.                 continue;  
  40.             Object value = fromMethod.invoke(from, new Object[0]);  
  41.             if(value == null)  
  42.                 continue;  
  43.             //集合类判空处理  
  44.             if(value instanceof Collection) {  
  45.                 Collection newValue = (Collection)value;  
  46.                 if(newValue.size() <= 0)  
  47.                     continue;  
  48.             }  
  49.             toMethod.invoke(to, new Object[] {value});  
  50.         }  
  51.     }  
  52.       
  53.     /** 
  54.      * 对象属性值复制,仅复制指定名称的属性值 
  55.      * @param from 
  56.      * @param to 
  57.      * @param includsArray 
  58.      * @throws Exception 
  59.      */  
  60.     @SuppressWarnings("unchecked")  
  61.     public static void copyPropertiesInclude(Object from, Object to, String[] includsArray) throws Exception {  
  62.         List<String> includesList = null;  
  63.         if(includsArray != null && includsArray.length > 0) {  
  64.             includesList = Arrays.asList(includsArray); //构造列表对象  
  65.         } else {  
  66.             return;  
  67.         }  
  68.         Method[] fromMethods = from.getClass().getDeclaredMethods();  
  69.         Method[] toMethods = to.getClass().getDeclaredMethods();  
  70.         Method fromMethod = null, toMethod = null;  
  71.         String fromMethodName = null, toMethodName = null;  
  72.         for (int i = 0; i < fromMethods.length; i++) {  
  73.             fromMethod = fromMethods[i];  
  74.             fromMethodName = fromMethod.getName();  
  75.             if (!fromMethodName.contains("get"))  
  76.                 continue;  
  77.             //排除列表检测  
  78.             String str = fromMethodName.substring(3);  
  79.             if(!includesList.contains(str.substring(0,1).toLowerCase() + str.substring(1))) {  
  80.                 continue;  
  81.             }  
  82.             toMethodName = "set" + fromMethodName.substring(3);  
  83.             toMethod = findMethodByName(toMethods, toMethodName);  
  84.             if (toMethod == null)  
  85.                 continue;  
  86.             Object value = fromMethod.invoke(from, new Object[0]);  
  87.             if(value == null)  
  88.                 continue;  
  89.             //集合类判空处理  
  90.             if(value instanceof Collection) {  
  91.                 Collection newValue = (Collection)value;  
  92.                 if(newValue.size() <= 0)  
  93.                     continue;  
  94.             }  
  95.             toMethod.invoke(to, new Object[] {value});  
  96.         }  
  97.     }  
  98.       
  99.       
  100.   
  101.     /** 
  102.      * 从方法数组中获取指定名称的方法 
  103.      *  
  104.      * @param methods 
  105.      * @param name 
  106.      * @return 
  107.      */  
  108.     public static Method findMethodByName(Method[] methods, String name) {  
  109.         for (int j = 0; j < methods.length; j++) {  
  110.             if (methods[j].getName().equals(name))  
  111.                 return methods[j];  
  112.         }  
  113.         return null;  
  114.     }  

2 利用apache commons-beanutils的开源实现。

BeanUtils.copyProperties(dst, src)。方法能够将源对象和目标对象中相同名称的属性值复制过去。注意的是参数前面的是目标对象,后面是源对象。使用该方法需要注意:不能将入口方法与源对象、目标对象之一放在同一源文件之内,否者将没有任何效果。

PropertyUtils.copyProperties(dst, src)。功能与BeanUtils.copyProperties类似,只是在同属性名称的类型参数之间可以执行转换操作。

3 利用Spring实现属性之间的复制。spring内部自有实现方法,如果我们需要在外面采用spring的托管复制,需要修改spring的源码,将spring中的org.springframework.beans.CachedIntrospectionResults类的forClass、getPropertyDescriptor、getBeanInfo改为可见的后重新打包。然后将Spring中关于复制的代码提取出来,最后修改成代码如下:

[java] view plain copy
print?
  1. /** 
  2.      * 利用spring实现bean之间属性复制 
  3.      * @param source 
  4.      * @param target 
  5.      */  
  6.     @SuppressWarnings("unchecked")  
  7.     public static void copyPropertiesBySpring(Object source, Object target) throws Exception {  
  8.         Class actualEditable = target.getClass();  
  9.         PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);  
  10.         for (int i = 0; i < targetPds.length; i++) {  
  11.             PropertyDescriptor targetPd = targetPds[i];  
  12.             if (targetPd.getWriteMethod() != null) {  
  13.                 PropertyDescriptor sourcePd = getPropertyDescriptor(source  
  14.                         .getClass(), targetPd.getName());  
  15.                 if (sourcePd != null && sourcePd.getReadMethod() != null) {  
  16.                     try {  
  17.                         Method readMethod = sourcePd.getReadMethod();  
  18.                         if (!Modifier.isPublic(readMethod.getDeclaringClass()  
  19.                                 .getModifiers())) {  
  20.                             readMethod.setAccessible(true);  
  21.                         }  
  22.                         Object value = readMethod.invoke(source, new Object[0]);  
  23.                         if(value == null)  
  24.                             continue;  
  25.                         //集合类判空处理  
  26.                         if(value instanceof Collection) {  
  27. //                          Collection newValue = (Collection)value;  
  28. //                          if(newValue.size() <= 0)  
  29.                                 continue;  
  30.                         }  
  31.                         Method writeMethod = targetPd.getWriteMethod();  
  32.                         if (!Modifier.isPublic(writeMethod.getDeclaringClass()  
  33.                                 .getModifiers())) {  
  34.                             writeMethod.setAccessible(true);  
  35.                         }  
  36.                         writeMethod.invoke(target, new Object[] { value });  
  37.                     } catch (Throwable ex) {  
  38.                     }  
  39.                 }  
  40.             }  
  41.         }  
  42.     }     
  43.     /** 
  44.      * 获取指定类指定名称的属性描述符 
  45.      * @param clazz 
  46.      * @param propertyName 
  47.      * @return 
  48.      * @throws BeansException 
  49.      */  
  50.     @SuppressWarnings("unchecked")  
  51.     public static PropertyDescriptor getPropertyDescriptor(Class clazz,  
  52.             String propertyName) throws BeansException {  
  53.         CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz);  
  54.         return cr.getPropertyDescriptor(propertyName);  
  55.     }  
  56.       
  57.       
  58.     /** 
  59.      * 获取指定类得所有属性描述符 
  60.      * @param clazz 
  61.      * @return 
  62.      * @throws BeansException 
  63.      */  
  64.     @SuppressWarnings("unchecked")  
  65.     public static PropertyDescriptor[] getPropertyDescriptors(Class clazz) throws BeansException {  
  66.         CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz);  
  67.         return cr.getBeanInfo().getPropertyDescriptors();  
  68.     }  
0 0
原创粉丝点击