- java.lang.Object
-
- org.microbean.interceptor.Interceptions
-
public final class Interceptions extends Object
A utility class that makesInterceptionFunction
s andRunnable
s that intercept lifecycle events, constructions, and invocations of methods in accordance with the Jakarta Interceptors specification.- Author:
- Laird Nelson
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static InterceptionFunction
ofConstruction(Collection<? extends InterceptorMethod> interceptorMethods, Constructor<?> constructor)
Returns anInterceptionFunction
whoseapply(Object...)
method will invoke all suppliedInterceptorMethod
s in encounter order before invoking the suppliedConstructor
'snewInstance(Object...)
method.static InterceptionFunction
ofConstruction(Collection<? extends InterceptorMethod> interceptorMethods, Constructor<?> constructor, Supplier<? extends Set<Annotation>> interceptorBindingsBootstrap)
Returns anInterceptionFunction
whoseapply(Object...)
method will invoke all suppliedInterceptorMethod
s in encounter order before invoking the suppliedConstructor
'snewInstance(Object...)
method.static InterceptionFunction
ofConstruction(Collection<? extends InterceptorMethod> interceptorMethods, BiFunction<? super Object,? super Object[],?> terminalBiFunction)
Returns anInterceptionFunction
whoseapply(Object...)
method will invoke all suppliedInterceptorMethod
s in encounter order before invoking the suppliedBiFunction
'sapply(Object, Object[])
method withnull
(the return value ofInvocationContext.getTarget()
, which will always benull
in this scenario) and the return value of an invocation ofInvocationContext.getParameters()
.static InterceptionFunction
ofConstruction(Collection<? extends InterceptorMethod> interceptorMethods, BiFunction<? super Object,? super Object[],?> terminalBiFunction, Supplier<? extends Set<Annotation>> interceptorBindingsBootstrap)
Returns anInterceptionFunction
whoseapply(Object...)
method will invoke all suppliedInterceptorMethod
s in encounter order before invoking the suppliedBiFunction
'sapply(Object Object[])
method withnull
(the return value ofInvocationContext.getTarget()
, which will always benull
in this scenario) and the return value of an invocation ofInvocationContext.getParameters()
.static InterceptionFunction
ofInvocation(Collection<? extends InterceptorMethod> interceptorMethods, Method method, Supplier<?> targetBootstrap)
Returns anInterceptionFunction
whoseapply(Object...)
method will invoke all suppliedInterceptorMethod
s in encounter order before invoking the suppliedMethod
'sinvoke(Object, Object...)
method with the return value ofInvocationContext.getTarget()
, and with the return value ofInvocationContext.getParameters()
.static InterceptionFunction
ofInvocation(Collection<? extends InterceptorMethod> interceptorMethods, Method method, Supplier<?> targetBootstrap, Supplier<? extends Set<Annotation>> interceptorBindingsBootstrap)
Returns anInterceptionFunction
whoseapply(Object...)
method will invoke all suppliedInterceptorMethod
s in encounter order before invoking the suppliedMethod
'sinvoke(Object, Object...)
method with the return value ofInvocationContext.getTarget()
, and with the return value ofInvocationContext.getParameters()
.static InterceptionFunction
ofInvocation(Collection<? extends InterceptorMethod> interceptorMethods, BiFunction<? super Object,? super Object[],?> terminalBiFunction, Supplier<?> targetBootstrap)
Returns anInterceptionFunction
whoseapply(Object...)
method will invoke all suppliedInterceptorMethod
s in encounter order before invoking the suppliedBiFunction
'sapply(Object Object[])
method with the return value ofInvocationContext.getTarget()
, and with the return value ofInvocationContext.getParameters()
.static InterceptionFunction
ofInvocation(Collection<? extends InterceptorMethod> interceptorMethods, BiFunction<? super Object,? super Object[],?> terminalBiFunction, Supplier<?> targetBootstrap, Supplier<? extends Set<Annotation>> interceptorBindingsBootstrap)
Returns anInterceptionFunction
whoseapply(Object...)
method will invoke all suppliedInterceptorMethod
s in encounter order before invoking the suppliedBiFunction
'sapply(Object Object[])
method with the return value ofInvocationContext.getTarget()
, and with the return value ofInvocationContext.getParameters()
.static Runnable
ofLifecycleEvent(Collection<? extends InterceptorMethod> interceptorMethods, Supplier<?> targetBootstrap)
Returns aRunnable
whoserun()
method will invoke all suppliedInterceptorMethod
s in encounter order.static Runnable
ofLifecycleEvent(Collection<? extends InterceptorMethod> interceptorMethods, Supplier<?> targetBootstrap, Supplier<? extends Set<Annotation>> interceptorBindingsBootstrap)
Returns aRunnable
whoserun()
method will invoke all suppliedInterceptorMethod
s in encounter order.static BiFunction<Object,Object[],Object>
terminalBiFunctionOf(MethodHandle mh)
Creates and returns aBiFunction
encapsulating the suppliedMethodHandle
.static BiFunction<Object,Object[],Object>
terminalBiFunctionOf(Constructor<?> c)
Creates and returns aBiFunction
encapsulating the suppliedConstructor
.static BiFunction<Object,Object[],Object>
terminalBiFunctionOf(Method m)
Creates and returns aBiFunction
encapsulating the suppliedMethod
.static void
validate(Class<?>[] parameterTypes, Object[] arguments)
A convenience method that ensures that every element of the suppliedarguments
array can be assigned to a reference bearing the correspondingClass
drawn from the suppliedparameterTypes
array.
-
-
-
Method Detail
-
ofLifecycleEvent
public static final Runnable ofLifecycleEvent(Collection<? extends InterceptorMethod> interceptorMethods, Supplier<?> targetBootstrap)
Returns aRunnable
whoserun()
method will invoke all suppliedInterceptorMethod
s in encounter order.- Parameters:
interceptorMethods
- theInterceptorMethod
s to invoke; may benull
in which case the returnedRunnable
'srun()
method will do nothingtargetBootstrap
- aSupplier
that will be called for the initial value to be returned by the first invocation ofInvocationContext.getTarget()
; may benull
in which case the value too will benull
- Returns:
- a
Runnable
; nevernull
-
ofLifecycleEvent
public static final Runnable ofLifecycleEvent(Collection<? extends InterceptorMethod> interceptorMethods, Supplier<?> targetBootstrap, Supplier<? extends Set<Annotation>> interceptorBindingsBootstrap)
Returns aRunnable
whoserun()
method will invoke all suppliedInterceptorMethod
s in encounter order.- Parameters:
interceptorMethods
- theInterceptorMethod
s to invoke; may benull
in which case the returnedRunnable
'srun()
method will do nothingtargetBootstrap
- aSupplier
that will be called for the initial value to be returned by the first invocation ofInvocationContext.getTarget()
; may benull
in which case the value too will benull
interceptorBindingsBootstrap
- aSupplier
of aSet
ofAnnotation
s that will be called for the value to be returned by the first invocation ofInvocationContext.getInterceptorBindings()
; may benull
in which case the value will be an empty, immutableSet
- Returns:
- a
Runnable
; nevernull
-
ofConstruction
public static final InterceptionFunction ofConstruction(Collection<? extends InterceptorMethod> interceptorMethods, Constructor<?> constructor) throws IllegalAccessException
Returns anInterceptionFunction
whoseapply(Object...)
method will invoke all suppliedInterceptorMethod
s in encounter order before invoking the suppliedConstructor
'snewInstance(Object...)
method.- Parameters:
interceptorMethods
- theInterceptorMethod
s to invoke; may benull
(rather uselessly)constructor
- theConstructor
to invoke; may benull
(rather uselessly)- Returns:
- an
InterceptionFunction
; nevernull
- Throws:
IllegalAccessException
- if unreflecting fails
-
ofConstruction
public static final InterceptionFunction ofConstruction(Collection<? extends InterceptorMethod> interceptorMethods, Constructor<?> constructor, Supplier<? extends Set<Annotation>> interceptorBindingsBootstrap) throws IllegalAccessException
Returns anInterceptionFunction
whoseapply(Object...)
method will invoke all suppliedInterceptorMethod
s in encounter order before invoking the suppliedConstructor
'snewInstance(Object...)
method.- Parameters:
interceptorMethods
- theInterceptorMethod
s to invoke; may benull
(rather uselessly)constructor
- theConstructor
to invoke; may benull
(rather uselessly)interceptorBindingsBootstrap
- aSupplier
of aSet
ofAnnotation
s that will be called for the value to be returned by the first invocation ofInvocationContext.getInterceptorBindings()
; may benull
in which case the value will be an empty, immutableSet
- Returns:
- an
InterceptionFunction
; nevernull
- Throws:
IllegalAccessException
- if unreflecting fails
-
ofConstruction
public static final InterceptionFunction ofConstruction(Collection<? extends InterceptorMethod> interceptorMethods, BiFunction<? super Object,? super Object[],?> terminalBiFunction)
Returns anInterceptionFunction
whoseapply(Object...)
method will invoke all suppliedInterceptorMethod
s in encounter order before invoking the suppliedBiFunction
'sapply(Object, Object[])
method withnull
(the return value ofInvocationContext.getTarget()
, which will always benull
in this scenario) and the return value of an invocation ofInvocationContext.getParameters()
.- Parameters:
interceptorMethods
- theInterceptorMethod
s to invoke; may benull
(rather uselessly)terminalBiFunction
- aBiFunction
serving as a notional constructor that takesnull
, always, as its first argument, and the return value of an invocation ofInvocationContext.getParameters()
as its second argument, and returns a new instance; may benull
(rather uselessly)- Returns:
- an
InterceptionFunction
; nevernull
-
ofConstruction
public static final InterceptionFunction ofConstruction(Collection<? extends InterceptorMethod> interceptorMethods, BiFunction<? super Object,? super Object[],?> terminalBiFunction, Supplier<? extends Set<Annotation>> interceptorBindingsBootstrap)
Returns anInterceptionFunction
whoseapply(Object...)
method will invoke all suppliedInterceptorMethod
s in encounter order before invoking the suppliedBiFunction
'sapply(Object Object[])
method withnull
(the return value ofInvocationContext.getTarget()
, which will always benull
in this scenario) and the return value of an invocation ofInvocationContext.getParameters()
.- Parameters:
interceptorMethods
- theInterceptorMethod
s to invoke; may benull
(rather uselessly)terminalBiFunction
- aBiFunction
serving as a notional constructor that takesnull
, always, as its first argument, and the return value of an invocation ofInvocationContext.getParameters()
as its second argument, and returns a new instance; may benull
(rather uselessly)interceptorBindingsBootstrap
- aSupplier
of aSet
ofAnnotation
s that will be called for the value to be returned by the first invocation ofInvocationContext.getInterceptorBindings()
; may benull
in which case the value will be an empty, immutableSet
- Returns:
- an
InterceptionFunction
; nevernull
-
ofInvocation
public static final InterceptionFunction ofInvocation(Collection<? extends InterceptorMethod> interceptorMethods, Method method, Supplier<?> targetBootstrap) throws IllegalAccessException
Returns anInterceptionFunction
whoseapply(Object...)
method will invoke all suppliedInterceptorMethod
s in encounter order before invoking the suppliedMethod
'sinvoke(Object, Object...)
method with the return value ofInvocationContext.getTarget()
, and with the return value ofInvocationContext.getParameters()
.- Parameters:
interceptorMethods
- theInterceptorMethod
s to invoke; may benull
(rather uselessly)method
- aMethod
encapsulating the invocation to be intercepted whoseinvoke(Object, Object...)
method takes the return value ofInvocationContext.getTarget()
as its first argument, and the return value ofInvocationContext.getParameters()
spread out appropriately as its trailing arguments; may benull
(rather uselessly)targetBootstrap
- aSupplier
that will be called for the initial value to be returned by the first invocation ofInvocationContext.getTarget()
; may benull
in which case the value too will benull
- Returns:
- an
InterceptionFunction
; nevernull
- Throws:
IllegalAccessException
- if unreflecting fails
-
ofInvocation
public static final InterceptionFunction ofInvocation(Collection<? extends InterceptorMethod> interceptorMethods, Method method, Supplier<?> targetBootstrap, Supplier<? extends Set<Annotation>> interceptorBindingsBootstrap) throws IllegalAccessException
Returns anInterceptionFunction
whoseapply(Object...)
method will invoke all suppliedInterceptorMethod
s in encounter order before invoking the suppliedMethod
'sinvoke(Object, Object...)
method with the return value ofInvocationContext.getTarget()
, and with the return value ofInvocationContext.getParameters()
.- Parameters:
interceptorMethods
- theInterceptorMethod
s to invoke; may benull
(rather uselessly)method
- aMethod
encapsulating the invocation to be intercepted whoseinvoke(Object, Object...)
method takes the return value ofInvocationContext.getTarget()
as its first argument, and the return value ofInvocationContext.getParameters()
spread out appropriately as its trailing arguments; may benull
(rather uselessly)targetBootstrap
- aSupplier
that will be called for the initial value to be returned by the first invocation ofInvocationContext.getTarget()
; may benull
in which case the value too will benull
interceptorBindingsBootstrap
- aSupplier
of aSet
ofAnnotation
s that will be called for the value to be returned by the first invocation ofInvocationContext.getInterceptorBindings()
; may benull
in which case the value will be an empty, immutableSet
- Returns:
- an
InterceptionFunction
; nevernull
- Throws:
IllegalAccessException
- if unreflecting fails
-
ofInvocation
public static final InterceptionFunction ofInvocation(Collection<? extends InterceptorMethod> interceptorMethods, BiFunction<? super Object,? super Object[],?> terminalBiFunction, Supplier<?> targetBootstrap)
Returns anInterceptionFunction
whoseapply(Object...)
method will invoke all suppliedInterceptorMethod
s in encounter order before invoking the suppliedBiFunction
'sapply(Object Object[])
method with the return value ofInvocationContext.getTarget()
, and with the return value ofInvocationContext.getParameters()
.- Parameters:
interceptorMethods
- theInterceptorMethod
s to invoke; may benull
(rather uselessly)terminalBiFunction
- aBiFunction
encapsulating the invocation to be intercepted that takes the return value ofInvocationContext.getTarget()
as its first argument, and the return value ofInvocationContext.getParameters()
as its second argument; may benull
(rather uselessly)targetBootstrap
- aSupplier
that will be called for the initial value to be returned by the first invocation ofInvocationContext.getTarget()
; may benull
in which case the value too will benull
- Returns:
- an
InterceptionFunction
; nevernull
-
ofInvocation
public static final InterceptionFunction ofInvocation(Collection<? extends InterceptorMethod> interceptorMethods, BiFunction<? super Object,? super Object[],?> terminalBiFunction, Supplier<?> targetBootstrap, Supplier<? extends Set<Annotation>> interceptorBindingsBootstrap)
Returns anInterceptionFunction
whoseapply(Object...)
method will invoke all suppliedInterceptorMethod
s in encounter order before invoking the suppliedBiFunction
'sapply(Object Object[])
method with the return value ofInvocationContext.getTarget()
, and with the return value ofInvocationContext.getParameters()
.- Parameters:
interceptorMethods
- theInterceptorMethod
s to invoke; may benull
(rather uselessly)terminalBiFunction
- aBiFunction
encapsulating the invocation to be intercepted that takes the return value ofInvocationContext.getTarget()
as its first argument, and the return value ofInvocationContext.getParameters()
as its second argument; may benull
(rather uselessly)targetBootstrap
- aSupplier
that will be called for the initial value to be returned by the first invocation ofInvocationContext.getTarget()
; may benull
in which case the value too will benull
interceptorBindingsBootstrap
- aSupplier
of aSet
ofAnnotation
s that will be called for the value to be returned by the first invocation ofInvocationContext.getInterceptorBindings()
; may benull
in which case the value will be an empty, immutableSet
- Returns:
- an
InterceptionFunction
; nevernull
-
terminalBiFunctionOf
public static final BiFunction<Object,Object[],Object> terminalBiFunctionOf(Constructor<?> c) throws IllegalAccessException
Creates and returns aBiFunction
encapsulating the suppliedConstructor
.- Parameters:
c
- aConstructor
; must not benull
- Returns:
- a
BiFunction
encapsulating the suppliedConstructor
; nevernull
- Throws:
NullPointerException
- ifc
isnull
IllegalAccessException
- if unreflecting fails
-
terminalBiFunctionOf
public static final BiFunction<Object,Object[],Object> terminalBiFunctionOf(Method m) throws IllegalAccessException
Creates and returns aBiFunction
encapsulating the suppliedMethod
.- Parameters:
m
- aMethod
; must not benull
- Returns:
- a
BiFunction
encapsulating the suppliedMethod
; nevernull
- Throws:
NullPointerException
- ifm
isnull
IllegalAccessException
- if unreflecting fails
-
terminalBiFunctionOf
public static final BiFunction<Object,Object[],Object> terminalBiFunctionOf(MethodHandle mh)
Creates and returns aBiFunction
encapsulating the suppliedMethodHandle
.- Parameters:
mh
- aMethodHandle
; must not benull
- Returns:
- a
BiFunction
encapsulating the suppliedMethodHandle
; nevernull
- Throws:
NullPointerException
- ifmh
isnull
-
validate
public static final void validate(Class<?>[] parameterTypes, Object[] arguments)
A convenience method that ensures that every element of the suppliedarguments
array can be assigned to a reference bearing the correspondingClass
drawn from the suppliedparameterTypes
array.Boxing, unboxing and widening conversions are taken into consideration.
This method implements the logic implied, but nowhere actually specified, by the contract of
InvocationContext.setParameters(Object[])
.- Parameters:
parameterTypes
- an array ofClass
instances; may benull
; must not containnull
elements orvoid.class
; must have a length equal to that of the suppliedarguments
arrayarguments
- an array ofObject
s; may benull
; must have a length equal to that of the suppliedparameterTypes
array- Throws:
IllegalArgumentException
- if validation fails, i.e. if the length ofparameterTypes
is not equal to the length ofarguments
, or if an element ofparameterTypes
isnull
orvoid.class
, or if not every element of the suppliedarguments
array can be assigned to a reference bearing the correspondingClass
drawn from the suppliedparameterTypes
array
-
-