java代码统计_Java代码行统计程序

1 /**

2 * LOCStatistics application3 *@author邝翼飞4 *@version1.05 * @date 2015/1/86 */

7

8 importjava.io.BufferedReader;9 importjava.io.File;10 importjava.io.FileReader;11 importjava.util.Stack;12

13 public classLOCStatistics {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 publicLOCStatistics(){}22

23 public static voidmain(String[] args) {24 String SRC_DIR = "D:\\InsightViewerTestCases\\MethodSimilarityTest";25 LOCStatistics loc = newLOCStatistics();26 if(loc.generateResult(newFile(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 booleangenerateResult(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(newFileReader(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 privateLineProperty extractLineProperty(String line) {81 line =line.trim();82 String subLine = null;83 LineProperty lineProperty = newLineProperty();84 boolean hasCode = false;85 boolean hasComment = false;86 if(line.length() == 0) {87 returnlineProperty;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("//")) {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 intx, y;131 x = line.indexOf("//");132 y = line.indexOf("/*");133 if(x==-1 && y==-1) {134 hasComment = false;135 }136 else{137 Stack stack = new Stack();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 returnlineProperty;200 }201

202 public longgetNumCommentLine() {203 returnnumCommentLine;204 }205

206 public void setNumCommentLine(longnumCommentLine) {207 this.numCommentLine =numCommentLine;208 }209

210 public longgetNumWhiteLine() {211 returnnumWhiteLine;212 }213

214 public void setNumWhiteLine(longnumWhiteLine) {215 this.numWhiteLine =numWhiteLine;216 }217

218 public longgetNumCodeLine() {219 returnnumCodeLine;220 }221

222 public void setNumCodeLine(longnumCodeLine) {223 this.numCodeLine =numCodeLine;224 }225

226 public longgetNumTotalLine() {227 returnnumTotalLine;228 }229

230 public void setNumTotalLine(longnumTotalLine) {231 this.numTotalLine =numTotalLine;232 }233

234 public intgetNumCompilationUnit() {235 returnnumCompilationUnit;236 }237

238 public void setNumCompilationUnit(intnumCompilationUnit) {239 this.numCompilationUnit =numCompilationUnit;240 }241 }242

243 classLineProperty244 {245 public boolean hasCode = false;246 public boolean hasComment = false;247 publicLineProperty() {248 this(false, false);249 }250 public LineProperty(boolean hasCode, booleanhasComment) {251 this.hasCode =hasCode;252 this.hasComment =hasComment;253 }254 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值