Java Class Hot-Swapping


Here’s a little IntelliJ Idea trick that many of you may know while many others have no clue. Have you ever needed to hot-swap a class in a VM? If you work on an overly complicated project like I do then it might be common need. Let me explain what I mean through example. (This same trick may work in Eclipse and other Java IDEs but I haven’t tried.)

If you wanna skip the long winded example and go right to the good stuff click here. Not everybody can tolerate pages of nonsense to prove a point.

Enter the enterprise app
You have a big elephant (*ahem* enterprise) app that deploys on a typical EJB-2x compliant application server like JBoss or Oracle. Your elephant (err enterprise app) contains a bajillion klocs. (A kloc is a kilo-lines-of-code or a standard performance measurement by which programmers of yersteryear were torutured, I mean rated.) Within your enterprise app (I mean your elephant) you do everything from from custom classloading to application level database check constraints and referential integrity logic. About 2% of your enter-umm-eleph- yes I mean enterprise app contains business logic, that’s the important stuff that the users of your project actually care about. (You’re lucky! You have a full two percent dedicated towards solving the actual business problems.) Within that 2% is some code you want to test, verify, profile or whatever. Let’s just say you have a Widget class that you’ve altered which gives birth to Smidget objects through a series of complicated iterations over random database tables involving multiple socket calls to in-VM EJB entity managers. (Yes these are true socket calls. Yes the objects are in the same VM. No, don’t ask why you’re doing it this way just follow my stoopid example, will ya’?) Are you still with me? Don’t worry, I’m getting to the cool part in a second. You have no unit tests to validate your new code because, well, unit testing is just completely against your religion. You go to church every Sunday and your pastor re-enforces how every unit-testing-agilist will burn in a murky lake of fire in some place called Hades and as a result you’ve never quite gotten around to seeing your first green bar yet. The only way to verify your alterations to the Smidget producing Widget is through an iteration of building your project, setting a hundred or so random flags, configs, and VM options, and redeploying to your preferred application server which you are fortunate enough to host on your development machine. After said build/deploy cycle completes you eyeball the results of invoking your altered client by initiating a series of client requests which eventually land you on the page or screen that involves your trapped widget.

Change the behavior of a Widget class

If the above paragraph is typical of one of your 9-5 monitor glaring sessions then you’ll appreciate what I’m about to show you. You should probably appreciate anything that removes a kloc or two or any tip that allows you to get at the 2% of important stuff that you just spent 100% of your time burying in rigid/useless brittle framework-ish programming. After you’ve eyeballed the behavior of your widget you will more than likely need to change it a little. If you’re running IntelliJ Idea (it likely works the same in Eclipse too) you can remove your mouse pointer from the stop-server icon/ shell or batch script. What you can do instead is start your remote debugger. (If you’ve prepared your appserver earlier you can just launch the remote debugger and link directly to your server, otherwise you will have to add VM-level flags to your appserver startup sequence and relaunch the server.) After starting the debugger, hit the compile option for the file you need to change. Compiling a Java file from your project that’s contained in a currently debugged remote server instance starts a bit of magic. I call this magic class hot-swapping. The IDE plugs the recompiled class into the app server under debug and tells the appserver to reload the class. Perhaps you’ve done this before with limited success. Perhaps it’s one of those cool features that only seems to work sometimes. I’ll explain why, There are limitations.

A hot-swapped class cannot differ in it’s schema. That means it must have the same methods and members as its older/original version. In other words:

public class Foo extends Object {
public void bar() { /* ... */ }
}

//cannot become
public class Foo extends Object {
public void bar() { fuzz(); }
private void fuzz() { /* ... */ }
}

You cannot add parameters to methods and recompile, you cannot add methods to classes, you cannot add static fields to classes, you cannot pass go, but you might be able to collect $100. Once you get a hang of the rules, hot-swapping can easily become your best friend. It works against any remote VM instance and does not require the use of an app server. It helps fix those hard to reach bugs and it’s gentle on glass surfaces. When used with the dynamic features of tools like the F8 evaluation window you can get extra flexibility of virtually writing your program as it runs and trying out different variations of an algorithm to determine optimal results. Of course it’s always much better to use JUnit. Until later, drop some science in my box down low…

5 thoughts on “Java Class Hot-Swapping

  1. No need to apologize, I like the criticism. While I wasn’t high or drunk when I wrote this, I do understand how my writing can be incoherent. I do want most of my content to be of some use to people. I’ll tell you what. I’m gonna do a reprint of the above with more emphasis on content and less focus on stoopidity. We’ll see how it turns out.

  2. I actually, found this a useful article. A colleague asked me If I’d seen this behaviour as he came across it while debugging but could not reproduce it. So after a quick search I came across this entry. So for me It’s not useless or unreadable.

  3. It is a valuable tip. However it is trapped in a bunch of verbiage and hard to follow… umm.. jokes. (I just finished re-reading and I actually chuckled once or twice but that’s just me. I laugh at dropped napkins, midgets selling financial freedom kits at 2am on infomercials, and random episodes of Family Guy so I’m kinda silly.) I like my readers to be entertained and educated but I don’t often get the mixture or ingredient set quite right. I’ve been meaning to rewrite this one for a while but I keep getting hung up on stoopid stuff like actually building an application that pays the bills at my employer and ultimately in my home. I’m gonna take a shortcut and link to the meat of the story from the top to save visitors from suffering through my acts of funny.

  4. Oh, and thanx for the compliment Chuck! See, not everyone thinks I’m corny! there is an occasional passerby that enjoys ha-has mixed in with oh-nos.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s