Package net.bytebuddy.implementation
Class InvocationHandlerAdapter
- java.lang.Object
-
- net.bytebuddy.implementation.InvocationHandlerAdapter
-
- All Implemented Interfaces:
InstrumentedType.Prepareable
,Implementation
- Direct Known Subclasses:
InvocationHandlerAdapter.ForField
,InvocationHandlerAdapter.ForInstance
public abstract class InvocationHandlerAdapter extends java.lang.Object implements Implementation
An adapter for adapting anInvocationHandler
. The adapter allows the invocation handler to also intercept method calls to non-interface methods.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static interface
InvocationHandlerAdapter.AssignerConfigurable
Allows for the configuration of anAssigner
of anInvocationHandlerAdapter
.protected static class
InvocationHandlerAdapter.ForField
An implementation of anInvocationHandlerAdapter
that delegates method invocations to an adapter that is stored in an instance field.protected static class
InvocationHandlerAdapter.ForInstance
An implementation of anInvocationHandlerAdapter
that delegates method invocations to an adapter that is stored in a static field.-
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 Assigner
assigner
The assigner that is used for assigning the return invocation handler's return value to the intercepted method's return value.protected boolean
cacheMethods
Determines if theMethod
instances that are handed to the intercepted methods are cached instatic
fields.protected static boolean
CACHING
Indicates that aMethod
should be cached.protected java.lang.String
fieldName
The name of the field for storing an invocation handler.
-
Constructor Summary
Constructors Modifier Constructor Description protected
InvocationHandlerAdapter(java.lang.String fieldName, boolean cacheMethods, Assigner assigner)
Creates a new invocation handler for a given field.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description 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.static InvocationHandlerAdapter
of(java.lang.reflect.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(java.lang.reflect.InvocationHandler invocationHandler, java.lang.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
toField(java.lang.String name)
Creates an implementation for anyInvocationHandler
that delegates all method interceptions to a field with the given name.static InvocationHandlerAdapter
toField(java.lang.String name, FieldLocator.Factory fieldLocatorFactory)
Creates an implementation for anyInvocationHandler
that delegates all method interceptions to a field with the given name.abstract InvocationHandlerAdapter.AssignerConfigurable
withoutMethodCache()
By default, anyMethod
instance that is handed over to anInvocationHandler
is cached in a static field.-
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.dynamic.scaffold.InstrumentedType.Prepareable
prepare
-
-
-
-
Field Detail
-
CACHING
protected static final boolean CACHING
Indicates that aMethod
should be cached.- See Also:
- Constant Field Values
-
fieldName
protected final java.lang.String fieldName
The name of the field for storing an invocation handler.
-
assigner
protected final Assigner assigner
The assigner that is used for assigning the return invocation handler's return value to the intercepted method's return value.
-
cacheMethods
protected final boolean cacheMethods
Determines if theMethod
instances that are handed to the intercepted methods are cached instatic
fields.
-
-
Constructor Detail
-
InvocationHandlerAdapter
protected InvocationHandlerAdapter(java.lang.String fieldName, boolean cacheMethods, Assigner assigner)
Creates a new invocation handler for a given field.- Parameters:
fieldName
- The name of the field.cacheMethods
- Determines if theMethod
instances that are handed to the intercepted methods are cached instatic
fields.assigner
- The assigner to apply when defining this implementation.
-
-
Method Detail
-
of
public static InvocationHandlerAdapter of(java.lang.reflect.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.- 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
public static InvocationHandlerAdapter of(java.lang.reflect.InvocationHandler invocationHandler, java.lang.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.- 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
public static InvocationHandlerAdapter toField(java.lang.String name)
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(java.lang.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.
-
withoutMethodCache
public abstract InvocationHandlerAdapter.AssignerConfigurable 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.
-
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.
-
-