Package net.bytebuddy.agent.builder
Class AgentBuilder.Transformer.ForAdvice
- java.lang.Object
-
- net.bytebuddy.agent.builder.AgentBuilder.Transformer.ForAdvice
-
- All Implemented Interfaces:
AgentBuilder.Transformer
- Enclosing interface:
- AgentBuilder.Transformer
@Enhance public static class AgentBuilder.Transformer.ForAdvice extends java.lang.Object implements AgentBuilder.Transformer
A transformer for applying anAdvice
where this advice class might reference types of both the agent's and the user's class loader. Using this transformer, it is possible to apply advice without including any library dependencies of this advice class which are then rather looked up from the transformed class's class loader. For this to work, it is required to register the advice class's class loader manually via theinclude
methods and to reference the advice class by its fully-qualified name. The advice class is then never loaded by rather described by aTypePool
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
AgentBuilder.Transformer.ForAdvice.Entry
An entry for an advice to apply.-
Nested classes/interfaces inherited from interface net.bytebuddy.agent.builder.AgentBuilder.Transformer
AgentBuilder.Transformer.ForAdvice, AgentBuilder.Transformer.ForBuildPlugin
-
-
Field Summary
Fields Modifier and Type Field Description private Advice.WithCustomMapping
advice
The advice to use.private Assigner
assigner
The assigner to use for the advice.private ClassFileLocator
classFileLocator
The class file locator to query for the advice class.private java.util.List<AgentBuilder.Transformer.ForAdvice.Entry>
entries
The advice entries to apply.private Advice.ExceptionHandler
exceptionHandler
The exception handler to register for the advice.private AgentBuilder.LocationStrategy
locationStrategy
The location strategy to use for class loaders when resolving advice classes.private AgentBuilder.PoolStrategy
poolStrategy
The pool strategy to use for looking up an advice.
-
Constructor Summary
Constructors Modifier Constructor Description ForAdvice()
Creates a new advice transformer with a default setup.ForAdvice(Advice.WithCustomMapping advice)
Creates a new advice transformer which applies the given advice.protected
ForAdvice(Advice.WithCustomMapping advice, Advice.ExceptionHandler exceptionHandler, Assigner assigner, ClassFileLocator classFileLocator, AgentBuilder.PoolStrategy poolStrategy, AgentBuilder.LocationStrategy locationStrategy, java.util.List<AgentBuilder.Transformer.ForAdvice.Entry> entries)
Creates a new advice transformer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AgentBuilder.Transformer.ForAdvice
advice(ElementMatcher<? super MethodDescription> matcher, java.lang.String name)
Applies the given advice class onto all methods that satisfy the supplied matcher.AgentBuilder.Transformer.ForAdvice
advice(ElementMatcher<? super MethodDescription> matcher, java.lang.String enter, java.lang.String exit)
Applies the given advice class onto all methods that satisfy the supplied matcher.AgentBuilder.Transformer.ForAdvice
advice(LatentMatcher<? super MethodDescription> matcher, java.lang.String name)
Applies the given advice class onto all methods that satisfy the supplied matcher.AgentBuilder.Transformer.ForAdvice
advice(LatentMatcher<? super MethodDescription> matcher, java.lang.String enter, java.lang.String exit)
Applies the given advice class onto all methods that satisfy the supplied matcher.AgentBuilder.Transformer.ForAdvice
include(java.lang.ClassLoader... classLoader)
Includes the supplied class loaders as a source for looking up an advice class or its dependencies.AgentBuilder.Transformer.ForAdvice
include(java.util.List<? extends ClassFileLocator> classFileLocators)
Includes the supplied class file locators as a source for looking up an advice class or its dependencies.AgentBuilder.Transformer.ForAdvice
include(ClassFileLocator... classFileLocator)
Includes the supplied class file locators as a source for looking up an advice class or its dependencies.DynamicType.Builder<?>
transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, java.lang.ClassLoader classLoader, JavaModule module)
Allows for a transformation of aDynamicType.Builder
.AgentBuilder.Transformer.ForAdvice
with(AgentBuilder.LocationStrategy locationStrategy)
Registers a location strategy for creating aClassFileLocator
from the class loader that is supplied during transformation that should be used for looking up advice-relevant classes.AgentBuilder.Transformer.ForAdvice
with(AgentBuilder.PoolStrategy poolStrategy)
Registers a pool strategy for creating aTypePool
that should be used for creating the advice class.AgentBuilder.Transformer.ForAdvice
with(Assigner assigner)
Registers an assigner to be used by the advice class.AgentBuilder.Transformer.ForAdvice
withExceptionHandler(Advice.ExceptionHandler exceptionHandler)
Registers an exception handler for suppressed exceptions to use by the registered advice.
-
-
-
Field Detail
-
advice
private final Advice.WithCustomMapping advice
The advice to use.
-
exceptionHandler
private final Advice.ExceptionHandler exceptionHandler
The exception handler to register for the advice.
-
assigner
private final Assigner assigner
The assigner to use for the advice.
-
classFileLocator
private final ClassFileLocator classFileLocator
The class file locator to query for the advice class.
-
poolStrategy
private final AgentBuilder.PoolStrategy poolStrategy
The pool strategy to use for looking up an advice.
-
locationStrategy
private final AgentBuilder.LocationStrategy locationStrategy
The location strategy to use for class loaders when resolving advice classes.
-
entries
private final java.util.List<AgentBuilder.Transformer.ForAdvice.Entry> entries
The advice entries to apply.
-
-
Constructor Detail
-
ForAdvice
public ForAdvice()
Creates a new advice transformer with a default setup.
-
ForAdvice
public ForAdvice(Advice.WithCustomMapping advice)
Creates a new advice transformer which applies the given advice.- Parameters:
advice
- The configured advice to use.
-
ForAdvice
protected ForAdvice(Advice.WithCustomMapping advice, Advice.ExceptionHandler exceptionHandler, Assigner assigner, ClassFileLocator classFileLocator, AgentBuilder.PoolStrategy poolStrategy, AgentBuilder.LocationStrategy locationStrategy, java.util.List<AgentBuilder.Transformer.ForAdvice.Entry> entries)
Creates a new advice transformer.- Parameters:
advice
- The configured advice to use.exceptionHandler
- The exception handler to use.assigner
- The assigner to use.classFileLocator
- The class file locator to use.poolStrategy
- The pool strategy to use for looking up an advice.locationStrategy
- The location strategy to use for class loaders when resolving advice classes.entries
- The advice entries to apply.
-
-
Method Detail
-
transform
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, java.lang.ClassLoader classLoader, JavaModule module)
Allows for a transformation of aDynamicType.Builder
.- Specified by:
transform
in interfaceAgentBuilder.Transformer
- Parameters:
builder
- The dynamic builder to transform.typeDescription
- The description of the type currently being instrumented.classLoader
- The class loader of the instrumented class. Might benull
to represent the bootstrap class loader.module
- The class's module ornull
if the current VM does not support modules.- Returns:
- A transformed version of the supplied
builder
.
-
with
public AgentBuilder.Transformer.ForAdvice with(AgentBuilder.PoolStrategy poolStrategy)
Registers a pool strategy for creating aTypePool
that should be used for creating the advice class.- Parameters:
poolStrategy
- The pool strategy to use.- Returns:
- A new instance of this advice transformer that applies the supplied pool strategy.
-
with
public AgentBuilder.Transformer.ForAdvice with(AgentBuilder.LocationStrategy locationStrategy)
Registers a location strategy for creating aClassFileLocator
from the class loader that is supplied during transformation that should be used for looking up advice-relevant classes.- Parameters:
locationStrategy
- The location strategy to use.- Returns:
- A new instance of this advice transformer that applies the supplied location strategy.
-
withExceptionHandler
public AgentBuilder.Transformer.ForAdvice withExceptionHandler(Advice.ExceptionHandler exceptionHandler)
Registers an exception handler for suppressed exceptions to use by the registered advice.- Parameters:
exceptionHandler
- The exception handler to use.- Returns:
- A new instance of this advice transformer that applies the supplied exception handler.
- See Also:
Advice.withExceptionHandler(StackManipulation)
-
with
public AgentBuilder.Transformer.ForAdvice with(Assigner assigner)
Registers an assigner to be used by the advice class.- Parameters:
assigner
- The assigner to use.- Returns:
- A new instance of this advice transformer that applies the supplied assigner.
- See Also:
Advice.withAssigner(Assigner)
-
include
public AgentBuilder.Transformer.ForAdvice include(java.lang.ClassLoader... classLoader)
Includes the supplied class loaders as a source for looking up an advice class or its dependencies. Note that the supplied class loaders are queried for types before the class loader of the instrumented class.- Parameters:
classLoader
- The class loaders to include when looking up classes in their order. Duplicates are filtered.- Returns:
- A new instance of this advice transformer that considers the supplied class loaders as a lookup source.
-
include
public AgentBuilder.Transformer.ForAdvice include(ClassFileLocator... classFileLocator)
Includes the supplied class file locators as a source for looking up an advice class or its dependencies. Note that the supplied class loaders are queried for types before the class loader of the instrumented class.- Parameters:
classFileLocator
- The class file locators to include when looking up classes in their order. Duplicates are filtered.- Returns:
- A new instance of this advice transformer that considers the supplied class file locators as a lookup source.
-
include
public AgentBuilder.Transformer.ForAdvice include(java.util.List<? extends ClassFileLocator> classFileLocators)
Includes the supplied class file locators as a source for looking up an advice class or its dependencies. Note that the supplied class loaders are queried for types before the class loader of the instrumented class.- Parameters:
classFileLocators
- The class file locators to include when looking up classes in their order. Duplicates are filtered.- Returns:
- A new instance of this advice transformer that considers the supplied class file locators as a lookup source.
-
advice
public AgentBuilder.Transformer.ForAdvice advice(ElementMatcher<? super MethodDescription> matcher, java.lang.String name)
Applies the given advice class onto all methods that satisfy the supplied matcher.- Parameters:
matcher
- The matcher to determine what methods the advice should be applied to.name
- The fully-qualified, binary name of the advice class.- Returns:
- A new instance of this advice transformer that applies the given advice to all matched methods of an instrumented type.
-
advice
public AgentBuilder.Transformer.ForAdvice advice(LatentMatcher<? super MethodDescription> matcher, java.lang.String name)
Applies the given advice class onto all methods that satisfy the supplied matcher.- Parameters:
matcher
- The matcher to determine what methods the advice should be applied to.name
- The fully-qualified, binary name of the advice class.- Returns:
- A new instance of this advice transformer that applies the given advice to all matched methods of an instrumented type.
-
advice
public AgentBuilder.Transformer.ForAdvice advice(ElementMatcher<? super MethodDescription> matcher, java.lang.String enter, java.lang.String exit)
Applies the given advice class onto all methods that satisfy the supplied matcher.- Parameters:
matcher
- The matcher to determine what methods the advice should be applied to.enter
- The fully-qualified, binary name of the enter advice class.exit
- The fully-qualified, binary name of the exit advice class.- Returns:
- A new instance of this advice transformer that applies the given advice to all matched methods of an instrumented type.
-
advice
public AgentBuilder.Transformer.ForAdvice advice(LatentMatcher<? super MethodDescription> matcher, java.lang.String enter, java.lang.String exit)
Applies the given advice class onto all methods that satisfy the supplied matcher.- Parameters:
matcher
- The matcher to determine what methods the advice should be applied to.enter
- The fully-qualified, binary name of the enter advice class.exit
- The fully-qualified, binary name of the exit advice class.- Returns:
- A new instance of this advice transformer that applies the given advice to all matched methods of an instrumented type.
-
-