Java中統計Class類中超長方法長度的code實現

項目開發過程中,我們可能會遇到很多難以維護的代碼,超長的方法,一看都頭大,正常來說,代碼一般不會超過100行,如果業務複雜,儘量分成多個小方法配合註釋加以說明。

如何便捷的查找項目中的超長代碼呢,一可以藉助阿里巴巴代碼規約插件,這個就不多說了,下面時另一種方式,利用java代碼進行實現:

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Stack;


/**
 * @desc: 獲取項目中方法長度超過指定行數的工具代碼,對超長方法進行優化
 * class{
 * 方法
 * method(){
 * if(){
 * ...
 * }
 * }
 * 匿名代碼塊
 * {
 * <p>
 * }
 * 內部類
 * class{
 * <p>
 * }
 * }在此我們將次一級都當做方法,採用先進後出的棧Stack存儲方法行和方法行號,通過記錄讀取當前行號與方法起始行號相減獲得方法行數
 * @auther: tjs
 * @date: 2019/8/13
 */
@Slf4j
public class MethodRowCountUtil {

    public static void main(String[] args) {
        String path = "E:\\qgs\\demo\\src\\main\\java\\com\\example\\demo\\config\\StringToDateConfig.java";
        moreRowCount(path);
    }

    /**
     * @desc: 超長方法行數統計
     * @auther: tjs
     * @date: 2019/8/13
     */
    public static void moreRowCount(String path) {
        File file = new File(path);
        readSourceDir(file);
    }

    /**
     * @desc: 源文件獲取
     * @auther: tjs
     * @date: 2019/8/13
     */
    private static void readSourceDir(File sourceFile){
        if (sourceFile.isDirectory()){
            for (File file:sourceFile.listFiles()){
                if(file.isDirectory()){
                    readSourceDir(sourceFile);
                }else {
                    readFile(file);
                }
            }
        }else {
            readFile(sourceFile);
        }
    }

    /**
     * @desc: 文件讀取
     * @auther: tjs
     * @date: 2019/8/13
     */
    private static void readFile(File file) {
        BufferedReader reader = null;
        String currentLineData;
        int currentLineNum = 0;
        try {
            reader = new BufferedReader(new FileReader(file));
            //先進後出
            Stack<String[]> stack = new Stack<>();
            while ((currentLineData = reader.readLine()) != null) {
                currentLineNum++;
                int num = appear(currentLineData, "{") - appear(currentLineData, "}");
                if (num > 0) {
                    for (int i = 0; i < num; i++) {
                        stack.push(new String[]{currentLineNum + "", currentLineData});
                    }
                } else {
                    for (int i = 0; i < -num; i++) {
                        if (stack.empty()) {
                            System.out.println(file.getName() + "match {} fail,currentLineNum:" + currentLineNum);
                        }
                        String[] arrData = stack.pop();
                        //當棧中取出後棧中只有一條數據時,說明該方法已經結束
                        if(stack.size() == 1){
                            if(currentLineNum-Integer.parseInt(arrData[0]) >80){
                                System.out.println("Class:"+file.getName()+" methodLength:"+(currentLineNum-Integer.parseInt(arrData[0])+1)+"methodRow:"+arrData[1]);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("read file exception");
        }finally {
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    log.error("reader close exception");
                }
            }
        }
    }

    /**
     * @desc: 獲取{或}出現次數
     * @auther: tjs
     * @date: 2019/8/13
     */
    private static int appear(String sourceData, String search) {
        int appear = 0;
        int index = -1;
        while ((index = sourceData.indexOf(search, index)) > -1) {
            index++;
            appear++;
        }
        return appear;
    }
}

ok,這樣即可~.~ 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章