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();
|
}
|
}
|
}
|
}
|
}
|