本文主要是介绍【工具】将类里的属性解析成属性描述的JSONArray数组,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
新建一个类,将类里的属性逐个解析出来,用一个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;
}
测试示例
@Testpublic 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~~
这篇关于【工具】将类里的属性解析成属性描述的JSONArray数组的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!