java - Instrumentation API 是否允许完全重新定义类,包括其结构?

标签 java instrumentation

我正在阅读 Instrumentation API 文档和在网络中找到的大量示例。 但是我仍然不明白我可以使用 Instrumentation API 对类做什么样的事情? 我是否认为在加载类之前我可以进行任何我想要的转换(添加新字段、删除、添加方法)但是一旦它被加载我只能更改方法体?

谢谢。

最佳答案

允许进行哪些更改取决于 JVM。通常您需要保持公共(public) API 和所有字段相同。您可以更改代码。

来自 http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/Instrumentation.html#retransformClasses%28java.lang.Class...%29

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/

相关文章:

java - Apache PDFBox PDF 创建中的字符间距相等

java - 如何检查 ConcurrentLinkedQueue 是否为 GC 留下垃圾(取消引用的实例)?

java - Handler在进程间工作吗?

java - 识别Java字节码中的循环

android - 当两个不同的类扩展它时,单个 LaunchActivity TestCase 不起作用

java - 是否可以使用 javassist java 核心类进行检测?引导类加载器加载的类

java - 从映射的 XML 文件中排除 java 字段

Java:代码的性能和复杂性

java - 无法使用 Robotium 与屏幕中的所有 TextView 进行交互

Java 附加 API : How to set agent properties?