Java. Restrictions on class redefinition

6.3k views Asked by At

Java Platform SE 5 API specification for method Instrumentation.redefineClasses(ClassDefinition[]) tells:

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.

What is meant by attributes? I supposed it can be access modifiers (private, public, ...), synchronized, final and other modifiers. But if I try to redefine not only the method body, but also its access modifier, or access modifier of the class, or try to make a method synchronized, I've got

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)

So what is attributes in this context?

2

There are 2 answers

2
StarPinkER On BEST ANSWER

J2SE5 currently only supports the modification on method body and value of fields. If you modify the access modifiers it means you totally change the interface of the class.

For attribute, it is a very vague concept in Java Programming, I have no idea why they put this word into their document. When we say attribute, we mean the Attribute in Java Naming API in most cases.

Redefinition class provide a dynamic way to change the runtime behavior of a existing system. Widely used by AOP I think.

0
Jan On

Attributes are pieces of additional information added to classes, methods and fields in the class file. See java vm specification http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7. There is also a short description of attributes in ASM user guide in Appendix A.3 (http://download.forge.objectweb.org/asm/asm4-guide.pdf).