Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

Count Line of source code

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
DWolve
Asked:
DWolve
  • 5
  • 4
  • 2
1 Solution
 
objectsCommented:
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
 
laaxCommented:
0
 
DWolveAuthor Commented:
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.

 
DWolveAuthor Commented:
Thanks but is the java source file I am evaluating, not a class file.... mistyped it.

0
 
objectsCommented:
Depends what your definition of a line of code is?

0
 
objectsCommented:
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
 
laaxCommented:
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
 
DWolveAuthor Commented:
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
 
DWolveAuthor Commented:
Ok. Managed to get something moving here.

Thanks guys!
B)
0
 
objectsCommented:
0
 
DWolveAuthor Commented:
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now