1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/ExceptionUtils.java $
3 * $Revision: 613003 $
4 * $Date: 2008-01-17 15:00:42 -0800 (Thu, 17 Jan 2008) $
5 *
6 * ====================================================================
7 * Licensed to the Apache Software Foundation (ASF) under one
8 * or more contributor license agreements.  See the NOTICE file
9 * distributed with this work for additional information
10 * regarding copyright ownership.  The ASF licenses this file
11 * to you under the Apache License, Version 2.0 (the
12 * "License"); you may not use this file except in compliance
13 * with the License.  You may obtain a copy of the License at
14 *
15 *   http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing,
18 * software distributed under the License is distributed on an
19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20 * KIND, either express or implied.  See the License for the
21 * specific language governing permissions and limitations
22 * under the License.
23 * ====================================================================
24 *
25 * This software consists of voluntary contributions made by many
26 * individuals on behalf of the Apache Software Foundation.  For more
27 * information on the Apache Software Foundation, please see
28 * <http://www.apache.org/>.
29 *
30 */
31package org.apache.http.util;
32
33import java.lang.reflect.Method;
34
35/**
36 * The home for utility methods that handle various exception-related tasks.
37 *
38 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
39 * @author <a href="mailto:laura@lwerner.org">Laura Werner</a>
40 *
41 * @since 4.0
42 *
43 * @deprecated Please use {@link java.net.URL#openConnection} instead.
44 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
45 *     for further details.
46 */
47@Deprecated
48public final class ExceptionUtils {
49
50    /** A reference to Throwable's initCause method, or null if it's not there in this JVM */
51    static private final Method INIT_CAUSE_METHOD = getInitCauseMethod();
52
53    /**
54     * Returns a <code>Method<code> allowing access to
55     * {@link Throwable#initCause(Throwable) initCause} method of {@link Throwable},
56     * or <code>null</code> if the method
57     * does not exist.
58     *
59     * @return A <code>Method<code> for <code>Throwable.initCause</code>, or
60     * <code>null</code> if unavailable.
61     */
62    static private Method getInitCauseMethod() {
63        try {
64            Class[] paramsClasses = new Class[] { Throwable.class };
65            return Throwable.class.getMethod("initCause", paramsClasses);
66        } catch (NoSuchMethodException e) {
67            return null;
68        }
69    }
70
71    /**
72     * If we're running on JDK 1.4 or later, initialize the cause for the given throwable.
73     *
74     * @param  throwable The throwable.
75     * @param  cause     The cause of the throwable.
76     */
77    public static void initCause(Throwable throwable, Throwable cause) {
78        if (INIT_CAUSE_METHOD != null) {
79            try {
80                INIT_CAUSE_METHOD.invoke(throwable, new Object[] { cause });
81            } catch (Exception e) {
82                // Well, with no logging, the only option is to munch the exception
83            }
84        }
85    }
86
87    private ExceptionUtils() {
88    }
89
90}
91