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 Object implements Implementation.Composable
This Implementation allows the invocation of a specified method while providing explicit arguments to this method.
  • Field Details

  • Constructor Details

  • Method Details

    • invoke

      public static MethodCall.WithoutSpecifiedTarget invoke(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 is static.
      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(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

      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 a StackOverflowError 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 for invokeSelf().onSuper().
      Returns:
      A method call that invokes the method being instrumented as a super method call.
    • call

      public static Implementation.Composable call(Callable<?> callable)
      Implements a method by invoking the provided Callable. 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(Runnable runnable)
      Implements a method by invoking the provided Runnable. 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(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(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 of String, method handles, types, method types as well as instances of TypeDescription or JavaConstant or null are loaded directly onto the operand stack. This might corrupt referential identity for these values. Any other values are stored within a static 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(ConstantValue... constant)
      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:
      constant - The constants to provide as arguments.
      Returns:
      A method call that hands the provided arguments to the invoked method.
    • with

      public MethodCall with(JavaConstant... constant)
      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:
      constant - The constants to provide as arguments.
      Returns:
      A method call that hands the provided arguments to the invoked method.
    • withReference

      public MethodCall withReference(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, the null 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 a Assigner.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 argument index.
    • withArgumentArrayElements

      public MethodCall withArgumentArrayElements(int index, int size)

      Creates a method call where the parameter with index is expected to be an array and where size 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 a Assigner.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 argument index.
    • 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 where size elements are loaded from the array as arguments for the invoked method. The first element is loaded from index start.

      Note: This is typically used in combination with dynamic type assignments which is activated via withAssigner(Assigner, Assigner.Typing) using a Assigner.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 from start.
      Returns:
      A method call that loads size elements from the array handed to the instrumented method as argument index.
    • withThis

      public MethodCall withThis()
      Assigns the this 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 the Class 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(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, 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, 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(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(Field field)
      Sets the result of the method call as a value of the specified field. If the instrumented method does not return void, 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 return void, 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 return void, 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 with andThen(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 interface Implementation.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 interface Implementation.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 interface InstrumentedType.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 interface Implementation
      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).