Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the code for a Java class for an already running instance?

Tags:

java

I wish to dynamically add and remove instrumentation code to a Java class file several times without restarting the Java JVM. Is this possible?

like image 280
yazz.com Avatar asked Dec 29 '22 02:12

yazz.com


2 Answers

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.

like image 104
aioobe Avatar answered Mar 11 '23 21:03

aioobe


You could use a helper class (Strategy Design Pattern) which may be swapped for another one at run-time.

like image 24
Highland Mark Avatar answered Mar 11 '23 19:03

Highland Mark