将一个对象的属性值赋给另一个对象的相同的属性 这两个对象必须都符合javaBean的标准
来源:互联网 发布:it 知乎 编辑:程序博客网 时间:2024/04/28 16:53
/***
* 将一个对象的属性值赋给另一个对象的相同的属性 这两个对象必须都符合javaBean的标准
*
* @param source
* 要赋值的源对象
* @param target
* 被赋值的目标对象
* @param ignoreProperties
* 被忽略赋值的属性数组
* @throws Exception
*
*/
@SuppressWarnings("unused")
public static void copyProperties(Object source, Object target, String ignoreProperties[]) throws Exception {
Class targetClass = target.getClass();
Class sourceClass = source.getClass();
// 得到目标对象和源对象的属性数组
PropertyDescriptor targetPds[] = Introspector.getBeanInfo(targetClass).getPropertyDescriptors();
PropertyDescriptor sourcetPds[] = Introspector.getBeanInfo(sourceClass).getPropertyDescriptors();
// 将忽略字段的数组转化为list
List<String> ignoreList = ignoreProperties == null ? null : Arrays.asList(ignoreProperties);
// 把源对象的所有属性放的map中
Map<String, PropertyDescriptor> sourcePropertyMap = new HashMap<String, PropertyDescriptor>();
for (int i = 0; i < sourcetPds.length; i++) {
PropertyDescriptor pd = sourcetPds[i];
sourcePropertyMap.put(pd.getName(), pd);
}
for (int i = 0; i < targetPds.length; i++) {
PropertyDescriptor targetPd = targetPds[i];
if (targetPd.getWriteMethod() == null || ignoreProperties != null && ignoreList.contains(targetPd.getName()))
continue;
PropertyDescriptor sourcePd = sourcePropertyMap.get(targetPd.getName());
if (sourcePd == null || sourcePd.getReadMethod() == null)
continue;
try {
// 得到源对象对应的属性值
Method readMethod = sourcePd.getReadMethod();
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers()))
readMethod.setAccessible(true);
Object value = readMethod.invoke(source, new Object[0]);
// 将源对象的属性值赋值给目标对象对应的属性
Method writeMethod = targetPd.getWriteMethod();
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers()))
writeMethod.setAccessible(true);
writeMethod.invoke(target, new Object[] { value });
} catch (Throwable ex) {
throw new IllegalArgumentException("Could not copy properties from source to target", ex);
}
}
}
* 将一个对象的属性值赋给另一个对象的相同的属性 这两个对象必须都符合javaBean的标准
*
* @param source
* 要赋值的源对象
* @param target
* 被赋值的目标对象
* @param ignoreProperties
* 被忽略赋值的属性数组
* @throws Exception
*
*/
@SuppressWarnings("unused")
public static void copyProperties(Object source, Object target, String ignoreProperties[]) throws Exception {
Class targetClass = target.getClass();
Class sourceClass = source.getClass();
// 得到目标对象和源对象的属性数组
PropertyDescriptor targetPds[] = Introspector.getBeanInfo(targetClass).getPropertyDescriptors();
PropertyDescriptor sourcetPds[] = Introspector.getBeanInfo(sourceClass).getPropertyDescriptors();
// 将忽略字段的数组转化为list
List<String> ignoreList = ignoreProperties == null ? null : Arrays.asList(ignoreProperties);
// 把源对象的所有属性放的map中
Map<String, PropertyDescriptor> sourcePropertyMap = new HashMap<String, PropertyDescriptor>();
for (int i = 0; i < sourcetPds.length; i++) {
PropertyDescriptor pd = sourcetPds[i];
sourcePropertyMap.put(pd.getName(), pd);
}
for (int i = 0; i < targetPds.length; i++) {
PropertyDescriptor targetPd = targetPds[i];
if (targetPd.getWriteMethod() == null || ignoreProperties != null && ignoreList.contains(targetPd.getName()))
continue;
PropertyDescriptor sourcePd = sourcePropertyMap.get(targetPd.getName());
if (sourcePd == null || sourcePd.getReadMethod() == null)
continue;
try {
// 得到源对象对应的属性值
Method readMethod = sourcePd.getReadMethod();
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers()))
readMethod.setAccessible(true);
Object value = readMethod.invoke(source, new Object[0]);
// 将源对象的属性值赋值给目标对象对应的属性
Method writeMethod = targetPd.getWriteMethod();
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers()))
writeMethod.setAccessible(true);
writeMethod.invoke(target, new Object[] { value });
} catch (Throwable ex) {
throw new IllegalArgumentException("Could not copy properties from source to target", ex);
}
}
}
0 0
- 将一个对象的属性值赋给另一个对象的相同的属性 这两个对象必须都符合javaBean的标准
- 【反射机制】两个类名不同但其属性相同的对象,如何将一个对象的值赋给另外一个对象
- 通过反射将一个对象的值赋给另一个对象中对应的属性 不需要用set、get
- 把一个对象的值赋给另一个对象中对应的属性
- 当需要把一个对象的值赋给另一个对象中对应的属性时……
- 通过反射获取类中的属性,并且将类中的属性名称以及对应的值赋给另一个类中的对象
- BeanUtils拷贝两个对象的相同属性
- 将一个 vector 赋给另一个 vector(迭代器的运用)_初学vector对象3
- 将JSON对象、数组的值赋给另外一个对象,(互不影响) 对象赋值
- 两个具有相同属性的对象之间属性的拷贝
- 比较两个对象的属性值是否相同
- 将一个类的对象的值,赋给另一对象
- java 对象比较 判断两个对象的某些或某个属性相同 这对象相等。
- 两个List比较各自对象的属性相同的问题
- java比较两个相同对象的属性是否一致
- 已知javabean形式类的类名,给这个类的一个对象赋初值
- xml 将解析的文件里的值赋给对象,将对象存入集合
- oracle把一个用户下数据库对象的某个权限赋给另一个用户
- 那些年犯过的2(出过的BUG)
- C#中构造函数的使用
- Cocos2d-x利用jni调用java层代码
- OCX控件MSI打包升级方案
- 图片加载框架
- 将一个对象的属性值赋给另一个对象的相同的属性 这两个对象必须都符合javaBean的标准
- 两种高效的并发模式:半同步/半异步和领导者/追随者
- 利用RTFtemplate生成rtf报表
- rsyslogd 日志服务
- myEclipse相同变量变色设置
- 韦东山开发板QT移植笔记
- 自定义TextView实现验证码功能
- 关于数据库有几个库的,一点笔记
- ionic cordova 创建自定义项目