代碼行數統計

介紹:一些公司會有代碼review,統計有效代碼行數(不包括註釋和空行)是一個指標,雖然不合理,但是它確實存在,所以就需要有這樣一個工具類,統計下自己相關模塊的代碼行數,爲自己做參考,代碼很簡單,僅供初學者參考學習。 
使用:指定文件基本目錄,調用count方法即可,可以自定義匹配正則表達式,默認爲"*.java" 
package org.godway.commons.utils;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * 代碼條數統計
 * @author user
 */
public class CodeCount{
 
    /**
     * 正則表達式主要是匹配多行註釋
     */
    private static Pattern pattern = Pattern.compile("/\\*([\\s|\\S])+?\\*/");
     
    // 基本文件目錄
    private File baseDir;
    // 擁有的文件列表
    private List<String> fileList = new ArrayList<String>();
    // 文件數量
    private int fileCount = 0;
    // 代碼行數
    private int lineCount = 0;
    // 匹配表達式
    private String matchRegex = "[a-zA-Z0-9_-]*.java";
 
    public CodeCount(File baseDir) {
        this.baseDir = baseDir;
    }
     
    public CodeCount(File baseDir, String matchRegex) {
        this.baseDir = baseDir;
        this.matchRegex = matchRegex;
    }
     
    /**
     * 統計入口
     */
    public void count() {
        if(!baseDir.isHidden()){
            if (baseDir.isDirectory()) {
                for (File child : baseDir.listFiles()) {
                    CodeCount count = new CodeCount(child, matchRegex);
                    count.count();
                    fileCount += count.fileCount;
                    lineCount += count.lineCount;
                    fileList.addAll(count.fileList);
                }
            } else {
                if(baseDir.getName().matches(matchRegex)){
                    try {
                        fileList.add(baseDir.getPath());
                        fileCount ++;
                        String content = getFileContent(baseDir);
                        int line = getCodeCount(content);
                        lineCount += line;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
 
    /**
     * 獲取文件f中的文本內容
     * 
     * @param f
     * @return
     * @throws IOException
     */
    public String getFileContent(File f) throws IOException {
        StringBuffer sb = new StringBuffer();
        FileInputStream input = new FileInputStream(f);
        byte[] b = new byte[1024];
        int count = 0;
        while ((count = input.read(b)) != -1) {
            sb.append(new String(b, 0, count));
        }
        input.close();
        return sb.toString();
    }
 
    /**
     * 統計文本中的代碼行數 1.用正則表達式出去塊註釋即多行註釋 2.再將字符串按照回車符分割成字符串數組 3.除去空白行或者單行註釋行
     * 
     * @param content
     *            輸入文本
     * @return 文本中代碼行數
     */
    public int getCodeCount(String content) {
        int rowCount = 0;
        Matcher matcher = pattern.matcher(content);
        content = matcher.replaceAll(" ");
        String ss[] = content.split("\n");
        for (String s : ss) {
            if (s.trim().length() > 0 && !s.trim().startsWith("//"))
                rowCount++;
        }
        return rowCount;
    }
 
    /**
     * 打印輸出結果
     */
    public void print(){
        // 統計結果輸出
        System.out.println(baseDir + "中所有文件:");
        for (String file : fileList) {
            System.out.println(file);
        }
        System.out.println();
        System.out.println("文件總數:" + fileCount);
        System.out.println("總代碼行數" + lineCount);
    }
     
    public File getBaseDir() {
        return baseDir;
    }
 
    public void setBaseDir(File baseDir) {
        this.baseDir = baseDir;
    }
 
    public List<String> getFileList() {
        return fileList;
    }
 
    public void setFileList(List<String> fileList) {
        this.fileList = fileList;
    }
 
    public int getFileCount() {
        return fileCount;
    }
 
    public void setFileCount(int fileCount) {
        this.fileCount = fileCount;
    }
 
    public int getLineCount() {
        return lineCount;
    }
 
    public void setLineCount(int lineCount) {
        this.lineCount = lineCount;
    }
 
    public String getMatchRegex() {
        return matchRegex;
    }
 
    public void setMatchRegex(String matchRegex) {
        this.matchRegex = matchRegex;
    }
 
    public static void main(String[] args) throws Exception {
        String path = "S:\\快盤\\workspace\\commons-godway";
        CodeCount code = new CodeCount(new File(path));
        code.count();
        code.print();
    }
}

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