我正在阅读 Instrumentation API 文档和在网络中找到的大量示例。 但是我仍然不明白我可以使用 Instrumentation API 对类做什么样的事情? 我是否认为在加载类之前我可以进行任何我想要的转换(添加新字段、删除、添加方法)但是一旦它被加载我只能更改方法体?
谢谢。
最佳答案
允许进行哪些更改取决于 JVM。通常您需要保持公共(public) API 和所有字段相同。您可以更改代码。
The retransformation may change method bodies, the constant pool and attributes. The retransformation must not add, remove or rename fields or methods, change the signatures of methods, or change inheritance. These restrictions maybe be lifted in future versions. The class file bytes are not checked, verified and installed until after the transformations have been applied, if the resultant bytes are in error this method will throw an exception.
关于java - Instrumentation API 是否允许完全重新定义类,包括其结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11046305/