1/*
2 * Copyright (C) 2013 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 */
16
17package android.hardware.camera2.utils;
18
19/**
20 * @hide
21 */
22public class UncheckedThrow {
23
24    /**
25     * Throw any kind of exception without needing it to be checked
26     * @param e any instance of a Exception
27     */
28    public static void throwAnyException(Exception e) {
29        /**
30         *  Abuse type erasure by making the compiler think we are throwing RuntimeException,
31         *  which is unchecked, but then inserting any exception in there.
32         */
33        UncheckedThrow.<RuntimeException>throwAnyImpl(e);
34    }
35
36    /**
37     * Throw any kind of throwable without needing it to be checked
38     * @param e any instance of a Throwable
39     */
40    public static void throwAnyException(Throwable e) {
41        /**
42         *  Abuse type erasure by making the compiler think we are throwing RuntimeException,
43         *  which is unchecked, but then inserting any exception in there.
44         */
45        UncheckedThrow.<RuntimeException>throwAnyImpl(e);
46    }
47
48    @SuppressWarnings("unchecked")
49    private static<T extends Throwable> void throwAnyImpl(Throwable e) throws T {
50        throw (T) e;
51    }
52}
53