您的位置:首页 > 编程语言 > Java开发

java 对象之间属性值复制

2012-07-28 13:12 721 查看
java对象之间属性值复制在许多开源框架中也有实现,在这里介绍下自己的实现、apache commons-beanutils、Spring三种方式实现。

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

/**
* 利用反射实现对象之间属性复制
* @param from
* @param to
*/
public static void copyProperties(Object from, Object to) throws Exception {
copyPropertiesExclude(from, to, null);
}

/**
* 复制对象属性
* @param from
* @param to
* @param excludsArray 排除属性列表
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static void copyPropertiesExclude(Object from, Object to, String[] excludsArray) throws Exception {
List<String> excludesList = null;
if(excludsArray != null && excludsArray.length > 0) {
excludesList = Arrays.asList(excludsArray);	//构造列表对象
}
Method[] fromMethods = from.getClass().getDeclaredMethods();
Method[] toMethods = to.getClass().getDeclaredMethods();
Method fromMethod = null, toMethod = null;
String fromMethodName = null, toMethodName = null;
for (int i = 0; i < fromMethods.length; i++) {
fromMethod = fromMethods[i];
fromMethodName = fromMethod.getName();
if (!fromMethodName.contains("get"))
continue;
//排除列表检测
if(excludesList != null && excludesList.contains(fromMethodName.substring(3).toLowerCase())) {
continue;
}
toMethodName = "set" + fromMethodName.substring(3);
toMethod = findMethodByName(toMethods, toMethodName);
if (toMethod == null)
continue;
Object value = fromMethod.invoke(from, new Object[0]);
if(value == null)
continue;
//集合类判空处理
if(value instanceof Collection) {
Collection newValue = (Collection)value;
if(newValue.size() <= 0)
continue;
}
toMethod.invoke(to, new Object[] {value});
}
}

/**
* 对象属性值复制,仅复制指定名称的属性值
* @param from
* @param to
* @param includsArray
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static void copyPropertiesInclude(Object from, Object to, String[] includsArray) throws Exception {
List<String> includesList = null;
if(includsArray != null && includsArray.length > 0) {
includesList = Arrays.asList(includsArray);	//构造列表对象
} else {
return;
}
Method[] fromMethods = from.getClass().getDeclaredMethods();
Method[] toMethods = to.getClass().getDeclaredMethods();
Method fromMethod = null, toMethod = null;
String fromMethodName = null, toMethodName = null;
for (int i = 0; i < fromMethods.length; i++) {
fromMethod = fromMethods[i];
fromMethodName = fromMethod.getName();
if (!fromMethodName.contains("get"))
continue;
//排除列表检测
String str = fromMethodName.substring(3);
if(!includesList.contains(str.substring(0,1).toLowerCase() + str.substring(1))) {
continue;
}
toMethodName = "set" + fromMethodName.substring(3);
toMethod = findMethodByName(toMethods, toMethodName);
if (toMethod == null)
continue;
Object value = fromMethod.invoke(from, new Object[0]);
if(value == null)
continue;
//集合类判空处理
if(value instanceof Collection) {
Collection newValue = (Collection)value;
if(newValue.size() <= 0)
continue;
}
toMethod.invoke(to, new Object[] {value});
}
}

/**
* 从方法数组中获取指定名称的方法
*
* @param methods
* @param name
* @return
*/
public static Method findMethodByName(Method[] methods, String name) {
for (int j = 0; j < methods.length; j++) {
if (methods[j].getName().equals(name))
return methods[j];
}
return null;
}

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中关于复制的代码提取出来,最后修改成代码如下:

/**
* 利用spring实现bean之间属性复制
* @param source
* @param target
*/
@SuppressWarnings("unchecked")
public static void copyPropertiesBySpring(Object source, Object target) throws Exception {
Class actualEditable = target.getClass();
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
for (int i = 0; i < targetPds.length; i++) {
PropertyDescriptor targetPd = targetPds[i];
if (targetPd.getWriteMethod() != null) {
PropertyDescriptor sourcePd = getPropertyDescriptor(source
.getClass(), targetPd.getName());
if (sourcePd != null && sourcePd.getReadMethod() != null) {
try {
Method readMethod = sourcePd.getReadMethod();
if (!Modifier.isPublic(readMethod.getDeclaringClass()
.getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source, new Object[0]);
if(value == null)
continue;
//集合类判空处理
if(value instanceof Collection) {
//							Collection newValue = (Collection)value;
//							if(newValue.size() <= 0)
continue;
}
Method writeMethod = targetPd.getWriteMethod();
if (!Modifier.isPublic(writeMethod.getDeclaringClass()
.getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, new Object[] { value });
} catch (Throwable ex) {
}
}
}
}
}
/**
* 获取指定类指定名称的属性描述符
* @param clazz
* @param propertyName
* @return
* @throws BeansException
*/
@SuppressWarnings("unchecked")
public static PropertyDescriptor getPropertyDescriptor(Class clazz,
String propertyName) throws BeansException {
CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz);
return cr.getPropertyDescriptor(propertyName);
}

/**
* 获取指定类得所有属性描述符
* @param clazz
* @return
* @throws BeansException
*/
@SuppressWarnings("unchecked")
public static PropertyDescriptor[] getPropertyDescriptors(Class clazz) throws BeansException {
CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz);
return cr.getBeanInfo().getPropertyDescriptors();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: