Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Count Line of source code

Posted on 2002-07-04
11
Medium Priority
?
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 450 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

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 Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

721 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