Solved

Count Line of source code

Posted on 2002-07-04
11
184 Views
Last Modified: 2010-03-31
My work requires me to write a java class to calculate the line of code and line of comments from a java class file...

Until now, I have yet to come up with the solution. The closest thing I came to was using the JavaParser which failed...

Does anyone have the source for finding Line of Code???

Thanks
0
Comment
Question by:DWolve
  • 5
  • 4
  • 2
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 7130938
A class file is byte code and does not include the source code. The source code has been compiled to produce the class file. So it is impossible to determine the number of lines of source that were in the original source file used to produce the class file (in fact the class file may not have even been generated from a source file and instead created directly).

One option would be to decompile the class file to produce a Java source file, but this would not be the same as the original source file used to create the class file. But it may be a good enough approximation for you.
0
 
LVL 1

Expert Comment

by:laax
ID: 7130945
0
 

Author Comment

by:DWolve
ID: 7130946
Corrections.... not the .class file...
I need to find the line of codes for a .java file..

Sorry
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:DWolve
ID: 7130952
Thanks but is the java source file I am evaluating, not a class file.... mistyped it.

0
 
LVL 92

Expert Comment

by:objects
ID: 7130956
Depends what your definition of a line of code is?

0
 
LVL 92

Accepted Solution

by:
objects earned 150 total points
ID: 7130959
Something like this would be a good start, count all non blank, non comment lines:

BufferedReader in = new BufferedReader(new FileReader("file.java"));
String line = null;
int count = 0;
while (null!=(line=in.readLine()))
{
   line = line.trim();
   if (line.length()>0 && !line.startsWith("//"))
   {
      count++;
   }
}

Though it still counts /*   */ comment blocks.
0
 
LVL 1

Expert Comment

by:laax
ID: 7130972
Use java.io.StreamTokenizer
You can configure this class to skip both (// and /*..*/)
type of comments.  Lot more switches available.  Please see JavaDoc for more information.
0
 

Author Comment

by:DWolve
ID: 7130975
Interesting ideas on both account.... I'm currently testing objects idea....
Laax... I'm not certain how to use it.... any sample codes you have to share??
0
 

Author Comment

by:DWolve
ID: 7131296
Ok. Managed to get something moving here.

Thanks guys!
B)
0
 
LVL 92

Expert Comment

by:objects
ID: 7131350
0
 

Author Comment

by:DWolve
ID: 7131494
Here's the result of this query:


public class LineOfCode{
     static int complexCommentFlag;

   public static void main (String [] args) {

String filename=null;

//     Add args=new String[]{"xxx"}; here

     int lineCount = 0;
     int blankSpace = 0;
     int simpleComment = 0;
     int complexComment = 0;
     complexCommentFlag = 0;

try
{
if(args.length==0)
{
     System.out.println("File Required");
}
else if(args.length==1)
{
     filename=args[0];
BufferedReader in = new BufferedReader(new FileReader(filename));
     String line = null;
     
     while (null!=(line=in.readLine()))
     {
          line = line.trim();
          if (line.length()>=0)
          {
               if (line.length()==0)
               {
                    blankSpace++;
               }
               
               if(line.startsWith("//"))
               {
                    simpleComment++;
               }              

               if(line.startsWith("/*"))
               {
                    complexCommentFlag=1;
               }
               if (complexCommentFlag==1)
               {
                    complexComment++;
               }
               if(line.endsWith("*/"))
               {
                    complexCommentFlag=0;
               }

               lineCount++;
          }
     }

     System.out.println("Number of Line = " + lineCount);
     System.out.println("Number of Blank Lines= " + blankSpace);
     System.out.println("Number of Coded Lines= " +lineCount +" - " +blankSpace+" = "+ (lineCount - blankSpace));
     System.out.println("Number of Lines that starts with // = " + simpleComment);
     System.out.println("Number of Lines that within /*--*/ border = " + complexComment);
     System.out.println("Number of Lines of Comment = "+simpleComment+" + "+complexComment+" = "+(complexComment+simpleComment)+"\n");

     System.out.println("Executing File test on "+filename);
     System.out.println("---------------------------------------------------------------");
     System.out.println("Number of Lines in File = " + lineCount);
     System.out.println("Number of Lines of Comment = "+(complexComment+simpleComment));
     System.out.println("Number of Executable Lines = "+(lineCount-complexComment-simpleComment-blankSpace));

}
}
catch (java.io.FileNotFoundException e)
{
     System.out.println("File not found.");
     return;
}
catch (java.io.IOException ioe){
}

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet requestdispatcher include and forward differences 1 39
login form jsp example 2 38
String array comparison 4 38
how to debug htl and js pages 8 39
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question