1. 程式人生 > >JavaBean物件轉換工具類

JavaBean物件轉換工具類

package com.example.util;

import com.alibaba.fastjson.JSON;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2018/11/16 11:06
 * @description V1.0 javabean 物件轉換工具類
 */
public class EntityUtils {

    /**
     * 陣列集合轉化為指定物件集合
     * 指定的實體物件必須包含所以欄位的構造方法,陣列的元素的順序將和構造方法順序和型別一一對應
     * @param list  集合
     * @param clazz c
     * @param <T>   型別
     * @return List<T>
     * @description 用於jpa查詢自定義vo用的
     */
    public static <T> List<T> castEntity(List<Object[]> list, Class<T> clazz) {
        List<T> returnList = new ArrayList<>();
        if (list.size() == 0) {
            return returnList;
        }
        Class[] c2 = null;
        Constructor[] constructors = clazz.getConstructors();
        for (Constructor constructor : constructors) {
            Class[] tClass = constructor.getParameterTypes();
            if (tClass.length == list.get(0).length) {
                c2 = tClass;
                break;
            }
        }
        //構造方法例項化物件
        for (Object[] o : list) {
            Constructor<T> constructor = null;
            try {
                constructor = clazz.getConstructor(c2);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            try {
                assert constructor != null;
                returnList.add(constructor.newInstance(o));
            } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }

        return returnList;
    }



    /**
     * @author xiaobu
     * @date 2018/11/23 11:54
     * @param object 要強轉的物件 , entityClass 強轉後的型別
     * @return T
     * @descprition   物件型別強轉
     * @version 1.0
     */
    public static <T> T convertBean(Object object, Class<T> entityClass) {
        if(null == object) {
            return null;
        }
        return JSON.parseObject(JSON.toJSONString(object), entityClass);
    }




    /**
     * @author xiaobu
     * @date 2018/11/23 11:57
     * @param object 要轉話的物件
     * @return java.util.Map<?,?>
     * @descprition   物件轉為map
     * @version 1.0
     */
    public static Map<?, ?> objectToMap(Object object){
        return convertBean(object, Map.class);
    }



    /**
     * @author xiaobu
     * @date 2018/11/23 12:00
     * @param map map集合, t 物件
     * @return T
     * @descprition   map轉換物件
     * @version 1.0
     */
    public static <T> T mapToObject(Map<String, Object> map, Class<T> t) throws InstantiationException, IllegalAccessException, InvocationTargetException {
        T instance = t.newInstance();
        org.apache.commons.beanutils.BeanUtils.populate(instance, map);
        return instance;
    }





    /**
     * @author xiaobu
     * @date 2018/11/23 12:30
     * @param source 資源物件, target 目標物件, ignoreProperties 賦值new String[]{}
     * @return T  target物件
     * @descprition   物件轉換
     * @version 1.0
     */
    public static <T> T copy(Object source, Class<T> target, String...ignoreProperties){
        T targetInstance = null;
        try {
            targetInstance = target.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(ArrayUtils.isEmpty(ignoreProperties)) {
            assert targetInstance != null;
            BeanUtils.copyProperties(source, targetInstance);
        }else {
            assert targetInstance != null;
            BeanUtils.copyProperties(source, targetInstance, ignoreProperties);
        }
        return targetInstance;

    }



    /**
     * @author xiaobu
     * @date 2018/11/23 12:32
     * @param list, target, ignoreProperties]
     * @return java.util.List<T>
     * @descprition   物件list轉換
     * @version 1.0
     */
    public static <T, E> List<T> copyList(List<E> list, Class<T> target, String...ignoreProperties){
        List<T> targetList = new ArrayList<>();
        if(CollectionUtils.isEmpty(list)) {
            return targetList;
        }
        for(E e : list) {
            targetList.add(copy(e, target, ignoreProperties));
        }
        return targetList;
    }


}