Created
October 9, 2012 16:32
-
-
Save skRyo/3859908 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
public class FileAnalyze { | |
private File javaFile; | |
enum LineStatus { | |
CODE(0), COMMENT(1), EMPTY(2), BLOCKCOMMENT(3); | |
private final int statusCode; | |
private LineStatus(int statusCode) { | |
this.statusCode = statusCode; | |
} | |
} | |
// 行の状態を解析して結果を返す | |
public LineStatus lineAnalyze(String line) { | |
LineStatus r = null; | |
// ↓の条件に当てはまらなかったらコード! | |
r = LineStatus.CODE; | |
// 「//」が入ってたらコメント! | |
if (line.indexOf("//") >= 0) { | |
r = LineStatus.COMMENT; | |
} | |
// 「/*」が入ってたらブロックコメント! | |
if (line.indexOf("/*") >= 0) { | |
r = LineStatus.BLOCKCOMMENT; | |
} | |
// 「*/」が入ってたらブロックコメント!だけど、続きがあったらコード | |
if ((line.indexOf("*/") >= 0) && (line.substring((line.indexOf("*/"))+2).trim().length() != 0)){ | |
r = LineStatus.CODE; | |
}else if (line.indexOf("*/") >= 0){ | |
r = LineStatus.BLOCKCOMMENT; | |
} | |
// 長さが0だったら空白 | |
if (line.trim().length() == 0) { | |
r = LineStatus.EMPTY; | |
} | |
return r; | |
} | |
public FileAnalyze(File javaFile) { | |
this.javaFile = javaFile; | |
// try (BufferedReader br = new BufferedReader(new FileReader( | |
// this.javaFile))) { | |
// ちょっと家のjava環境だと怒られるので… | |
try { | |
BufferedReader br = new BufferedReader( | |
new FileReader(this.javaFile)); | |
StepCount StepCount = new StepCount(); | |
boolean blockcomment = false; | |
// 一行毎取り出してカウントアップ | |
String line; | |
while ((line = br.readLine()) != null) { | |
// 全行カウントアップ | |
StepCount.countupAllStep(); | |
// 行の解析 | |
LineStatus statusCode = lineAnalyze(line); | |
System.out.println(line); | |
System.out.println(statusCode); | |
if (statusCode == LineStatus.CODE) { | |
// コードライン数カウントアップ | |
StepCount.countupCodeStep(); | |
} | |
if (statusCode == LineStatus.COMMENT) { | |
// コメントライン数カウントアップ | |
StepCount.countupCommentStep(); | |
} | |
if (statusCode == LineStatus.EMPTY) { | |
// 空白ライン数カウントアップ | |
StepCount.countupEmptyStep(); | |
} | |
if (statusCode == LineStatus.BLOCKCOMMENT) { | |
StepCount.countupCommentStep(); | |
} | |
// あれ…今のままだと今ブロック内だよ判定ができない?? | |
// if (statusCode == LineStatus.BLOCKCOMMENT && blockcomment ) { | |
// // コメントライン数カウントアップ | |
// StepCount.countupCommentStep(); | |
// }else if (statusCode == LineStatus.BLOCKCOMMENT || blockcomment ){ | |
// // コードライン数カウントアップ | |
// StepCount.countupCodeStep(); | |
// } | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
でこうか
https://gist.github.com/3864866