Class HotSwapperJpda
- java.lang.Object
-
- org.hotswap.agent.plugin.hotswapper.HotSwapperJpda
-
public class HotSwapperJpda extends Object
Example HotSwapperJpda class from javaassist is copied to the plugin, because it needs to reside in the application classloader to avoid NoClassDefFound error on tools.jar classes. Otherwise it is the same code as in javassist. A utility class for dynamically reloading a class by the Java Platform Debugger Architecture (JPDA), orHotSwap. 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. SinceHotSwapperJpdadoes 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
HotSwapperJpdais 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 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 sameHotSwapperJpdaobject so that they can reload a number of classes.- Since:
- 3.1
-
-
Constructor Summary
Constructors Constructor Description HotSwapperJpda(int port)Connects to the JVM.HotSwapperJpda(String port)Connects to the JVM.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidreload(String className, byte[] classFile)Reloads a class.voidreload(Map<String,byte[]> classFiles)Reloads a class.voidswapClasses(Class original, String swap)Swap class definition from another class file.
-
-
-
Constructor Detail
-
HotSwapperJpda
public HotSwapperJpda(int port) throws IOException, com.sun.jdi.connect.IllegalConnectorArgumentsExceptionConnects to the JVM.- Parameters:
port- the port number used for the connection to the JVM.- Throws:
IOExceptioncom.sun.jdi.connect.IllegalConnectorArgumentsException
-
HotSwapperJpda
public HotSwapperJpda(String port) throws IOException, com.sun.jdi.connect.IllegalConnectorArgumentsException
Connects to the JVM.- Parameters:
port- the port number used for the connection to the JVM.- Throws:
IOExceptioncom.sun.jdi.connect.IllegalConnectorArgumentsException
-
-
Method Detail
-
reload
public void reload(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(Map<String,byte[]> 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[].
-
swapClasses
public void swapClasses(Class original, String swap) throws Exception
Swap class definition from another class file. This is mainly useful for unit testing - declare multiple version of a class and then hotswap definition and do the tests.- Parameters:
original- original class currently in useswap- fully qualified class name of class to swap- Throws:
Exception- swap exception
-
-