当前位置:网站首页>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;
}
}边栏推荐
猜你喜欢
随机推荐
Power amplifier and matching network learning
url相关知识点
Poj3259 wormhole solution
Operator3 - design an operator
DXF reading and writing: align the calculation of the position of the dimension text in the middle and above
IP black and white list
QT自制软键盘 最完美、最简单、跟自带虚拟键盘一样
正则表达式
【Utils】JsonUtil
TS扫盲大法-基础篇
牛客多校-Link with Level Edito I-(线性dp)
webSocket聊天
Four ways to create thread pools
Clickhouse架构与设计
Security assurance is based on software life cycle -istio authorization mechanism
RSA用私钥加密数据公钥解密数据(不是签名验证过程)
MySQL development skills - View
How does vos3000 send incoming calls to okcc
Machine learning (Zhou Zhihua) Chapter 6 notes on Support Vector Learning
Verification code brute force cracking test [easy to understand]








