Class Advice.AssignReturned
- java.lang.Object
-
- net.bytebuddy.asm.Advice.AssignReturned
-
- All Implemented Interfaces:
Advice.PostProcessor
- Direct Known Subclasses:
Advice.AssignReturned.ForArray
,Advice.AssignReturned.ForScalar
- Enclosing class:
- Advice
@Enhance public abstract static class Advice.AssignReturned extends java.lang.Object implements Advice.PostProcessor
A post processor that uses the return value of an advice method to define values for fields, arguments, the instrumented method's return value or the exception being thrown. This post processor allows for the assignment of values from advice methods that use delegation. When inlining advice code, it is recommended to assign values directly to annotated parameters.
Assignments can either be performed by returning a single value from an advice method or by returning an array with multiple values. If the return type of an advice method is declared as an array type, the latter is assumed what can be overridden by annotating the advice method with
Advice.AssignReturned.AsScalar
. With a scalar assignment, the return type's default value is considered as an indication to skip any assignment what can be configured via the annotation's property. When returningnull
when using an array-typed assignment, the assignment will always be skipped.Important: This post processor is not registered by default but requires explicit registration via
Advice.WithCustomMapping.with(Advice.PostProcessor.Factory)
.Important: Assignment exceptions will not be handled by a suppression handler but will be propagated.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Advice.AssignReturned.AsScalar
Indicates that the advice method's return value is to be treated as a scalar value also if it is of an array type.protected static class
Advice.AssignReturned.DefaultValueSkip
A stack manipulation that applies a null-check on the returned value which indicates if an assignment should be skipped, if discovered.protected static class
Advice.AssignReturned.ExceptionHandler
An exception handler to handle exceptions during assignment.static class
Advice.AssignReturned.Factory
A factory to create aAdvice.AssignReturned
post processor.protected static class
Advice.AssignReturned.ForArray
A post processor implementation ofAdvice.AssignReturned
that works on the value of an array.protected static class
Advice.AssignReturned.ForScalar
A post processor implementation ofAdvice.AssignReturned
that uses the returned value as such.static interface
Advice.AssignReturned.Handler
A handler for anAdvice.AssignReturned
post processor to assign a value that was returned by advice to a value of the instrumented method.static interface
Advice.AssignReturned.ToAllArguments
Assigns the advice method's return value as an array to a number of arguments which are returned as an array where each element assigns a single value with the same index as the instrumented method's parameter.static interface
Advice.AssignReturned.ToArguments
Assigns the advice method's return value to an argument of the instrumented method of the given index.static interface
Advice.AssignReturned.ToFields
Assigns the advice method's return value to a given field.static interface
Advice.AssignReturned.ToReturned
Assigns the advice method's return value to the instrumented method's return value.static interface
Advice.AssignReturned.ToThis
Assigns the advice method's return value to thethis
reference of the instrumented method.static interface
Advice.AssignReturned.ToThrown
Assigns the advice method's return value as the instrumented method's thrown exception.-
Nested classes/interfaces inherited from interface net.bytebuddy.asm.Advice.PostProcessor
Advice.PostProcessor.Compound, Advice.PostProcessor.NoOp
-
-
Field Summary
Fields Modifier and Type Field Description protected Advice.AssignReturned.ExceptionHandler.Factory
exceptionHandlerFactory
The exception handler factory to use.protected boolean
exit
true
if this post processor is used within exit advice.static int
NO_INDEX
Indicates that a value is not assigned from an array but as a scalar value.protected boolean
skipOnDefaultValue
true
if a default value indicates that no assignment should be conducted.protected TypeDescription
typeDescription
The advice method's return type.
-
Constructor Summary
Constructors Modifier Constructor Description protected
AssignReturned(TypeDescription typeDescription, Advice.AssignReturned.ExceptionHandler.Factory exceptionHandlerFactory, boolean exit, boolean skipOnDefaultValue)
Creates a new post processor for assigning an advice method's return value.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract java.util.Collection<Advice.AssignReturned.Handler>
getHandlers()
Returns a collection of all handlers to apply.protected abstract TypeDescription
getType()
Returns the assigned type that is handled by any handler.StackManipulation
resolve(TypeDescription instrumentedType, MethodDescription instrumentedMethod, Assigner assigner, Advice.ArgumentHandler argumentHandler, Advice.StackMapFrameHandler.ForPostProcessor stackMapFrameHandler, StackManipulation exceptionHandler)
Resolves this post processor for a given instrumented method.protected abstract StackManipulation
toLoadInstruction(Advice.AssignReturned.Handler handler, int offset)
Creates a load instruction for the given handler.
-
-
-
Field Detail
-
NO_INDEX
public static final int NO_INDEX
Indicates that a value is not assigned from an array but as a scalar value.- See Also:
- Constant Field Values
-
typeDescription
protected final TypeDescription typeDescription
The advice method's return type.
-
exceptionHandlerFactory
protected final Advice.AssignReturned.ExceptionHandler.Factory exceptionHandlerFactory
The exception handler factory to use.
-
exit
protected final boolean exit
true
if this post processor is used within exit advice.
-
skipOnDefaultValue
protected final boolean skipOnDefaultValue
true
if a default value indicates that no assignment should be conducted.
-
-
Constructor Detail
-
AssignReturned
protected AssignReturned(TypeDescription typeDescription, Advice.AssignReturned.ExceptionHandler.Factory exceptionHandlerFactory, boolean exit, boolean skipOnDefaultValue)
Creates a new post processor for assigning an advice method's return value.- Parameters:
typeDescription
- The advice method's return type.exceptionHandlerFactory
- The exception handler factory to use.exit
-true
if this post processor is used within exit advice.skipOnDefaultValue
-true
if a default value indicates that no assignment should be conducted.
-
-
Method Detail
-
resolve
public StackManipulation resolve(TypeDescription instrumentedType, MethodDescription instrumentedMethod, Assigner assigner, Advice.ArgumentHandler argumentHandler, Advice.StackMapFrameHandler.ForPostProcessor stackMapFrameHandler, StackManipulation exceptionHandler)
Resolves this post processor for a given instrumented method.- Specified by:
resolve
in interfaceAdvice.PostProcessor
- Parameters:
instrumentedType
- The instrumented type.instrumentedMethod
- The instrumented method.assigner
- The assigner to use.argumentHandler
- The argument handler to use.stackMapFrameHandler
- The argument handler for the instrumented method.exceptionHandler
- The exception handler that is resolved for the instrumented method.- Returns:
- The stack manipulation to apply.
-
getType
protected abstract TypeDescription getType()
Returns the assigned type that is handled by any handler.- Returns:
- The handled type.
-
getHandlers
protected abstract java.util.Collection<Advice.AssignReturned.Handler> getHandlers()
Returns a collection of all handlers to apply.- Returns:
- The handlers to apply.
-
toLoadInstruction
protected abstract StackManipulation toLoadInstruction(Advice.AssignReturned.Handler handler, int offset)
Creates a load instruction for the given handler.- Parameters:
handler
- The handler for which to apply a load instruction.offset
- The offset of the value that is returned by the advice method.- Returns:
- A stack manipulation to load the handled value.
-
-