javaは、プロジェクトのパッケージ名とクラス名を出力してから、コメントを出力します。

指定されたクラスのアノテーションを読み取ります

ActionEnter.javaコメントをクロールしたい:

package com.baidu.ueditor;

import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.RootDoc;

/**
 * 读取指定类的注释
 * @Desc
 */
public class JavaDocUse {

    private static RootDoc rootDoc;

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

    public static void show() {
        ClassDoc[] classes = rootDoc.classes();
        for (ClassDoc classDoc : classes) {
            System.out.println(classDoc.name() + "类的注释:" + classDoc.commentText());
        }
    }


    public static void main(String[] args) {
        String property = System.getProperty("user.dir");
        com.sun.tools.javadoc.Main.execute(new String[]{"-doclet",
                Doclet.class.getName(),
                "-encoding", "utf-8", "-classpath",
                // 不大清楚作用,默认为本工具类存放地址
                property + "\\gp6-system\\src\\main\\java\\com\\baidu\\ueditor"
                // 指定要提取注释的文件夹/类
                , property + "\\gp6-system\\src\\main\\java\\com\\baidu\\ueditor\\ActionEnter.java"

        });
        show();
    }
}

結果: 

 

 

コンソールは、プロジェクトのパッケージ名とクラス名を出力してから、コメントを出力します

JavaがJavadocを生成するためのAPIを提供することはわかっています。また、Javadocを介してプロジェクトコメントを抽出することもできます。

package com.baidu.ueditor;

import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.RootDoc;
import java.io.File;

/**
 * 打印出包名和类名,再打印出注释
 */
public class TreeUtil {
    private static RootDoc rootDoc;

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

    public static void main(String[] args) throws Exception {
        String packageName = "";
        // 获取当前项目地址
        File root = new File(System.getProperty("user.dir") + "\\");
        loop(root, packageName);
    }

    public static void loop(File folder, String packageName) throws Exception {
        // 加载当前项目所有文件
        File[] files = folder.listFiles();
        for (int fileIndex = 0; fileIndex < files.length; fileIndex++) {
            File file = files[fileIndex];
            if (file.isDirectory()) {
                // 当是文件夹的时候接着遍历
                loop(file, packageName + file.getName() + "/");
            } else {
                // 文件时,进行注释提取
                listMethodNames(file.getName(), packageName);
            }
        }
    }

    public static void listMethodNames(String filename, String packageName) {
        try {
            // 提取后缀
            String suffix = filename.substring(filename.length() - 5, filename.length());
            // 当是Java文件时,查询它的注释
            if (suffix.equals(".java")) {
                String property = System.getProperty("user.dir");
                com.sun.tools.javadoc.Main.execute(new String[]{"-doclet",
                        TreeUtil.Doclet.class.getName(),
                        "-encoding", "utf-8", "-classpath",
                        property + "\\gp6-system\\src\\main\\java\\com\\baidu\\ueditor"
                        , packageName + filename

                });
                // 拿到类注释
                ClassDoc[] classes = rootDoc.classes();
                for (ClassDoc classDoc : classes) {
                    System.out.println(packageName + filename + "-------------------------"+ classDoc.commentText());
                }
            }
        } catch (Exception e) {
            System.out.println("exception = " + e.getLocalizedMessage());
        }
    }
}

結果: 

コメントに対応するtxt出力アイテムのクラス名

 上記はコンソールに多くのエラーを出力するため、コメントを取得するのに影響します。コメントはtxtファイルに書き込むことができます。

package com.baidu.ueditor;

import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.RootDoc;

import java.io.File;

/**
 * 打印出包名和类名,再打印出注释
 */
public class TreeUtil {
    private static RootDoc rootDoc;

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

    public static void main(String[] args) throws Exception {
        String packageName = "";
        // 获取当前项目地址
        File root = new File(System.getProperty("user.dir") + "\\");
        loop(root, packageName);
    }

    public static void loop(File folder, String packageName) throws Exception {
        // 加载当前项目所有文件
        File[] files = folder.listFiles();
        for (int fileIndex = 0; fileIndex < files.length; fileIndex++) {
            File file = files[fileIndex];
            if (file.isDirectory()) {
                // 当是文件夹的时候接着遍历
                loop(file, packageName + file.getName() + "/");
            } else {
                // 文件时,进行注释提取
                listMethodNames(file.getName(), packageName);
            }
        }
    }

    public static void listMethodNames(String filename, String packageName) {
        try {
            // 提取后缀
            String suffix = filename.substring(filename.length() - 5, filename.length());
            // 当是Java文件时,查询它的注释
            if (suffix.equals(".java")) {
                String property = System.getProperty("user.dir");
                com.sun.tools.javadoc.Main.execute(new String[]{"-doclet",
                        TreeUtil.Doclet.class.getName(),
                        "-encoding", "utf-8", "-classpath",
                        property + "\\gp6-system\\src\\main\\java\\com\\baidu\\ueditor"
                        , packageName + filename

                });
                // 拿到类注释
                ClassDoc[] classes = rootDoc.classes();
                for (ClassDoc classDoc : classes) {
                    System.out.println(filename + "-------------------------" + classDoc.commentText());
                    txtExport.creatTxtFile("ces");
                    txtExport.writeTxtFile(filename + "-------------------------" + classDoc.commentText());
                }
            }
        } catch (Exception e) {
            System.out.println("exception = " + e.getLocalizedMessage());
        }
    }
}
package com.baidu.ueditor;

import java.io.*;

public  class txtExport {
    private static String path = "D:/";
    private static String filenameTemp;

    /**
     * 创建文件
     * @throws IOException
     */
    public static boolean creatTxtFile(String name) throws IOException {
        boolean flag = false;
        filenameTemp = path + name + ".txt";
        File filename = new File(filenameTemp);
        if (!filename.exists()) {
            filename.createNewFile();
            flag = true;
        }
        return flag;
    }

    /**
     * 写文件
     * @param newStr 新内容
     * @throws IOException
     */
    public static boolean writeTxtFile(String newStr) throws IOException {
    // 先读取原有文件内容,然后进行写入操作
        boolean flag = false;
        String filein = newStr + "\r\n";
        String temp = "";
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        FileOutputStream fos = null;
        PrintWriter pw = null;
        try {
            // 文件路径
            File file = new File(filenameTemp);
            // 将文件读入输入流
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            StringBuffer buf = new StringBuffer();
            // 保存该文件原有的内容
            for (int j = 1; (temp = br.readLine()) != null; j++) {
                        buf = buf.append(temp);
                    // 行与行之间的分隔符 相当于“\n”
                buf = buf.append(System.getProperty("line.separator"));
            }
            buf.append(filein);
            fos = new FileOutputStream(file);
            pw = new PrintWriter(fos);
            pw.write(buf.toString().toCharArray());
            pw.flush();
            flag = true;
        } catch (IOException e1) {
            // TODO 自动生成 catch 块
            throw e1;
        } finally {
            if (pw != null) {
                pw.close();
            }
            if (fos != null) {
                fos.close();
            }
            if (br != null) {
                br.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
        return flag;
    }
}

結果: 

おすすめ

転載: blog.csdn.net/Ciel_Y/article/details/123032562