Class MethodCall
- java.lang.Object
-
- net.bytebuddy.implementation.MethodCall
-
- All Implemented Interfaces:
InstrumentedType.Prepareable
,Implementation
,Implementation.Composable
- Direct Known Subclasses:
MethodCall.WithoutSpecifiedTarget
@Enhance public class MethodCall extends java.lang.Object implements Implementation.Composable
ThisImplementation
allows the invocation of a specified method while providing explicit arguments to this method.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
MethodCall.Appender
The appender being used to implement aMethodCall
.static interface
MethodCall.ArgumentLoader
An argument loader is responsible for loading an argument for an invoked method onto the operand stack.static class
MethodCall.FieldSetting
AMethodCall
that sets the call's result as the value of a field.protected static interface
MethodCall.MethodInvoker
A method invoker is responsible for creating a method invocation that is to be applied by aMethodCall
.static interface
MethodCall.MethodLocator
A method locator is responsible for identifying the method that is to be invoked by aMethodCall
.protected static interface
MethodCall.TargetHandler
A target handler is responsible for invoking a method for aMethodCall
.protected static interface
MethodCall.TerminationHandler
A termination handler is responsible to handle the return value of a method that is invoked via aMethodCall
.static class
MethodCall.WithoutSpecifiedTarget
Represents aMethodCall
that invokes a method without specifying an invocation method.-
Nested classes/interfaces inherited from interface net.bytebuddy.implementation.Implementation
Implementation.Composable, Implementation.Compound, Implementation.Context, Implementation.Simple, Implementation.SpecialMethodInvocation, Implementation.Target
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<MethodCall.ArgumentLoader.Factory>
argumentLoaders
The argument loader to load arguments onto the operand stack in their application order.protected Assigner
assigner
The assigner to use.protected MethodCall.MethodInvoker.Factory
methodInvoker
The method invoker to use.protected MethodCall.MethodLocator.Factory
methodLocator
The method locator to use.protected MethodCall.TargetHandler.Factory
targetHandler
The target handler to use.protected MethodCall.TerminationHandler.Factory
terminationHandler
The termination handler to use.protected Assigner.Typing
typing
Indicates if dynamic type castings should be attempted for incompatible assignments.
-
Constructor Summary
Constructors Modifier Constructor Description protected
MethodCall(MethodCall.MethodLocator.Factory methodLocator, MethodCall.TargetHandler.Factory targetHandler, java.util.List<MethodCall.ArgumentLoader.Factory> argumentLoaders, MethodCall.MethodInvoker.Factory methodInvoker, MethodCall.TerminationHandler.Factory terminationHandler, Assigner assigner, Assigner.Typing typing)
Creates a new method call implementation.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Implementation
andThen(Implementation implementation)
Appends the supplied implementation to this implementation.Implementation.Composable
andThen(Implementation.Composable implementation)
Appends the supplied composable implementation to this implementation.ByteCodeAppender
appender(Implementation.Target implementationTarget)
Creates a byte code appender that determines the implementation of the instrumented type's methods.static Implementation.Composable
call(java.util.concurrent.Callable<?> callable)
Implements a method by invoking the providedCallable
.static MethodCall
construct(java.lang.reflect.Constructor<?> constructor)
Invokes the given constructor in order to create an instance.static MethodCall
construct(MethodDescription methodDescription)
Invokes the given constructor in order to create an instance.static MethodCall.WithoutSpecifiedTarget
invoke(java.lang.reflect.Constructor<?> constructor)
Invokes the given constructor on the instance of the instrumented type.static MethodCall.WithoutSpecifiedTarget
invoke(java.lang.reflect.Method method)
Invokes the given method.static MethodCall.WithoutSpecifiedTarget
invoke(MethodDescription methodDescription)
Invokes the given method.static MethodCall.WithoutSpecifiedTarget
invoke(MethodCall.MethodLocator.Factory methodLocator)
Invokes a method using the provided method locator.static MethodCall.WithoutSpecifiedTarget
invoke(ElementMatcher<? super MethodDescription> matcher)
Invokes a unique virtual method or constructor of the instrumented type that is matched by the specified matcher.static MethodCall.WithoutSpecifiedTarget
invoke(ElementMatcher<? super MethodDescription> matcher, MethodGraph.Compiler methodGraphCompiler)
Invokes a unique virtual method or constructor of the instrumented type that is matched by the specified matcher.static MethodCall.WithoutSpecifiedTarget
invokeSelf()
Invokes the instrumented method recursively.static MethodCall
invokeSuper()
Invokes the instrumented method as a super method call on the instance itself.InstrumentedType
prepare(InstrumentedType instrumentedType)
Prepares a given instrumented type.static Implementation.Composable
run(java.lang.Runnable runnable)
Implements a method by invoking the providedRunnable
.MethodCall.FieldSetting
setsField(java.lang.reflect.Field field)
Sets the result of the method call as a value of the specified field.MethodCall.FieldSetting
setsField(FieldDescription fieldDescription)
Sets the result of the method call as a value of the specified field.MethodCall.FieldSetting
setsField(ElementMatcher<? super FieldDescription> matcher)
Sets the result of the method call as a value of the specified field.MethodCall
with(java.lang.Object... argument)
Defines a number of arguments to be handed to the method that is being invoked by this implementation.MethodCall
with(java.util.List<? extends MethodCall.ArgumentLoader.Factory> argumentLoaders)
Defines a method call that resolves arguments by the supplied argument loader factories.MethodCall
with(EnumerationDescription... enumerationDescription)
Defines the given enumeration values to be provided as arguments to the invoked method where the values are read from the enumeration class on demand.MethodCall
with(TypeDescription... typeDescription)
Defines the given types to be provided as arguments to the invoked method where the represented types are stored in the generated class's constant pool.MethodCall
with(StackManipulation stackManipulation, java.lang.reflect.Type type)
Adds a stack manipulation as an assignment to the next parameter.MethodCall
with(StackManipulation stackManipulation, TypeDefinition typeDefinition)
Adds a stack manipulation as an assignment to the next parameter.MethodCall
with(MethodCall.ArgumentLoader.Factory... argumentLoader)
Defines a method call that resolves arguments by the supplied argument loader factories.MethodCall
with(JavaConstant... javaConstant)
Defines the given Java instances to be provided as arguments to the invoked method where the given instances are stored in the generated class's constant pool.MethodCall
withAllArguments()
Adds all arguments of the instrumented method as arguments to the invoked method to this method call.MethodCall
withArgument(int... index)
Defines a number of arguments of the instrumented method by their parameter indices to be handed to the invoked method as an argument.MethodCall
withArgumentArray()
Adds an array containing all arguments of the instrumented method to this method call.MethodCall
withArgumentArrayElements(int index)
Creates a method call where the parameter withindex
is expected to be an array and where each element of the array is expected to represent an argument for the method being invoked.MethodCall
withArgumentArrayElements(int index, int size)
Creates a method call where the parameter withindex
is expected to be an array and wheresize
elements are loaded from the array as arguments for the invoked method.MethodCall
withArgumentArrayElements(int index, int start, int size)
Creates a method call where the parameter withindex
is expected to be an array and wheresize
elements are loaded from the array as arguments for the invoked method.Implementation.Composable
withAssigner(Assigner assigner, Assigner.Typing typing)
Defines an assigner to be used for assigning values to the parameters of the invoked method.MethodCall
withField(java.lang.String... name)
Defines a method call which fetches a value from a list of existing fields.MethodCall
withField(FieldLocator.Factory fieldLocatorFactory, java.lang.String... name)
Defines a method call which fetches a value from a list of existing fields.MethodCall
withMethodCall(MethodCall methodCall)
Defines a method call which fetches a value from a method call.MethodCall
withOwnType()
Assigns theClass
value of the instrumented type.MethodCall
withReference(java.lang.Object... argument)
Defines a number of arguments to be handed to the method that is being invoked by this implementation.MethodCall
withThis()
Assigns thethis
reference to the next parameter.
-
-
-
Field Detail
-
methodLocator
protected final MethodCall.MethodLocator.Factory methodLocator
The method locator to use.
-
targetHandler
protected final MethodCall.TargetHandler.Factory targetHandler
The target handler to use.
-
argumentLoaders
protected final java.util.List<MethodCall.ArgumentLoader.Factory> argumentLoaders
The argument loader to load arguments onto the operand stack in their application order.
-
methodInvoker
protected final MethodCall.MethodInvoker.Factory methodInvoker
The method invoker to use.
-
terminationHandler
protected final MethodCall.TerminationHandler.Factory terminationHandler
The termination handler to use.
-
assigner
protected final Assigner assigner
The assigner to use.
-
typing
protected final Assigner.Typing typing
Indicates if dynamic type castings should be attempted for incompatible assignments.
-
-
Constructor Detail
-
MethodCall
protected MethodCall(MethodCall.MethodLocator.Factory methodLocator, MethodCall.TargetHandler.Factory targetHandler, java.util.List<MethodCall.ArgumentLoader.Factory> argumentLoaders, MethodCall.MethodInvoker.Factory methodInvoker, MethodCall.TerminationHandler.Factory terminationHandler, Assigner assigner, Assigner.Typing typing)
Creates a new method call implementation.- Parameters:
methodLocator
- The method locator to use.targetHandler
- The target handler to use.argumentLoaders
- The argument loader to load arguments onto the operand stack in their application order.methodInvoker
- The method invoker to use.terminationHandler
- The termination handler to use.assigner
- The assigner to use.typing
- Indicates if dynamic type castings should be attempted for incompatible assignments.
-
-
Method Detail
-
invoke
public static MethodCall.WithoutSpecifiedTarget invoke(java.lang.reflect.Method method)
Invokes the given method. Without further specification, the method is invoked without any arguments on the instance of the instrumented class or statically, if the given method isstatic
.- Parameters:
method
- The method to invoke.- Returns:
- A method call implementation that invokes the given method without providing any arguments.
-
invoke
public static MethodCall.WithoutSpecifiedTarget invoke(java.lang.reflect.Constructor<?> constructor)
Invokes the given constructor on the instance of the instrumented type.
Important: A constructor invocation can only be applied within another constructor to invoke the super constructor or an auxiliary constructor. To construct a new instance, use
construct(Constructor)
.- Parameters:
constructor
- The constructor to invoke.- Returns:
- A method call implementation that invokes the given constructor without providing any arguments.
-
invoke
public static MethodCall.WithoutSpecifiedTarget invoke(MethodDescription methodDescription)
Invokes the given method. If the method description describes a constructor, it is automatically invoked as a special method invocation on the instance of the instrumented type. The same is true for
private
methods. Finally,static
methods are invoked statically.Important: A constructor invocation can only be applied within another constructor to invoke the super constructor or an auxiliary constructor. To construct a new instance, use
construct(MethodDescription)
.- Parameters:
methodDescription
- The method to invoke.- Returns:
- A method call implementation that invokes the given method without providing any arguments.
-
invoke
public static MethodCall.WithoutSpecifiedTarget invoke(ElementMatcher<? super MethodDescription> matcher)
Invokes a unique virtual method or constructor of the instrumented type that is matched by the specified matcher.- Parameters:
matcher
- The matcher to identify the method to invoke.- Returns:
- A method call for the uniquely identified method.
-
invoke
public static MethodCall.WithoutSpecifiedTarget invoke(ElementMatcher<? super MethodDescription> matcher, MethodGraph.Compiler methodGraphCompiler)
Invokes a unique virtual method or constructor of the instrumented type that is matched by the specified matcher.- Parameters:
matcher
- The matcher to identify the method to invoke.methodGraphCompiler
- The method graph compiler to use.- Returns:
- A method call for the uniquely identified method.
-
invoke
public static MethodCall.WithoutSpecifiedTarget invoke(MethodCall.MethodLocator.Factory methodLocator)
Invokes a method using the provided method locator.- Parameters:
methodLocator
- The method locator to apply for locating the method to invoke given the instrumented method.- Returns:
- A method call implementation that uses the provided method locator for resolving the method to be invoked.
-
invokeSelf
public static MethodCall.WithoutSpecifiedTarget invokeSelf()
Invokes the instrumented method recursively. Invoking this method on the same instance causes aStackOverflowError
due to infinite recursion.- Returns:
- A method call that invokes the method being instrumented.
-
invokeSuper
public static MethodCall invokeSuper()
Invokes the instrumented method as a super method call on the instance itself. This is a shortcut forinvokeSelf().onSuper()
.- Returns:
- A method call that invokes the method being instrumented as a super method call.
-
call
public static Implementation.Composable call(java.util.concurrent.Callable<?> callable)
Implements a method by invoking the providedCallable
. The return value of the provided object is casted to the implemented method's return type, if necessary.- Parameters:
callable
- The callable to invoke when a method is intercepted.- Returns:
- A composable method implementation that invokes the given callable.
-
run
public static Implementation.Composable run(java.lang.Runnable runnable)
Implements a method by invoking the providedRunnable
. If the instrumented method returns a value,null
is returned.- Parameters:
runnable
- The runnable to invoke when a method is intercepted.- Returns:
- A composable method implementation that invokes the given runnable.
-
construct
public static MethodCall construct(java.lang.reflect.Constructor<?> constructor)
Invokes the given constructor in order to create an instance.- Parameters:
constructor
- The constructor to invoke.- Returns:
- A method call that invokes the given constructor without providing any arguments.
-
construct
public static MethodCall construct(MethodDescription methodDescription)
Invokes the given constructor in order to create an instance.- Parameters:
methodDescription
- A description of the constructor to invoke.- Returns:
- A method call that invokes the given constructor without providing any arguments.
-
with
public MethodCall with(java.lang.Object... argument)
Defines a number of arguments to be handed to the method that is being invoked by this implementation. Any wrapper type instances for primitive values, instances ofString
, method handles, types, method types as well as instances ofTypeDescription
orJavaConstant
ornull
are loaded directly onto the operand stack. This might corrupt referential identity for these values. Any other values are stored within astatic
field that is added to the instrumented type.- Parameters:
argument
- The arguments to provide to the method that is being called in their order.- Returns:
- A method call that hands the provided arguments to the invoked method.
-
with
public MethodCall with(TypeDescription... typeDescription)
Defines the given types to be provided as arguments to the invoked method where the represented types are stored in the generated class's constant pool.- Parameters:
typeDescription
- The type descriptions to provide as arguments.- Returns:
- A method call that hands the provided arguments to the invoked method.
-
with
public MethodCall with(EnumerationDescription... enumerationDescription)
Defines the given enumeration values to be provided as arguments to the invoked method where the values are read from the enumeration class on demand.- Parameters:
enumerationDescription
- The enumeration descriptions to provide as arguments.- Returns:
- A method call that hands the provided arguments to the invoked method.
-
with
public MethodCall with(JavaConstant... javaConstant)
Defines the given Java instances to be provided as arguments to the invoked method where the given instances are stored in the generated class's constant pool.- Parameters:
javaConstant
- The Java instances to provide as arguments.- Returns:
- A method call that hands the provided arguments to the invoked method.
-
withReference
public MethodCall withReference(java.lang.Object... argument)
Defines a number of arguments to be handed to the method that is being invoked by this implementation. Any value is stored within a field in order to preserve referential identity. As an exception, thenull
value is not stored within a field.- Parameters:
argument
- The arguments to provide to the method that is being called in their order.- Returns:
- A method call that hands the provided arguments to the invoked method.
-
withArgument
public MethodCall withArgument(int... index)
Defines a number of arguments of the instrumented method by their parameter indices to be handed to the invoked method as an argument.- Parameters:
index
- The parameter indices of the instrumented method to be handed to the invoked method as an argument in their order. The indices are zero-based.- Returns:
- A method call that hands the provided arguments to the invoked method.
-
withAllArguments
public MethodCall withAllArguments()
Adds all arguments of the instrumented method as arguments to the invoked method to this method call.- Returns:
- A method call that hands all arguments of the instrumented method to the invoked method.
-
withArgumentArray
public MethodCall withArgumentArray()
Adds an array containing all arguments of the instrumented method to this method call.- Returns:
- A method call that adds an array containing all arguments of the instrumented method to the invoked method.
-
withArgumentArrayElements
public MethodCall withArgumentArrayElements(int index)
Creates a method call where the parameter with
index
is expected to be an array and where each element of the array is expected to represent an argument for the method being invoked.Note: This is typically used in combination with dynamic type assignments which is activated via
withAssigner(Assigner, Assigner.Typing)
using aAssigner.Typing.DYNAMIC
.- Parameters:
index
- The index of the parameter.- Returns:
- A method call that loads
size
elements from the array handed to the instrumented method as argumentindex
.
-
withArgumentArrayElements
public MethodCall withArgumentArrayElements(int index, int size)
Creates a method call where the parameter with
index
is expected to be an array and wheresize
elements are loaded from the array as arguments for the invoked method.Note: This is typically used in combination with dynamic type assignments which is activated via
withAssigner(Assigner, Assigner.Typing)
using aAssigner.Typing.DYNAMIC
.- Parameters:
index
- The index of the parameter.size
- The amount of elements to load from the array.- Returns:
- A method call that loads
size
elements from the array handed to the instrumented method as argumentindex
.
-
withArgumentArrayElements
public MethodCall withArgumentArrayElements(int index, int start, int size)
Creates a method call where the parameter with
index
is expected to be an array and wheresize
elements are loaded from the array as arguments for the invoked method. The first element is loaded from indexstart
.Note: This is typically used in combination with dynamic type assignments which is activated via
withAssigner(Assigner, Assigner.Typing)
using aAssigner.Typing.DYNAMIC
.- Parameters:
index
- The index of the parameter.start
- The first array index to consider.size
- The amount of elements to load from the array with increasing index fromstart
.- Returns:
- A method call that loads
size
elements from the array handed to the instrumented method as argumentindex
.
-
withThis
public MethodCall withThis()
Assigns thethis
reference to the next parameter.- Returns:
- This method call where the next parameter is a assigned a reference to the
this
reference of the instance of the intercepted method.
-
withOwnType
public MethodCall withOwnType()
Assigns theClass
value of the instrumented type.- Returns:
- This method call where the next parameter is a assigned a reference to the
Class
value of the instrumented type.
-
withField
public MethodCall withField(java.lang.String... name)
Defines a method call which fetches a value from a list of existing fields.- Parameters:
name
- The names of the fields.- Returns:
- A method call which assigns the next parameters to the values of the given fields.
-
withField
public MethodCall withField(FieldLocator.Factory fieldLocatorFactory, java.lang.String... name)
Defines a method call which fetches a value from a list of existing fields.- Parameters:
fieldLocatorFactory
- The field locator factory to use.name
- The names of the fields.- Returns:
- A method call which assigns the next parameters to the values of the given fields.
-
withMethodCall
public MethodCall withMethodCall(MethodCall methodCall)
Defines a method call which fetches a value from a method call.- Parameters:
methodCall
- The method call to use.- Returns:
- A method call which assigns the parameter to the result of the given method call.
-
with
public MethodCall with(StackManipulation stackManipulation, java.lang.reflect.Type type)
Adds a stack manipulation as an assignment to the next parameter.- Parameters:
stackManipulation
- The stack manipulation loading the value.type
- The type of the argument being loaded.- Returns:
- A method call that adds the stack manipulation as the next argument to the invoked method.
-
with
public MethodCall with(StackManipulation stackManipulation, TypeDefinition typeDefinition)
Adds a stack manipulation as an assignment to the next parameter.- Parameters:
stackManipulation
- The stack manipulation loading the value.typeDefinition
- The type of the argument being loaded.- Returns:
- A method call that adds the stack manipulation as the next argument to the invoked method.
-
with
public MethodCall with(MethodCall.ArgumentLoader.Factory... argumentLoader)
Defines a method call that resolves arguments by the supplied argument loader factories.- Parameters:
argumentLoader
- The argument loaders to apply to the subsequent arguments of the- Returns:
- A method call that adds the arguments of the supplied argument loaders to the invoked method.
-
with
public MethodCall with(java.util.List<? extends MethodCall.ArgumentLoader.Factory> argumentLoaders)
Defines a method call that resolves arguments by the supplied argument loader factories.- Parameters:
argumentLoaders
- The argument loaders to apply to the subsequent arguments of the- Returns:
- A method call that adds the arguments of the supplied argument loaders to the invoked method.
-
setsField
public MethodCall.FieldSetting setsField(java.lang.reflect.Field field)
Sets the result of the method call as a value of the specified field. If the instrumented method does not returnvoid
, this instrumentation must be chained with another instrumentation.- Parameters:
field
- The field to set.- Returns:
- A new instance of this method call that sets the resulting value as the specified field's value.
-
setsField
public MethodCall.FieldSetting setsField(FieldDescription fieldDescription)
Sets the result of the method call as a value of the specified field. If the instrumented method does not returnvoid
, this instrumentation must be chained with another instrumentation.- Parameters:
fieldDescription
- The field to set.- Returns:
- A new instance of this method call that sets the resulting value as the specified field's value.
-
setsField
public MethodCall.FieldSetting setsField(ElementMatcher<? super FieldDescription> matcher)
Sets the result of the method call as a value of the specified field. If the instrumented method does not returnvoid
, this instrumentation must be chained with another instrumentation.- Parameters:
matcher
- A matcher that locates a field in the instrumented type's hierarchy.- Returns:
- A new instance of this method call that sets the resulting value as the specified field's value.
-
withAssigner
public Implementation.Composable withAssigner(Assigner assigner, Assigner.Typing typing)
Defines an assigner to be used for assigning values to the parameters of the invoked method. This assigner is also used for assigning the invoked method's return value to the return type of the instrumented method, if this method is not chained withandThen(Implementation)
such that a return value of this method call is discarded.- Parameters:
assigner
- The assigner to use.typing
- Indicates if dynamic type castings should be attempted for incompatible assignments.- Returns:
- This method call using the provided assigner.
-
andThen
public Implementation andThen(Implementation implementation)
Appends the supplied implementation to this implementation.- Specified by:
andThen
in interfaceImplementation.Composable
- Parameters:
implementation
- The subsequent implementation.- Returns:
- An implementation that combines this implementation with the provided one.
-
andThen
public Implementation.Composable andThen(Implementation.Composable implementation)
Appends the supplied composable implementation to this implementation.- Specified by:
andThen
in interfaceImplementation.Composable
- Parameters:
implementation
- The subsequent composable implementation.- Returns:
- A composable implementation that combines this implementation with the provided one.
-
prepare
public InstrumentedType prepare(InstrumentedType instrumentedType)
Prepares a given instrumented type.- Specified by:
prepare
in interfaceInstrumentedType.Prepareable
- Parameters:
instrumentedType
- The instrumented type in its current form.- Returns:
- The prepared instrumented type.
-
appender
public ByteCodeAppender appender(Implementation.Target implementationTarget)
Creates a byte code appender that determines the implementation of the instrumented type's methods.- Specified by:
appender
in interfaceImplementation
- Parameters:
implementationTarget
- The target of the current implementation.- Returns:
- A byte code appender for implementing methods delegated to this implementation. This byte code appender
is also responsible for handling methods that were added by this implementation on the call to
InstrumentedType.Prepareable.prepare(InstrumentedType)
.
-
-