浏览 1814 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-10-25
在进行企业应用的开发过程中,我们经常会遇到原因而必须要从一个对象中拷贝值到另一个对象中,这两个对象有部份相同的字段,必须将这些相同字段的值从源对象拷贝到目标对象中,因此,一个值拷贝的实用类就变得很必要了,下面我们就用JAVA范型与JAVA影射去实现这样一个实用类吧,也算是JAVA范型的一个应用实例了,同时也是一个JAVA影身的简单应用实例。 package common; import java.lang.reflect.Array; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; /** * * @author Dao */ public class ValueCopyUtil { public static <T> T copyValue(T sourceObject) { return (T) copyValue(sourceObject, sourceObject.getClass()); } public static <T> T copyValue(Object sourceObject, Class<T> targetObjectClass) { return copyValue(sourceObject, targetObjectClass, null); } public static <T> T copyValue(Object sourceObject, Class<T> targetObjectClass, HashMap classMap) { try { Object targetObject = targetObjectClass.newInstance(); return (T) copyValue(sourceObject, targetObject, classMap); } catch (Exception e) { return null; } } public static <T> T copyValue(Object sourceObject, Object targetObject, HashMap classMap) { Class sourceObjectClass = sourceObject.getClass(); Class targetObjectClass = targetObject.getClass(); List<String> sourceClassFieldNameList = getClassFieldNameList(sourceObjectClass); List<String> targetClassFieldNameList = getClassFieldNameList(targetObjectClass); List<String> fieldNameList = getSameFieldNameList(sourceClassFieldNameList, targetClassFieldNameList); try { for (String fieldName : fieldNameList) { Field sourceField = sourceObjectClass.getDeclaredField(fieldName); Field targetField = targetObjectClass.getDeclaredField(fieldName); Class sourceFieldType = sourceField.getType(); Class targetFieldType = targetField.getType(); if (sourceFieldType == targetFieldType) { sourceField.setAccessible(true); targetField.setAccessible(true); Object sourceValue = sourceField.get(sourceObject); if (sourceValue != null) { if (isSimpleType(sourceFieldType)) { targetField.set(targetObject, sourceValue); } else { Object copiedObject = copyComplicatedType(sourceValue, classMap); targetField.set(targetObject, copiedObject); } } } } return (T) targetObject; } catch (Exception e) { return null; } } private static List<String> getClassFieldNameList(Class cla) { Field[] fields = cla.getDeclaredFields(); List<String> fieldNameList = new ArrayList(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; String fieldName = field.getName(); fieldNameList.add(fieldName); } return fieldNameList; } private static List<String> getSameFieldNameList(List<String> sourceList, List<String> targetList) { List<String> resultList = new ArrayList(); for (String source : sourceList) { for (String target : targetList) { if (source.equals(target)) { resultList.add(source); } } } return resultList; } private static boolean isSimpleType(Class cla) { if (cla.isPrimitive()) { return true; } if (cla == Date.class) { return true; } return false; } private static Object copyComplicatedType(Object sourceObject, HashMap classMap) { // System.out.println("========= copyComplicatedType ========="); Class sourceClass = sourceObject.getClass(); if (sourceClass.isArray()) { return copyArray(sourceObject, classMap); } else if (sourceObject instanceof List) { return copyList(sourceObject, classMap); } else if (sourceObject instanceof Map) { return copyMap(sourceObject, classMap); } return copySubObject(sourceObject, classMap); } private static Object copySubObject(Object subSourceObject, HashMap classMap) { // System.out.println("========= copySubobject ========="); Object mapClassObject = classMap.get(subSourceObject.getClass()); if (mapClassObject != null) { Class mapClass = (Class) mapClassObject; Object subTargetObject = copyValue(subSourceObject, mapClass, classMap); return subTargetObject; } return subSourceObject; } private static Object copyArray(Object sourceObjectArray, HashMap classMap) { // System.out.println("========= copyArray ========="); int arraySize = Array.getLength(sourceObjectArray); if (arraySize > 0) { Object testObject = Array.get(sourceObjectArray, 0); Object targetObjectArray = Array.newInstance(testObject.getClass(), arraySize); for (int i = 0; i < arraySize; i++) { Object subSourceObject = Array.get(sourceObjectArray, i); Object subTargetObject = copySubObject(subSourceObject, classMap); Array.set(targetObjectArray, i, subTargetObject); } } return sourceObjectArray; } private static Object copyMap(Object sourceObject, HashMap classMap) { // System.out.println("========= copyMap ========="); try { Class sourceObjectClass = sourceObject.getClass(); Map targetObjectMap = (Map) sourceObjectClass.newInstance(); Map sourceObjectMap = (Map) sourceObject; Set set = sourceObjectMap.entrySet(); Iterator<Entry> iterator = set.iterator(); while (iterator.hasNext()) { Entry sourceEntry = iterator.next(); Object subSourceObject = sourceEntry.getValue(); Object subTargetObject = copySubObject(subSourceObject, classMap); targetObjectMap.put(sourceEntry.getKey(), subTargetObject); } return targetObjectMap; } catch (Exception e) { e.printStackTrace(); } return sourceObject; } private static Object copyList(Object sourceObject, HashMap classMap) { // System.out.println("========= copyList ========="); try { Class sourceObjectClass = sourceObject.getClass(); List targetObjectList = (List) sourceObjectClass.newInstance(); List sourceObjectList = (List) sourceObject; for (Object subObject : sourceObjectList) { Object subTargetObject = copySubObject(subObject, classMap); targetObjectList.add(subTargetObject); } return targetObjectList; } catch (Exception e) { e.printStackTrace(); } return sourceObject; } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |