追溯地向方法中添加Java注释? [英] Retro-actively add Java annotations to methods?
问题描述
是否有一种方法可以修改.class文件,以便将Java批注添加到某些方法中?基本上,我想遍历jar文件中每个类文件的方法并注释某些方法.请注意,在使用jar文件时,这不是在运行时.相反,完成后,我想修改带有注释的类文件.
Is there a way to modify .class files in order to add Java annotations to certain methods? Basically I want to traverse methods of each class file in a jar file and annotate certain ones. Note that this is not at run-time while using the jar file. Rather, after I'm done I want to have modified class files with the annotations.
我确实可以访问源代码,因此,如果有一个自动的源代码修改器,那也可以工作...
I do have access to the source code, so if there's an automatic source code modifier, that would work as well...
我假设我需要一个 Javassist 或
I'm assuming I'll need a tool such as Javassist or ASM. If so, which one should I use and how would I go about it?
推荐答案
谷歌搜索一个小时左右,变成 ASM .要使用更改后的字节码编写类文件,请使用 ClassWriter .
Googling for an hour or so turned this article up which seems to completely answer my question: use ASM. To write class files using the changed bytecode, use ClassWriter.
好吧,我想是时候上班了. :)
Well, time to get to work then, I guess. :)
这篇关于追溯地向方法中添加Java注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!