HandlerDispatcherpublic class HandlerDispatcher extends Object implements DispatchableForward all interface calls into a handler by posting it as a {@code Runnable}.
All calls will return immediately; functions with return values will return a default
value of {@code null}, {@code 0}, or {@code false} where that value is legal.
Any exceptions thrown on the handler while trying to invoke a method
will be re-thrown. Throwing checked exceptions on a handler which doesn't expect any
checked exceptions to be thrown will result in "undefined" behavior
(although in practice it is usually thrown as normal). |
Fields Summary |
---|
private static final String | TAG | private final Dispatchable | mDispatchTarget | private final android.os.Handler | mHandler |
Constructors Summary |
---|
public HandlerDispatcher(Dispatchable dispatchTarget, android.os.Handler handler)Create a dispatcher that forwards it's dispatch calls by posting
them onto the {@code handler} as a {@code Runnable}.
mDispatchTarget = checkNotNull(dispatchTarget, "dispatchTarget must not be null");
mHandler = checkNotNull(handler, "handler must not be null");
|
Methods Summary |
---|
public java.lang.Object | dispatch(java.lang.reflect.Method method, java.lang.Object[] args)
mHandler.post(new Runnable() {
@Override
public void run() {
try {
mDispatchTarget.dispatch(method, args);
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
// Potential UB. Hopefully 't' is a runtime exception.
UncheckedThrow.throwAnyException(t);
} catch (IllegalAccessException e) {
// Impossible
Log.wtf(TAG, "IllegalAccessException while invoking " + method, e);
} catch (IllegalArgumentException e) {
// Impossible
Log.wtf(TAG, "IllegalArgumentException while invoking " + method, e);
} catch (Throwable e) {
UncheckedThrow.throwAnyException(e);
}
}
});
// TODO handle primitive return values that would avoid NPE if unboxed
return null;
|
|