zoukankan      html  css  js  c++  java
  • 合并两个java bean对象非空属性(泛型)

    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);
                    }
    
                }
            }
        }
    }
  • 相关阅读:
    oracle 主键自动地址实现
    解构赋值
    那些朋友那些话系列
    那些朋友那些话
    白鹭记事
    该如何存在
    上海秋季HCC小记
    For the person you never see again
    寻城记
    2013年的国庆
  • 原文地址:https://www.cnblogs.com/zhangpengshou/p/5565750.html
Copyright © 2011-2022 走看看