views:

197

answers:

6

Hello, I made a tool that measure some cohesion metrics on the Java source files, but these metrics depends that you can determine the list of attributes that are accessed by the method, to do this I made a parser that extract the method code, but I got some problems, so I ask if there any tool to parse the Java file and extract the method code as text ?

Thanks,

A: 

A pretty old tool to decompile java-classes is jad. It works fine up to jdk 1.5.

Burkhard
Thanks a lot for your answer, but I don't need for a de-compiler, actually I want to associate each Method object in the class to a string that contains the method text, For example if I have this class:ClassA{void m1 { int x;}}Then I need a java library that could make something like wrapper for Method m1 that is generated in reflection to contain also a text = { int x;}} that represents the method code.I tried to use the com.sun.org.apache.bcel but I couldn't extract the method code using the available methods in the class com.sun.org.apache.bcel.internal.classfile.MethodThanks
You mean, the wrapper of ClassA is able to know the source code of each method of ClassA?
Burkhard
Yes, exactly. in the normal reflection framework the Method class has attributes and methods to know the method signature but not have any method to retrieve the method code, so I made a manual code that is wrapper to reflection framework that assigns for each Method object a String represents the method code, but I found some issues in my code that made me seek for any existing tested tool for this.
A: 

Working on the class files may indeed be easier (more tools for it), but there seem to be some Java source parsers as well. "javaparser" looks nice: http://code.google.com/p/javaparser/

Boris Terzic
Thanks for your reply,My main problem is that, if I have a method m, I want to get the list of attributes that accessed by this method, and a list of other methods that method m calls, that is why I thought that parsing the code file is a good way, but If you know much easier way it will be very helpful to tell me about it, also is this possible by the normal reflection framework or by the com.sun.org.apache.bcel framework? Thanks,
I haven't done anything like this myself yet, note that reflection is not the approach since you first of all need to be able to load the class in question and you will not be able to introspect inside the methods themselves. Martin v. Löwis who also answered seems to have some experience with BCEL.
Boris Terzic
+1  A: 

I'm skeptical that what you ask is what you want. Supposedly you had the source code, how would you then determine the list of attributes that are accessed?

Instead, you really do need to walk the bytecode, and look for getfield and setfield opcodes. BCEL is the proper approach to do that.

Martin v. Löwis
+1  A: 

Probably you will be satisfied with Eclipse AST Parser.

This is how you can use it: http://www.ibm.com/developerworks/opensource/library/os-ast/

nanda
A: 

Use ASM its pretty easy to "analyze" a class for whatever you wish. To solve your problem if i understand it correctly you basically write a visitor and watch out for the fields within said method.

mP
A: 

To get at the fine details of a method body, you'll need a Java parser. To know what each symbol in the Java code represents, you'll need something that can build what amounts to a symbol table for each method/class/package.

The Java Front End is a full-featured Java parser which builds symbol tables for a set of Java source files (using class files for unavailable source). The Java Front End builds ASTs, and can be used to split those ASTs into subtrees for methods, and a prettyprinter can regenerate the text of each method (including any comments). This is all built on top of a program transformation system, DMS.

Ira Baxter