Count the number of lines of code for a Java method (function)

Source: Internet
Author: User

Today you want to do some code optimization for a Java project that has more than 100 rows. You need to count how the Java classes in your project have more than 100 lines of code. Did not find a similar statistical tool on the Internet, wrote a piece of code to do their own statistics.

Coding idea: Because of a Java class, the outermost {} can identify the class, and the second outer layer {} is the method or inner class. For the sake of coding, I have also treated the inner class as a method. Just pairing the next outer {and} is a complete method. So I used an advanced stack to store an array. The first element of the array is the start row of a method, and the second element is the line number of the row. This way, the number of rows of the method can be subtracted from the line number, and the location of the method can be recorded.

The specific 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/");//absolute path to 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++;
Encountered {when the current row and row number as an array of elements into the stack, encountered} when calculating the number of lines and printing lines, out of the stack
int matchnum = Getoccur (line, "{")-getoccur (lines, "}");//A row may have more than {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 ()) {
System.out.println (File.getname () + "file {} does not match, cannot count rows ... The current line number is "+linenum";
Return
}
string[] Poparr = Stack.pop ();
if (Stack.size () ==1) {//when the stack size is 1 indicates that a method has ended
int beginnum = Integer.parseint (poparr[1]);//line number at which the method starts
if (Linenum-beginnum >100) {//code within the method exceeds 100 lines
System.out.println (File.getname () + "+poparr[0]+" Method (section "+beginnum+" line) up to "+ (linenum-beginnum+1) +" line ");
}
}
}
}
}
} catch (Exception e) {
E.printstacktrace ();
}finally{
if (reader! = null) {
try {
Reader.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}

/**
* SRC source, find finds the target, returns the number found
* @param src Source
* @param find Target
* Number of @return 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 NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Count the number of lines of code for a Java method (function)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.