Java:从 JAR 文件中的类文件中获取方法存根的简单方法?反射?
我正在寻找一种方法来获取 jar 文件中所有类的方法存根列表。 我不知道从哪里开始...我可以使用 Reflection 或 Javassist 或其他一些我还没听说过的工具吗? 至少可以解压 jar、反编译类文件并使用行解析器扫描方法,但我认为这是最肮脏的方法;-)
有什么想法吗?
亲切的问候
I'm searching for a way to get a list of method stubs of all classes within a jar file.
I'm not sure where to start... May I use Reflection or Javassist or some other tools of which I've not heard yet!?
At least it may be possible to unpack the jar, decompile the class files and scan with a line parser for methods, but I think that is the most dirtiest way ;-)
Any ideas?
Kind Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
构建于 aioobe 的回答,您还可以使用 ASM 的树 API(而不是其访问者 API)来解析 JAR 文件中包含的类文件的内容。此外,您还可以使用 JarFile 类。以下是如何完成此操作的示例:
printMethodStubs
方法接受JarFile
并继续打印所有类文件中包含的所有方法的描述。describeClass
方法接受ClassNode
对象并继续描述它及其关联的方法:describeMethod
方法接受MethodNode
并返回描述方法签名的字符串:Building upon aioobe's answer, you can also use ASM's tree API (as opposed to its visitor API) to parse the contents of the class files contained within your JAR file. As well, you can read the files contained within the JAR file using the JarFile class. Here is an example of how this could be done:
The
printMethodStubs
method accepts aJarFile
and proceeds to print out descriptions of all methods contained within all class files.The
describeClass
method accepts aClassNode
object and proceeds to describe it and its associated methods:The
describeMethod
method accepts aMethodNode
and returns a String describing the method's signature:我能想到的最好的方法是使用 ASM 字节码框架。那么至少您不必使用行解析器进行一些反编译输出。事实上,获取方法存根应该类似于其访问者接口之一的 20 行实现。
我自己用它来重写字节码,它相当简单直接(特别是如果您只是阅读类文件)。
http://asm.ow2.org/
The best way I can think of is to use the ASM-bytecode framework. Then at least you wouldn't have to go through some decompilation-output with a line-parser. In fact, getting the method-stubs should be like a 20-line implementation of one of their Visitor-interfaces.
I've used it myself for bytecode rewriting and it's fairly simple and straight forward (especially if you're just reading the class-files).
http://asm.ow2.org/