1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package android.hardware.camera2.dispatch;
17
18import android.hardware.camera2.utils.UncheckedThrow;
19import android.util.Log;
20
21import java.lang.reflect.InvocationTargetException;
22import java.lang.reflect.Method;
23
24import static com.android.internal.util.Preconditions.*;
25
26
27public class InvokeDispatcher<T> implements Dispatchable<T> {
28
29    private static final String TAG = "InvocationSink";
30    private final T mTarget;
31
32    public InvokeDispatcher(T target) {
33        mTarget = checkNotNull(target, "target must not be null");
34    }
35
36    @Override
37    public Object dispatch(Method method, Object[] args) {
38        try {
39            return method.invoke(mTarget, args);
40        } catch (InvocationTargetException e) {
41            Throwable t = e.getTargetException();
42            // Potential UB. Hopefully 't' is a runtime exception.
43            UncheckedThrow.throwAnyException(t);
44        } catch (IllegalAccessException e) {
45            // Impossible
46            Log.wtf(TAG, "IllegalAccessException while invoking " + method, e);
47        } catch (IllegalArgumentException e) {
48            // Impossible
49            Log.wtf(TAG, "IllegalArgumentException while invoking " + method, e);
50        }
51
52        // unreachable
53        return null;
54    }
55}
56