View Javadoc
1   /*
2    * Copyright (c), Data Geekery GmbH, contact@datageekery.com
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  
17  package org.basepom.mojo.propertyhelper;
18  
19  
20  import java.io.IOException;
21  import java.io.UncheckedIOException;
22  import java.lang.reflect.InvocationTargetException;
23  
24  public final class Sneaky {
25  
26      private Sneaky() {
27      }
28  
29      public static RuntimeException throwAnyway(Throwable t) {
30  
31          if (t instanceof Error) {
32              throw (Error) t;
33          } else if (t instanceof RuntimeException) {
34              throw (RuntimeException) t;
35          } else if (t instanceof IOException) {
36              throw new UncheckedIOException((IOException) t);
37          } else if (t instanceof InterruptedException) {
38              Thread.currentThread().interrupt();
39          } else if (t instanceof InvocationTargetException) {
40              throw throwAnyway(t.getCause());
41          }
42  
43          throw throwEvadingChecks(t);
44      }
45  
46      @SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"})
47      private static <E extends Throwable> E throwEvadingChecks(Throwable throwable) throws E {
48          throw (E) throwable;
49      }
50  }