开发者

Using JRebel to swap the bytecode of a class

As said, I'd like to change the bytecode during execution. I am not running any sort of application or web server, it's just for a command line program.

Of course I could just create a new ClassLoader, but that's not feasible from the performance point of view.

I ran into JRebel, which should be capable of exactly this things, but I cannot find any examples, tutorial to archive this.

Java Hotswap is not an option, because it cannot deal with multiple Classloaders

Simple example to demonstrate what I want:

Class Car
{
   public void print() { System.out.println("I am Type A"); }
} 

First I wanna load class Car:

Car myCar = new Car();

Do some stuff

myCar.print();  // => I am Type A

Change the source code

sourceCode.replace("Type A", "Type b");

Recompile and change the byte code in the same classloader

Execute same class again

myCar.print(); // => I a开发者_运维百科m Type B

Hope I made my point clear.


JRebel swaps the bytes for you after you recompile, you do not have to call an API to achieve that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜