Solved

obtaining line of codes from a class file

Posted on 2002-06-27
8
239 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now