BeanUtils.copyProperties忽略null值

主要思路是继承BeanUtilsBean这个类,然后重载copyProperties方法,用法new BeanCopyUtil(). copyProperties()

最后一个参数,如果是true,忽略;false,不忽略

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;


import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BeanCopyUtil extends BeanUtilsBean{

private static final Logger log=LoggerFactory.getLogger(BeanCopyUtil.class);
/**

* @param dest 目标对象
* @param orig 源对象
* @param ignoreNullFlag 是否忽略null值
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
 public void copyProperties(Object dest, Object orig,boolean ignoreNullFlag)
       throws IllegalAccessException, InvocationTargetException {


       // Validate existence of the specified beans
       if (dest == null) {
           throw new IllegalArgumentException
                   ("No destination bean specified");
       }
       if (orig == null) {
           throw new IllegalArgumentException("No origin bean specified");
       }
       if (log.isDebugEnabled()) {
           log.debug("BeanUtils.copyProperties(" + dest + ", " +
                     orig + "," + ignoreNullFlag + ")");
       }


       // Copy the properties, converting as necessary
       if (orig instanceof DynaBean) {
           DynaProperty[] origDescriptors =
               ((DynaBean) orig).getDynaClass().getDynaProperties();
           for (int i = 0; i < origDescriptors.length; i++) {
               String name = origDescriptors[i].getName();
               // Need to check isReadable() for WrapDynaBean
               // (see Jira issue# BEANUTILS-61)
               if (getPropertyUtils().isReadable(orig, name) &&
                   getPropertyUtils().isWriteable(dest, name)) {
                   Object value = ((DynaBean) orig).get(name);
                   if(ignoreNullFlag){
                    if(value!=null){
                    copyProperty(dest, name, value);
                    }
                   }else{
                    copyProperty(dest, name, value);
                   }
               }
           }
       } else if (orig instanceof Map) {
           @SuppressWarnings("unchecked")
           // Map properties are always of type
           Map propMap = (Map) orig;
           for (Map.Entry entry : propMap.entrySet()) {
               String name = entry.getKey();
               Object value=entry.getValue();
               if (getPropertyUtils().isWriteable(dest, name)) {
                   if(ignoreNullFlag){
                    if(value!=null){
                    copyProperty(dest, name, value);
                    }
                   }else{
                    copyProperty(dest, name, value);
                   }
               }
           }
       } else /* if (orig is a standard JavaBean) */ {
           PropertyDescriptor[] origDescriptors =
               getPropertyUtils().getPropertyDescriptors(orig);
           for (int i = 0; i < origDescriptors.length; i++) {
               String name = origDescriptors[i].getName();
               if ("class".equals(name)) {
                   continue; // No point in trying to set an object's class
               }
               if (getPropertyUtils().isReadable(orig, name) &&
                   getPropertyUtils().isWriteable(dest, name)) {
                   try {
                       Object value =
                           getPropertyUtils().getSimpleProperty(orig, name);
                       if(ignoreNullFlag){
                    if(value!=null){
                    copyProperty(dest, name, value);
                    }
                   }else{
                    copyProperty(dest, name, value);
                   }
                   } catch (NoSuchMethodException e) {
                       // Should not happen
                   }
               }
           }
       }
 }
}

你可能感兴趣的:(apache,java)