Java反射應用之樹形轉換為一層list數(shù)據(jù)結構

平時工作中都會遇到包含層級機構的數(shù)據(jù)轉換成樹形結構,但是反過來也是很常見的,就是存在層級關系的數(shù)據(jù)已經(jīng)是樹形結構了,需要我們處理成一個普通的一層的list數(shù)據(jù)結構,下面就是我在工作中實際遇到的情況,我獲取到的是樹形結構的數(shù)據(jù)集合,需要轉換成普通的list,本文也是通過反射來完成的,下面是詳細的代碼,和大家一起分享學習!

public final static String Children = "children";

    public static <T> List<T> toList(List<T> trees) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        List<T> datalist = new ArrayList<T>();
        if (CollectionUtils.isEmpty(trees))
            return null;
        T t = trees.get(0);
        Class<? extends Object> clazz = t.getClass();
        return toList(clazz, trees);

    }

    @SuppressWarnings("unchecked")
    private static <T> List<T> toList(Class<? extends Object> clazz, List<T> trees) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        List<T> datalist = new ArrayList<T>();
        if (CollectionUtils.isEmpty(trees))
            return null;
        for (T t : trees) {

            // 設置直接點
            List<T> childTree = (List<T>) ClassReflectUtils.getfieldValue(clazz, t, Children);

            if (!CollectionUtils.isEmpty(childTree)) {
                List<T> childList = toList(clazz, childTree);
                if (!CollectionUtils.isEmpty(childList)) {
                    datalist.addAll(childList);
                }
            }

            // ClassReflectUtils.setValue(clazz, t, Children, new ArrayList<clazz>());
            datalist.add(t);
        }
        return datalist;
    }

創(chuàng)建一個工具類,然后將兩個方法粘貼進去即可,然后寫一個測試類,造幾條測試數(shù)據(jù)測試即可!

ClassReflectUtils類

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class ClassReflectUtils {
    private final static Logger log = LoggerFactory.getLogger(ClassReflectUtils.class);

    /**
     * 獲取類中所有字段集合
     * @param clazz 類
     * @return
     */
    public static Map<String, Field> getFieldsMap(Class<? extends Object> clazz) {
        Map<String, Field> fieldMap = new HashMap<String, Field>();
        Field[] fieldArr = clazz.getDeclaredFields();
        for (Field field : fieldArr) {
            fieldMap.put(field.getName(), field);
        }
        Class<?> superClazz = clazz.getSuperclass();
        if (null != superClazz) {
            Map<String, Field> superFieldMap = getFieldsMap(superClazz);
            fieldMap.putAll(superFieldMap);
        }
        return fieldMap;
    }

    /**
     * 獲取對象中對應字段的set方法
     * @param clazz 類
     * @param fieldName 字段名稱
     * @param fieldClass 字段類
     * @return
     * @throws NoSuchFieldException
     * @throws NoSuchMethodException
     */
    public static Method getDeclaredSetMethod(Class<? extends Object> clazz, String fieldName, Class<?>... fieldClass) throws NoSuchFieldException, NoSuchMethodException {

        if(hasField(clazz,fieldName)){
            String nkey = String.valueOf(fieldName.charAt(0)).toUpperCase() + ((fieldName.length() == 1) ? "" : fieldName.substring(1));
            return clazz.getDeclaredMethod("set" + nkey, fieldClass);
        }else{
            if(null != clazz.getSuperclass()){
                return getDeclaredSetMethod(clazz.getSuperclass(),fieldName);
            }else{
                throw new NoSuchFieldException(clazz.getName()+"對象中無"+ fieldName+"屬性");
            }

        }


    }

    /**
     * 為對象的對應字段賦值
     * @param clazz  對象
     * @param instance 實體
     * @param fieldName 字段名稱
     * @param fieldValue 字段值
     * @param fieldType 字段類型
     * @throws Exception
     */
    public static void setValue(Class<? extends Object> clazz, Object instance, String fieldName, Object fieldValue, Class<?> fieldType) throws Exception {
        // logger.info("{}設置{}的值{},值類型:{}", clazz.getName(), fieldName, fieldValue.toString(), fieldType.getName());
        Method setChildMethod = ClassReflectUtils.getDeclaredSetMethod(clazz, fieldName, fieldType);
        setChildMethod.invoke(instance, fieldValue);
    }
    /***
     * 獲取對應字段的get方法
     * @param clazz 對象
     * @param fieldName 字段名稱
     * @return
     * @throws NoSuchMethodException
     */
    public static Method getDeclaredGetMethod(Class<? extends Object> clazz, String fieldName) throws NoSuchFieldException, NoSuchMethodException {
        if(hasField(clazz,fieldName)){
            String nkey = String.valueOf(fieldName.charAt(0)).toUpperCase() + ((fieldName.length() == 1) ? "" : fieldName.substring(1));
            Method getKeyMethod = clazz.getDeclaredMethod("get" + nkey);
            return getKeyMethod;
        }else{
            if(null != clazz.getSuperclass()){
                return getDeclaredGetMethod(clazz.getSuperclass(),fieldName);
            }else{
                throw new NoSuchFieldException(clazz.getName()+"對象中無"+ fieldName+"屬性");
            }

        }
    }

    /**
     * 判斷對象中是否存在對應的字段
     * @param clazz
     * @param key
     * @return
     */
    private static boolean hasField(Class<?> clazz, String key) {
        if(null == clazz){
            return false;
        }
    //  logger.info(clazz.getName());
        Field[] fieldArr = clazz.getDeclaredFields();
        for (Field field : fieldArr) {
            if(StringUtils.equals(field.getName(),key))
                return true;
        }
        return false;
    }

    /**
     * 獲取對象中對應字段的值
     * @param clazz 對象類型
     * @param instance 實體
     * @param fieldName 字段名稱
     * @param <T>
     * @return
     * @throws NoSuchFieldException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static <T> Object getfieldValue(Class<?> clazz, T instance, String fieldName) throws NoSuchFieldException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        // logger.info("{}獲取{}的get方法", clazz.getName(), fieldName);
        Object fieldKeyValue = null;
        Method getKeyMethod = ClassReflectUtils.getDeclaredGetMethod(clazz, fieldName);
        if (null != instance) {
            fieldKeyValue = getKeyMethod.invoke(instance);
        }
        return fieldKeyValue;

    }
    /**
     * 獲取對象中對應字段的值,并轉換成String類型返回
     * @param clazz 對象類型
     * @param instance 實體
     * @param fieldName 字段名稱
     * @param <T>
     * @return
     * @throws NoSuchFieldException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static <T> String getfieldStringValue(Class<?> clazz, T instance, String fieldName) {
        /// logger.info("{}獲取{}的值", clazz.getName(), fieldName);
        String fieldStringValue = null;
        try {
            Method method = ClassReflectUtils.getDeclaredGetMethod(clazz, fieldName);
            Object fieldValue = method.invoke(instance);
            Type type = method.getGenericReturnType();
            if (type.equals(Date.class)) {
                fieldStringValue = DateUtils.formatDate((Date) fieldValue, DateUtils.TIME_PATTERN);
            } else  {
                fieldStringValue = TransUtils.transString(fieldValue);
            }
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            // TODO Auto-generated catch block
            log.error("系統(tǒng)異常:",e);
        } catch (Exception e) {
            log.error("系統(tǒng)異常:",e);
        }
        return fieldStringValue;

    }
}
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容