obtaining line of codes from a class file

Posted on 2002-06-27
Last Modified: 2010-03-31
i'm currently developing a metric tool (e.g., depth of inheritance, number of private methods, number of public fields and etc). my input(s) to the tool is(are) java class(es)- (.class extension).

i wonder is it possible to obtain metrics such as LINE OF CODES (line of comments, line of executable codes and etc) just from java classes (files with .class extension).

some samples are appreciated.
Question by:ervinloh

Accepted Solution

mraible earned 50 total points
ID: 7114033
I would venture to say that .class files don't have lines of code being that they're binary files.  And also, .class files don't have comments.  The compiler strips them out.

However, you might be able to get fields, methods, etc. using the java.lang.reflection package.

For methods available on a class see:

So to answer your question: my guess is no.

Author Comment

ID: 7114041
to mraible,

getting fields, methods and etc is not a problem. only line of codes is giving me the problems.
LVL 35

Expert Comment

ID: 7114221
 You can get the lines of code from the Java source file (if it is available) and not from the class file itself. Just read the file and check for the "\n" character (newline).

  Hope it helps.
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

LVL 86

Expert Comment

ID: 7115015
Can't get source code from class file. Only from source file.
LVL 92

Expert Comment

ID: 7115401
You can use a decompiler to generate source from a class file. However this will not be the same as the source code before it was compiled so i'm not sure of its value for metrics purposes. But it may help you.


Expert Comment

ID: 7124904
The class file does not have lines. The compiler strips comments and blank line. It also generates binary code instead of the source code, which means the source code is not available any more. The compilation process lossses the information about number of lines, comments, and the explicit code written.
LVL 35

Expert Comment

ID: 8893445
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to mraible

Please leave any comments here within the
next seven days.


Cleanup Volunteer

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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
Spring Framework HTTPSession management 1 45
MySQL  on Tomcat 8 68
MySqlDump not dumping triggers 1 43
Which non-HTML GUI front end to use with Java? 3 24
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…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

860 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