Count the number of lines of code in the java method (function) and the number of lines in java
Today, I want to optimize code for a java project that contains more than 100 lines. You need to calculate which methods of java classes in the project have more than 100 lines of code. I did not find a similar statistical tool on the Internet, so I wrote some code for statistics.
Encoding idea: because of a java class, the {} of the outermost layer can identify the class, and the {} of the outer layer is the method or internal class. To facilitate coding, I also treat internal classes as methods. As long as the outer {And} are paired, it is a complete method. Therefore, I use an advanced stack to store an array. The first element of the array is the starting row of a method, and the second element is the row number of the row. In this way, the number of rows in the method can be reduced by the row number, and the location of the method can be recorded.
The Code is as follows:
Package query;
Import java. io. BufferedReader;
Import java. io. File;
Import java. io. FileReader;
Import java. io. IOException;
Import java. util. Stack;
Public class StatNumForMethod {
Public static void main (String [] args ){
File dir = new File ("E:/workspace/XXX/src/main/java/"); // the absolute path of the java package to be counted
ErgodicDir (dir );
}
/**
* Traverse all java files in the directory
* @ Param dir
*/
Private static void ergodicDir (File dir ){
If (dir. isDirectory ()){
For (File file: dir. listFiles ()){
If (file. isDirectory ()){
ErgodicDir (file );
}
If (file. isFile () & file. getName (). endsWith ("java ")){
StatInFile (file );
}
}
}
}
Private static void statInFile (File file ){
BufferedReader reader = null;
Try {
// Read a file input stream
Reader = new BufferedReader (new FileReader (file ));
String line = null;
Stack <String []> stack = new Stack <String []> ();
Int lineNum = 0;
While (line = reader. readLine ())! = Null) {// traverse rows
LineNum ++;
// When {is encountered, the current row and row number are used as elements of the array into the stack. When} is encountered, the number of rows of the method is calculated, the rows are printed, and the stacks are output.
Int matchNum = getOccur (line, "{")-getOccur (line, "}"); // a row may contain multiple {or}
If (matchNum> 0 ){
For (int I = 0; I <matchNum; I ++ ){
Stack. push (new String [] {line, lineNum + ""});
}
} Else {
For (int I = 0; I <-matchNum; I ++ ){
If (stack. isEmpty ()){
The {} of System. out. println (file. getName () + "file does not match, and the number of rows cannot be counted... The current row number is "+ lineNum );
Return;
}
String [] popArr = stack. pop ();
If (stack. size () = 1) {// when the stack size is 1, it indicates that a method is finished.
Int beginNum = Integer. parseInt (popArr [1]); // The row number starting with the method
If (lineNum-beginNum> 100) {// more than 100 lines of code in the Method
System. out. println (file. getName () + "file" + popArr [0] + "method (row" + beginNum + ") up to" + (lineNum-beginNum + 1) + "row ");
}
}
}
}
}
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
If (reader! = Null ){
Try {
Reader. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
}
/**
* Src Source, find target, and return the number of found
* @ Param src Source
* @ Param find the target
* @ Return: number of records found
*/
Private static int getOccur (String src, String find ){
Int o = 0;
Int index =-1;
While (index = src. indexOf (find, index)>-1 ){
++ Index;
++ O;
}
Return o;
}
}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.