Solved

obtaining line of codes from a class file

Posted on 2002-06-27
8
243 Views
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.
0
Comment
Question by:ervinloh
[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
8 Comments
 
LVL 1

Accepted Solution

by:
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:

http://java.sun.com/j2se/1.3/docs/api/java/lang/Class.html

So to answer your question: my guess is no.
0
 

Author Comment

by:ervinloh
ID: 7114041
to mraible,

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

Expert Comment

by:girionis
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.
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!

 
LVL 86

Expert Comment

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

Expert Comment

by:objects
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.

0
 
LVL 3

Expert Comment

by:yasser_helmy
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.
0
 
LVL 35

Expert Comment

by:girionis
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 51
How  can  i  resolve  HTTP Status 404 -? 8 58
Java Eclipse Loop 3 29
program arguments vs VM arguments 4 26
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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.

735 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