方法 Instrumentation.redefineClasses(ClassDefinition[])
的 Java Platform SE 5 API 规范说明:
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.
属性
是什么意思?我认为它可以是访问修饰符(私有(private)、公共(public)、...)、同步、最终和其他修饰符。但是,如果我不仅尝试重新定义方法体,还尝试重新定义它的访问修饰符或类的访问修饰符,或者尝试使方法同步,我得到了
java.lang.UnsupportedOperationException: class redefinition failed: attempted to change method modifiers
at sun.instrument.InstrumentationImpl.redefineClasses0(Native Method)
at sun.instrument.InstrumentationImpl.redefineClasses(Unknown Source)
那么在此上下文中属性
是什么?
最佳答案
J2SE5目前只支持对方法体和字段值的修改。如果您修改访问修饰符,则意味着您完全更改了类的接口(interface)。
属性,在Java编程中是一个非常模糊的概念,我不知道他们为什么把这个词放在他们的文档中。当我们说属性时,在大多数情况下,我们指的是 Java Naming API 中的属性。
重定义类提供了一种动态方式来改变现有系统的运行时行为。我认为被AOP广泛使用。
关于 java 。类重定义的限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12925919/