T
- source dispatch type, whose methods with dispatch(java.lang.reflect.Method, java.lang.Object[])
will be calledTArg
- argument replacement type, args in dispatch(java.lang.reflect.Method, java.lang.Object[])
matching argumentIndex
will be overriden to objects of this typepublic class ArgumentReplacingDispatcher<T,TArg> extends Object implements Dispatchable<T>
For example, we can override an void onSomething(int x)
calls to have x
always
equal to 1. Or, if using this with a duck typing dispatcher, we could even overwrite x
to
be something
that's not an int
.
Constructor and Description |
---|
ArgumentReplacingDispatcher(Dispatchable<T> target,
int argumentIndex,
TArg replaceWith)
Create a new argument replacing dispatcher; dispatches are forwarded to
target
after the argument is replaced. |
Modifier and Type | Method and Description |
---|---|
Object |
dispatch(Method method,
Object[] args)
Dispatch the method and arguments to this object.
|
public ArgumentReplacingDispatcher(Dispatchable<T> target, int argumentIndex, TArg replaceWith)
target
after the argument is replaced.
For example, if a method onAction(T1 a, Integer b, T2 c)
is invoked, and we wanted
to replace all occurrences of b
with 0xDEADBEEF
, we would set
argumentIndex = 1
and replaceWith = 0xDEADBEEF
.
If a method dispatched has less arguments than argumentIndex
, it is
passed through with the arguments unchanged.
target
- destination dispatch type, methods will be redirected to this dispatcherargumentIndex
- the numeric index of the argument >= 0
replaceWith
- arguments matching argumentIndex
will be replaced with this objectpublic Object dispatch(Method method, Object[] args) throws Throwable
Dispatchable
dispatch
in interface Dispatchable<T>
method
- a method defined in class T
args
- arguments corresponding to said method
method
Throwable
- any exception that might have been raised while invoking the method