|
1 /* |
|
2 * ==================================================================== |
|
3 * Licensed to the Apache Software Foundation (ASF) under one |
|
4 * or more contributor license agreements. See the NOTICE file |
|
5 * distributed with this work for additional information |
|
6 * regarding copyright ownership. The ASF licenses this file |
|
7 * to you under the Apache License, Version 2.0 (the |
|
8 * "License"); you may not use this file except in compliance |
|
9 * with the License. You may obtain a copy of the License at |
|
10 * |
|
11 * http://www.apache.org/licenses/LICENSE-2.0 |
|
12 * |
|
13 * Unless required by applicable law or agreed to in writing, |
|
14 * software distributed under the License is distributed on an |
|
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
16 * KIND, either express or implied. See the License for the |
|
17 * specific language governing permissions and limitations |
|
18 * under the License. |
|
19 * ==================================================================== |
|
20 * |
|
21 * This software consists of voluntary contributions made by many |
|
22 * individuals on behalf of the Apache Software Foundation. For more |
|
23 * information on the Apache Software Foundation, please see |
|
24 * <http://www.apache.org/>. |
|
25 * |
|
26 */ |
|
27 package ch.boye.httpclientandroidlib.util; |
|
28 |
|
29 import java.lang.reflect.Method; |
|
30 |
|
31 /** |
|
32 * The home for utility methods that handle various exception-related tasks. |
|
33 * |
|
34 * |
|
35 * @since 4.0 |
|
36 */ |
|
37 public final class ExceptionUtils { |
|
38 |
|
39 /** A reference to Throwable's initCause method, or null if it's not there in this JVM */ |
|
40 static private final Method INIT_CAUSE_METHOD = getInitCauseMethod(); |
|
41 |
|
42 /** |
|
43 * Returns a <code>Method<code> allowing access to |
|
44 * {@link Throwable#initCause(Throwable) initCause} method of {@link Throwable}, |
|
45 * or <code>null</code> if the method |
|
46 * does not exist. |
|
47 * |
|
48 * @return A <code>Method<code> for <code>Throwable.initCause</code>, or |
|
49 * <code>null</code> if unavailable. |
|
50 */ |
|
51 static private Method getInitCauseMethod() { |
|
52 try { |
|
53 Class[] paramsClasses = new Class[] { Throwable.class }; |
|
54 return Throwable.class.getMethod("initCause", paramsClasses); |
|
55 } catch (NoSuchMethodException e) { |
|
56 return null; |
|
57 } |
|
58 } |
|
59 |
|
60 /** |
|
61 * If we're running on JDK 1.4 or later, initialize the cause for the given throwable. |
|
62 * |
|
63 * @param throwable The throwable. |
|
64 * @param cause The cause of the throwable. |
|
65 */ |
|
66 public static void initCause(Throwable throwable, Throwable cause) { |
|
67 if (INIT_CAUSE_METHOD != null) { |
|
68 try { |
|
69 INIT_CAUSE_METHOD.invoke(throwable, new Object[] { cause }); |
|
70 } catch (Exception e) { |
|
71 // Well, with no logging, the only option is to munch the exception |
|
72 } |
|
73 } |
|
74 } |
|
75 |
|
76 private ExceptionUtils() { |
|
77 } |
|
78 |
|
79 } |