Top Description Fields Constructors Methods
javassist.util

public Class HotSwapper

Additional top-level class in compilation unit: Trigger.

extends Object
Class Inheritance
Imports
java.io.IOException, java.util.HashMap, .List, .Map, com.sun.jdi.Bootstrap, .ReferenceType, .VirtualMachine, com.sun.jdi.connect.AttachingConnector, .Connector, .IllegalConnectorArgumentsException, com.sun.jdi.event.Event, .EventIterator, .EventQueue, .EventSet, .MethodEntryEvent, com.sun.jdi.request.EventRequest, .EventRequestManager, .MethodEntryRequest

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 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.

HotSwap depends on the debug agent to perform hot-swapping but it is reported that the debug agent is buggy under massively multithreaded environments. If you encounter a problem, try HotSwapAgent.

Since
3.1
See Also
HotSwapAgent

Field Summary

Modifier and TypeField and Description
private static final String
private VirtualMachine
private Map<ReferenceType, byte[]>
private MethodEntryRequest
private Trigger
private static final String

Constructor Summary

AccessConstructor and Description
public
HotSwapper(int
the port number used for the connection to the JVM.
port
)

Connects to the JVM.

public
HotSwapper(String
the port number used for the connection to the JVM.
port
)

Connects to the JVM.

Method Summary

Modifier and TypeMethod and Description
private void
private Connector
findConnector(String connector)

pack-priv void
private static MethodEntryRequest
public void
reload(String
the fully-qualified class name.
className
,
byte[]
the contents of the class file.
classFile
)

Reloads a class.

public void
reload(Map<String, byte[]>
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[].
classFiles
)

Reloads a class.

private void
reload2(Map<ReferenceType, byte[]> map, String msg)

private void
private ReferenceType
toRefType(String className)

pack-priv EventSet
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

HOST_NAMEback to summary
private static final String HOST_NAME
jvmback to summary
private VirtualMachine jvm
newClassFilesback to summary
private Map<ReferenceType, byte[]> newClassFiles
requestback to summary
private MethodEntryRequest request
triggerback to summary
private Trigger trigger
TRIGGER_NAMEback to summary
private static final String TRIGGER_NAME

Constructor Detail

HotSwapperback to summary
public HotSwapper(int port) throws IOException, IllegalConnectorArgumentsException

Connects to the JVM.

Parameters
port:int

the port number used for the connection to the JVM.

HotSwapperback to summary
public HotSwapper(String port) throws IOException, IllegalConnectorArgumentsException

Connects to the JVM.

Parameters
port:String

the port number used for the connection to the JVM.

Method Detail

deleteEventRequestback to summary
private void deleteEventRequest(EventRequestManager manager, MethodEntryRequest request)
Annotations
@SuppressWarnings:unused
findConnectorback to summary
private Connector findConnector(String connector) throws IOException
hotswapback to summary
pack-priv void hotswap()
methodEntryRequestsback to summary
private static MethodEntryRequest methodEntryRequests(EventRequestManager manager, String classpattern)
reloadback to summary
public void reload(String className, byte[] classFile)

Reloads a class.

Parameters
className:String

the fully-qualified class name.

classFile:byte[]

the contents of the class file.

reloadback to summary
public void reload(Map<String, byte[]> classFiles)

Reloads a class.

Parameters
classFiles:Map<String, byte[]>

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[].

reload2back to summary
private void reload2(Map<ReferenceType, byte[]> map, String msg)
startDaemonback to summary
private void startDaemon()
toRefTypeback to summary
private ReferenceType toRefType(String className)
waitEventback to summary
pack-priv EventSet waitEvent() throws InterruptedException