合并两个java bean对象非空属性(泛型)
2016-06-07 07:36
567 查看
import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; class Beanutils{ //merge two bean by discovering differences public static <M> void merge(M target, M destination) throws Exception { BeanInfo beanInfo = Introspector.getBeanInfo(target.getClass()); // Iterate over all the attributes for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) { // Only copy writable attributes if (descriptor.getWriteMethod() != null) { Object originalValue = descriptor.getReadMethod() .invoke(target); // Only copy values values where the destination values is null if (originalValue == null) { Object defaultValue = descriptor.getReadMethod().invoke( destination); descriptor.getWriteMethod().invoke(target, defaultValue); } } } } }
相关文章推荐
- java学习总结(16.06.07)类的静态成员和非静态成员
- 从零到一 新手教程 - JAVA Spring MVC
- java如何计算程序运行时间
- JAVA中分为基本数据类型及引用数据类型
- Spring依赖注入的四种方式(重点是注解方式)
- Eclipse中Maven+Spring3.2.8+SpringMVC HelloWorld项目
- JavaFX实现简单聊天室应用(服务器端)
- javaFX实现简单聊天室应用(客户端)
- java中的抽象类和接口
- jdk之Interger.toString(int i, int radix)
- eclipse安装SVN报错
- Java堆内存Heap与非堆内存Non-Heap简介和设置
- Java连接MySQL数据库。。。
- Spring:协调作用域不同步的Bean
- Collection 接口 & List 接口
- Java WebSocket基本原理
- Struts2(三):新建Struts2工程
- Java反射获取类和对象信息全解析
- JAVA字符串占位符替换
- Windows下java调用c的dll动态库--Dev_Cpp编译c生成dll