views:

49

answers:

3

I have one GUI with one list box to display the list of methods in the class. I can achieve it using reflection. But can I view the source code in anotehr text area on selecting the method name ?

I knew about decompilers. but I don't want to see source code in their window.

I want to use some thirdparty lib so that I can see the source code of specific method in my own GUI.

Please suggest is there any API is avaliable for this ?

+2  A: 

You will need a decompiler of some sort, that you can link to. I am not sure there are any libraries, but here's a link to the JD Java Decompiler.

Remember that you lose variable names and such during compilation, so if you decompile the resulting source code may be less readable.

If you have access to the source you could link it to the class files, and find the chosen method source in the source files linked. This can be achieved by a simple one-pass parse of the source files.

Your biggest problem will be determining when a method ends, and a simple solution is to count {'s and }'s and determine when the { of the method declaration is closed.

Jes
I got one solution. But wanna check whether it is feasible.As you told we could use some decompiler and get the complete source code.Then we could use eclipse libraries to parse the code using AST and then find the curresponding method.Theoretically this solution should work.But I wanna spend some effort to achieve theis using eclipse libraries.Thanks all of you for helping me to find the solution.
Sreejesh
A: 

I once created application that included it's own source code viewer. I think it's a good alternative to decompilers, which can come with quite dependencies.

I was using NetBeans so packaging the .java files was as easy as changing one filter option. I checked java properties to find the jar file and scanned it just as any zip file for java source files. With this approach having a GUI with JTreeTable populated with source files and JTextArea displaying source code was trivial.

I believe You could do the same with addition of one step more - clipping the source to contain only the selected method. I think it should boil down to simple parser, one that counts opening and closing brackets.

Rekin
A: 

I'm leaving the earlier answer up in case you need it, but JODE hasn't been updated in a long time. Searching around, I can't find any decompiler that is open-source or available in library form.

JODE may be just what you want. The core decompiler is released as a library under the GNU LGPL, so you can integrate it into your program with no issues.

Chinmay Kanchi