zoukankan      html  css  js  c++  java
  • 源码阅读工具_解析所有类的使用情况

    ParseClass.java主类:

    --------------------------------------------ParseClass.java开始-------------------------------------------------------------

    package test.code.walkthrough;

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

    import test.code.walkthrough.utils.ParseJavaDirectory;

    public class ParseClass {

    public static void main(String[] args) {
    // String directory = "O:/swdf/解压的文件/com/hikvision/swdf";
    String directory = "D:/workspace/baseline_3.1_dev";
    List<File> files = ParseJavaDirectory.getFileList(directory);
    List<String> classes = ParseJavaDirectory.getClassList(files);
    ParseJavaDirectory.staticClassReferNum(classes);
    }
    }

    -------------------------------------------------ParseClass.java结束-------------------------------------------------------------

    ParseJavaDirectory.java文件

    -------------------------------------------------ParseJavaDirectory.java开始----------------------------------------------------

    package test.code.walkthrough.utils;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Set;

    public class ParseJavaDirectory {

    public static List<File> filelist = new ArrayList<>();

    /**
    * @param strPath 传入的父目录
    * @return 所有的文件
    */
    public static List<File> getFileList(String strPath) {
    File dir = new File(strPath);
    File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
    if (files != null && files.length > 0) {
    for (int i = 0; i < files.length; i++) {
    String fileName = files[i].getName();
    if (files[i].isDirectory()) { // 判断是文件还是文件夹
    String filePath = files[i].getAbsolutePath();
    filePath = filePath.replaceAll("\\", "/");
    getFileList(filePath); // 获取文件绝对路径
    } else {
    if (fileName.endsWith("java")) { // 判断文件名以.java结尾
    String strFileName = files[i].getAbsolutePath();
    filelist.add(files[i]);
    }
    }
    }
    }
    return filelist;
    }

    /**
    * @param files 传入需要处理的文件列表
    * @return 所有的类列表
    */
    public static List<String> getClassList(List<File> files) {
    List<String> classlist = new ArrayList<>();
    if (null==files || files.size()==0) {
    return classlist;
    }
    //处理各个File
    for (File file : files) {
    InputStream is = null;
    BufferedReader br = null;
    try {
    is = new FileInputStream(file);
    br = new BufferedReader(new InputStreamReader(is));
    String str = null;
    while ((str=br.readLine()) != null) {
    if (str.trim().equals("")) {
    continue;
    }

    if (str.trim().contains("public")) {
    break;
    }

    if (str.trim().contains("import")) {
    str = str.replaceAll(";", "");
    str = str.replaceAll("import", "");
    str = str.trim();
    classlist.add(str);
    }
    }
    } catch (Exception e) {
    System.out.println(e.getMessage());
    } finally {
    try {
    br.close();
    is.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    return classlist;
    }

    //需要将类计数作统计次数 类名 : 次数
    //按照次数排序,降序排列
    public static void staticClassReferNum(List<String> classes) {
    Map<String, Integer> classesReferMap = new HashMap<String, Integer>();
    if (null==classes || classes.size()==0) {
    // return classesReferMap;
    return;
    }
    //统计各个类
    for (String classStr : classes) {
    Integer referNo = classesReferMap.get(classStr.trim());
    if (null == referNo) {
    referNo = 0;
    }
    int num = ++referNo;
    classesReferMap.put(classStr.trim(), num);
    }

    //排序
    Set<Entry<String,Integer>> classesReferMapEntries = classesReferMap.entrySet();
    List<Entry<String,Integer>> classesReferList = new LinkedList<Entry<String,Integer>>(classesReferMapEntries);
    Collections.sort(classesReferList, new Comparator<Entry<String,Integer>>() {

    @Override
    public int compare(Entry<String, Integer> ele1,Entry<String, Integer> ele2) {

    return ele2.getValue().compareTo(ele1.getValue());

    }
    });

    BufferedWriter bw = FileUtils.write2File4Path("O:/source_research/research.txt");
    try {
    for (Entry<String,Integer> entry : classesReferList) {
    // System.out.println(entry.getValue() + " : " + entry.getKey());
    bw.write(entry.getValue() + " : " + entry.getKey());
    bw.write(" ");
    }
    } catch (IOException e) {
    System.out.println(e.getMessage());
    } finally {
    try {
    bw.close();
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    }

    }

    }

    -------------------------------------------------ParseJavaDirectory.java结束----------------------------------------------------

    FileUtils.java文件

    ---------------------------------------------------------FileUtils.java开始---------------------------------------------------------

    package test.code.walkthrough.utils;

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;

    public class FileUtils {

    public static BufferedWriter write2File4Path(String path) {
    File file = new File(path);
    BufferedWriter bw = null;
    OutputStream os = null;
    try {
    os = new FileOutputStream(file);
    OutputStreamWriter osw = new OutputStreamWriter(os);
    bw = new BufferedWriter(osw);
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    return bw;
    }

    }

    ---------------------------------------------------------FileUtils.java结束---------------------------------------------------------

    备注:只需要将要解析的目录作为值赋值给directory变量,就会将统计结果写入到O:source_research esearch.txt文件里面。

    结果展示如下:第一列是引用次数,第二列是类的全路径名称

    1905 : java.util.List
    1462 : java.util.Map
    934 : org.slf4j.LoggerFactory
    931 : org.slf4j.Logger
    903 : java.util.ArrayList
    653 : java.util.HashMap

  • 相关阅读:
    Internet上的音频/视频概述
    防火墙
    数据链路层安全
    两类密码体制
    Windows Terminal 美化分享
    2019.11.14 启用了FlagCounter
    检测一个App是不是有UWP血统
    UWP 记一次x64平台无法单步调试的bug
    UWP 使用FontIcon
    Git和Github简单教程
  • 原文地址:https://www.cnblogs.com/man-li/p/6494764.html
Copyright © 2011-2022 走看看