I wish to dynamically add and remove instrumentation code to a Java class file several times without restarting the Java JVM. Is this possible?
I suggest you look at the java.lang.instrument
package, especially the ClassFileTransformer
.
Here is a good article: Instrumentation: Modify Applications with Java 5 Class File Transformations
For actual bytecode generation, I suggest you have a look at libraries such as BCEL or ASM.
The JRebel framework may also interest you. It can change implementation of method bodies, add / remove methods and constructors, add/remove fields, add/remove classes etc, all in runtime.
You could use a helper class (Strategy Design Pattern) which may be swapped for another one at run-time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With