Interface AgentBuilder
-
- All Known Subinterfaces:
AgentBuilder.Identified.Extendable
,AgentBuilder.Ignored
,AgentBuilder.RedefinitionListenable
,AgentBuilder.RedefinitionListenable.WithImplicitDiscoveryStrategy
,AgentBuilder.RedefinitionListenable.WithoutBatchStrategy
- All Known Implementing Classes:
AgentBuilder.Default
,AgentBuilder.Default.Delegator
,AgentBuilder.Default.Ignoring
,AgentBuilder.Default.Redefining
,AgentBuilder.Default.Transforming
public interface AgentBuilder
An agent builder provides a convenience API for defining a Java agent. By default, this transformation is applied by rebasing the type if not specified otherwise by setting a
AgentBuilder.TypeStrategy
.When defining several
AgentBuilder.Transformer
s, the agent builder always applies the transformers that were supplied with the last applicable matcher. Therefore, more general transformers should be defined first.Note: Any transformation is performed using the
AccessControlContext
of an agent's creator.Important: Types that implement lambda expressions (functional interfaces) are not instrumented by default but only when enabling the builder's
AgentBuilder.LambdaInstrumentationStrategy
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
AgentBuilder.CircularityLock
A circularity lock is responsible for preventing that aClassFileLocator
is used recursively.static interface
AgentBuilder.ClassFileBufferStrategy
This strategy determines how the provided class file buffer is used.static class
AgentBuilder.Default
The default implementation of anAgentBuilder
.static interface
AgentBuilder.DescriptionStrategy
A description strategy is responsible for resolving aTypeDescription
when transforming or retransforming/-defining a type.static interface
AgentBuilder.FallbackStrategy
A fallback strategy allows to reattempt a transformation or a consideration for redefinition/retransformation in case an exception occurs.static interface
AgentBuilder.Identified
Describes anAgentBuilder
which was handed a matcher for identifying types to instrumented in order to supply one or severalAgentBuilder.Transformer
s.static interface
AgentBuilder.Ignored
Allows to further specify ignored types.static interface
AgentBuilder.InitializationStrategy
An initialization strategy which determines the handling ofLoadedTypeInitializer
s and the loading of auxiliary types.static interface
AgentBuilder.InjectionStrategy
A strategy for injecting auxiliary types into a class loader.static interface
AgentBuilder.InstallationListener
A listener that is notified during the installation and the resetting of a class file transformer.static class
AgentBuilder.LambdaInstrumentationStrategy
Implements the instrumentation of theLambdaMetafactory
if this feature is enabled.static interface
AgentBuilder.Listener
A listener that is informed about events that occur during an instrumentation process.static interface
AgentBuilder.LocationStrategy
A strategy for creating aClassFileLocator
when instrumenting a type.static interface
AgentBuilder.Matchable<T extends AgentBuilder.Matchable<T>>
An abstraction for extending a matcher.static interface
AgentBuilder.PoolStrategy
A type locator allows to specify howTypeDescription
s are resolved by anAgentBuilder
.static interface
AgentBuilder.RawMatcher
A matcher that allows to determine if aAgentBuilder.Transformer
should be applied during the execution of aClassFileTransformer
that was generated by anAgentBuilder
.static interface
AgentBuilder.RedefinitionListenable
An agent builder configuration that allows the registration of listeners to the redefinition process.static class
AgentBuilder.RedefinitionStrategy
A redefinition strategy regulates how already loaded classes are modified by a built agent.static interface
AgentBuilder.Transformer
A transformer allows to apply modifications to aDynamicType
.static interface
AgentBuilder.TransformerDecorator
A decorator that allows to change the class file transformer that is registered.static interface
AgentBuilder.TypeStrategy
A type strategy is responsible for creating a type builder for a type that is being instrumented.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description AgentBuilder
assureReadEdgeFromAndTo(java.lang.instrument.Instrumentation instrumentation, java.lang.Class<?>... type)
Assures that all modules of the supplied types are read by the module of any instrumented type and vice versa.AgentBuilder
assureReadEdgeFromAndTo(java.lang.instrument.Instrumentation instrumentation, java.util.Collection<? extends JavaModule> modules)
Assures that all supplied modules are read by the module of any instrumented type and vice versa.AgentBuilder
assureReadEdgeFromAndTo(java.lang.instrument.Instrumentation instrumentation, JavaModule... module)
Assures that all supplied modules are read by the module of any instrumented type and vice versa.AgentBuilder
assureReadEdgeTo(java.lang.instrument.Instrumentation instrumentation, java.lang.Class<?>... type)
Assures that all modules of the supplied types are read by the module of any instrumented type.AgentBuilder
assureReadEdgeTo(java.lang.instrument.Instrumentation instrumentation, java.util.Collection<? extends JavaModule> modules)
Assures that all supplied modules are read by the module of any instrumented type.AgentBuilder
assureReadEdgeTo(java.lang.instrument.Instrumentation instrumentation, JavaModule... module)
Assures that all supplied modules are read by the module of any instrumented type.AgentBuilder
disableClassFormatChanges()
Disables all implicit changes on a class file that Byte Buddy would apply for certain instrumentations.AgentBuilder
disableNativeMethodPrefix()
Disables the use of a native method prefix for instrumented methods.AgentBuilder
enableNativeMethodPrefix(java.lang.String prefix)
Enables the use of the given native method prefix for instrumented methods.AgentBuilder.Ignored
ignore(AgentBuilder.RawMatcher rawMatcher)
Excludes any type that is matched by the raw matcher provided to this method.AgentBuilder.Ignored
ignore(ElementMatcher<? super TypeDescription> typeMatcher)
Excludes any type that is matched by the provided matcher from instrumentation and considers types by allClassLoader
s.AgentBuilder.Ignored
ignore(ElementMatcher<? super TypeDescription> typeMatcher, ElementMatcher<? super java.lang.ClassLoader> classLoaderMatcher)
Excludes any type that is matched by the provided matcher and is loaded by a class loader matching the second matcher.AgentBuilder.Ignored
ignore(ElementMatcher<? super TypeDescription> typeMatcher, ElementMatcher<? super java.lang.ClassLoader> classLoaderMatcher, ElementMatcher<? super JavaModule> moduleMatcher)
Excludes any type that is matched by the provided matcher and is loaded by a class loader matching the second matcher.ResettableClassFileTransformer
installOn(java.lang.instrument.Instrumentation instrumentation)
Creates and installs aResettableClassFileTransformer
that implements the configuration of this agent builder with a givenInstrumentation
.ResettableClassFileTransformer
installOnByteBuddyAgent()
Creates and installs aResettableClassFileTransformer
that implements the configuration of this agent builder with the Byte Buddy-agent which must be installed prior to calling this method.java.lang.instrument.ClassFileTransformer
makeRaw()
Creates aResettableClassFileTransformer
that implements the configuration of this agent builder.ResettableClassFileTransformer
patchOn(java.lang.instrument.Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer)
Creates and installs aResettableClassFileTransformer
that implements the configuration of this agent builder with a givenInstrumentation
.ResettableClassFileTransformer
patchOnByteBuddyAgent(ResettableClassFileTransformer classFileTransformer)
Creates and installs aResettableClassFileTransformer
that implements the configuration of this agent builder with the Byte Buddy-agent which must be installed prior to calling this method.AgentBuilder.Identified.Narrowable
type(AgentBuilder.RawMatcher matcher)
Matches a type being loaded in order to apply the suppliedAgentBuilder.Transformer
s before loading this type.AgentBuilder.Identified.Narrowable
type(ElementMatcher<? super TypeDescription> typeMatcher)
Matches a type being loaded in order to apply the suppliedAgentBuilder.Transformer
s before loading this type.AgentBuilder.Identified.Narrowable
type(ElementMatcher<? super TypeDescription> typeMatcher, ElementMatcher<? super java.lang.ClassLoader> classLoaderMatcher)
Matches a type being loaded in order to apply the suppliedAgentBuilder.Transformer
s before loading this type.AgentBuilder.Identified.Narrowable
type(ElementMatcher<? super TypeDescription> typeMatcher, ElementMatcher<? super java.lang.ClassLoader> classLoaderMatcher, ElementMatcher<? super JavaModule> moduleMatcher)
Matches a type being loaded in order to apply the suppliedAgentBuilder.Transformer
s before loading this type.AgentBuilder
with(AgentBuilder.CircularityLock circularityLock)
Defines a circularity lock that is acquired upon executing code that potentially loads new classes.AgentBuilder
with(AgentBuilder.ClassFileBufferStrategy classFileBufferStrategy)
Specifies a class file buffer strategy that determines the use of the buffer supplied to a class file transformer.AgentBuilder
with(AgentBuilder.DescriptionStrategy descriptionStrategy)
Specifies a strategy to be used for resolvingTypeDescription
for any type handled by the created transformer.AgentBuilder
with(AgentBuilder.FallbackStrategy fallbackStrategy)
Specifies a fallback strategy to that this agent builder applies upon installing an agent and during class file transformation.AgentBuilder
with(AgentBuilder.InitializationStrategy initializationStrategy)
Defines a given initialization strategy to be applied to generated types.AgentBuilder
with(AgentBuilder.InjectionStrategy injectionStrategy)
Defines a strategy for injecting auxiliary types into the target class loader.AgentBuilder
with(AgentBuilder.InstallationListener installationListener)
Adds an installation listener that is notified during installation events.AgentBuilder
with(AgentBuilder.LambdaInstrumentationStrategy lambdaInstrumentationStrategy)
Enables or disables management of the JVM'sLambdaMetafactory
which is responsible for creating classes that implement lambda expressions.AgentBuilder
with(AgentBuilder.Listener listener)
Defines the givenAgentBuilder.Listener
to be notified by the created agent.AgentBuilder
with(AgentBuilder.LocationStrategy locationStrategy)
Defines the use of the given location strategy for locating binary data to given class names.AgentBuilder
with(AgentBuilder.PoolStrategy poolStrategy)
Defines the use of the given type locator for locating aTypeDescription
for an instrumented type.AgentBuilder.RedefinitionListenable.WithoutBatchStrategy
with(AgentBuilder.RedefinitionStrategy redefinitionStrategy)
Specifies a strategy for modifying types that were already loaded prior to the installation of this transformer.AgentBuilder
with(AgentBuilder.TransformerDecorator transformerDecorator)
Adds a decorator for the created class file transformer.AgentBuilder
with(AgentBuilder.TypeStrategy typeStrategy)
Defines how types should be transformed, e.g.AgentBuilder
with(ByteBuddy byteBuddy)
Defines the givenByteBuddy
instance to be used by the created agent.
-
-
-
Method Detail
-
with
AgentBuilder with(ByteBuddy byteBuddy)
Defines the givenByteBuddy
instance to be used by the created agent.- Parameters:
byteBuddy
- The Byte Buddy instance to be used.- Returns:
- A new instance of this agent builder which makes use of the given
byteBuddy
instance.
-
with
AgentBuilder with(AgentBuilder.Listener listener)
Defines the givenAgentBuilder.Listener
to be notified by the created agent. The given listener is notified after any other listener that is already registered. If a listener is registered twice, it is also notified twice.- Parameters:
listener
- The listener to be notified.- Returns:
- A new instance of this agent builder which creates an agent that informs the given listener about events.
-
with
AgentBuilder with(AgentBuilder.CircularityLock circularityLock)
Defines a circularity lock that is acquired upon executing code that potentially loads new classes. While the lock is acquired, any class file transformer refrains from transforming any classes. By default, all created agents use a sharedAgentBuilder.CircularityLock
to avoid that any classes that are required to execute an agent causes aClassCircularityError
.- Parameters:
circularityLock
- The circularity lock to use.- Returns:
- A new instance of this agent builder which creates an agent that uses the supplied circularity lock.
-
with
AgentBuilder with(AgentBuilder.PoolStrategy poolStrategy)
Defines the use of the given type locator for locating aTypeDescription
for an instrumented type.- Parameters:
poolStrategy
- The type locator to use.- Returns:
- A new instance of this agent builder which uses the given type locator for looking up class files.
-
with
AgentBuilder with(AgentBuilder.LocationStrategy locationStrategy)
Defines the use of the given location strategy for locating binary data to given class names.- Parameters:
locationStrategy
- The location strategy to use.- Returns:
- A new instance of this agent builder which uses the given location strategy for looking up class files.
-
with
AgentBuilder with(AgentBuilder.TypeStrategy typeStrategy)
Defines how types should be transformed, e.g. if they should be rebased or redefined by the created agent.- Parameters:
typeStrategy
- The type strategy to use.- Returns:
- A new instance of this agent builder which uses the given type strategy.
-
with
AgentBuilder with(AgentBuilder.InitializationStrategy initializationStrategy)
Defines a given initialization strategy to be applied to generated types. An initialization strategy is responsible for setting up a type after it was loaded. This initialization must be performed after the transformation because a Java agent is only invoked before loading a type. By default, the initialization logic is added to a class's type initializer which queries a global object for any objects that are to be injected into the generated type.- Parameters:
initializationStrategy
- The initialization strategy to use.- Returns:
- A new instance of this agent builder that applies the given initialization strategy.
-
with
AgentBuilder.RedefinitionListenable.WithoutBatchStrategy with(AgentBuilder.RedefinitionStrategy redefinitionStrategy)
Specifies a strategy for modifying types that were already loaded prior to the installation of this transformer.
Note: Defining a redefinition strategy resets any refinements of a previously set redefinition strategy.
Important: Most JVMs do not support changes of a class's structure after a class was already loaded. Therefore, it is typically required that this class file transformer was built while enabling
disableClassFormatChanges()
.- Parameters:
redefinitionStrategy
- The redefinition strategy to apply.- Returns:
- A new instance of this agent builder that applies the given redefinition strategy.
-
with
AgentBuilder with(AgentBuilder.LambdaInstrumentationStrategy lambdaInstrumentationStrategy)
Enables or disables management of the JVM's
LambdaMetafactory
which is responsible for creating classes that implement lambda expressions. Without this feature enabled, classes that are represented by lambda expressions are not instrumented by the JVM such that Java agents have no effect on them when a lambda expression's class is loaded for the first time.When activating this feature, Byte Buddy instruments the
LambdaMetafactory
and takes over the responsibility of creating classes that represent lambda expressions. In doing so, Byte Buddy has the opportunity to apply the built class file transformer. If the current VM does not support lambda expressions, activating this feature has no effect.Important: If this feature is active, it is important to release the built class file transformer when deactivating it. Normally, it is sufficient to call
Instrumentation.removeTransformer(ClassFileTransformer)
. When this feature is enabled, it is however also required to invokeAgentBuilder.LambdaInstrumentationStrategy.release(ClassFileTransformer, Instrumentation)
. Otherwise, the executing VMs class loader retains a reference to the class file transformer what can cause a memory leak.- Parameters:
lambdaInstrumentationStrategy
-true
if this feature should be enabled.- Returns:
- A new instance of this agent builder where this feature is explicitly enabled or disabled.
-
with
AgentBuilder with(AgentBuilder.DescriptionStrategy descriptionStrategy)
Specifies a strategy to be used for resolvingTypeDescription
for any type handled by the created transformer.- Parameters:
descriptionStrategy
- The description strategy to use.- Returns:
- A new instance of this agent builder that applies the given description strategy.
-
with
AgentBuilder with(AgentBuilder.FallbackStrategy fallbackStrategy)
Specifies a fallback strategy to that this agent builder applies upon installing an agent and during class file transformation.- Parameters:
fallbackStrategy
- The fallback strategy to be used.- Returns:
- A new agent builder that applies the supplied fallback strategy.
-
with
AgentBuilder with(AgentBuilder.ClassFileBufferStrategy classFileBufferStrategy)
Specifies a class file buffer strategy that determines the use of the buffer supplied to a class file transformer.- Parameters:
classFileBufferStrategy
- The class file buffer strategy to use.- Returns:
- A new agent builder that applies the supplied class file buffer strategy.
-
with
AgentBuilder with(AgentBuilder.InstallationListener installationListener)
Adds an installation listener that is notified during installation events. Installation listeners are only invoked if a class file transformer is installed using this agent builder's installation methods and uninstalled via the createdResettableClassFileTransformer
'sreset
methods.- Parameters:
installationListener
- The installation listener to register.- Returns:
- A new agent builder that applies the supplied installation listener.
-
with
AgentBuilder with(AgentBuilder.InjectionStrategy injectionStrategy)
Defines a strategy for injecting auxiliary types into the target class loader.- Parameters:
injectionStrategy
- The injection strategy to use.- Returns:
- A new agent builder with the supplied injection strategy configured.
-
with
AgentBuilder with(AgentBuilder.TransformerDecorator transformerDecorator)
Adds a decorator for the created class file transformer.- Parameters:
transformerDecorator
- A decorator to wrap the created class file transformer.- Returns:
- A new agent builder that applies the supplied transformer decorator.
-
enableNativeMethodPrefix
AgentBuilder enableNativeMethodPrefix(java.lang.String prefix)
Enables the use of the given native method prefix for instrumented methods. Note that this prefix is also applied when preserving non-native methods. The use of this prefix is also registered when installing the final agent with anInstrumentation
.- Parameters:
prefix
- The prefix to be used.- Returns:
- A new instance of this agent builder which uses the given native method prefix.
-
disableNativeMethodPrefix
AgentBuilder disableNativeMethodPrefix()
Disables the use of a native method prefix for instrumented methods.- Returns:
- A new instance of this agent builder which does not use a native method prefix.
-
disableClassFormatChanges
AgentBuilder disableClassFormatChanges()
Disables all implicit changes on a class file that Byte Buddy would apply for certain instrumentations. When using this option, it is no longer possible to rebase a method, i.e. intercepted methods are fully replaced. Furthermore, it is no longer possible to implicitly apply loaded type initializers for explicitly initializing the generated type.
This is equivalent to setting
AgentBuilder.InitializationStrategy.NoOp
andAgentBuilder.TypeStrategy.Default.REDEFINE_FROZEN
(unless it is configured asAgentBuilder.TypeStrategy.Default.DECORATE
where this strategy is retained) as well as configuring the underlyingByteBuddy
instance to use aImplementation.Context.Disabled
. Using this strategy also configures Byte Buddy to create frozen instrumented types and discards any explicit configuration.- Returns:
- A new instance of this agent builder that does not apply any implicit changes to the received class file.
-
assureReadEdgeTo
AgentBuilder assureReadEdgeTo(java.lang.instrument.Instrumentation instrumentation, java.lang.Class<?>... type)
Assures that all modules of the supplied types are read by the module of any instrumented type. If the current VM does not support the Java module system, calling this method has no effect and this instance is returned.- Parameters:
instrumentation
- The instrumentation instance that is used for adding a module read-dependency.type
- The types for which to assure their module-visibility from any instrumented class.- Returns:
- A new instance of this agent builder that assures the supplied types module visibility.
- See Also:
AgentBuilder.Listener.ModuleReadEdgeCompleting
-
assureReadEdgeTo
AgentBuilder assureReadEdgeTo(java.lang.instrument.Instrumentation instrumentation, JavaModule... module)
Assures that all supplied modules are read by the module of any instrumented type.- Parameters:
instrumentation
- The instrumentation instance that is used for adding a module read-dependency.module
- The modules for which to assure their module-visibility from any instrumented class.- Returns:
- A new instance of this agent builder that assures the supplied types module visibility.
- See Also:
AgentBuilder.Listener.ModuleReadEdgeCompleting
-
assureReadEdgeTo
AgentBuilder assureReadEdgeTo(java.lang.instrument.Instrumentation instrumentation, java.util.Collection<? extends JavaModule> modules)
Assures that all supplied modules are read by the module of any instrumented type.- Parameters:
instrumentation
- The instrumentation instance that is used for adding a module read-dependency.modules
- The modules for which to assure their module-visibility from any instrumented class.- Returns:
- A new instance of this agent builder that assures the supplied types module visibility.
- See Also:
AgentBuilder.Listener.ModuleReadEdgeCompleting
-
assureReadEdgeFromAndTo
AgentBuilder assureReadEdgeFromAndTo(java.lang.instrument.Instrumentation instrumentation, java.lang.Class<?>... type)
Assures that all modules of the supplied types are read by the module of any instrumented type and vice versa. If the current VM does not support the Java module system, calling this method has no effect and this instance is returned. Setting this option will also ensure that the instrumented type's package is opened to the target module, if applicable.- Parameters:
instrumentation
- The instrumentation instance that is used for adding a module read-dependency.type
- The types for which to assure their module-visibility from and to any instrumented class.- Returns:
- A new instance of this agent builder that assures the supplied types module visibility.
- See Also:
AgentBuilder.Listener.ModuleReadEdgeCompleting
-
assureReadEdgeFromAndTo
AgentBuilder assureReadEdgeFromAndTo(java.lang.instrument.Instrumentation instrumentation, JavaModule... module)
Assures that all supplied modules are read by the module of any instrumented type and vice versa. Setting this option will also ensure that the instrumented type's package is opened to the target module.- Parameters:
instrumentation
- The instrumentation instance that is used for adding a module read-dependency.module
- The modules for which to assure their module-visibility from and to any instrumented class.- Returns:
- A new instance of this agent builder that assures the supplied types module visibility.
- See Also:
AgentBuilder.Listener.ModuleReadEdgeCompleting
-
assureReadEdgeFromAndTo
AgentBuilder assureReadEdgeFromAndTo(java.lang.instrument.Instrumentation instrumentation, java.util.Collection<? extends JavaModule> modules)
Assures that all supplied modules are read by the module of any instrumented type and vice versa. Setting this option will also ensure that the instrumented type's package is opened to the target module.- Parameters:
instrumentation
- The instrumentation instance that is used for adding a module read-dependency.modules
- The modules for which to assure their module-visibility from and to any instrumented class.- Returns:
- A new instance of this agent builder that assures the supplied types module visibility.
- See Also:
AgentBuilder.Listener.ModuleReadEdgeCompleting
-
type
AgentBuilder.Identified.Narrowable type(ElementMatcher<? super TypeDescription> typeMatcher)
Matches a type being loaded in order to apply the supplied
AgentBuilder.Transformer
s before loading this type. If several matchers positively match a type only the latest registered matcher is considered for transformation.If this matcher is chained with additional subsequent matchers, any matcher is executed in registration order with matchers that were registered first being executed first. Doing so, later transformations can override transformations that are applied by this matcher. To avoid this, it is possible to register this transformation as terminal via
AgentBuilder.Identified.Extendable.asTerminalTransformation()
where no subsequent matchers are applied if this matcher matched a given type.Note: When applying a matcher, regard the performance implications by
ignore(ElementMatcher)
. The former matcher is applied first such that it makes sense to ignore name spaces that are irrelevant to instrumentation. If possible, it is also recommended, to exclude class loaders such as for example the bootstrap class loader by usingtype(ElementMatcher, ElementMatcher)
instead.- Parameters:
typeMatcher
- AnElementMatcher
that is applied on the type being loaded that decides if the entailedAgentBuilder.Transformer
s should be applied for that type.- Returns:
- A definable that represents this agent builder which allows for the definition of one or several
AgentBuilder.Transformer
s to be applied when the giventypeMatcher
indicates a match.
-
type
AgentBuilder.Identified.Narrowable type(ElementMatcher<? super TypeDescription> typeMatcher, ElementMatcher<? super java.lang.ClassLoader> classLoaderMatcher)
Matches a type being loaded in order to apply the supplied
AgentBuilder.Transformer
s before loading this type. If several matchers positively match a type only the latest registered matcher is considered for transformation.If this matcher is chained with additional subsequent matchers, any matcher is executed in registration order with matchers that were registered first being executed first. Doing so, later transformations can override transformations that are applied by this matcher. To avoid this, it is possible to register this transformation as terminal via
AgentBuilder.Identified.Extendable.asTerminalTransformation()
where no subsequent matchers are applied if this matcher matched a given type.Note: When applying a matcher, regard the performance implications by
ignore(ElementMatcher)
. The former matcher is applied first such that it makes sense to ignore name spaces that are irrelevant to instrumentation. If possible, it is also recommended, to exclude class loaders such as for example the bootstrap class loader.- Parameters:
typeMatcher
- AnElementMatcher
that is applied on the type being loaded that decides if the entailedAgentBuilder.Transformer
s should be applied for that type.classLoaderMatcher
- AnElementMatcher
that is applied to theClassLoader
that is loading the type being loaded. This matcher is always applied first where the type matcher is not applied in case that this matcher does not indicate a match.- Returns:
- A definable that represents this agent builder which allows for the definition of one or several
AgentBuilder.Transformer
s to be applied when both the giventypeMatcher
andclassLoaderMatcher
indicate a match.
-
type
AgentBuilder.Identified.Narrowable type(ElementMatcher<? super TypeDescription> typeMatcher, ElementMatcher<? super java.lang.ClassLoader> classLoaderMatcher, ElementMatcher<? super JavaModule> moduleMatcher)
Matches a type being loaded in order to apply the supplied
AgentBuilder.Transformer
s before loading this type. If several matchers positively match a type only the latest registered matcher is considered for transformation.If this matcher is chained with additional subsequent matchers, any matcher is executed in registration order with matchers that were registered first being executed first. Doing so, later transformations can override transformations that are applied by this matcher. To avoid this, it is possible to register this transformation as terminal via
AgentBuilder.Identified.Extendable.asTerminalTransformation()
where no subsequent matchers are applied if this matcher matched a given type.Note: When applying a matcher, regard the performance implications by
ignore(ElementMatcher)
. The former matcher is applied first such that it makes sense to ignore name spaces that are irrelevant to instrumentation. If possible, it is also recommended, to exclude class loaders such as for example the bootstrap class loader.- Parameters:
typeMatcher
- AnElementMatcher
that is applied on the type being loaded that decides if the entailedAgentBuilder.Transformer
s should be applied for that type.classLoaderMatcher
- AnElementMatcher
that is applied to theClassLoader
that is loading the type being loaded. This matcher is always applied second where the type matcher is not applied in case that this matcher does not indicate a match.moduleMatcher
- AnElementMatcher
that is applied to theJavaModule
of the type being loaded. This matcher is always applied first where the class loader and type matchers are not applied in case that this matcher does not indicate a match. On a JVM that does not support the Java modules system, this matcher is not applied.- Returns:
- A definable that represents this agent builder which allows for the definition of one or several
AgentBuilder.Transformer
s to be applied when both the giventypeMatcher
andclassLoaderMatcher
indicate a match.
-
type
AgentBuilder.Identified.Narrowable type(AgentBuilder.RawMatcher matcher)
Matches a type being loaded in order to apply the supplied
AgentBuilder.Transformer
s before loading this type. If several matchers positively match a type only the latest registered matcher is considered for transformation.If this matcher is chained with additional subsequent matchers, any matcher is executed in registration order with matchers that were registered first being executed first. Doing so, later transformations can override transformations that are applied by this matcher. To avoid this, it is possible to register this transformation as terminal via
AgentBuilder.Identified.Extendable.asTerminalTransformation()
where no subsequent matchers are applied if this matcher matched a given type.Note: When applying a matcher, regard the performance implications by
ignore(ElementMatcher)
. The former matcher is applied first such that it makes sense to ignore name spaces that are irrelevant to instrumentation. If possible, it is also recommended, to exclude class loaders such as for example the bootstrap class loader.- Parameters:
matcher
- A matcher that decides if the entailedAgentBuilder.Transformer
s should be applied for a type that is being loaded.- Returns:
- A definable that represents this agent builder which allows for the definition of one or several
AgentBuilder.Transformer
s to be applied when the givenmatcher
indicates a match.
-
ignore
AgentBuilder.Ignored ignore(ElementMatcher<? super TypeDescription> typeMatcher)
Excludes any type that is matched by the provided matcher from instrumentation and considers types by all
ClassLoader
s. By default, Byte Buddy does not instrument synthetic types or types that are loaded by the bootstrap class loader.When ignoring a type, any subsequently chained matcher is applied after this matcher in the order of their registration. Also, if any matcher indicates that a type is to be ignored, none of the following chained matchers is executed.
Note: For performance reasons, it is recommended to always include a matcher that excludes as many namespaces as possible. Byte Buddy can determine a type's name without parsing its class file and can therefore discard such types with minimal overhead. When a different property of a type - such as for example its modifiers or its annotations is accessed - Byte Buddy parses the class file lazily in order to allow for such a matching. Therefore, any exclusion of a name should always be done as a first step and even if it does not influence the selection of what types are matched. Without changing this property, the class file of every type is being parsed!
Warning: If a type is loaded during the instrumentation of the same type, this causes the original call site that loads the type to remain unbound, causing a
LinkageError
. It is therefore important to not instrument types that may be loaded during the application of aAgentBuilder.Transformer
. For this reason, it is not recommended to instrument classes of the bootstrap class loader that Byte Buddy might require for instrumenting a class or to instrument any of Byte Buddy's classes. If such instrumentation is desired, it is important to assert for each class that they are not loaded during instrumentation.- Parameters:
typeMatcher
- A matcher that identifies types that should not be instrumented.- Returns:
- A new instance of this agent builder that ignores all types that are matched by the provided matcher. All previous matchers for ignored types are discarded.
-
ignore
AgentBuilder.Ignored ignore(ElementMatcher<? super TypeDescription> typeMatcher, ElementMatcher<? super java.lang.ClassLoader> classLoaderMatcher)
Excludes any type that is matched by the provided matcher and is loaded by a class loader matching the second matcher. By default, Byte Buddy does not instrument synthetic types, types within a
net.bytebuddy.*
package or types that are loaded by the bootstrap class loader.When ignoring a type, any subsequently chained matcher is applied after this matcher in the order of their registration. Also, if any matcher indicates that a type is to be ignored, none of the following chained matchers is executed.
Note: For performance reasons, it is recommended to always include a matcher that excludes as many namespaces as possible. Byte Buddy can determine a type's name without parsing its class file and can therefore discard such types with minimal overhead. When a different property of a type - such as for example its modifiers or its annotations is accessed - Byte Buddy parses the class file lazily in order to allow for such a matching. Therefore, any exclusion of a name should always be done as a first step and even if it does not influence the selection of what types are matched. Without changing this property, the class file of every type is being parsed!
Warning: If a type is loaded during the instrumentation of the same type, this causes the original call site that loads the type to remain unbound, causing a
LinkageError
. It is therefore important to not instrument types that may be loaded during the application of aAgentBuilder.Transformer
. For this reason, it is not recommended to instrument classes of the bootstrap class loader that Byte Buddy might require for instrumenting a class or to instrument any of Byte Buddy's classes. If such instrumentation is desired, it is important to assert for each class that they are not loaded during instrumentation.- Parameters:
typeMatcher
- A matcher that identifies types that should not be instrumented.classLoaderMatcher
- A matcher that identifies a class loader that identifies classes that should not be instrumented.- Returns:
- A new instance of this agent builder that ignores all types that are matched by the provided matcher. All previous matchers for ignored types are discarded.
-
ignore
AgentBuilder.Ignored ignore(ElementMatcher<? super TypeDescription> typeMatcher, ElementMatcher<? super java.lang.ClassLoader> classLoaderMatcher, ElementMatcher<? super JavaModule> moduleMatcher)
Excludes any type that is matched by the provided matcher and is loaded by a class loader matching the second matcher. By default, Byte Buddy does not instrument synthetic types, types within a
net.bytebuddy.*
package or types that are loaded by the bootstrap class loader.When ignoring a type, any subsequently chained matcher is applied after this matcher in the order of their registration. Also, if any matcher indicates that a type is to be ignored, none of the following chained matchers is executed.
Note: For performance reasons, it is recommended to always include a matcher that excludes as many namespaces as possible. Byte Buddy can determine a type's name without parsing its class file and can therefore discard such types with minimal overhead. When a different property of a type - such as for example its modifiers or its annotations is accessed - Byte Buddy parses the class file lazily in order to allow for such a matching. Therefore, any exclusion of a name should always be done as a first step and even if it does not influence the selection of what types are matched. Without changing this property, the class file of every type is being parsed!
Warning: If a type is loaded during the instrumentation of the same type, this causes the original call site that loads the type to remain unbound, causing a
LinkageError
. It is therefore important to not instrument types that may be loaded during the application of aAgentBuilder.Transformer
. For this reason, it is not recommended to instrument classes of the bootstrap class loader that Byte Buddy might require for instrumenting a class or to instrument any of Byte Buddy's classes. If such instrumentation is desired, it is important to assert for each class that they are not loaded during instrumentation.- Parameters:
typeMatcher
- A matcher that identifies types that should not be instrumented.classLoaderMatcher
- A matcher that identifies a class loader that identifies classes that should not be instrumented.moduleMatcher
- A matcher that identifies a module that identifies classes that should not be instrumented. On a JVM that does not support the Java modules system, this matcher is not applied.- Returns:
- A new instance of this agent builder that ignores all types that are matched by the provided matcher. All previous matchers for ignored types are discarded.
-
ignore
AgentBuilder.Ignored ignore(AgentBuilder.RawMatcher rawMatcher)
Excludes any type that is matched by the raw matcher provided to this method. By default, Byte Buddy does not instrument synthetic types, types within a
net.bytebuddy.*
package or types that are loaded by the bootstrap class loader.When ignoring a type, any subsequently chained matcher is applied after this matcher in the order of their registration. Also, if any matcher indicates that a type is to be ignored, none of the following chained matchers is executed.
Note: For performance reasons, it is recommended to always include a matcher that excludes as many namespaces as possible. Byte Buddy can determine a type's name without parsing its class file and can therefore discard such types with minimal overhead. When a different property of a type - such as for example its modifiers or its annotations is accessed - Byte Buddy parses the class file lazily in order to allow for such a matching. Therefore, any exclusion of a name should always be done as a first step and even if it does not influence the selection of what types are matched. Without changing this property, the class file of every type is being parsed!
Warning: If a type is loaded during the instrumentation of the same type, this causes the original call site that loads the type to remain unbound, causing a
LinkageError
. It is therefore important to not instrument types that may be loaded during the application of aAgentBuilder.Transformer
. For this reason, it is not recommended to instrument classes of the bootstrap class loader that Byte Buddy might require for instrumenting a class or to instrument any of Byte Buddy's classes. If such instrumentation is desired, it is important to assert for each class that they are not loaded during instrumentation.- Parameters:
rawMatcher
- A raw matcher that identifies types that should not be instrumented.- Returns:
- A new instance of this agent builder that ignores all types that are matched by the provided matcher. All previous matchers for ignored types are discarded.
-
makeRaw
java.lang.instrument.ClassFileTransformer makeRaw()
Creates aResettableClassFileTransformer
that implements the configuration of this agent builder. When using a raw class file transformer, theAgentBuilder.InstallationListener
callbacks are not invoked and the setAgentBuilder.RedefinitionStrategy
is not applied onto currently loaded classes.- Returns:
- A class file transformer that implements the configuration of this agent builder.
-
installOn
ResettableClassFileTransformer installOn(java.lang.instrument.Instrumentation instrumentation)
Creates and installs a
ResettableClassFileTransformer
that implements the configuration of this agent builder with a givenInstrumentation
. If retransformation is enabled, the installation also causes all loaded types to be retransformed.In order to assure the correct handling of the
AgentBuilder.InstallationListener
, an uninstallation should be applied via theResettableClassFileTransformer
'sreset
methods.- Parameters:
instrumentation
- The instrumentation on which this agent builder's configuration is to be installed.- Returns:
- The installed class file transformer.
-
installOnByteBuddyAgent
ResettableClassFileTransformer installOnByteBuddyAgent()
Creates and installs a
ResettableClassFileTransformer
that implements the configuration of this agent builder with the Byte Buddy-agent which must be installed prior to calling this method. If retransformation is enabled, the installation also causes all loaded types to be retransformed.In order to assure the correct handling of the
AgentBuilder.InstallationListener
, an uninstallation should be applied via theResettableClassFileTransformer
'sreset
methods.- Returns:
- The installed class file transformer.
- See Also:
installOn(Instrumentation)
-
patchOn
ResettableClassFileTransformer patchOn(java.lang.instrument.Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer)
Creates and installs a
ResettableClassFileTransformer
that implements the configuration of this agent builder with a givenInstrumentation
. If retransformation is enabled, the installation also causes all loaded types to be retransformed which have changed compared to the previous class file transformer that is provided as an argument.In order to assure the correct handling of the
AgentBuilder.InstallationListener
, an uninstallation should be applied via theResettableClassFileTransformer
'sreset
methods.- Parameters:
instrumentation
- The instrumentation on which this agent builder's configuration is to be installed.classFileTransformer
- The class file transformer that is being patched.- Returns:
- The installed class file transformer.
-
patchOnByteBuddyAgent
ResettableClassFileTransformer patchOnByteBuddyAgent(ResettableClassFileTransformer classFileTransformer)
Creates and installs a
ResettableClassFileTransformer
that implements the configuration of this agent builder with the Byte Buddy-agent which must be installed prior to calling this method. If retransformation is enabled, the installation also causes all loaded types to be retransformed which have changed compared to the previous class file transformer that is provided as an argument.In order to assure the correct handling of the
AgentBuilder.InstallationListener
, an uninstallation should be applied via theResettableClassFileTransformer
'sreset
methods.- Parameters:
classFileTransformer
- The class file transformer that is being patched.- Returns:
- The installed class file transformer.
- See Also:
patchOn(Instrumentation, ResettableClassFileTransformer)
-
-