读取javadoc注释

IYBin 732 2019-09-19

maven添加tools引用

<dependency>
	<groupId>com.sun</groupId>
	<artifactId>tools</artifactId>
	<scope>system</scope>
	<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>

代码

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.FieldDoc;
import com.sun.javadoc.RootDoc;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;

/**
 * JavaDoc的使用方法
 */
public class JavaDocUse {
    private static RootDoc rootDoc;
    private static JSONObject types = new JSONObject();

    static {
        types.put("class java.lang.String", "string");
        types.put("interface java.util.List", "array");
        types.put("class java.lang.Integer", "integer");
        types.put("class java.util.Date", "string");
    }

    /**
     * 显示DocRoot中的基本信息
     *
     * @param resultJSONObject
     */
    public static void show(JSONObject resultJSONObject) throws ClassNotFoundException, NoSuchFieldException {
        ClassDoc[] classes = rootDoc.classes();
        for (ClassDoc classDoc : classes) {
            System.out.println(classDoc.name() +
                    "类的注释:" + classDoc.commentText());
            Class<?> moduleType = Class.forName(classDoc.qualifiedTypeName());
            FieldDoc[] fields = classDoc.fields(false);
            for (FieldDoc fieldDoc : fields) {
                if (fieldDoc.isStatic()) {
                    continue;
                }
                // 打印出属性上的注释
                System.out.println("类"
                        + classDoc.name() + ","
                        + fieldDoc.name() +
                        "属性注释:"
                        + fieldDoc.commentText());
                JSONObject jsonObject = new JSONObject();

                jsonObject.put("description", fieldDoc.commentText());
//                jsonObject.put("required", new JSONArray());


                JSONObject jsonObject1 = new JSONObject();

                Field declaredField = moduleType.getDeclaredField(fieldDoc.name());

                Class<?> declaredFieldType = declaredField.getType();
                if (types.containsKey(declaredFieldType.toString())) {
                    jsonObject.put("type", types.getString(declaredFieldType.toString()));
                } else {
                    jsonObject.put("type", declaredFieldType.getSimpleName().toLowerCase());
                }

                java.lang.reflect.Type genericType = declaredField.getGenericType();


                if (genericType instanceof ParameterizedType) {
                    ParameterizedType parameterizedType = (ParameterizedType) genericType;

                    java.lang.reflect.Type[] fieldArgTypes = parameterizedType.getActualTypeArguments();
                    for (java.lang.reflect.Type fieldArgType : fieldArgTypes) {
                        Class fieldArgClass = (Class) fieldArgType;
                        System.out.println("泛型字段的类型:" + fieldArgClass);
                        execute(fieldArgClass.getName(), jsonObject1);
                    }
                }
                if (!jsonObject1.isEmpty()) {
                    JSONObject jsonObject2  = new JSONObject();
                    jsonObject.put("items",jsonObject2);
                    jsonObject2.put("type","object");
                    jsonObject2.put("properties", jsonObject1);
                }

                resultJSONObject.put(fieldDoc.name(), jsonObject);

            }
//            MethodDoc[] methodDocs = classDoc.methods();
//            for(MethodDoc methodDoc : methodDocs){
//                // 打印出方法上的注释
//                System.out.println("类"
//                        +classDoc.name()+","
//                        +methodDoc.name()+
//                        "方法注释:"
//                        +methodDoc.commentText());
//            }
        }
    }

    public static void main(String[] args) throws NoSuchFieldException, ClassNotFoundException {
        JSONObject resultJSONObject = new JSONObject();

        String javaPath0 = "com.infinibase.modular.system.model.DataMaster";
        execute(javaPath0, resultJSONObject);
        String javaPath1 = "com.infinibase.modular.bizdata.transfer.DataMasterDto";
        execute(javaPath1, resultJSONObject);

        System.out.println(resultJSONObject.toJSONString());
        System.out.println(types.toJSONString());
    }

    public static void execute(String packagePath, JSONObject resultJSONObject) throws NoSuchFieldException, ClassNotFoundException {
        String relPath = "C:\\Users\\Bin\\git\\xzsx\\wc-admin\\src\\main\\java\\";
        String javaPath = String.format("%s%s.java", relPath, packagePath.replaceAll("\\.", "\\\\"));

        if (!FileUtil.exist(javaPath)) {
            javaPath = javaPath.replaceAll("wc-admin", "wc-core");
            if (!FileUtil.exist(javaPath)) {
                return;
            }
        }
        com.sun.tools.javadoc.Main.execute(new String[]{"-doclet",
                Doclet.class.getName(),
                "-encoding", "utf-8", "-classpath",
                "C:\\Users\\Bin\\git\\xzsx\\wc-admin\\target\\classes", javaPath});
        show(resultJSONObject);
    }

    public static class Doclet {
        public static boolean start(RootDoc rootDoc) {
            JavaDocUse.rootDoc = rootDoc;
            return true;
        }
    }
}


# 实用 # java