Interface HandlerMatcher<T,M>
- Type Parameters:
T- the type of the handler instanceM- the type of the message
- All Known Implementing Classes:
HandlerInspector.MethodHandlerMatcher, HandlerInspector.ObjectHandlerMatcher, WebHandlerMatcher
A HandlerMatcher is a stateless strategy that inspects a target object and a message to:
- Determine whether the target can handle the message (
canHandle(Object)) - Expose the applicable handler methods (
matchingMethods(Object)) - Return a
HandlerInvokercapable of executing the handler method (getInvoker(Object, Object))
Unlike a Handler, a HandlerMatcher does not resolve or manage instances.
It simply inspects a provided target instance and message to resolve possible invocations.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault HandlerMethod<M> bindHandlerMethod(T target) Binds this matcher to a stable target instance, if it can expose a reusable method plan.default HandlerMethodPlanner<M> bindHandlerMethodPlanner(T target) Creates a planner whose invocation plans call handler methods on the supplied target instance.default HandlerMethodPlanner<M> Creates a planner for handler methods declared on the payload itself.booleanReturns whether the given message can be handled by a handler instance of typeT.getInvoker(T target, M message) Attempts to resolve aHandlerInvokerfor the given target instance and message.default HandlerInvokergetInvokerOrNull(T target, M message) Attempts to resolve aHandlerInvokerfor the given target instance and message.matchingMethods(M message) Returns a stream of methods from the target class that match the given message.default HandlerMatcher<T, M> or(HandlerMatcher<T, M> next) Combines thisHandlerMatcherwith anotherHandlerMatcherto form a composite matcher.default HandlerMethodPlan<M> prepareHandlerMethod(T target, M message) Selects and prepares a handler method on the supplied target for the given message.
-
Method Details
-
canHandle
Returns whether the given message can be handled by a handler instance of typeT. This is a lightweight check and may be used for fast filtering or diagnostics.- Parameters:
message- the message to check- Returns:
trueif the matcher may be able to produce an invoker for the given message
-
matchingMethods
Returns a stream of methods from the target class that match the given message. Typically used for diagnostics or documentation tools.- Parameters:
message- the message to match against- Returns:
- a stream of matching
Executablehandler methods
-
getInvoker
Attempts to resolve aHandlerInvokerfor the given target instance and message.- Parameters:
target- the handler objectmessage- the message to be handled- Returns:
- an optional invoker if the message is supported by the target; empty otherwise
-
getInvokerOrNull
Attempts to resolve aHandlerInvokerfor the given target instance and message.This is a lower-allocation counterpart to
getInvoker(Object, Object)for internal hot paths.- Parameters:
target- the handler objectmessage- the message to be handled- Returns:
- an invoker if the message is supported by the target;
nullotherwise
-
bindHandlerMethod
Binds this matcher to a stable target instance, if it can expose a reusable method plan.Most matcher implementations may return
null. Returning a method is only appropriate when the method metadata and target are stable for all matching messages.- Parameters:
target- the handler object- Returns:
- a reusable handler method, or
nullwhen the matcher requires per-message invokers
-
prepareHandlerMethod
Selects and prepares a handler method on the supplied target for the given message.The default uses
bindHandlerMethodPlanner(Object)when available. Returningnulldoes not reject the message; it tells the caller to use regular per-message matching instead.- Parameters:
target- the object that contains the handler methodmessage- the representative message- Returns:
- the reusable invocation plan, or
nullwhen no method matches or safe preparation is unsupported
-
bindHandlerMethodPlanner
Creates a planner whose invocation plans call handler methods on the supplied target instance.The planner may be cached and used concurrently. The default returns
null, so matchers are not required to support prepared invocation.- Parameters:
target- the object that contains the handler methods- Returns:
- a thread-safe planner bound to the target, or
nullwhen preparation is unsupported
-
bindPayloadHandlerMethodPlanner
Creates a planner for handler methods declared on the payload itself.The returned planner may be reused for many payload objects. Its plans must therefore obtain the target from
HandlerInput.getPayload()for every invocation and must never retain the representative payload used to select the method.- Returns:
- a thread-safe payload-target planner, or
nullwhen this matcher cannot safely prepare one
-
or
Combines thisHandlerMatcherwith anotherHandlerMatcherto form a composite matcher. The resulting matcher is capable of delegating matching responsibilities to both the current matcher and the provided next matcher.- Parameters:
next- the nextHandlerMatcherto combine with the current matcher- Returns:
- a new
HandlerMatcherthat combines the current matcher and the provided next matcher
-