jar
jinlin
2025-03-04 23f02e6b45dd7cf0ab2e7827144913ca59575ea4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.example.server.utils;
 
import org.apache.commons.lang3.StringUtils;
 
import java.lang.reflect.Method;
import java.util.List;
 
/**
 * 树形结构工具类,如:菜单、部门等
 *
 * @author hehz
 * @since 1.0.0
 */
public class TreeFieldUtils {
 
    /**
     * 根据pid,构建树节点
     */
    public static void build(List<?> list, String[] treeFields) {
        String[] oldTreeFields = new String[treeFields.length];
        String[] newTreeFields = new String[treeFields.length];
        Method[] methods = new Method[treeFields.length];
        for (int i = 0; i < list.size(); i++) {
            Object object = list.get(i);
            Class c1azz = object.getClass();
            for (int j = 0; j < treeFields.length; j++) {
                Method method = null;
                try {
                    String fieldName = treeFields[j];
                    if (methods[j] == null) {
                        methods[j] = c1azz.getMethod("get" + StringUtils.capitalize(fieldName));
                    }
                    Object value = methods[j].invoke(object);
                    String valueStr = value == null ? "" : value.toString();
                    newTreeFields[j] = valueStr;
                    Boolean isSetBlank = true;
                    for (int k=0;k<=j;k++){
                        if (!newTreeFields[k].equals(oldTreeFields[k])){
                            isSetBlank = false;
                            break;
                        }
                    }
                    if (isSetBlank){
                        method = c1azz.getMethod("set" + StringUtils.capitalize(fieldName), String.class);
                        method.invoke(object, "");
                    }
                    oldTreeFields[j] = valueStr;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}