문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

This is an old question, but seeing as the decompiler landscape has changed significantly in the past year, I feel it's worth resurrecting.

Procyon is an open source framework that contains, among other things, a Java decompiler. It is written in Java, and the decompiler APIs can be integrated into another application fairly easily. In fact, there are already two third-party GUI front-ends, including the SecureTeam Java Decompiler.

CFR does not have source code available yet, but it is also an excellent decompiler. It too is written in Java, and while I have not tried to integrate it with an existing application, it should certainly be possible.

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.

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top