Class HotSwapAgent
- java.lang.Object
-
- javassist.util.HotSwapAgent
-
public class HotSwapAgent extends java.lang.Object
A utility class for dynamically adding a new method or modifying an existing method body. This class providesredefine(Class, CtClass)
andredefine(Class[], CtClass[])
, which replace the existing class definition with a new one. These methods perform the replacement byjava.lang.instrument.Instrumentation
. For details of acceptable modification, see theInstrumentation
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 namedhotswap.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
requirestools.jar
as well asjavassist.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:
redefine(Class, CtClass)
,redefine(Class[], CtClass[])
-
-
Constructor Summary
Constructors Constructor Description HotSwapAgent()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
agentmain(java.lang.String agentArgs, java.lang.instrument.Instrumentation inst)
The entry point invoked when this agent is started after the JVM starts.static java.io.File
createAgentJarFile(java.lang.String fileName)
Creates an agent file for usingHotSwapAgent
.java.lang.instrument.Instrumentation
instrumentation()
Obtains theInstrumentation
object.static void
premain(java.lang.String agentArgs, java.lang.instrument.Instrumentation inst)
The entry point invoked when this agent is started by-javaagent
.static void
redefine(java.lang.Class<?>[] oldClasses, CtClass[] newClasses)
Redefines classes.static void
redefine(java.lang.Class<?> oldClass, CtClass newClass)
Redefines a class.
-
-
-
Method Detail
-
instrumentation
public java.lang.instrument.Instrumentation instrumentation()
Obtains theInstrumentation
object.- Returns:
- null when it is not available.
-
premain
public static void premain(java.lang.String agentArgs, java.lang.instrument.Instrumentation inst) throws java.lang.Throwable
The entry point invoked when this agent is started by-javaagent
.- Throws:
java.lang.Throwable
-
agentmain
public static void agentmain(java.lang.String agentArgs, java.lang.instrument.Instrumentation inst) throws java.lang.Throwable
The entry point invoked when this agent is started after the JVM starts.- Throws:
java.lang.Throwable
-
redefine
public static void redefine(java.lang.Class<?> oldClass, CtClass newClass) throws NotFoundException, java.io.IOException, CannotCompileException
Redefines a class.- Throws:
NotFoundException
java.io.IOException
CannotCompileException
-
redefine
public static void redefine(java.lang.Class<?>[] oldClasses, CtClass[] newClasses) throws NotFoundException, java.io.IOException, CannotCompileException
Redefines classes.- Throws:
NotFoundException
java.io.IOException
CannotCompileException
-
createAgentJarFile
public static java.io.File createAgentJarFile(java.lang.String fileName) throws java.io.IOException, CannotCompileException, NotFoundException
Creates an agent file for usingHotSwapAgent
.- Throws:
java.io.IOException
CannotCompileException
NotFoundException
-
-