Top Description Inners Fields Constructors Methods
net.bytebuddy.implementation

public Class MethodDelegation

extends Object
implements Composable
Class Inheritance
All Implemented Interfaces
net.bytebuddy.implementation.Implementation.Composable, net.bytebuddy.implementation.Implementation, net.bytebuddy.dynamic.scaffold.InstrumentedType.Prepareable
Annotations
@Enhance
Imports
net.bytebuddy.build.HashCodeAndEqualsPlugin, net.bytebuddy.description.field.FieldDescription, net.bytebuddy.description.method.MethodDescription, .MethodList, net.bytebuddy.description.type.TypeDefinition, .TypeDescription, net.bytebuddy.dynamic.scaffold.FieldLocator, .InstrumentedType, .MethodGraph, net.bytebuddy.implementation.bind.MethodDelegationBinder, net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder, net.bytebuddy.implementation.bytecode.ByteCodeAppender, .Duplication, .StackManipulation, .TypeCreation, net.bytebuddy.implementation.bytecode.assign.Assigner, net.bytebuddy.implementation.bytecode.member.FieldAccess, .MethodInvocation, .MethodVariableAccess, net.bytebuddy.matcher.ElementMatcher, net.bytebuddy.utility.CompoundList, .RandomString, net.bytebuddy.jar.asm.MethodVisitor, .Opcodes, java.lang.reflect.Type, java.util.ArrayList, .Arrays, .Collections, .List

This implementation delegates a method call to another method which can either be static by providing a reference to a java.lang.Class or an instance method when another object is provided. The potential targets of the method delegation can further be filtered by applying a filter. The method delegation can be customized by invoking the MethodDelegation's several builder methods.

 

Without any customization, the method delegation will work as follows:

 

Binding an instrumented method to a given delegate method

 

A method will be bound parameter by parameter. Considering a method Foo#bar being bound to a method Qux#baz, the method delegation will be decided on basis of the following annotations: If a method is not annotated with any of the above methods, it will be treated as if it was annotated net.bytebuddy.implementation.bind.annotation.Argument using the next unbound parameter index of the source method as its parameter. This means that a method Qux#baz(@Argument(2) Object p1, Object p2, @Argument(0) Object p3 would be treated as if p2 was annotated with @Argument(1).

 

In addition, the net.bytebuddy.implementation.bind.annotation.RuntimeType annotation can instruct a parameter to be bound by a net.bytebuddy.implementation.bytecode.assign.Assigner with considering the runtime type of the parameter.

 

Selecting among different methods that can be used for binding a method of the instrumented type

 

When deciding between two methods Foo#bar and Foo#qux that could both be used to delegating a method call, the following consideration is applied in the given order:
  1. net.bytebuddy.implementation.bind.annotation.BindingPriority: A method that is annotated with this annotation is given a specific priority where the default priority is set to net.bytebuddy.implementation.bind.annotation.BindingPriority#DEFAULT for non-annotated method. A method with a higher priority is considered a better target for delegation.
  2. net.bytebuddy.implementation.bind.DeclaringTypeResolver: If a target method is declared by a more specific type than another method, the method with the most specific type is bound.
  3. net.bytebuddy.implementation.bind.MethodNameEqualityResolver: If a source method Baz#qux is the source method, it will rather be assigned to Foo#qux because of their equal names. Similar names and case-insensitive equality are not considered.
  4. net.bytebuddy.implementation.bind.ArgumentTypeResolver: The most specific type resolver will consider all bindings that are using the net.bytebuddy.implementation.bind.annotation.Argument annotation for resolving a binding conflict. In this context, the resolution will equal the most-specific type resolution that is performed by the Java compiler. This means that a source method Bar#baz(String) will rather be bound to a method Foo#bar(String) than Foo#qux(Object) because the String type is more specific than the Object type. If two methods are equally adequate by their parameter types, then the method with the higher numbers of @Argument annotated parameters is considered as the better delegation target.
  5. net.bytebuddy.implementation.bind.ParameterLengthResolver: If a target methods has a higher number of total parameters that were successfully bound, the method with the higher number will be considered as the better delegation target.

Additionally, if a method is annotated by net.bytebuddy.implementation.bind.annotation.IgnoreForBinding, it is never considered as a target for a method delegation.

Important Note

For invoking a method on another instance, use the MethodCall implementation. A method delegation intends to bind a interceptor class and its resolution algorithm will not necessarily yield a delegation to the intercepted method.

See Also
MethodCall, net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder.ParameterBinder.ForFixedValue

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected static class
protected static interface
MethodDelegation.ImplementationDelegate

An implementation delegate is responsible for executing the actual method delegation and for resolving the target methods.

public static class

Field Summary

Modifier and TypeField and Description
private final MethodDelegationBinder.AmbiguityResolver
private final Assigner
assigner

The net.bytebuddy.implementation.bytecode.assign.Assigner to be used by this method delegation.

private final MethodDelegationBinder.BindingResolver
bindingResolver

The binding resolver being used to select the relevant method binding.

private final MethodDelegation.ImplementationDelegate
implementationDelegate

The implementation delegate for this method delegation.

private final List<TargetMethodAnnotationDrivenBinder.ParameterBinder<?>>
private final MethodDelegationBinder.TerminationHandler
terminationHandler

The termination handler to apply.

Constructor Summary

AccessConstructor and Description
protected
MethodDelegation(MethodDelegation.ImplementationDelegate
The implementation delegate to use by this method delegator.
implementationDelegate
,
List<TargetMethodAnnotationDrivenBinder.ParameterBinder<?>>
The parameter binders to use by this method delegator.
parameterBinders
,
MethodDelegationBinder.AmbiguityResolver
The ambiguity resolver to use by this method delegator.
ambiguityResolver
,
MethodDelegationBinder.BindingResolver
The binding resolver being used to select the relevant method binding.
bindingResolver
)

Creates a new method delegation.

private
MethodDelegation(MethodDelegation.ImplementationDelegate
The implementation delegate to use by this method delegator.
implementationDelegate
,
List<TargetMethodAnnotationDrivenBinder.ParameterBinder<?>>
The parameter binders to use by this method delegator.
parameterBinders
,
MethodDelegationBinder.AmbiguityResolver
The ambiguity resolver to use by this method delegator.
ambiguityResolver
,
MethodDelegationBinder.TerminationHandler
The termination handler to apply.
terminationHandler
,
MethodDelegationBinder.BindingResolver
The binding resolver being used to select the relevant method binding.
bindingResolver
,
Assigner
The assigner to be supplied by this method delegator.
assigner
)

Creates a new method delegation.

Method Summary

Modifier and TypeMethod and Description
public Implementation
andThen(Implementation
The subsequent implementation.
implementation
)

Implements net.bytebuddy.implementation.Implementation.Composable.andThen.

Appends the supplied implementation to this implementation.
public Implementation.Composable
andThen(Implementation.Composable
The subsequent composable implementation.
implementation
)

Implements net.bytebuddy.implementation.Implementation.Composable.andThen.

Appends the supplied composable implementation to this implementation.
public ByteCodeAppender
appender(Implementation.Target
The target of the current implementation.
implementationTarget
)

Implements net.bytebuddy.implementation.Implementation.appender.

Creates a byte code appender that determines the implementation of the instrumented type's methods.
public InstrumentedType
prepare(InstrumentedType
The instrumented type in its current form.
instrumentedType
)

Implements net.bytebuddy.dynamic.scaffold.InstrumentedType.Prepareable.prepare.

Prepares a given instrumented type.
public static MethodDelegation

Returns:

A method delegation that redirects method calls to a static method of the supplied type.
to
(Class<?>
The target type for the delegation.
type
)

Delegates any intercepted method to invoke a static method that is declared by the supplied type.

public static MethodDelegation

Returns:

A method delegation that redirects method calls to a static method of the supplied type.
to
(TypeDescription
The target type for the delegation.
typeDescription
)

Delegates any intercepted method to invoke a static method that is declared by the supplied type.

public static MethodDelegation

Returns:

A method delegation that redirects method calls to a static method of the supplied type.
to
(Object
The target instance for the delegation.
target
)

Delegates any intercepted method to invoke a non-static method that is declared by the supplied type's instance or any of its super types.

public static MethodDelegation

Returns:

A method delegation that redirects method calls to a static method of the supplied type.
to
(Object
The target instance for the delegation.
target
,
MethodGraph.Compiler
The method graph compiler to use.
methodGraphCompiler
)

Delegates any intercepted method to invoke a non-static method that is declared by the supplied type's instance or any of its super types.

public static MethodDelegation

Returns:

A method delegation that redirects method calls to a static method of the supplied type.
to
(Object
The target instance for the delegation.
target
,
String
The name of the field that is holding the target instance.
fieldName
)

Delegates any intercepted method to invoke a non-static method that is declared by the supplied type's instance or any of its super types.

public static MethodDelegation

Returns:

A method delegation that redirects method calls to a static method of the supplied type.
to
(Object
The target instance for the delegation.
target
,
String
The name of the field that is holding the target instance.
fieldName
,
MethodGraph.Compiler
The method graph compiler to use.
methodGraphCompiler
)

Delegates any intercepted method to invoke a non-static method that is declared by the supplied type's instance or any of its super types.

public static MethodDelegation

Returns:

A method delegation that redirects method calls to a static method of the supplied type.
to
(Object
The target instance for the delegation.
target
,
Type
The most specific type of which target should be considered. Must be a super type of the target's actual type.
type
)

Delegates any intercepted method to invoke a non-static method that is declared by the supplied type's instance or any of its super types.

public static MethodDelegation

Returns:

A method delegation that redirects method calls to a static method of the supplied type.
to
(Object
The target instance for the delegation.
target
,
Type
The most specific type of which target should be considered. Must be a super type of the target's actual type.
type
,
MethodGraph.Compiler
The method graph compiler to use.
methodGraphCompiler
)

Delegates any intercepted method to invoke a non-static method that is declared by the supplied type's instance or any of its super types.

public static MethodDelegation

Returns:

A method delegation that redirects method calls to a static method of the supplied type.
to
(Object
The target instance for the delegation.
target
,
Type
The most specific type of which target should be considered. Must be a super type of the target's actual type.
type
,
String
The name of the field that is holding the target instance.
fieldName
)

Delegates any intercepted method to invoke a non-static method that is declared by the supplied type's instance or any of its super types.

public static MethodDelegation

Returns:

A method delegation that redirects method calls to a static method of the supplied type.
to
(Object
The target instance for the delegation.
target
,
Type
The most specific type of which target should be considered. Must be a super type of the target's actual type.
type
,
String
The name of the field that is holding the target instance.
fieldName
,
MethodGraph.Compiler
The method graph compiler to use.
methodGraphCompiler
)

Delegates any intercepted method to invoke a non-static method that is declared by the supplied type's instance or any of its super types.

public static MethodDelegation

Returns:

A method delegation that redirects method calls to a static method of the supplied type.
to
(Object
The target instance for the delegation.
target
,
TypeDefinition
The most specific type of which target should be considered. Must be a super type of the target's actual type.
typeDefinition
)

Delegates any intercepted method to invoke a non-static method that is declared by the supplied type's instance or any of its super types.

public static MethodDelegation

Returns:

A method delegation that redirects method calls to a static method of the supplied type.
to
(Object
The target instance for the delegation.
target
,
TypeDefinition
The most specific type of which target should be considered. Must be a super type of the target's actual type.
typeDefinition
,
MethodGraph.Compiler
The method graph compiler to use.
methodGraphCompiler
)

Delegates any intercepted method to invoke a non-static method that is declared by the supplied type's instance or any of its super types.

public static MethodDelegation

Returns:

A method delegation that redirects method calls to a static method of the supplied type.
to
(Object
The target instance for the delegation.
target
,
TypeDefinition
The most specific type of which target should be considered. Must be a super type of the target's actual type.
typeDefinition
,
String
The name of the field that is holding the target instance.
fieldName
)

Delegates any intercepted method to invoke a non-static method that is declared by the supplied type's instance or any of its super types.

public static MethodDelegation

Returns:

A method delegation that redirects method calls to a static method of the supplied type.
to
(Object
The target instance for the delegation.
target
,
TypeDefinition
The most specific type of which target should be considered. Must be a super type of the target's actual type.
typeDefinition
,
String
The name of the field that is holding the target instance.
fieldName
,
MethodGraph.Compiler
The method graph compiler to use.
methodGraphCompiler
)

Delegates any intercepted method to invoke a non-static method that is declared by the supplied type's instance or any of its super types.

public static MethodDelegation

Returns:

A delegation that redirects method calls to a constructor of the supplied type.
toConstructor
(Class<?>
The type to construct.
type
)

Delegates any intercepted method to invoke a constructor of the supplied type.

public static MethodDelegation

Returns:

A delegation that redirects method calls to a constructor of the supplied type.
toConstructor
(TypeDescription
The type to construct.
typeDescription
)

Delegates any intercepted method to invoke a constructor of the supplied type.

public static MethodDelegation

Returns:

A delegation that redirects invocations to a method of the specified field's instance.
toField
(String
The field's name.
name
)

Delegates any intercepted method to invoke a non-static method on the instance of the supplied field.

public static MethodDelegation

Returns:

A delegation that redirects invocations to a method of the specified field's instance.
toField
(String
The field's name.
name
,
FieldLocator.Factory
The field locator factory to use.
fieldLocatorFactory
)

Delegates any intercepted method to invoke a non-static method on the instance of the supplied field.

public static MethodDelegation

Returns:

A delegation that redirects invocations to a method of the specified field's instance.
toField
(String
The field's name.
name
,
MethodGraph.Compiler
The method graph compiler to use.
methodGraphCompiler
)

Delegates any intercepted method to invoke a non-static method on the instance of the supplied field.

public static MethodDelegation

Returns:

A delegation that redirects invocations to a method of the specified field's instance.
toField
(String
The field's name.
name
,
FieldLocator.Factory
The field locator factory to use.
fieldLocatorFactory
,
MethodGraph.Compiler
The method graph compiler to use.
methodGraphCompiler
)

Delegates any intercepted method to invoke a non-static method on the instance of the supplied field.

public static MethodDelegation

Returns:

A delegation that redirects invocations to the return value of a method that is declared by the instrumented type.
toMethodReturnOf
(String
The name of the method that returns the delegation target.
name
)

Delegates any intercepted method to invoke a method on an instance that is returned by a parameterless method of the given name.

public static MethodDelegation

Returns:

A delegation that redirects invocations to the return value of a method that is declared by the instrumented type.
toMethodReturnOf
(String
The name of the method that returns the delegation target.
name
,
MethodGraph.Compiler
The method graph compiler to use.
methodGraphCompiler
)

Delegates any intercepted method to invoke a method on an instance that is returned by a parameterless method of the given name.

public Implementation.Composable

Returns:

A method delegation implementation that makes use of the given designer.
withAssigner
(Assigner
The assigner to apply.
assigner
)

Applies an assigner to the method delegation that is used for assigning method return and parameter types.

public static MethodDelegation.WithCustomProperties

Returns:

A method delegation configuration with pre-configuration.
withDefaultConfiguration
()

Creates a configuration builder for a method delegation that is pre-configured with the ambiguity resolvers defined by net.bytebuddy.implementation.bind.MethodDelegationBinder.AmbiguityResolver#DEFAULT and the parameter binders defined by net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder.ParameterBinder#DEFAULTS.

public static MethodDelegation.WithCustomProperties

Returns:

A method delegation configuration without any pre-configuration.
withEmptyConfiguration
()

Creates a configuration builder for a method delegation that does not apply any pre-configured net.bytebuddy.implementation.bind.MethodDelegationBinder.AmbiguityResolvers or net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder.ParameterBinders.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait