dagblog.
← Archive

Do you know about Hot Code Replace in Eclipse?


Eclipse has had HotSwap support for some time now.

The Java Platform Debugger Architecture (JPDA) defines an interface the

virtual machine must provide so the debugger can communicate with it. As an aside,

they talk over the Java Debug Wire Protocol. Java 1.4

introduced a feature called HotSwap, which lets a debugger update bytecode

at runtime. Eclipse’s debugger takes advantage of it.

What does this mean for ordinary mortals, and how might it help them? Perhaps

you’ve asked yourself one of those questions. Here’s one situation

HotSwap can help with. You start an application in debug mode and realize you forgot

to fix some code. Normally you’d have to stop the application, fix the code,

and start the application again.

Restarting an application is dull and can take a while, as anyone who’s started an

application server knows. With HotSwap you can skip the restart, fix the

offending code, save it, and let Eclipse take care of the rest. In Eclipse the

HotSwap feature goes by the name Hot Code Replace. It does have limits, of course.

You can’t change half the application logic in the VM in one go.

The second scenario also involves editing code and applying it at runtime.

Imagine you’re stepping through a method and spot a bug. What do you do? Fix it,

restart the application and wait for the program to reach the breakpoint. Hot Code Replace

can apply your changes and call the method again

after you fix the bug. So you can use it while debugging

to rerun a method.

If you like the idea and want to try it, remember

to run the application in debug mode.

It might be worth explaining why I’ve gone on about HotSwap. Filemon was raving to me today about what the AOP

framework AspectWerkz can do. One point he made

was that it can change bytecode at runtime. My immediate reaction was

that Eclipse has done that with its eyes closed for ages. I’m unsure whether

AW uses HotSwap in online mode. I suspect it doesn’t, since it’s doing something

a little different, but that would take us into pointcuts, which is a topic

for another day.

Medium version

Related articles

Related CZ Podcast episodes