zoukankan      html  css  js  c++  java
  • BeanUtils(空值空集合空List不再复制)

    BeanUtils.copyProperties使用的时候,空值也被拷贝使它边的很鸡肋。

    下面对他进行扩展,支持多种空值不再拷贝

    public abstract class BeanUtils extends org.springframework.beans.BeanUtils {  
        
        public static void copyProperties(Object source, Object target) throws BeansException {  
          Assert.notNull(source, "Source must not be null");  
          Assert.notNull(target, "Target must not be null");  
          Class<?> actualEditable = target.getClass();  
          PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);  
          for (PropertyDescriptor targetPd : targetPds) {  
            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); 
                 
                  
                  // 这里判断以下value是否为空 当然这里也能进行一些特殊要求的处理 例如绑定时格式转换等等  
                  if (value != null) {  
                      boolean isEmpty = false;
                        if (value instanceof Set) {
                            Set s = (Set) value;
                            if (s == null || s.isEmpty()) {
                                isEmpty = true;
                            }
                        } else if (value instanceof Map) {
                            Map m = (Map) value;
                            if (m == null || m.isEmpty()) {
                                isEmpty = true;
                            }
                        } else if (value instanceof List) {
                            List l = (List) value;
                            if (l == null || l.size() < 1) {
                                isEmpty = true;
                            }
                        } else if (value instanceof Collection) {
                            Collection c = (Collection) value;
                            if (c == null || c.size() < 1) {
                                isEmpty = true;
                            }
                        }
                      
                    if (!isEmpty) { 
                    Method writeMethod = targetPd.getWriteMethod();  
                    if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {  
                      writeMethod.setAccessible(true);  
                    }  
                    writeMethod.invoke(target, value);  
                  }  
                  }
                } catch (Throwable ex) {  
                  throw new FatalBeanException("Could not copy properties from source to target", ex);  
                }  
              }  
            }  
          }  
        }  
      }  
  • 相关阅读:
    极验验证(滑动验证)的使用
    from close /destory
    tmeminifile and tinifile
    Delphi的OverRide、OverLoad和Virtual方法
    XE6 FMX之控件绘制与显示
    Delphi Android程序启动过程
    Delphi中的容器类
    接口
    集合
    class methed
  • 原文地址:https://www.cnblogs.com/wsy123/p/4463192.html
Copyright © 2011-2022 走看看