Package net.bytebuddy.implementation
Class InvocationHandlerAdapter
java.lang.Object
net.bytebuddy.implementation.InvocationHandlerAdapter
- All Implemented Interfaces:
InstrumentedType.Prepareable
,Implementation
,Implementation.Composable
- Direct Known Subclasses:
InvocationHandlerAdapter.ForField
,InvocationHandlerAdapter.ForInstance
@Enhance
public abstract class InvocationHandlerAdapter
extends Object
implements Implementation.Composable
An adapter for adapting an
InvocationHandler
. The adapter allows the invocation handler
to also intercept method calls to non-interface methods.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Allows for the configuration of anAssigner
of anInvocationHandlerAdapter
.protected static class
An implementation of anInvocationHandlerAdapter
that delegates method invocations to an adapter that is stored in an instance field.protected static class
An implementation of anInvocationHandlerAdapter
that delegates method invocations to an adapter that is stored in a static field.static interface
Allows the configuration of privileged lookup for the resolution ofMethod
constants that are provided to the invocation handler.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 Assigner
The assigner that is used for assigning the return invocation handler's return value to the intercepted method's return value.protected final boolean
Determines if theMethod
instances that are handed to the intercepted methods are cached instatic
fields.private static final boolean
Indicates that aMethod
should be cached.private static final boolean
Indicates that a handler is dropping its return value.protected final String
The name of the field for storing an invocation handler.private static final TypeDescription.Generic
A type description of theInvocationHandler
.protected final boolean
Determines if theMethod
instances are retrieved by using anjava.security.AccessController
.private static final boolean
Indicates that a lookup of a method constant should be looked up using anjava.security.AccessController
.protected final boolean
Determines if this implementation is returning the result value or is dropping it.private static final boolean
Indicates that a handler is returning its return value.private static final boolean
Indicates that a value should not be cached.private static final boolean
Indicates that a lookup of a method constant should not be looked up using anjava.security.AccessController
. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
InvocationHandlerAdapter
(String fieldName, boolean cached, boolean privileged, boolean returning, Assigner assigner) Creates a new invocation handler for a given field. -
Method Summary
Modifier and TypeMethodDescriptionprotected ByteCodeAppender.Size
apply
(org.objectweb.asm.MethodVisitor methodVisitor, Implementation.Context implementationContext, MethodDescription instrumentedMethod, StackManipulation preparingManipulation, FieldDescription fieldDescription) Applies an implementation that delegates to a invocation handler.private List<StackManipulation>
argumentValuesOf
(MethodDescription instrumentedMethod) Returns a list of stack manipulations that loads all arguments of an instrumented method.static InvocationHandlerAdapter
of
(InvocationHandler invocationHandler) Creates an implementation for any instance of anInvocationHandler
that delegates all method interceptions to the given instance which will be stored in astatic
field.static InvocationHandlerAdapter
of
(InvocationHandler invocationHandler, String fieldName) Creates an implementation for any instance of anInvocationHandler
that delegates all method interceptions to the given instance which will be stored in astatic
field.static InvocationHandlerAdapter
Creates an implementation for anyInvocationHandler
that delegates all method interceptions to a field with the given name.static InvocationHandlerAdapter
toField
(String name, FieldLocator.Factory fieldLocatorFactory) Creates an implementation for anyInvocationHandler
that delegates all method interceptions to a field with the given name.abstract Implementation
withAssigner
(Assigner assigner) Configures an assigner to use with this invocation handler adapter.By default, anyMethod
instance that is handed over to anInvocationHandler
is cached in a static field.Configures that the method constants supplied to the invocation handler adapter are resolved using anjava.security.AccessController
.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface net.bytebuddy.implementation.Implementation
appender
Methods inherited from interface net.bytebuddy.implementation.Implementation.Composable
andThen, andThen
Methods inherited from interface net.bytebuddy.dynamic.scaffold.InstrumentedType.Prepareable
prepare
-
Field Details
-
INVOCATION_HANDLER_TYPE
A type description of theInvocationHandler
. -
UNCACHED
private static final boolean UNCACHEDIndicates that a value should not be cached.- See Also:
-
CACHED
private static final boolean CACHEDIndicates that aMethod
should be cached.- See Also:
-
UNPRIVILEGED
private static final boolean UNPRIVILEGEDIndicates that a lookup of a method constant should not be looked up using anjava.security.AccessController
.- See Also:
-
PRIVILEGED
private static final boolean PRIVILEGEDIndicates that a lookup of a method constant should be looked up using anjava.security.AccessController
.- See Also:
-
RETURNING
private static final boolean RETURNINGIndicates that a handler is returning its return value.- See Also:
-
DROPPING
private static final boolean DROPPINGIndicates that a handler is dropping its return value.- See Also:
-
fieldName
The name of the field for storing an invocation handler. -
cached
protected final boolean cachedDetermines if theMethod
instances that are handed to the intercepted methods are cached instatic
fields. -
privileged
protected final boolean privilegedDetermines if theMethod
instances are retrieved by using anjava.security.AccessController
. -
returning
protected final boolean returningDetermines if this implementation is returning the result value or is dropping it. -
assigner
The assigner that is used for assigning the return invocation handler's return value to the intercepted method's return value.
-
-
Constructor Details
-
InvocationHandlerAdapter
protected InvocationHandlerAdapter(String fieldName, boolean cached, boolean privileged, boolean returning, Assigner assigner) Creates a new invocation handler for a given field.- Parameters:
fieldName
- The name of the field.cached
- Determines if theMethod
instances that are handed to the intercepted methods are cached instatic
fields.privileged
- Determines if theMethod
instances are retrieved by using anjava.security.AccessController
.returning
- Determines if this implementation is returning the result value or is dropping it.assigner
- The assigner to apply when defining this implementation.
-
-
Method Details
-
of
Creates an implementation for any instance of anInvocationHandler
that delegates all method interceptions to the given instance which will be stored in astatic
field.- Parameters:
invocationHandler
- The invocation handler to which all method calls are delegated.- Returns:
- An implementation that delegates all method interceptions to the given invocation handler.
-
of
Creates an implementation for any instance of anInvocationHandler
that delegates all method interceptions to the given instance which will be stored in astatic
field.- Parameters:
invocationHandler
- The invocation handler to which all method calls are delegated.fieldName
- The name of the field.- Returns:
- An implementation that delegates all method interceptions to the given invocation handler.
-
toField
Creates an implementation for anyInvocationHandler
that delegates all method interceptions to a field with the given name. This field has to be of a subtype of invocation handler and needs to be set before any invocations are intercepted. Otherwise, aNullPointerException
will be thrown.- Parameters:
name
- The name of the field.- Returns:
- An implementation that delegates all method interceptions to an instance field of the given name.
-
toField
public static InvocationHandlerAdapter toField(String name, FieldLocator.Factory fieldLocatorFactory) Creates an implementation for anyInvocationHandler
that delegates all method interceptions to a field with the given name. This field has to be of a subtype of invocation handler and needs to be set before any invocations are intercepted. Otherwise, aNullPointerException
will be thrown.- Parameters:
name
- The name of the field.fieldLocatorFactory
- The field locator factory- Returns:
- An implementation that delegates all method interceptions to an instance field of the given name.
-
argumentValuesOf
Returns a list of stack manipulations that loads all arguments of an instrumented method.- Parameters:
instrumentedMethod
- The method that is instrumented.- Returns:
- A list of stack manipulation that loads all arguments of an instrumented method.
-
withoutMethodCache
By default, anyMethod
instance that is handed over to anInvocationHandler
is cached in a static field. By invoking this method, this feature can be disabled.- Returns:
- A similar invocation handler adapter that applies caching.
-
withAssigner
Configures an assigner to use with this invocation handler adapter.- Parameters:
assigner
- The assigner to apply when defining this implementation.- Returns:
- This instrumentation with the given
assigner
configured.
-
withPrivilegedLookup
Configures that the method constants supplied to the invocation handler adapter are resolved using anjava.security.AccessController
.- Returns:
- This instrumentation with a privileged lookup configured.
-
apply
protected ByteCodeAppender.Size apply(org.objectweb.asm.MethodVisitor methodVisitor, Implementation.Context implementationContext, MethodDescription instrumentedMethod, StackManipulation preparingManipulation, FieldDescription fieldDescription) Applies an implementation that delegates to a invocation handler.- Parameters:
methodVisitor
- The method visitor for writing the byte code to.implementationContext
- The implementation context for the current implementation.instrumentedMethod
- The method that is instrumented.preparingManipulation
- A stack manipulation that applies any preparation to the operand stack.fieldDescription
- The field that contains the value for the invocation handler.- Returns:
- The size of the applied assignment.
-