当前位置:网站首页>Using reflection to build a menu spanning tree
Using reflection to build a menu spanning tree
2022-07-28 14:15:00 【Huzz's childhood paper plane】
utilize java Dynamic calling class and class attribute mechanism of , Write a tool class to automatically create a menu tree for us , So that we don't have to write some logic repeatedly every time we encounter the tree structure , Very unfriendly .
/**
* Build a tree structure
*
* @author Huzz
* @create 2022-07-25 18:36
*/
public class TreeBuilder<T> {
private static final String SET_METHOD_PREFIX = "set";
private static final String GET_METHOD_PREFIX = "get";
private static final String DEFAULT_ID_NAME = "id";
private static final String DEFAULT_PARENT_ID_NAME = "parentId";
private static final String DEFAULT_CHILDREN_NAME = "children";
/**
* Building a tree structure
* @param rootId The root node id
* @param dataList All node data , contain id| Parent id| children Member attribute .
* @return
* @throws Exception
*/
public static List builder(String rootId, List dataList) throws Exception {
List tree = new ArrayList<>();
for (Object data : dataList) {
String parentId = data.getClass().getMethod(generateGetMethodName(DEFAULT_PARENT_ID_NAME)).invoke(data).toString();
if (rootId.equals(parentId)) {
// If it is the root node , Recursively find this node id As parentId The children of
List children = builder(data.getClass().getMethod(generateGetMethodName(DEFAULT_ID_NAME)).invoke(data).toString(), dataList);
// Set up your child
data.getClass().getMethod(generateSetMethodName(DEFAULT_CHILDREN_NAME), List.class).invoke(data, children);
// Save the current node
tree.add(data);
}
}
return tree;
}
/**
* Building a tree structure
* @param rootId The root node id
* @param dataList All node data , contain id| Parent id| children Member attribute .
* @param IDFiledName id Field name
* @param parentIdFiledName Parent ID Field name
* @param childrenFiledName Child field name
* @return
* @throws Exception
*/
public static List builder(String rootId, List dataList,String IDFiledName, String parentIdFiledName, String childrenFiledName) throws Exception {
TreeBuilder treeBuilder = new TreeBuilder<>();
return treeBuilder.getTree(rootId, dataList, IDFiledName, parentIdFiledName, childrenFiledName);
}
/**
* Get tree
* @param rootId The root node id
* @param dataList All node data , contain id| Parent id| children Member attribute .
* @return
* @throws Exception
*/
public List<T> getTree(String rootId, List<T> dataList) throws Exception {
return builder(rootId, dataList);
}
/**
* Get tree
* @param rootId The root node id
* @param dataList All node data , contain id| Parent id| children Member attribute .
* @param IDFiledName id Field name
* @param parentIdFiledName Parent ID Field name
* @param childrenFiledName Child field name
* @return
* @throws Exception
*/
public List<T> getTree(String rootId, List<T> dataList, String IDFiledName, String parentIdFiledName, String childrenFiledName) throws Exception {
List<T> tree = new ArrayList<>();
for (T data : dataList) {
String parentId = data.getClass().getMethod(generateGetMethodName(parentIdFiledName)).invoke(data).toString();
if (rootId.equals(parentId)) {
// If it is the root node , Find this node id As parentId The children of
List<T> children = getTree(data.getClass().getMethod(generateGetMethodName(IDFiledName)).invoke(data).toString(), dataList, IDFiledName, parentIdFiledName, childrenFiledName);
// Set up your child
data.getClass().getMethod(generateSetMethodName(childrenFiledName), List.class).invoke(data, children);
// Save the current node
tree.add(data);
}
}
return tree;
}
private static String generateGetMethodName(String filedName){
return GET_METHOD_PREFIX +filedName.substring(0, 1).toUpperCase() + filedName.substring(1);
}
private static String generateSetMethodName(String filedName){
return SET_METHOD_PREFIX +filedName.substring(0, 1).toUpperCase() + filedName.substring(1);
}
}test :
class Test{
public static void main(String[] args) throws Exception {
// Create an entity class
@Data
@AllArgsConstructor
class Node {
private String id;
private String data;
private String parentId;
private List<Node> children;
public Node(String id, String data, String parentId) {
this.id = id;
this.data = data;
this.parentId = parentId;
}
}
// Simulate a database Node surface
List<Node> nodeList = new ArrayList<>();
nodeList.add(new Node("1", " User management ", "0"));
nodeList.add(new Node("2", " Role management ", "0"));
nodeList.add(new Node("3", " Menu management ", "0"));
nodeList.add(new Node("4", " Work order management ", "0"));
nodeList.add(new Node("5", " Login management ", "0"));
nodeList.add(new Node("6", " Log management ", "0"));
nodeList.add(new Node("7", " The user to create ", "1"));
nodeList.add(new Node("8", " User role configuration ", "1"));
nodeList.add(new Node("9", " User disabled ", "1"));
nodeList.add(new Node("10", " Character creation ", "2"));
nodeList.add(new Node("11", " Character delete ", "2"));
nodeList.add(new Node("12", " Role disabled ", "2"));
nodeList.add(new Node("13", " Menu delete ", "3"));
nodeList.add(new Node("14", " Menu editing ", "3"));
nodeList.add(new Node("15", " Menu disabled ", "3"));
nodeList.add(new Node("16", " Create job ", "4"));
nodeList.add(new Node("17", " Modify work order ", "4"));
nodeList.add(new Node("18", " Login configuration ", "5"));
nodeList.add(new Node("19", " Login verification ", "5"));
nodeList.add(new Node("20", " system log ", "6"));
nodeList.add(new Node("21", " Log in ", "6"));
nodeList.add(new Node("22", " Common creation ", "7"));
nodeList.add(new Node("23", " Special creation ", "7"));
nodeList.add(new Node("24", " To configure A", "8"));
nodeList.add(new Node("25", " To configure B", "8"));
nodeList.add(new Node("26", " Regular log ", "20"));
nodeList.add(new Node("27", " Abnormal log ", "20"));
// ==== Call statically ====
List<Node> tree = TreeBuilder.builder("0", nodeList);
// Declare field name , If your entity class is different from the default field name , Declare it from the parameter
List<Node> tree0 = TreeBuilder.builder("0", nodeList, "id", "parentId", "children");
// ==== Create object mode call ====
TreeBuilder<Node> builder = new TreeBuilder<>();
List<Node> tree1 = builder.getTree("0", nodeList);
// Declare field name , If your entity class is different from the default field name , Declare it from the parameter
List<Node> tree2 = builder.getTree("0", nodeList, "id", "parentId", "children");
return;
}
}边栏推荐
- 【Utils】ServletUtil
- DXF reading and writing: Chinese description of dimension style group codes
- Entering the world of audio and video -- flv video packaging format
- Jmeter安装教程及登录增加token
- 了解虚拟列表背后原理,轻松实现虚拟列表
- 第六章 支持向量机
- 【翻译】如何为你的私有云选择一个网络网关
- How to write test cases in software testing technology
- 83.(cesium之家)cesium示例如何运行
- a标签_文件下载(download属性)
猜你喜欢

多线程与高并发(三)—— 源码解析 AQS 原理

深度学习基础----GNN谱域和空域 (不断完善更新积累)

Jmeter安装教程及登录增加token

RSA用私钥加密数据公钥解密数据(不是签名验证过程)

用友BIP CRM新品发布,赋能大中型企业营销增长

MySQL开发技巧——视图

RSA encrypts data with private key and decrypts data with public key (not a signature verification process)

一文读懂如何部署具有外部数据库的高可用 K3s

Socket class understanding and learning about TCP character stream programming

LeetCode 105.从前序与中序遍历序列构造二叉树 && 106.从中序与后序遍历序列构造二叉树
随机推荐
VOS3000如何呼入送到OKCC
每日一题——奖学金
Security assurance is based on software life cycle - networkpolicy application
Vite configuring path aliases in the project
Postgresql14安装及主从配置
Duplicate data in leetcode (442) array
LeetCode 105.从前序与中序遍历序列构造二叉树 && 106.从中序与后序遍历序列构造二叉树
Intersectionobserver
Alibaba, jd.com, Tiktok: push cloud to the heart of industry
Security assurance is based on software life cycle -istio authentication mechanism
LeetCode 1331.数组序号转换
leetcode(442)数组中重复的数据
【服务器数据恢复】HP StorageWorks系列服务器RAID5两块盘离线的数据恢复
【LVGL事件(Events)】事件代码
MySQL development skills - View
Implementation of StrCmp, strstr, memcpy, memmove
Four ways to create thread pools
webSocket聊天
Postgresql14 installation and master-slave configuration
【飞控开发基础教程7】疯壳·开源编队无人机-SPI(气压计数据获取)