Dozer封装类

☆樱花仙子☆ 提交于 2020-03-16 11:03:40

POM:

<dependency>
    <groupId>net.sf.dozer</groupId>
    <artifactId>dozer</artifactId>
    <version>5.5.1</version>
</dependency>

 

JAVA:

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dozer.DozerBeanMapper;

import com.nike.gcsc.annotation.FieldName;

/**
 * dozer util class
 * Notice: if you wan't use this util,please import dozer artifact in you pom
 * @author roger yang
 * @date 6/13/2019
 */
public final class BeanMapper {
    private static final Set<String> IGNORE_FILEDS = new HashSet<>(1);
    
    static {
        IGNORE_FILEDS.add("serialVersionUID");
    }
    
    private BeanMapper() {}

    private static DozerBeanMapper dozer = new DozerBeanMapper();
    
    public static <T> T map(Object source, Class<T> destinationClass) {
        if (source == null) {
            return null;
        }
        return dozer.map(source, destinationClass);
    }

    public static <T> List<T> mapList(Collection<?> sourceList, Class<T> destinationClass) {
        if (sourceList == null) {
            return Collections.emptyList();
        }
        List<T> destinationList = new ArrayList<>();
        for (Object sourceObject : sourceList) {
            T destinationObject = dozer.map(sourceObject, destinationClass);
            destinationList.add(destinationObject);
        }
        return destinationList;
    }

    public static void copy(Object source, Object destinationObject) {
        if (source == null || destinationObject == null) {
            return;
        }
        dozer.map(source, destinationObject);
    }

    public static Map<String, Object> bean2Map(Object source) {
        Map<String, Object> result = new HashMap<>();
        try {
            Class<?> sourceClass = source.getClass();
            Field[] sourceFiled = sourceClass.getDeclaredFields();
            for (Field field : sourceFiled) {
                field.setAccessible(true);
                FieldName fieldName = field.getAnnotation(FieldName.class);
                if (fieldName == null) {
                    result.put(field.getName(), field.get(source));
                } else {
                    if (fieldName.Ignore()) continue;
                    result.put(fieldName.value(), field.get(source));
                }
            }
            return result;
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T map2Bean(Map<String, Object> source, Class<T> instance) {
        try {
            T object = instance.newInstance();
            Field[] fields = object.getClass().getDeclaredFields();
            Method[] methods = object.getClass().getMethods();
            for (Field field : fields) {
                Method method = getSetMethodName(field, methods);
                if(null == method) {
                    throw new RuntimeException("Filed:" + field.getName() + " no set method");
                }
                method.invoke(object, source.get(field.getName()));
            }
            return object;
        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    
    /**
     * get field set method
     * @param field
     * @param methods
     * @return
     */
    private static Method getSetMethodName(Field field, Method[] methods) {
        String fieldName = field.getName();
        String methodName = "set".concat(fieldName.substring(0, 1).toUpperCase()).concat(fieldName.substring(1));
        Method method = filterMethod(methods, methodName);
        return method;
    }
    
    /**
     * @param methods
     * @param methodName
     * @return
     */
    private static Method filterMethod(Method[] methods, String methodName) {
        if(methods!=null && methods.length>0) {
            for(Method method : methods) {
                if(method.getName().equals(methodName)) {
                    return method;
                }
            }
        }
        return null;
    }
}

 

 

其中反射的那块可以把Method和Field进行缓存,不用每次都去反射获取,可以参考:https://www.cnblogs.com/yangzhilong/p/8085019.html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!