Class HotSwapAgent
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:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidagentmain(String agentArgs, Instrumentation inst) The entry point invoked when this agent is started after the JVM starts.static FilecreateAgentJarFile(String fileName) Creates an agent file for usingHotSwapAgent.Obtains theInstrumentationobject.static voidpremain(String agentArgs, Instrumentation inst) The entry point invoked when this agent is started by-javaagent.static voidRedefines classes.static voidRedefines a class.
-
Constructor Details
-
HotSwapAgent
public HotSwapAgent()
-
-
Method Details
-
instrumentation
Obtains theInstrumentationobject.- Returns:
- null when it is not available.
-
premain
The entry point invoked when this agent is started by-javaagent.- Throws:
Throwable
-
agentmain
The entry point invoked when this agent is started after the JVM starts.- Throws:
Throwable
-
redefine
public static void redefine(Class<?> oldClass, CtClass newClass) throws NotFoundException, IOException, CannotCompileException Redefines a class. -
redefine
public static void redefine(Class<?>[] oldClasses, CtClass[] newClasses) throws NotFoundException, IOException, CannotCompileException Redefines classes. -
createAgentJarFile
public static File createAgentJarFile(String fileName) throws IOException, CannotCompileException, NotFoundException Creates an agent file for usingHotSwapAgent.
-