001/*
002 * Copyright (c), Data Geekery GmbH, contact@datageekery.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.basepom.mojo.propertyhelper;
018
019
020import java.io.IOException;
021import java.io.UncheckedIOException;
022import java.lang.reflect.InvocationTargetException;
023
024public final class Sneaky {
025
026    private Sneaky() {
027    }
028
029    public static RuntimeException throwAnyway(Throwable t) {
030
031        if (t instanceof Error) {
032            throw (Error) t;
033        } else if (t instanceof RuntimeException) {
034            throw (RuntimeException) t;
035        } else if (t instanceof IOException) {
036            throw new UncheckedIOException((IOException) t);
037        } else if (t instanceof InterruptedException) {
038            Thread.currentThread().interrupt();
039        } else if (t instanceof InvocationTargetException) {
040            throw throwAnyway(t.getCause());
041        }
042
043        throw throwEvadingChecks(t);
044    }
045
046    @SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"})
047    private static <E extends Throwable> E throwEvadingChecks(Throwable throwable) throws E {
048        throw (E) throwable;
049    }
050}