Search code examples
javadecompilingdecompiler

How to view the source code from jar file in my own JAVA GUI?


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 another 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 if there is an API available for this.


Solution

  • 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.