1. Data model

package com.example.demo;

import java.util.List;

/**
 * Description:
 *
 * @author jack
 * @date 2021/7/13 5:15 下午
 */
public class DocVO {

    private List<FieldVO> fieldVOList;

    public DocVO(List<FieldVO> fieldVOList) {
        this.fieldVOList = fieldVOList;
    }

    public static class FieldVO {
        /** * Attribute name */
        private String fieldName;

        /** * Attribute type */
        private String fieldType;

        /** * attribute comments */
        private String describe;

        public FieldVO(a) {}public FieldVO(String fieldName, String fieldType, String describe) {
            this.fieldName = fieldName;
            this.fieldType = fieldType;
            this.describe = describe;
        }

        public String getFieldName(a) {
            return fieldName;
        }

        public void setFieldName(String fieldName) {
            this.fieldName = fieldName;
        }

        public String getFieldType(a) {
            return fieldType;
        }

        public void setFieldType(String fieldType) {
            this.fieldType = fieldType;
        }

        public String getDescribe(a) {
            return describe;
        }

        public void setDescribe(String describe) {
            this.describe = describe;
        }

        @Override
        public String toString(a) {
            return "FieldVO{" +
                    "fieldName='" + fieldName + '\' ' +
                    ", fieldType='" + fieldType + '\' ' +
                    ", describe='" + describe + '\' ' +
                    '} '; }}public List<FieldVO> getFieldVOList(a) {
        return fieldVOList;
    }

    public void setFieldVOList(List<FieldVO> fieldVOList) {
        this.fieldVOList = fieldVOList; }}Copy the code

2. The tools

package com.example.demo;

import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.FieldDoc;
import com.sun.javadoc.RootDoc;
import com.sun.tools.javadoc.Main;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 *
 * @author jack
 * @date 2021/7/13 5:10 下午
 */
public class DocUtil {

    /** * will automatically inject */
    private static RootDoc rootDoc;

    /** * will automatically call this method **@param root root
     * @return true
     */
    public static boolean start(RootDoc root) {
        rootDoc = root;
        return true;
    }

    /** * generate document **@paramBeanFilePath Note that this is the absolute path to the. Java file *@returnDocument comments */
    public static DocVO execute(String beanFilePath) {
        Main.execute(new String[]{"-doclet", DocUtil.class.getName(), "-docletpath",
                DocUtil.class.getResource("/").getPath(), "-encoding"."utf-8", beanFilePath});

        ClassDoc[] classes = rootDoc.classes();

        if (classes == null || classes.length == 0) {
            return null;
        }
        ClassDoc classDoc = classes[0];
        // Get the attribute name and comment
        FieldDoc[] fields = classDoc.fields(false);

        List<DocVO.FieldVO> fieldVOList = new ArrayList<>(fields.length);

        for (FieldDoc field : fields) {
            fieldVOList.add(new DocVO.FieldVO(field.name(), field.type().typeName(), field.commentText()));
        }
        return newDocVO(fieldVOList); }}Copy the code

3. The test

package com.example.demo;

import java.util.Objects;

/**
 * Description:
 *
 * @author jack
 * @date 2021/7/13 4:11 下午
 */
public class ClassTest {
    public static void main(String[] args) {
        String beanFilePath = "/Documents/demo/src/main/java/com/example/demo/dto/MailDTO.java";
        DocVO docVO = DocUtil.execute(beanFilePath);
        if(Objects.nonNull(docVO) && Objects.nonNull(docVO.getFieldVOList())){ docVO.getFieldVOList().forEach(System.out::println); }}}Copy the code