Class Interceptions
java.lang.Object
org.microbean.interceptor.Interceptions
A utility class that makes
InterceptionFunctions and Runnables that intercept lifecycle events,
constructions, and invocations of methods in accordance with the Jakarta Interceptors specification.- Author:
- Laird Nelson
-
Method Summary
Modifier and TypeMethodDescriptionstatic final InterceptionFunctionofConstruction(Collection<? extends InterceptorMethod> interceptorMethods, MethodHandles.Lookup lookup, Constructor<?> constructor) Returns anInterceptionFunctionwhoseapply(Object...)method will invoke all suppliedInterceptorMethods in encounter order before invoking the suppliedConstructor'snewInstance(Object...)method.static final InterceptionFunctionofConstruction(Collection<? extends InterceptorMethod> interceptorMethods, MethodHandles.Lookup lookup, Constructor<?> constructor, Supplier<? extends Set<Annotation>> interceptorBindingsBootstrap) Returns anInterceptionFunctionwhoseapply(Object...)method will invoke all suppliedInterceptorMethods in encounter order before invoking the suppliedConstructor'snewInstance(Object...)method.static final InterceptionFunctionofConstruction(Collection<? extends InterceptorMethod> interceptorMethods, BiFunction<? super Object, ? super Object[], ?> terminalBiFunction) Returns anInterceptionFunctionwhoseapply(Object...)method will invoke all suppliedInterceptorMethods in encounter order before invoking the suppliedBiFunction'sapply(Object, Object[])method withnull(the return value ofInvocationContext.getTarget(), which will always benullin this scenario) and the return value of an invocation ofInvocationContext.getParameters().static final InterceptionFunctionofConstruction(Collection<? extends InterceptorMethod> interceptorMethods, BiFunction<? super Object, ? super Object[], ?> terminalBiFunction, Supplier<? extends Set<Annotation>> interceptorBindingsBootstrap) Returns anInterceptionFunctionwhoseapply(Object...)method will invoke all suppliedInterceptorMethods in encounter order before invoking the suppliedBiFunction'sapply(Object Object[])method withnull(the return value ofInvocationContext.getTarget(), which will always benullin this scenario) and the return value of an invocation ofInvocationContext.getParameters().static final InterceptionFunctionofInvocation(Collection<? extends InterceptorMethod> interceptorMethods, MethodHandles.Lookup lookup, Method method, Supplier<?> targetBootstrap) Returns anInterceptionFunctionwhoseapply(Object...)method will invoke all suppliedInterceptorMethods 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 final InterceptionFunctionofInvocation(Collection<? extends InterceptorMethod> interceptorMethods, MethodHandles.Lookup lookup, Method method, Supplier<?> targetBootstrap, Supplier<? extends Set<Annotation>> interceptorBindingsBootstrap) Returns anInterceptionFunctionwhoseapply(Object...)method will invoke all suppliedInterceptorMethods 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 final InterceptionFunctionofInvocation(Collection<? extends InterceptorMethod> interceptorMethods, BiFunction<? super Object, ? super Object[], ?> terminalBiFunction, Supplier<?> targetBootstrap) Returns anInterceptionFunctionwhoseapply(Object...)method will invoke all suppliedInterceptorMethods 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 final InterceptionFunctionofInvocation(Collection<? extends InterceptorMethod> interceptorMethods, BiFunction<? super Object, ? super Object[], ?> terminalBiFunction, Supplier<?> targetBootstrap, Supplier<? extends Set<Annotation>> interceptorBindingsBootstrap) Returns anInterceptionFunctionwhoseapply(Object...)method will invoke all suppliedInterceptorMethods 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 final RunnableofLifecycleEvent(Collection<? extends InterceptorMethod> interceptorMethods, Supplier<?> targetBootstrap) Returns aRunnablewhoserun()method will invoke all suppliedInterceptorMethods in encounter order.static final RunnableofLifecycleEvent(Collection<? extends InterceptorMethod> interceptorMethods, Supplier<?> targetBootstrap, Supplier<? extends Set<Annotation>> interceptorBindingsBootstrap) Returns aRunnablewhoserun()method will invoke all suppliedInterceptorMethods in encounter order.static final BiFunction<Object, Object[], Object> Creates and returns aBiFunctionencapsulating the suppliedMethodHandle.static final BiFunction<Object, Object[], Object> terminalBiFunctionOf(MethodHandles.Lookup lookup, Constructor<?> c) Creates and returns aBiFunctionencapsulating the suppliedConstructor.static final BiFunction<Object, Object[], Object> terminalBiFunctionOf(MethodHandles.Lookup lookup, Method m) Creates and returns aBiFunctionencapsulating the suppliedMethod.static final voidA convenience method that ensures that every element of the suppliedargumentsarray can be assigned to a reference bearing the correspondingClassdrawn from the suppliedparameterTypesarray.
-
Method Details
-
ofLifecycleEvent
public static final Runnable ofLifecycleEvent(Collection<? extends InterceptorMethod> interceptorMethods, Supplier<?> targetBootstrap) Returns aRunnablewhoserun()method will invoke all suppliedInterceptorMethods in encounter order.- Parameters:
interceptorMethods- theInterceptorMethods to invoke; may benullin which case the returnedRunnable'srun()method will do nothingtargetBootstrap- aSupplierthat will be called for the initial value to be returned by the first invocation ofInvocationContext.getTarget(); may benullin 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 aRunnablewhoserun()method will invoke all suppliedInterceptorMethods in encounter order.- Parameters:
interceptorMethods- theInterceptorMethods to invoke; may benullin which case the returnedRunnable'srun()method will do nothingtargetBootstrap- aSupplierthat will be called for the initial value to be returned by the first invocation ofInvocationContext.getTarget(); may benullin which case the value too will benullinterceptorBindingsBootstrap- aSupplierof aSetofAnnotations that will be called for the value to be returned by the first invocation ofInvocationContext.getInterceptorBindings(); may benullin which case the value will be an empty, immutableSet- Returns:
- a
Runnable; nevernull
-
ofConstruction
public static final InterceptionFunction ofConstruction(Collection<? extends InterceptorMethod> interceptorMethods, MethodHandles.Lookup lookup, Constructor<?> constructor) throws IllegalAccessException Returns anInterceptionFunctionwhoseapply(Object...)method will invoke all suppliedInterceptorMethods in encounter order before invoking the suppliedConstructor'snewInstance(Object...)method.- Parameters:
interceptorMethods- theInterceptorMethods to invoke; may benull(rather uselessly)lookup- aMethodHandles.Lookup; must not benullconstructor- theConstructorto invoke; may benull(rather uselessly)- Returns:
- an
InterceptionFunction; nevernull - Throws:
NullPointerException- iflookupisnullIllegalAccessException- if unreflecting fails
-
ofConstruction
public static final InterceptionFunction ofConstruction(Collection<? extends InterceptorMethod> interceptorMethods, MethodHandles.Lookup lookup, Constructor<?> constructor, Supplier<? extends Set<Annotation>> interceptorBindingsBootstrap) throws IllegalAccessException Returns anInterceptionFunctionwhoseapply(Object...)method will invoke all suppliedInterceptorMethods in encounter order before invoking the suppliedConstructor'snewInstance(Object...)method.- Parameters:
interceptorMethods- theInterceptorMethods to invoke; may benull(rather uselessly)lookup- aMethodHandles.Lookup; must not benullconstructor- theConstructorto invoke; may benull(rather uselessly)interceptorBindingsBootstrap- aSupplierof aSetofAnnotations that will be called for the value to be returned by the first invocation ofInvocationContext.getInterceptorBindings(); may benullin which case the value will be an empty, immutableSet- Returns:
- an
InterceptionFunction; nevernull - Throws:
NullPointerException- iflookupisnullIllegalAccessException- if unreflecting fails
-
ofConstruction
public static final InterceptionFunction ofConstruction(Collection<? extends InterceptorMethod> interceptorMethods, BiFunction<? super Object, ? super Object[], ?> terminalBiFunction) Returns anInterceptionFunctionwhoseapply(Object...)method will invoke all suppliedInterceptorMethods in encounter order before invoking the suppliedBiFunction'sapply(Object, Object[])method withnull(the return value ofInvocationContext.getTarget(), which will always benullin this scenario) and the return value of an invocation ofInvocationContext.getParameters().- Parameters:
interceptorMethods- theInterceptorMethods to invoke; may benull(rather uselessly)terminalBiFunction- aBiFunctionserving 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 anInterceptionFunctionwhoseapply(Object...)method will invoke all suppliedInterceptorMethods in encounter order before invoking the suppliedBiFunction'sapply(Object Object[])method withnull(the return value ofInvocationContext.getTarget(), which will always benullin this scenario) and the return value of an invocation ofInvocationContext.getParameters().- Parameters:
interceptorMethods- theInterceptorMethods to invoke; may benull(rather uselessly)terminalBiFunction- aBiFunctionserving 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- aSupplierof aSetofAnnotations that will be called for the value to be returned by the first invocation ofInvocationContext.getInterceptorBindings(); may benullin which case the value will be an empty, immutableSet- Returns:
- an
InterceptionFunction; nevernull
-
ofInvocation
public static final InterceptionFunction ofInvocation(Collection<? extends InterceptorMethod> interceptorMethods, MethodHandles.Lookup lookup, Method method, Supplier<?> targetBootstrap) throws IllegalAccessException Returns anInterceptionFunctionwhoseapply(Object...)method will invoke all suppliedInterceptorMethods 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- theInterceptorMethods to invoke; may benull(rather uselessly)lookup- aMethodHandles.Lookup; must not benullmethod- aMethodencapsulating 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- aSupplierthat will be called for the initial value to be returned by the first invocation ofInvocationContext.getTarget(); may benullin which case the value too will benull- Returns:
- an
InterceptionFunction; nevernull - Throws:
NullPointerException- iflookupisnullIllegalAccessException- if unreflecting fails
-
ofInvocation
public static final InterceptionFunction ofInvocation(Collection<? extends InterceptorMethod> interceptorMethods, MethodHandles.Lookup lookup, Method method, Supplier<?> targetBootstrap, Supplier<? extends Set<Annotation>> interceptorBindingsBootstrap) throws IllegalAccessException Returns anInterceptionFunctionwhoseapply(Object...)method will invoke all suppliedInterceptorMethods 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- theInterceptorMethods to invoke; may benull(rather uselessly)lookup- aMethodHandles.Lookup; must not benullmethod- aMethodencapsulating 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- aSupplierthat will be called for the initial value to be returned by the first invocation ofInvocationContext.getTarget(); may benullin which case the value too will benullinterceptorBindingsBootstrap- aSupplierof aSetofAnnotations that will be called for the value to be returned by the first invocation ofInvocationContext.getInterceptorBindings(); may benullin which case the value will be an empty, immutableSet- Returns:
- an
InterceptionFunction; nevernull - Throws:
NullPointerException- iflookupisnullIllegalAccessException- if unreflecting fails
-
ofInvocation
public static final InterceptionFunction ofInvocation(Collection<? extends InterceptorMethod> interceptorMethods, BiFunction<? super Object, ? super Object[], ?> terminalBiFunction, Supplier<?> targetBootstrap) Returns anInterceptionFunctionwhoseapply(Object...)method will invoke all suppliedInterceptorMethods 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- theInterceptorMethods to invoke; may benull(rather uselessly)terminalBiFunction- aBiFunctionencapsulating 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- aSupplierthat will be called for the initial value to be returned by the first invocation ofInvocationContext.getTarget(); may benullin 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 anInterceptionFunctionwhoseapply(Object...)method will invoke all suppliedInterceptorMethods 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- theInterceptorMethods to invoke; may benull(rather uselessly)terminalBiFunction- aBiFunctionencapsulating 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- aSupplierthat will be called for the initial value to be returned by the first invocation ofInvocationContext.getTarget(); may benullin which case the value too will benullinterceptorBindingsBootstrap- aSupplierof aSetofAnnotations that will be called for the value to be returned by the first invocation ofInvocationContext.getInterceptorBindings(); may benullin which case the value will be an empty, immutableSet- Returns:
- an
InterceptionFunction; nevernull
-
terminalBiFunctionOf
public static final BiFunction<Object,Object[], terminalBiFunctionOfObject> (MethodHandles.Lookup lookup, Constructor<?> c) throws IllegalAccessException Creates and returns aBiFunctionencapsulating the suppliedConstructor.- Parameters:
lookup- aMethodHandles.Lookup; must not benullc- aConstructor; must not benull- Returns:
- a
BiFunctionencapsulating the suppliedConstructor; nevernull - Throws:
NullPointerException- if any argumebnt isnullIllegalAccessException- if unreflecting fails
-
terminalBiFunctionOf
public static final BiFunction<Object,Object[], terminalBiFunctionOfObject> (MethodHandles.Lookup lookup, Method m) throws IllegalAccessException Creates and returns aBiFunctionencapsulating the suppliedMethod.- Parameters:
lookup- aMethodHandles.Lookup; must not benullm- aMethod; must not benull- Returns:
- a
BiFunctionencapsulating the suppliedMethod; nevernull - Throws:
NullPointerException- ifmisnullIllegalAccessException- if unreflecting fails
-
terminalBiFunctionOf
Creates and returns aBiFunctionencapsulating the suppliedMethodHandle.- Parameters:
mh- aMethodHandle; must not benull- Returns:
- a
BiFunctionencapsulating the suppliedMethodHandle; nevernull - Throws:
NullPointerException- ifmhisnull
-
validate
A convenience method that ensures that every element of the suppliedargumentsarray can be assigned to a reference bearing the correspondingClassdrawn from the suppliedparameterTypesarray.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 ofClassinstances; may benull; must not containnullelements orvoid.class; must have a length equal to that of the suppliedargumentsarrayarguments- an array ofObjects; may benull; must have a length equal to that of the suppliedparameterTypesarray- Throws:
IllegalArgumentException- if validation fails, i.e. if the length ofparameterTypesis not equal to the length ofarguments, or if an element ofparameterTypesisnullorvoid.class, or if not every element of the suppliedargumentsarray can be assigned to a reference bearing the correspondingClassdrawn from the suppliedparameterTypesarray
-