Solved

Count Line of source code

Posted on 2002-07-04
11
185 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse Java import and method not resolved 4 135
Eclipse for Java EE development 2 68
Eclipse neon2 "Java build path" correctness 7 44
Overriding a method 3 13
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

751 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