BeanUtil

package com.XXXXXX.XXXXXX.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;


/**描述:实体bean工具类<br>
 * 修改日期:2013年12月19日上午10:07:36 <br>
 */
public class ECSBeanUtils {
    private static final Logger logger = Logger.getLogger(ECSBeanUtils.class);
    
    private ECSBeanUtils(){}
    
    /**
     * 方法名称: copyProperties<br>
     * 描述:拷贝对象
     * 修改日期:2014年1月10日下午4:53:34
     * @param source 源对象
     * @param target 目标对象
     */
    /*public static void copyProperties(Object source,Object target){
        copyProperties(source,target,null);
    }*/
    
    /**
     * 方法名称: copyProperties<br>
     * 描述:
     * 修改日期:2014年1月10日下午4:54:17
     * @param source 源对象
     * @param target 目标对象
     * @param ignoreProperties 数组类型 忽略的字段
     */
    public static void copyProperties(Object source,Object target,String...  ignoreProperties){
        if(ignoreProperties != null){
            BeanUtils.copyProperties(source, target, ignoreProperties);
        }
        else{
            BeanUtils.copyProperties(source, target);
        }
        
    }
    
    public static Map<String, Object> toMap(Object bean) {
        if (bean == null) {
            return null;
        }

        Map<String, Object> result = new HashMap<String, Object>();
        Class<?> clazz = bean.getClass();
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            if (field.isAccessible()) {
                continue;
            }
            String fieldName = field.getName();
            if (fieldName.equals("serialVersionUID")) {
                continue;
            }
            StringBuffer methodName = new StringBuffer();
            if (field.getType().getName().equals("boolean")) {
                methodName.append("is");
            } else {
                methodName.append("get");
            }

            methodName.append(ECSStringUtils.upperFirstChar(fieldName));
            try {
                Method method = clazz.getDeclaredMethod(methodName.toString());
                Object value = method.invoke(bean);

                result.put(fieldName, value);
            } catch (NoSuchMethodException e) {
                logger.warn("toMap(" + bean.getClass().getName()
                        + "):NoSuchMethodException-->" + e.getMessage());
                return null;
            } catch (SecurityException e) {
                logger.warn("toMap(" + bean.getClass().getName()
                        + "):SecurityException-->" + e.getMessage());
                return null;
            } catch (IllegalAccessException e) {
                logger.warn("toMap(" + bean.getClass().getName()
                        + "):IllegalAccessException-->" + e.getMessage());
                return null;
            } catch (IllegalArgumentException e) {
                logger.warn("toMap(" + bean.getClass().getName()
                        + "):IllegalArgumentException-->" + e.getMessage());
                return null;
            } catch (InvocationTargetException e) {
                logger.warn("toMap(" + bean.getClass().getName()
                        + "):InvocationTargetException-->" + e.getMessage());
                return null;
            }
        }

        return result;
    }

    public static String toString(Object bean, String[] ignoreFields) {
        if (bean == null) {
            return "";
        }

        StringBuffer result = new StringBuffer();
        result.append("[");

        Map<String, Object> beanMap = toMap(bean);
        boolean find = false;
        if (beanMap != null) {
            for (Map.Entry<String, Object> entry : beanMap.entrySet()) {
                find = false;
                if (ignoreFields != null && ignoreFields.length > 0) {
                    for (String field : ignoreFields) {
                        if (field.equals(entry.getKey())) {
                            find = true;
                            break;
                        }
                    }
                }
                
                if (!find) {
                    result.append((String) entry.getKey()).append("=")
                        .append(entry.getValue()).append(",");
                }
            }
        }

        result.append("]");
        return result.toString();
    }

    public static String toString(Object bean) {
        return toString(bean, null);
    }
}
 

猜你喜欢

转载自my.oschina.net/u/2277088/blog/1621161