java两个对象中相同的属性值复制
2014-06-26 17:03
127 查看
/**
* 两个对象中相同的属性值复制
* @param source
* @param dest
* @throws Exception
*/
public static void Copy(Object source, Object dest)throws Exception {
//获取属性
BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(), java.lang.Object.class);
PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors();
BeanInfo destBean = Introspector.getBeanInfo(dest.getClass(), java.lang.Object.class);
PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors();
try{
for(int i=0;i<sourceProperty.length;i++){
for(int j=0;j<destProperty.length;j++){
if(sourceProperty[i].getName().equals(destProperty[j].getName())){
//调用source的getter方法和dest的setter方法
destProperty[j].getWriteMethod().invoke(dest, sourceProperty[i].getReadMethod().invoke(source));
break;
}
}
}
}catch(Exception e){
throw new Exception("属性复制失败:"+e.getMessage());
}
}
* 两个对象中相同的属性值复制
* @param source
* @param dest
* @throws Exception
*/
public static void Copy(Object source, Object dest)throws Exception {
//获取属性
BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(), java.lang.Object.class);
PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors();
BeanInfo destBean = Introspector.getBeanInfo(dest.getClass(), java.lang.Object.class);
PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors();
try{
for(int i=0;i<sourceProperty.length;i++){
for(int j=0;j<destProperty.length;j++){
if(sourceProperty[i].getName().equals(destProperty[j].getName())){
//调用source的getter方法和dest的setter方法
destProperty[j].getWriteMethod().invoke(dest, sourceProperty[i].getReadMethod().invoke(source));
break;
}
}
}
}catch(Exception e){
throw new Exception("属性复制失败:"+e.getMessage());
}
}
相关文章推荐
- java中两个对象间的属性值复制,比较,转为map方法实现
- java两个相同Bean对象复制
- java 反射实现不同对象相同属性值复制
- Java相同类型的两个对象比较属性值,得到不同属性的名称和对应的值,附代码实例
- BeanUtils的使用-在具有很多相同属性的两个bean之间复制属性值
- Java:类的两个相同对象数组的克隆问题
- java通过xstream将xml转换为java对象遇到的问题(一个对像中两个子类的类名相同)
- C#用反射实现两个类的对象之间相同属性的值的复制
- java对象复制和属性值复制工具类
- Java比较两个对象是否相同并获得不同的字段
- BeanUtils的使用-在具有很多相同属性的两个bean之间复制属性值
- Java两个对象copy属性值
- Java中比较两个对象是否相同
- C#利用反射实现两个类的对象之间相同属性的值的复制
- java怎么判断两个Set 里的对象的值是否相同【两个set中的值是否相等】、java treeset和hashset如何判断元素是否相同【即对象是否完全相同;利用一个set去除重复元素】
- C#中比较两个对象的地址是否相同(也是引用计数的问题,和Java一样)
- C#用反射实现两个类的对象之间相同属性的值的复制
- JS判断两个对象相同属性的属性值是否相等
- Java -- 利用反射实现对象之间相同属性复制BeanUtil
- java 反射比较两个类型相同的对象 内部的变量是否相等