一直好奇 这个插件如何统计代码行数 注释行数 和空行数的, 下载其jar包,查看源码发现,是这样的 学习下
boolean isInComment = false;
Integer total = 0;
Integer blank = 0;
Integer comment = 0;
Integer code = 0;
try {
BufferedInputStream inputStream = new BufferedInputStream(stream);
LineNumberReader streamReader = new LineNumberReader(new InputStreamReader(inputStream));
while(true) {
while(true) {
String line;
do {
if (!streamReader.ready()) {
return new LOCBean(total, blank, comment, code);
}
line = streamReader.readLine();
} while(line == null);
total = total + 1;
line = line.trim();
if (line.length() == 0) {
blank = blank + 1;
} else if (!line.startsWith("//") && !line.startsWith("#")) {
if (isInComment) {
comment = comment + 1;
if (line.contains("*/") || line.contains("\"\"\"") || line.contains("'''") || line.contains("-->")) {
isInComment = false;
}
} else if (!line.equals("\"\"\"") && !line.equals("'''")) {
if ((!line.startsWith("/*") || !line.endsWith("*/")) && (!line.startsWith("\"\"\"") || !line.endsWith("\"\"\"")) && (!line.startsWith("<!--") || !line.endsWith("-->"))) {
if (!line.startsWith("/*") && !line.startsWith("\"\"\"") && !line.startsWith("<!--")) {
code = code + 1;
} else {
comment = comment + 1;
if (!line.contains("*/") || !line.contains("\"\"\"") || !line.contains("-->")) {
isInComment = true;
}
}
} else {
comment = comment + 1;
}
} else {
comment = comment + 1;
isInComment = true;
}
} else {
comment = comment + 1;
}
}
}
} catch (IOException var11) {
var11.printStackTrace();
return new LOCBean(total, blank, comment, code);
}
|