Java获取实体类中属性名为key,属性注释为value

Java获取实体类中属性名为key,属性注释为value

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.jayden.entity.Role;
import com.sun.javadoc.AnnotationDesc;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.FieldDoc;
import com.sun.javadoc.RootDoc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
    public static Logger logger = LoggerFactory.getLogger(Test.class);

    private static RootDoc rootDoc;
    private String javaBeanFilePath;

    public static boolean start(RootDoc root) {
        rootDoc = root;
        return true;
    }
    public Test(){}
    public Test(String javaBeanFilePath) {
        this.javaBeanFilePath = javaBeanFilePath;
    }

    public String exec() {
        List<Map<String, String>> result = new ArrayList<>();
        com.sun.tools.javadoc.Main.execute(new String[]{"-doclet", Test.class.getName(), "-docletpath",
                Test.class.getResource("/").getPath(), "-encoding", "utf-8", javaBeanFilePath});
        ClassDoc[] classes = rootDoc.classes();

        if (classes == null || classes.length == 0) {
            logger.warn(javaBeanFilePath + " 无信息");
            return null;
        }
        ClassDoc classDoc = classes[0];
        // 获取类的名称
        //String className = classDoc.name();
        // 获取类的注释
        //String classComment = ReflectUtil.getFieldValue(classDoc, "documentation").toString();

        // 获取属性名称和注释
        FieldDoc[] fields = classDoc.fields(false);
        for (Field f : Role.class.getDeclaredFields())
        {
            Map<String, String> map = new HashMap<>();
            String fieldName = "";
            //获取属性上@JsonProperty注解
            JsonProperty annotation = f.getAnnotation(JsonProperty.class);
            // 判断是否有注解
            if (null == annotation)
            {
                fieldName = f.getName(); // 没有注解使用属性名
            } else {
                fieldName = annotation.value(); // 有注解使用注解的value值
            }
            // 获取注释
            for (FieldDoc field : fields) {
                // 如果属性名相同则将属性注释信息作为map的value值,有注解key就是注解的value值,否则就是属性名
                if (field.name().equals(f.getName())) {
                    map.put(fieldName, field.commentText());
                    break;
                }
            }
            result.add(map);
        }

        Map<String, Object> resMap = new HashMap<>();
        resMap.put("code", 0);
        resMap.put("msg", "success");
        resMap.put("data", result);

        return JSON.toJSONString(resMap);
    }
    
    public static void main(String[] args) {
        String path =new File("").getAbsolutePath();
        String className = "Role"; // 类名
        Test test = new Test(path + "\\src\\main\\java\\com\\jayden\\entity\\"+ className +".java");

        System.out.println(test.exec());
    }

}

感谢大家的阅读,请多多指教,如有帮助到大家,请留言告诉我哦~

你可能感兴趣的:(常见问题)