java 对象之间属性值复制
2016-07-05 21:54
363 查看
Java对象之间属性值复制在许多开源框架中也有实现,在这里介绍下自己的实现、apache commons-beanutils、spring三种方式实现。
1 自己实现。采用反射,通过源对象getter 方法获取属性值,并通过目标对象的setter方法设置到目标对象中去。没有考虑太多性能,如果需要批量或者经常性执行该操作,建议不采用此种实现。下面给出源码:
[java]
view plain
copy
print?
/**
* 利用反射实现对象之间属性复制
* @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中关于复制的代码提取出来,最后修改成代码如下:
[java]
view plain
copy
print?
/**
* 利用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)
a0a9
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();
}
1 自己实现。采用反射,通过源对象getter 方法获取属性值,并通过目标对象的setter方法设置到目标对象中去。没有考虑太多性能,如果需要批量或者经常性执行该操作,建议不采用此种实现。下面给出源码:
[java]
view plain
copy
print?
/**
* 利用反射实现对象之间属性复制
* @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;
}
/** * 利用反射实现对象之间属性复制 * @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中关于复制的代码提取出来,最后修改成代码如下:
[java]
view plain
copy
print?
/**
* 利用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)
a0a9
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();
}
相关文章推荐
- 【Java进阶七】内部类
- Java Date和MySQL datetime数据类型转换
- Java如何判断线程池所有任务是否执行完毕
- 问题: Cannot change version of project facet Dynamic Web Module to 2.5.
- JAVA中的关键字
- [无异常,才正常]java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
- java.lang.InterruptedException: sleep interrupted异常
- java封装、继承和多态
- Java使用Collections.sort对一个列表进行自定义排序
- Spring整体架构
- java多线程系列02
- 【风马一族_Java】 Java的文件类 java.io
- java设计模式之工厂方法模式
- 吸血鬼数字(JAVA)
- Java集合排序(看完秒懂)
- Java开发必备:java帮助文档网址下载或在线帮助
- eclipse各个版本介绍(转)
- 3、继承
- java.net.SocketException中的异常
- Java性能优化指南系列(二):Java 性能分析工具