Top Description Fields Constructors Methods
javassist.util

public Class HotSwapAgent

extends Object
Class Inheritance
Imports
java.io.File, .FileOutputStream, .IOException, java.lang.instrument.ClassDefinition, .Instrumentation, .UnmodifiableClassException, java.lang.management.ManagementFactory, java.util.jar.Attributes, .JarEntry, .JarOutputStream, .Manifest, com.sun.tools.attach.VirtualMachine, javassist.CannotCompileException, .ClassPool, .CtClass, .NotFoundException

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
redefine(Class, CtClass), redefine(Class[], CtClass[])

Field Summary

Modifier and TypeField and Description
private static Instrumentation

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static void
agentmain(String agentArgs, Instrumentation inst)

The entry point invoked when this agent is started after the JVM starts.

public static File
createAgentJarFile(String fileName)

Creates an agent file for using HotSwapAgent.

private static File
private static File
public Instrumentation

Returns:

null when it is not available.
instrumentation
()

Obtains the Instrumentation object.

public static void
premain(String agentArgs, Instrumentation inst)

The entry point invoked when this agent is started by -javaagent.

public static void
redefine(Class<?> oldClass, CtClass newClass)

Redefines a class.

public static void
redefine(Class<?>[] oldClasses, CtClass[] newClasses)

Redefines classes.

private static void
startAgent()

Ensures that the agent is ready.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

instrumentationback to summary
private static Instrumentation instrumentation

Constructor Detail

HotSwapAgentback to summary
public HotSwapAgent()

Method Detail

agentmainback to summary
public static void agentmain(String agentArgs, Instrumentation inst) throws Throwable

The entry point invoked when this agent is started after the JVM starts.

createAgentJarFileback to summary
public static File createAgentJarFile(String fileName) throws IOException, CannotCompileException, NotFoundException

Creates an agent file for using HotSwapAgent.

createJarFileback to summary
private static File createJarFile() throws IOException, CannotCompileException, NotFoundException
createJarFileback to summary
private static File createJarFile(File jar) throws IOException, CannotCompileException, NotFoundException
instrumentationback to summary
public Instrumentation instrumentation()

Obtains the Instrumentation object.

Returns:Instrumentation

null when it is not available.

premainback to summary
public static void premain(String agentArgs, Instrumentation inst) throws Throwable

The entry point invoked when this agent is started by -javaagent.

redefineback to summary
public static void redefine(Class<?> oldClass, CtClass newClass) throws NotFoundException, IOException, CannotCompileException

Redefines a class.

redefineback to summary
public static void redefine(Class<?>[] oldClasses, CtClass[] newClasses) throws NotFoundException, IOException, CannotCompileException

Redefines classes.

startAgentback to summary
private static void startAgent() throws NotFoundException

Ensures that the agent is ready. It attempts to dynamically start the agent if necessary.