java通用菜单树结构(反射与泛型实现)

java通用菜单树结构

反射是java框架的灵魂,在编写管理页面是,各种功能树,不胜其烦,所以写了个工具类。

首先创建两个类备用(角色,菜单)

角色类

package com.sean.seandemo0621.entity;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Data
public class Role implements Serializable {

    public Role(String id,String pid,String name){
        this.id = id;
        this.pid = pid;
        this.name = name;
    }

    private String id;

    private String pid;

    private String name;

    private List<Role> sonList;

}

菜单类

package com.sean.seandemo0621.entity;

import lombok.Data;

import java.io.Serializable;
import java.util.List;
@Data
public class Menu implements Serializable {
    public Menu(Integer id,Integer pid,String name,String url){
        this.id = id;
        this.pid = pid;
        this.name = name;
        this.url = url;
    }

    private Integer id;

    private Integer pid;

    private String name;

    private String url;

    private List<Menu> sonList;
}

工具类Demo

package com.sean.seandemo0621.reflection;

import com.sean.seandemo0621.entity.Menu;
import com.sean.seandemo0621.entity.Role;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class Demo<E> {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        showMenu();
        showRole();
    }

    private static void showMenu() throws NoSuchFieldException, IllegalAccessException {
        List<Menu> menus = new ArrayList<>();
        menus.add(new Menu(1,0,"1父","1.j"));
        menus.add(new Menu(2,0,"2父","1.j"));
        menus.add(new Menu(3,0,"3父","1.j"));
        menus.add(new Menu(21,2,"2一子","1.j"));
        menus.add(new Menu(22,2,"22子","1.j"));
        menus.add(new Menu(23,2,"23子","1.j"));
        menus.add(new Menu(231,23,"231子","1.j"));
        menus.add(new Menu(232,23,"232子","1.j"));
        menus.add(new Menu(233,23,"233子","1.j"));
        menus.add(new Menu(31,3,"31","1.j"));
        menus.add(new Menu(32,3,"32","1.j"));
        System.out.println(menus);
        Demo demo = new Demo();
        List<Menu> parentSon = demo.createParentSonByReflection2(menus,0);
        System.out.println(parentSon);

    }

    private static void showRole() throws NoSuchFieldException, IllegalAccessException {
        List<Role> roles = new ArrayList<>();

        roles.add(new Role("1","0","张父"));
        roles.add(new Role("2","0","李父"));
        roles.add(new Role("3","0","王父"));

        roles.add(new Role("11","1","张一"));
        roles.add(new Role("12","1","张三"));
        roles.add(new Role("13","1","张二"));

        roles.add(new Role("111","11","张一一子"));
        roles.add(new Role("112","11","张一二子"));
        roles.add(new Role("113","11","张一三子"));

        System.out.println(roles);
        Demo demo = new Demo();
        List<Menu> parentSon = demo.createParentSonByReflection2(roles,"0");
        System.out.println(parentSon);
    }


    public <T> List<T> createParentSonByReflection2(List<T> t,E pid) throws NoSuchFieldException, IllegalAccessException {
        List<T> roleList = new ArrayList<>();
        for (int i = 0; i < t.size(); i++) {
            T t1 = t.get(i);
            Class<?> aClass = t1.getClass();
            Field id = aClass.getDeclaredField("id");
            Class<?> type = id.getType();
            id.setAccessible(true);
            if(type.getTypeName().equals("java.lang.String")){
                String id1 = id.get(t1).toString();
                Field parentId = aClass.getDeclaredField("pid");
                parentId.setAccessible(true);
                String parentId1 = parentId.get(t1).toString();
                if(parentId1.equals(pid)){
                    Field roleList1 = aClass.getDeclaredField("sonList");
                    roleList1.setAccessible(true);
                    roleList1.set(t1,createParentSonByReflection2(t,(E)id1));
                    roleList.add(t1);
                }
            }else{
                Integer id1 = (Integer) id.get(t1);
                Field parentId = aClass.getDeclaredField("pid");
                parentId.setAccessible(true);
                Integer parentId1 = (Integer) parentId.get(t1);
                if(parentId1 == pid){
                    Field roleList1 = aClass.getDeclaredField("sonList");
                    roleList1.setAccessible(true);
                    roleList1.set(t1,createParentSonByReflection2(t,(E)id1));
                    roleList.add(t1);
                }
            }
        }
        return roleList;
    }
}

Tips
使用时将自己的属性名称对应起来即可,这里使用的是idpidsonList 分别表示 id,父级id以及子级集合,其余属性根据业务添加即可,只要保证这三个属性名称即可。

注意
本Demo目前id的类型只支持IntegerString.

你可能感兴趣的:(反射,菜单树,递归)