代码;生成树形结构 工具类及其用法

package com.sdyy.asset.dataeye.common.util;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.cglib.beans.BeanGenerator;
import org.springframework.cglib.beans.BeanMap;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TreeFunction {
    private static volatile TreeFunction instance = null;
    private static TreeFunction getInstance(){
        if (instance == null) {
            synchronized (TreeFunction.class) {
                if (instance == null) {
                    instance = new TreeFunction();
                }
            }
        }
        return instance;
    }

    /**
     * 将列表转换为树
     * @param source 要转换的集合列表
     * @param rootNodeValue 根节点的parentId的值
     * @param idName 主键字段名
     * @param parentIdName 父节点字段名
     * @return
     */
    public static TreeNode transferToTree(List source,String rootNodeValue, String idName,String parentIdName){
       return getInstance().innerRecursive(source,rootNodeValue,idName,parentIdName);
    }

    private TreeNode innerRecursive(List obj,String rootNodeName, String idName, String parentIdName) {
        if(obj != null){
            TreeNode result = new TreeNode();
            coreRecursive(result,obj,rootNodeName,idName,parentIdName);
            return result;
        }
        return null;
    }

    private void coreRecursive(TreeNode result, List obj, Object parentId, String idName, String parentIdName){
        Iterator iterator = obj.iterator();
        while(iterator.hasNext()){
            Object current = iterator.next();
            Object currentId = getValue(current,idName);
            Object current_parentId = getValue(current,parentIdName);
            boolean isChildren = compare(current_parentId,parentId);
            if(isChildren){
                TreeNode newCurrent = TreeNode.transfer(current,currentId);
                result.addChildren(newCurrent);
                coreRecursive(newCurrent,obj,currentId,idName,parentIdName);
            }
        }
    }

    private boolean compare(Object a,Object b){
        //TODO 修改为parentId
        if(a !=null && b != null && a.equals(b)){
            return true;
        }else{
            return false;
        }
    }

    private Object getValue(Object current,String idName) {
        try {
            Method getIdMethod = current.getClass().getMethod("get"+idName.substring(0,1).toUpperCase()+idName.substring(1),null);
            Object id = getIdMethod.invoke(current,null);
            return id;
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static class TreeNode{
        private List children;
        public TreeNode(){
        }

        public List getChildren() {
            return children;
        }

        public void setChildren(List children) {
            this.children = children;
        }

        public void addChildren(TreeNode node){
            if(this.children == null){
                this.children = new ArrayList<>();
            }
            this.children.add(node);
        }

        public static TreeNode transfer(Object node,Object id) {
            //将node的属性类型赋予treenode
            BeanGenerator generator = new BeanGenerator();
            generator.setSuperclass(TreeNode.class);
            Field[] fields = node.getClass().getDeclaredFields();
            // 增加对继承实体的支持
            if(!node.getClass().getGenericSuperclass().toString().equals("Object")){
                Field[] parentFiled = node.getClass().getSuperclass().getDeclaredFields();
                fields = ArrayUtils.addAll(fields,parentFiled);
            }
            node.getClass().getSuperclass().getDeclaredFields();
            for (Field field : fields) {
                //System.out.println(field.getName());//$cglib_prop_username
                //System.out.println(field.getType());//class java.lang.String
                generator.addProperty(field.getName(),field.getType());
            }
            Object newNode = generator.create();
            BeanMap beanMap = BeanMap.create(newNode);
            for (Field field : fields) {
                field.setAccessible(true);
                try {
                    beanMap.put(field.getName(),field.get(node));
                } catch (IllegalAccessException e) {
                    //TODO
                    e.printStackTrace();
                }
            }

            return ((TreeNode)newNode);
        }
    }
}


用法
Controller

@ApiOperation("2020 查询代码集树")
@RequestMapping(value = {"/codelist.json"}, method = {RequestMethod.POST},
        produces = {"application/json"})
public Response codelist() {
    List codelist = dataelementService.codelist(new ArrayList());
    TreeFunction.TreeNode rootNode = TreeFunction.transferToTree(codelist,"-1","flowId","parentId");
    return Response.success(rootNode.getChildren());
}

你可能感兴趣的:(springboot)