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

}

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值