利用javadocApi自定义读取注释生成内容

需求时帮别人写了几个接口,然后字段挺多的,我都加了注释,但是不能给人家看类啊,当时又没有用swagger,太懒了,所以就干脆根据注释生成一个markdown字段说明表吧,一番折腾,搞个Demo

package com.server.simple.markdown;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ClassUtil;
import com.sun.javadoc.*;
import pers.conan.mdcoffee.exception.DisablePutException;
import pers.conan.mdcoffee.markdown.*;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

public class CustomerDoclet extends Doclet {

    private static ClassDoc[] docClasses = null;
    private static RootDoc docRoot = null;

    public static boolean start(RootDoc root) {
        ClassDoc[] classes = root.classes();
        docRoot = root;
        docClasses = classes;
        //注释文档信息,自己爱怎么解析注释就怎么解析了,看自己需求
        return true;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(ClassUtil.getClassPathURL());
        List<File> files = FileUtil.loopFiles("/Users/tony/workspace/IdeaProjects/Server/src/main/java/com/server/simple/entity");
        List<String> filePaths = files.stream().map(file -> file.getAbsolutePath()).collect(Collectors.toList());

        String[] docArgs =
                new String[]{
                        "-private",
                        "-doclet", CustomerDoclet.class.getName()
                };
        for (String filePath : filePaths) {
            docArgs = ArrayUtil.append(docArgs, filePath);
        }
        com.sun.tools.javadoc.Main.execute(docArgs);

        show();
    }


    // 显示DocRoot中的基本信息
    public static void show() throws DisablePutException {
        ClassDoc[] classes = docClasses;
        TopMark content = new TopMark();
        for (ClassDoc classDoc : classes) {
            TableMark tableMark = handClass(classDoc);
            TopMark top = new TopMark();
            TitleMark titleMark = new TitleMark(tableMark.getText());
            top.put(titleMark);
            top.put(tableMark);
            content.put(top);

        }
        FileUtil.writeString(content.translate(), FileUtil.file("/Users/tony/workspace/pro/markdown/完整.md"), "UTF-8");
    }

    public static TableMark handClass(ClassDoc classDoc) {

        CustomTableMark tableMark = new CustomTableMark();
        tableMark.setText(classDoc.commentText());
        TableHeadMark headMark = new TableHeadMark(CollUtil.newArrayList(new CellMark("字段"), new CellMark("类型"), new CellMark("说明")));
        tableMark.setHead(headMark);
        for (FieldDoc field : classDoc.fields()) {
            String type = field.type().simpleTypeName();
            String comment = field.commentText();
            String fieldName = field.name();
            TableRowMark rowMark = new TableRowMark(CollUtil.newArrayList(new CellMark(fieldName), new CellMark(type), new CellMark(comment)));
            tableMark.put(rowMark);
        }
        return tableMark;
    }

}

注释写法必须是/**多行注释的那种,例子:

@Data
/**
 * 测试用户
 */
public class User {
    
    /**
     * 姓名
     */
    public String name;
}

这里导出Markdown我用了大神写的代码:
https://gitee.com/ConanJordan/markdowncoffee
不过导出表格稍微有点Bug,自己改改就行了,就是那个表格头的下一行--- 那种,源代码只加了一列,需要自己改改代码,或者有时间提个merge吧。

posted @ 2022-03-24 17:42  哦哈呦  阅读(75)  评论(0)    收藏  举报