Solved

Count Line of source code

Posted on 2002-07-04
11
181 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
Comment Utility
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
Comment Utility
0
 

Author Comment

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

Sorry
0
 

Author Comment

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

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Depends what your definition of a line of code is?

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 92

Accepted Solution

by:
objects earned 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Ok. Managed to get something moving here.

Thanks guys!
B)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 

Author Comment

by:DWolve
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now