新建一个类,将类里的属性逐个解析出来,用一个json对象封装每一个属性的描述,展示上下级关系;
maven依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.1.19</version>
</dependency>
PropertyTypesUtil
package com.xx.util;
import cn.hutool.core.util.ReflectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Objects;
public class PropertyTypesUtil {
// 白名单包名
final static String PKG_NAME = "com.xx";
/**
* 解析类的参数列表
*
* @param clazz
* @return
*/
public static JSONArray parsePropertyTypes(Class clazz) {
JSONArray allInput = new JSONArray();
getPropertyTypes(clazz, allInput, null, null);
return allInput;
}
/**
* 递归获取类的属性类型
*/
public static void getPropertyTypes(Class<?> clazz, JSONArray allInput, String parentFieldName, String parentPath) {
// 获取当前类的所有属性
Field[] fields = ReflectUtil.getFields(clazz);
for (Field field : fields) {
// 获取属性名称和类型
String propertyName = field.getName();
Class<?> propertyType = field.getType();
String path = (Objects.isNull(parentPath) ? "" : parentPath + ".") + propertyName;
if (!Objects.equals("serialVersionUID", propertyName)) {
// 存储属性类型信息
JSONObject obj = new JSONObject();
obj.put("code", propertyName);
obj.put("parentCode", parentFieldName);
obj.put("type", propertyType.getName().startsWith(PKG_NAME) ? "object" : propertyType.getSimpleName());
obj.put("path", path);
allInput.add(obj);
}
// 如果是集合,还需要把泛型解析出来
if (List.class.isAssignableFrom(propertyType)) {
Type genericType = field.getGenericType();
if (genericType instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) genericType;
Type[] actualTypeArguments = pt.getActualTypeArguments();
if (actualTypeArguments.length > 0) {
Type actualTypeArgument = actualTypeArguments[0];
try {
getPropertyTypes(Class.forName(actualTypeArguments[0].getTypeName()), allInput, propertyName, path + "[]");
} catch (Exception e) {
//log.error("解析:{}异常", actualTypeArgument.toString());
}
}
}
}
// 如果属性类型是一个类(而不是基本类型),则递归获取这个类的属性类型
if (!propertyType.isPrimitive() && propertyType.getName().startsWith(PKG_NAME)) {
getPropertyTypes(propertyType, allInput, propertyName, path);
}
}
// 获取父类的属性类型
if (clazz.getSuperclass() != null && !clazz.getSuperclass().equals(Object.class)) {
getPropertyTypes(clazz.getSuperclass(), allInput, null, null);
}
}
}
新增两个类
Pojo
package com.xx.util;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Pojo {
private String name;
private String address;
private SubPojo subPojo;
}
SubPojo
package com.xx.util;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class SubPojo {
private String email;
private String qq;
}
测试示例
@Test
public void runTest() {
JSONArray objects = PropertyTypesUtil.parsePropertyTypes(Pojo.class);
System.out.println(objects);
}
测试结果
[
{
"path": "name",
"code": "name",
"type": "String"
},
{
"path": "address",
"code": "address",
"type": "String"
},
{
"path": "subPojo",
"code": "subPojo",
"type": "object"
},
{
"path": "subPojo.email",
"code": "email",
"parentCode": "subPojo",
"type": "String"
},
{
"path": "subPojo.qq",
"code": "qq",
"parentCode": "subPojo",
"type": "String"
}
]
over~~