A tool to take Java project class annotations and generate tree diagrams

The leader asked me to write a software to extract the comments of all Java files in a project. And as long as the class annotation, not the method annotation, generate a tree structure

Then think that you can usually generate javaDoc, then Java should provide a method:

package com.baidu.ueditor;

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

import java.io.File;
import java.io.IOException;

public class LastUtil {
    private static RootDoc rootDoc;

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

    public static void main(String[] args) throws IOException {
        // 新建txt文件
        txtExport.creatTxtFile("ces");
        // 指定项目文件位置
        File f = new File(System.getProperty("user.dir"));
        // 打印项目名
        System.out.println(f.getName());
        txtExport.writeTxtFile(f.getName());
        // 方法!进入子文件夹中 并打印子文件名
        tree(f, 1);
    }

    private static void tree(File f, int level) throws IOException {
        // 缩进量
        String preStr = "";
        for (int i = 0; i < level; i++) {
            if (i == level - 1) {
                preStr = preStr + "└─";
            } else {
                // 级别 - 代表这个目下下地子文件夹
                preStr = preStr + "|   ";
            }
        }
        // 返回一个抽象路径名数组,这些路径名表示此抽象路径名所表示目录中地文件
        File[] childs = f.listFiles();
        for (int i = 0; i < childs.length; i++) {
            String path = childs[i].getPath();
            String name = childs[i].getName();
            // 当是文件时
            if (!childs[i].isDirectory()) {
                // 提取Java文件注释
                listMethodNames(preStr, childs[i].getName(), childs[i].getPath());
            } else {
                // 文件夹时,打印子文件的名字,并写入txt文件中
                System.out.println(preStr + childs[i].getName());
                txtExport.writeTxtFile(preStr + childs[i].getName());
                // 假如子目录下还有子目录,递归子目录调用此方法
                tree(childs[i], level + 1);
            }
        }
    }

    private static void listMethodNames(String preStr, 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",
                        LastUtil.Doclet.class.getName(),
                        "-encoding", "utf-8", "-classpath",
                        // 不大清楚,理解为工具类在该项目中的地址
                        property + "\\gp6-system\\src\\main\\java\\com\\baidu\\ueditor",
                        // 要爬取的项目的地址
                        packageName

                });
                // 拿到类注释
                ClassDoc[] classes = rootDoc.classes();
                for (ClassDoc classDoc : classes) {
                    System.out.println(preStr + filename + "-------------------------" + classDoc.commentText());
                    txtExport.writeTxtFile(preStr + 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;
    }
}

result:

In the ces.txt under the D drive, some screenshots:

Guess you like

Origin blog.csdn.net/Ciel_Y/article/details/122964797