public class ByteBuddyAgent extends Object
The Byte Buddy agent provides a JVM Instrumentation
in order to allow Byte Buddy the
redefinition of already loaded classes. An agent must normally be specified via the command line via the
javaagent
parameter. As an argument to this parameter, one must specify the location of this agent's jar
file such as for example in
java -javaagent:byte-buddy-agent.jar -jar app.jar
Note: The runtime installation of a Java agent is not possible on all JVMs. See the documentation for
install()
for details on JVMs that are supported out of the box.
Important: This class's name is known to the Byte Buddy main application and must not be altered.
Modifier and Type | Class and Description |
---|---|
protected static interface |
ByteBuddyAgent.AgentProvider
An agent provider is responsible for handling and providing the jar file of an agent that is being attached.
|
static interface |
ByteBuddyAgent.AttachmentProvider
An attachment provider is responsible for making the Java attachment API available.
|
static interface |
ByteBuddyAgent.ProcessProvider
A process provider is responsible for providing the process id of the current VM.
|
Modifier and Type | Method and Description |
---|---|
static void |
attach(File agentJar,
String processId)
Attaches the given agent Jar on the target process which must be a virtual machine process.
|
static void |
attach(File agentJar,
String processId,
ByteBuddyAgent.AttachmentProvider attachmentProvider)
Attaches the given agent Jar on the target process which must be a virtual machine process.
|
static Instrumentation |
getInstrumentation()
Looks up the
Instrumentation instance of an installed Byte Buddy agent. |
static Instrumentation |
install()
Installs an agent on the currently running Java virtual machine.
|
static Instrumentation |
install(ByteBuddyAgent.AttachmentProvider attachmentProvider)
Installs a Java agent using the Java attach API.
|
static Instrumentation |
install(ByteBuddyAgent.AttachmentProvider attachmentProvider,
ByteBuddyAgent.ProcessProvider processProvider)
Installs a Java agent using the Java attach API.
|
static Instrumentation |
install(ByteBuddyAgent.ProcessProvider processProvider)
Installs a Java agent using the Java attach API.
|
public static Instrumentation getInstrumentation()
Looks up the Instrumentation
instance of an installed Byte Buddy agent. Note that
this method implies reflective lookup and reflective invocation such that the returned value should be cached
rather than calling this method several times.
Note: This method throws an IllegalStateException
If the Byte Buddy agent is not
properly installed.
Instrumentation
instance which is provided by an installed
Byte Buddy agent.public static void attach(File agentJar, String processId)
IllegalStateException
is thrown.agentJar
- The agent jar file.processId
- The target process id.public static void attach(File agentJar, String processId, ByteBuddyAgent.AttachmentProvider attachmentProvider)
agentJar
- The agent jar file.processId
- The target process id.attachmentProvider
- The attachment provider to use.public static Instrumentation install()
Installs an agent on the currently running Java virtual machine. Unfortunately, this does not always work. The runtime installation of a Java agent is supported for:
If an agent cannot be installed, an IllegalStateException
is thrown.
Important: This is a rather computation-heavy operation. Therefore, this operation is
not repeated after an agent was successfully installed for the first time. Instead, the previous
instrumentation instance is returned. However, invoking this method requires synchronization
such that subsequently to an installation, getInstrumentation()
should
be invoked instead.
public static Instrumentation install(ByteBuddyAgent.AttachmentProvider attachmentProvider)
IllegalStateException
is thrown. The same happens if the default process provider
cannot resolve a process id for the current VM.attachmentProvider
- The attachment provider to use for the installation.public static Instrumentation install(ByteBuddyAgent.ProcessProvider processProvider)
IllegalStateException
is thrown. The same happens if the default attachment
provider cannot be used.processProvider
- The provider for the current JVM's process id.public static Instrumentation install(ByteBuddyAgent.AttachmentProvider attachmentProvider, ByteBuddyAgent.ProcessProvider processProvider)
IllegalStateException
is thrown.attachmentProvider
- The attachment provider to use for the installation.processProvider
- The provider for the current JVM's process id.Copyright © 2014–2016. All rights reserved.