主动向方法添加 Java 注释? [英] Retro-actively add Java annotations to methods?

查看:26
本文介绍了主动向方法添加 Java 注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法修改 .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...

我假设我需要一个工具,例如 JavassistASM.如果是这样,我应该使用哪个,我将如何处理?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆