Class HotSwapAgent

java.lang.Object
javassist.util.HotSwapAgent

public class HotSwapAgent extends Object
A utility class for dynamically adding a new method or modifying an existing method body. This class provides redefine(Class, CtClass) and redefine(Class[], CtClass[]), which replace the existing class definition with a new one. These methods perform the replacement by java.lang.instrument.Instrumentation. For details of acceptable modification, see the Instrumentation interface.

Before calling the redefine methods, the hotswap agent has to be deployed.

To create a hotswap agent, run createAgentJarFile(String). For example, the following command creates an agent file named hotswap.jar.

 $ jshell --class-path javassist.jar
 jshell> javassist.util.HotSwapAgent.createAgentJarFile("hotswap.jar") 
 

Then, run the JVM with the VM argument -javaagent:hotswap.jar to deploy the hotswap agent.

If the -javaagent option is not given to the JVM, HotSwapAgent attempts to automatically create and start the hotswap agent on demand. This automated deployment may fail. If it fails, manually create the hotswap agent and deploy it by -javaagent.

The HotSwapAgent requires tools.jar as well as javassist.jar.

The idea of this class was given by Adam Lugowski. Shigeru Chiba wrote this class by referring to his RedefineClassAgent. For details, see this discussion.

Since:
3.22
See Also: