Java Instrumentation Agent 是否支持 class 中的新方法定义?

Does Java Instrumentation Agent support New Method Definition in a class?

我使用 Instrumentation#redefineClasses(ClassDefinition) 方法重新加载了我的测试应用程序 classes。当我尝试在 class 文件中添加新方法并从现有方法调用它时。我这样做并不高兴。但是当我调用一些现有方法和其他 Java 内置库方法时,它工作正常。

我的问题是 - 此限制 known/acknowledged 是 Oracle 还是 Open JDK 实施?我怀疑即使你可以 redefine/retransform 你的 classes 使用 INstrumentation Manifest.MF 文件 - 你可以使用它的程度一定有一些限制。

有没有人有这方面的经验?

来自Instrumentation.html#redefineClasses

The redefinition may change method bodies, the constant pool and attributes. The redefinition 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.

所以答案是否定的,这是不可能的。