Class HotSwapperJpda
A utility class for dynamically reloading a class by the Java Platform Debugger Architecture (JPDA), or HotSwap It works only with JDK 1.4 and later.
Note: The new definition of the reloaded class must declare the same set of methods and fields as the original definition. The schema change between the original and new definitions is not allowed by the JPDA.
To use this class, the JVM must be launched with the following command line options:
For Java 1.4, java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000 For Java 5, java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000
Note that 8000 is the port number used by HotSwapperJpda
.
Any port number can be specified. Since HotSwapperJpda
does not
launch another JVM for running a target application, this port number
is used only for inter-thread communication.
Furthermore, JAVA_HOME/lib/tools.jar
must be included
in the class path.
Using HotSwapperJpda
is easy. See the following example:
CtClass clazz = ...
byte[] classFile = clazz.toBytecode();
HotSwapperJpda hs = new HotSwapperJpda(8000); // 8000 is a port number.
hs.reload("Test", classFile);
reload()
first unload the Test
class and load a new version of
the Test
class.
classFile
is a byte array containing the new contents of
the class file for the Test
class. The developers can
repatedly call reload()
on the same HotSwapperJpda
object so that they can reload a number of classes.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static interface
-
Constructor Summary
ConstructorsConstructorDescriptionHotSwapperJpda
(int port, String host) Connects to the JVM.HotSwapperJpda
(String port, String host) Connects to the JVM. -
Method Summary
Modifier and TypeMethodDescriptionvoid
dispose()
void
reload
(Map<String, byte[]> classFiles, HotSwapperJpda.NoSuchClassListener noSuchClassListener) Reloads a class.
-
Constructor Details
-
HotSwapperJpda
Connects to the JVM.- Parameters:
port
- the port number used for the connection to the JVM.- Throws:
IOException
IllegalConnectorArgumentsException
-
HotSwapperJpda
public HotSwapperJpda(String port, String host) throws IOException, IllegalConnectorArgumentsException Connects to the JVM.- Parameters:
port
- the port number used for the connection to the JVM.- Throws:
IOException
IllegalConnectorArgumentsException
-
-
Method Details
-
reload
public void reload(Map<String, byte[]> classFiles, HotSwapperJpda.NoSuchClassListener noSuchClassListener) throws HotSwapperJpda.NoSuchClassExceptionReloads a class.- Parameters:
classFiles
- a map between fully-qualified class names and class files. The type of the class names isString
and the type of the class files isbyte[]
.- Throws:
HotSwapperJpda.NoSuchClassException
-
dispose
public void dispose()
-