public class HotSwapper
extends java.lang.Object
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 HotSwapper.
Any port number can be specified. Since HotSwapper 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 HotSwapper is easy. See the following example:
CtClass clazz = ...
byte[] classFile = clazz.toBytecode();
HotSwapper hs = new HostSwapper(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 HotSwapper
object so that they can reload a number of classes.
| Modifier and Type | Field and Description |
|---|---|
private static java.lang.String |
HOST_NAME |
private com.sun.jdi.VirtualMachine |
jvm |
private java.util.Map |
newClassFiles |
private com.sun.jdi.request.MethodEntryRequest |
request |
private Trigger |
trigger |
private static java.lang.String |
TRIGGER_NAME |
| Constructor and Description |
|---|
HotSwapper(int port)
Connects to the JVM.
|
HotSwapper(java.lang.String port)
Connects to the JVM.
|
| Modifier and Type | Method and Description |
|---|---|
private void |
deleteEventRequest(com.sun.jdi.request.EventRequestManager manager,
com.sun.jdi.request.MethodEntryRequest request) |
private com.sun.jdi.connect.Connector |
findConnector(java.lang.String connector) |
(package private) void |
hotswap() |
private static com.sun.jdi.request.MethodEntryRequest |
methodEntryRequests(com.sun.jdi.request.EventRequestManager manager,
java.lang.String classpattern) |
void |
reload(java.util.Map classFiles)
Reloads a class.
|
void |
reload(java.lang.String className,
byte[] classFile)
Reloads a class.
|
private void |
reload2(java.util.Map map,
java.lang.String msg) |
private void |
startDaemon() |
private com.sun.jdi.ReferenceType |
toRefType(java.lang.String className) |
(package private) com.sun.jdi.event.EventSet |
waitEvent() |
private com.sun.jdi.VirtualMachine jvm
private com.sun.jdi.request.MethodEntryRequest request
private java.util.Map newClassFiles
private Trigger trigger
private static final java.lang.String HOST_NAME
private static final java.lang.String TRIGGER_NAME
public HotSwapper(int port)
throws java.io.IOException,
com.sun.jdi.connect.IllegalConnectorArgumentsException
port - the port number used for the connection to the JVM.java.io.IOExceptioncom.sun.jdi.connect.IllegalConnectorArgumentsExceptionpublic HotSwapper(java.lang.String port)
throws java.io.IOException,
com.sun.jdi.connect.IllegalConnectorArgumentsException
port - the port number used for the connection to the JVM.java.io.IOExceptioncom.sun.jdi.connect.IllegalConnectorArgumentsExceptionprivate com.sun.jdi.connect.Connector findConnector(java.lang.String connector)
throws java.io.IOException
java.io.IOExceptionprivate static com.sun.jdi.request.MethodEntryRequest methodEntryRequests(com.sun.jdi.request.EventRequestManager manager,
java.lang.String classpattern)
private void deleteEventRequest(com.sun.jdi.request.EventRequestManager manager,
com.sun.jdi.request.MethodEntryRequest request)
public void reload(java.lang.String className,
byte[] classFile)
className - the fully-qualified class name.classFile - the contents of the class file.public void reload(java.util.Map classFiles)
classFiles - a map between fully-qualified class names
and class files. The type of the class names
is String and the type of the
class files is byte[].private com.sun.jdi.ReferenceType toRefType(java.lang.String className)
private void reload2(java.util.Map map,
java.lang.String msg)
private void startDaemon()
com.sun.jdi.event.EventSet waitEvent()
throws java.lang.InterruptedException
java.lang.InterruptedExceptionvoid hotswap()