国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Java代碼行統計程序

2019-11-14 22:32:25
字體:
來源:轉載
供稿:網友
java代碼行統計程序

1、統計粒度

能夠統計包括:代碼行數、注釋、空行、總行數。

2、適用的編程語言

目前只測試過Java,其它編程語言暫時未知。

3、代碼簡析

代碼行統計需要考慮的因素包括字符串匹配、編程語言本身的字符串,后者非常重要,往往導致難以發現的bug。該程序的核心在于遞歸的使用和棧的構造,需要深刻理解遞歸的原因和棧的幾種狀態。

4、參考程序

程序一:http://wenku.baidu.com/view/2a3dcbe3524de518964b7dec.html

程序二:http://www.downxia.com/downinfo/11131.html

程序三:https://code.google.com/p/boomworks/wiki/SourceCounterCN 版本:3.5.33.73 - Boom 2011-6-22

5、程序源代碼

  1 /**  2  * LOCStatistics application  3  * @author 鄺翼飛  4  * @version 1.0   5  * @date 2015/1/8  6  */  7   8 import java.io.BufferedReader;  9 import java.io.File; 10 import java.io.FileReader; 11 import java.util.Stack; 12  13 public class LOCStatistics { 14     PRivate long numCommentLine = 0; 15     private long numWhiteLine = 0; 16     private long numCodeLine = 0; 17     private long numTotalLine = 0; 18     private int numCompilationUnit = 0; 19     private boolean isCommenting = false; 20      21     public LOCStatistics(){} 22      23     public static void main(String[] args) { 24         String SRC_DIR = "D://InsightViewerTestCases//MethodSimilarityTest"; 25         LOCStatistics loc = new LOCStatistics(); 26         if(loc.generateResult(new File(SRC_DIR))) { 27             System.out.println("numCommentLine:/t" + loc.numCommentLine); 28             System.out.println("numWhiteLine:/t" + loc.numWhiteLine); 29             System.out.println("numCodeLine:/t" + loc.numCodeLine); 30             System.out.println("numTotalLine:/t" + loc.numTotalLine); 31             System.out.println("numCompilationUnit:/t" + loc.numCompilationUnit); 32         } 33         else { 34             System.out.println("Failed to generate results!"); 35         } 36     } 37      38     public boolean generateResult(File dir) { 39         if(dir==null || !dir.exists() || !dir.isDirectory()) { 40             return false; 41         } 42         isCommenting = false; 43         File[] files = dir.listFiles(); 44         for(int i = 0; i < files.length; i++) { 45             if (files[i].isDirectory()) { 46                 generateResult(files[i]); 47             }  48             else { 49                 try { 50                     if (files[i].getName().endsWith(".java")) { 51                         numCompilationUnit++; 52                         BufferedReader br = new BufferedReader(new FileReader(files[i])); 53                         String line = br.readLine(); 54                         while (line != null) { 55                             numTotalLine++; 56                             LineProperty lineProperty = extractLineProperty(line); 57                             if(lineProperty.hasComment) { 58                                 numCommentLine++; 59                             } 60                             if(lineProperty.hasCode) { 61                                 numCodeLine++; 62                             } 63                             if(!lineProperty.hasComment && !lineProperty.hasCode) { 64                                 numWhiteLine++; 65                             } 66                             line = br.readLine(); 67                         } 68                         br.close(); 69                     } 70                 }  71                 catch (Exception e) { 72                     e.printStackTrace(); 73                     return false; 74                 }  75             } 76         } 77         return true; 78     } 79      80     private LineProperty extractLineProperty(String line) { 81         line = line.trim(); 82         String subLine = null; 83         LineProperty lineProperty = new LineProperty(); 84         boolean hasCode = false; 85         boolean hasComment = false; 86         if(line.length() == 0) { 87             return lineProperty; 88         } 89         else if(isCommenting == true) { 90             hasComment = true; 91             if(line.matches(".*//*/.*")) { 92                 isCommenting = false; 93                 int index = line.indexOf("*/"); 94                 if(line.length() == index + 2) { 95                     hasCode = false; 96                 } 97                 else { 98                     subLine = line.substring(index + 2); 99                     lineProperty = extractLineProperty(subLine);100                     hasCode = lineProperty.hasCode;101                 }102             }103             else {104                 hasCode = false;105             }106         }107         else if(line.startsWith("http://")) {108             hasComment = true;109         }110         else if(line.startsWith("/*") && !line.matches(".{2,}//*/.*")){111             hasCode = false;112             hasComment = true;113             isCommenting = true;114         }115         else if(line.startsWith("/*") && line.matches(".{2,}//*/.*")) {116             if(!line.matches(".{2,}//*/.+")) {117                 hasCode = false;118                 hasComment = true;119             }120             else {121                 String tmps = line.substring(2);122                 subLine = tmps.substring(tmps.indexOf("*/")+2);/*/test*/123                 lineProperty = extractLineProperty(subLine);124                 hasCode = lineProperty.hasCode;125                 hasComment = true;126             }127         }128         else {129             hasCode = true;130             int x, y;131             x = line.indexOf("http://");132             y = line.indexOf("/*");133             if(x==-1 && y==-1) {134                 hasComment = false;135             }136             else {137                 Stack<Character> stack = new Stack<Character>();138                 int count = 0;139                 for(Character ch: line.toCharArray()) {140                     count++;141                     // 首先判斷上一個字符是否是轉義符 / 若是,則拋棄該字符142                     if(!stack.isEmpty() && stack.peek()=='//') {143                         stack.pop();144                         continue;145                     }146                     if(ch != '"'){147                         if(ch == '//') {148                             stack.push(ch);149                         }150                         else if(!stack.isEmpty() && stack.peek()=='"') {151                             continue;152                         }153                         else if(ch=='*' || ch=='/') {154                             if (stack.isEmpty()) {155                                 stack.push(ch);156                             }157                             else {158                                 if(stack.peek() == '/') {159                                     break;160                                 }161                                 else {162                                     stack.push(ch);163                                 }164                             }165                         }166                     }167                     else {168                         if(stack.isEmpty()) {169                             stack.push(ch);170                         }171                         else {172                             char tmp = stack.peek();173                             while(tmp!='"' && !stack.isEmpty()) {174                                 tmp = stack.pop();175                             }176                             if(tmp!='"' && stack.isEmpty()) {177                                 stack.push(ch);178                             }179                             else {180                                 while(!stack.isEmpty()) {181                                     stack.pop();182                                 }183                             }184                         }185                     }186                 }187                 if(count <= line.length()) {188                     subLine = line.substring(count-2);189                     lineProperty = extractLineProperty(subLine);190                     hasComment = lineProperty.hasComment;191                 }192                 else {193                     hasComment = false;194                 }195             }196         }197         lineProperty.hasCode = hasCode;198         lineProperty.hasComment = hasComment;199         return lineProperty;200     }201 202     public long getNumCommentLine() {203         return numCommentLine;204     }205 206     public void setNumCommentLine(long numCommentLine) {207         this.numCommentLine = numCommentLine;208     }209 210     public long getNumWhiteLine() {211         return numWhiteLine;212     }213 214     public void setNumWhiteLine(long numWhiteLine) {215         this.numWhiteLine = numWhiteLine;216     }217 218     public long getNumCodeLine() {219         return numCodeLine;220     }221 222     public void setNumCodeLine(long numCodeLine) {223         this.numCodeLine = numCodeLine;224     }225 226     public long getNumTotalLine() {227         return numTotalLine;228     }229 230     public void setNumTotalLine(long numTotalLine) {231         this.numTotalLine = numTotalLine;232     }233 234     public int getNumCompilationUnit() {235         return numCompilationUnit;236     }237 238     public void setNumCompilationUnit(int numCompilationUnit) {239         this.numCompilationUnit = numCompilationUnit;240     }241 }242 243 class LineProperty244 {245     public boolean hasCode = false;246     public boolean hasComment = false;247     public LineProperty() {248         this(false, false);249     }250     public LineProperty(boolean hasCode, boolean hasComment) {251         this.hasCode = hasCode;252         this.hasComment = hasComment;253     }254 }
View Code

6、測試用例

 1 import java.util.ArrayList; 2 import java.util.List; 3  4 @SuppressWarnings("rawtypes") 5 public class MethodSimilarityTest { 6  7     public void overLoad(AA a){} 8     public void overLoad(double x){} 9     public void overLoad(List list){}10     11     12     13     public static void main(String[] args) { 14 //        new MethodSimilarityTest().overLoad((new BB()).getAA());15 //        new MethodSimilarityTest().overLoad(1);16         new MethodSimilarityTest().overLoad(new ArrayList());17 /** // sfska */        String s = "/*dls"; // fsdafl18 /** // sfska */19         s = "/*dls"; 20     }21 22 }23 24 class AA25 { 26     public AA getAA(){return new AA();}27     public void c(int b){}28 }29 class BB extends AA30 {31     public BB(){this(1);this.c(1); }32     public BB(int x){}33 }
View Code


上一篇:JavaSE學習心得

下一篇:spring cron表達式

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贺州市| 泽库县| 轮台县| 巴彦县| 剑阁县| 黄石市| 田东县| 永兴县| 会东县| 华蓥市| 修水县| 疏勒县| 麻江县| 佛山市| 赤壁市| 定州市| 临夏市| 灵川县| 松滋市| 西盟| 绥滨县| 习水县| 马山县| 哈巴河县| 武邑县| 滁州市| 宿迁市| 防城港市| 贵德县| 尖扎县| 合肥市| 包头市| 中超| 丁青县| 鹿邑县| 墨脱县| 囊谦县| 民县| 体育| 开阳县| 长葛市|