Class HotSwapper
- java.lang.Object
-
- javassist.util.HotSwapper
-
public class HotSwapper extends java.lang.ObjectA 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. SinceHotSwapperdoes not launch another JVM for running a target application, this port number is used only for inter-thread communication.Furthermore,
JAVA_HOME/lib/tools.jarmust be included in the class path.Using
HotSwapperis 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 theTestclass and load a new version of theTestclass.classFileis a byte array containing the new contents of the class file for theTestclass. The developers can repatedly callreload()on the sameHotSwapperobject so that they can reload a number of classes.- Since:
- 3.1
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringHOST_NAMEprivate com.sun.jdi.VirtualMachinejvmprivate java.util.MapnewClassFilesprivate com.sun.jdi.request.MethodEntryRequestrequestprivate Triggertriggerprivate static java.lang.StringTRIGGER_NAME
-
Constructor Summary
Constructors Constructor Description HotSwapper(int port)Connects to the JVM.HotSwapper(java.lang.String port)Connects to the JVM.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voiddeleteEventRequest(com.sun.jdi.request.EventRequestManager manager, com.sun.jdi.request.MethodEntryRequest request)private com.sun.jdi.connect.ConnectorfindConnector(java.lang.String connector)(package private) voidhotswap()private static com.sun.jdi.request.MethodEntryRequestmethodEntryRequests(com.sun.jdi.request.EventRequestManager manager, java.lang.String classpattern)voidreload(java.lang.String className, byte[] classFile)Reloads a class.voidreload(java.util.Map classFiles)Reloads a class.private voidreload2(java.util.Map map, java.lang.String msg)private voidstartDaemon()private com.sun.jdi.ReferenceTypetoRefType(java.lang.String className)(package private) com.sun.jdi.event.EventSetwaitEvent()
-
-
-
Field Detail
-
jvm
private com.sun.jdi.VirtualMachine jvm
-
request
private com.sun.jdi.request.MethodEntryRequest request
-
newClassFiles
private java.util.Map newClassFiles
-
trigger
private Trigger trigger
-
HOST_NAME
private static final java.lang.String HOST_NAME
- See Also:
- Constant Field Values
-
TRIGGER_NAME
private static final java.lang.String TRIGGER_NAME
-
-
Constructor Detail
-
HotSwapper
public HotSwapper(int port) throws java.io.IOException, com.sun.jdi.connect.IllegalConnectorArgumentsExceptionConnects to the JVM.- Parameters:
port- the port number used for the connection to the JVM.- Throws:
java.io.IOExceptioncom.sun.jdi.connect.IllegalConnectorArgumentsException
-
HotSwapper
public HotSwapper(java.lang.String port) throws java.io.IOException, com.sun.jdi.connect.IllegalConnectorArgumentsExceptionConnects to the JVM.- Parameters:
port- the port number used for the connection to the JVM.- Throws:
java.io.IOExceptioncom.sun.jdi.connect.IllegalConnectorArgumentsException
-
-
Method Detail
-
findConnector
private com.sun.jdi.connect.Connector findConnector(java.lang.String connector) throws java.io.IOException- Throws:
java.io.IOException
-
methodEntryRequests
private static com.sun.jdi.request.MethodEntryRequest methodEntryRequests(com.sun.jdi.request.EventRequestManager manager, java.lang.String classpattern)
-
deleteEventRequest
private void deleteEventRequest(com.sun.jdi.request.EventRequestManager manager, com.sun.jdi.request.MethodEntryRequest request)
-
reload
public void reload(java.lang.String className, byte[] classFile)Reloads a class.- Parameters:
className- the fully-qualified class name.classFile- the contents of the class file.
-
reload
public void reload(java.util.Map classFiles)
Reloads a class.- Parameters:
classFiles- a map between fully-qualified class names and class files. The type of the class names isStringand the type of the class files isbyte[].
-
toRefType
private com.sun.jdi.ReferenceType toRefType(java.lang.String className)
-
reload2
private void reload2(java.util.Map map, java.lang.String msg)
-
startDaemon
private void startDaemon()
-
waitEvent
com.sun.jdi.event.EventSet waitEvent() throws java.lang.InterruptedException- Throws:
java.lang.InterruptedException
-
hotswap
void hotswap()
-
-