Class MethodCall
- All Implemented Interfaces:
InstrumentedType.Prepareable
,Implementation
,Implementation.Composable
- Direct Known Subclasses:
MethodCall.WithoutSpecifiedTarget
Implementation
allows the invocation of a specified method while
providing explicit arguments to this method.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected class
The appender being used to implement aMethodCall
.static interface
An argument loader is responsible for loading an argument for an invoked method onto the operand stack.static class
AMethodCall
that sets the call's result as the value of a field.protected static interface
A method invoker is responsible for creating a method invocation that is to be applied by aMethodCall
.static interface
A method locator is responsible for identifying the method that is to be invoked by aMethodCall
.protected static interface
A target handler is responsible for invoking a method for aMethodCall
.protected static interface
A termination handler is responsible to handle the return value of a method that is invoked via aMethodCall
.static class
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
Nested classes/interfaces inherited from interface net.bytebuddy.dynamic.scaffold.InstrumentedType.Prepareable
InstrumentedType.Prepareable.NoOp
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final List<MethodCall.ArgumentLoader.Factory>
The argument loader to load arguments onto the operand stack in their application order.protected final Assigner
The assigner to use.protected final MethodCall.MethodInvoker.Factory
The method invoker to use.protected final MethodCall.MethodLocator.Factory
The method locator to use.protected final MethodCall.TargetHandler.Factory
The target handler to use.protected final MethodCall.TerminationHandler.Factory
The termination handler to use.protected final Assigner.Typing
Indicates if dynamic type castings should be attempted for incompatible assignments. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
MethodCall
(MethodCall.MethodLocator.Factory methodLocator, MethodCall.TargetHandler.Factory targetHandler, 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
Modifier and TypeMethodDescriptionandThen
(Implementation implementation) Appends the supplied implementation to this implementation.andThen
(Implementation.Composable implementation) Appends the supplied composable implementation to this implementation.appender
(Implementation.Target implementationTarget) Creates a byte code appender that determines the implementation of the instrumented type's methods.static Implementation.Composable
Implements a method by invoking the providedCallable
.static MethodCall
construct
(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.invoke
(Constructor<?> constructor) Invokes the given constructor on the instance of the instrumented type.Invokes the given method.invoke
(MethodDescription methodDescription) Invokes the given method.invoke
(MethodCall.MethodLocator.Factory methodLocator) Invokes a method using the provided method locator.invoke
(ElementMatcher<? super MethodDescription> matcher) Invokes a unique virtual method or constructor of the instrumented type that is matched by the specified matcher.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.Invokes the instrumented method recursively.static MethodCall
Invokes the instrumented method as a super method call on the instance itself.prepare
(InstrumentedType instrumentedType) Prepares a given instrumented type.static Implementation.Composable
Implements a method by invoking the providedRunnable
.Sets the result of the method call as a value of the specified field.setsField
(FieldDescription fieldDescription) Sets the result of the method call as a value of the specified field.setsField
(ElementMatcher<? super FieldDescription> matcher) Sets the result of the method call as a value of the specified field.Defines a number of arguments to be handed to the method that is being invoked by this implementation.with
(List<? extends MethodCall.ArgumentLoader.Factory> argumentLoaders) Defines a method call that resolves arguments by the supplied argument loader factories.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.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.with
(StackManipulation stackManipulation, Type type) Adds a stack manipulation as an assignment to the next parameter.with
(StackManipulation stackManipulation, TypeDefinition typeDefinition) Adds a stack manipulation as an assignment to the next parameter.with
(MethodCall.ArgumentLoader.Factory... argumentLoader) Defines a method call that resolves arguments by the supplied argument loader factories.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.Adds all arguments of the instrumented method as arguments to the invoked method to this method call.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.Adds an array containing all arguments of the instrumented method to this method call.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.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.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.withAssigner
(Assigner assigner, Assigner.Typing typing) Defines an assigner to be used for assigning values to the parameters of the invoked method.Defines a method call which fetches a value from a list of existing fields.withField
(FieldLocator.Factory fieldLocatorFactory, String... name) Defines a method call which fetches a value from a list of existing fields.withMethodCall
(MethodCall methodCall) Defines a method call which fetches a value from a method call.Assigns theClass
value of the instrumented type.withReference
(Object... argument) Defines a number of arguments to be handed to the method that is being invoked by this implementation.withThis()
Assigns thethis
reference to the next parameter.
-
Field Details
-
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.
-
-
Constructor Details
-
MethodCall
protected MethodCall(MethodCall.MethodLocator.Factory methodLocator, MethodCall.TargetHandler.Factory targetHandler, 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 Details
-
invoke
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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)
.
-