michael@0: /*
michael@0: * ====================================================================
michael@0: * Licensed to the Apache Software Foundation (ASF) under one
michael@0: * or more contributor license agreements. See the NOTICE file
michael@0: * distributed with this work for additional information
michael@0: * regarding copyright ownership. The ASF licenses this file
michael@0: * to you under the Apache License, Version 2.0 (the
michael@0: * "License"); you may not use this file except in compliance
michael@0: * with the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing,
michael@0: * software distributed under the License is distributed on an
michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0: * KIND, either express or implied. See the License for the
michael@0: * specific language governing permissions and limitations
michael@0: * under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: * .
michael@0: *
michael@0: */
michael@0: package ch.boye.httpclientandroidlib.util;
michael@0:
michael@0: import java.lang.reflect.Method;
michael@0:
michael@0: /**
michael@0: * The home for utility methods that handle various exception-related tasks.
michael@0: *
michael@0: *
michael@0: * @since 4.0
michael@0: */
michael@0: public final class ExceptionUtils {
michael@0:
michael@0: /** A reference to Throwable's initCause method, or null if it's not there in this JVM */
michael@0: static private final Method INIT_CAUSE_METHOD = getInitCauseMethod();
michael@0:
michael@0: /**
michael@0: * Returns a Method allowing access to
michael@0: * {@link Throwable#initCause(Throwable) initCause} method of {@link Throwable},
michael@0: * or null
if the method
michael@0: * does not exist.
michael@0: *
michael@0: * @return A Method for Throwable.initCause
, or
michael@0: * null
if unavailable.
michael@0: */
michael@0: static private Method getInitCauseMethod() {
michael@0: try {
michael@0: Class[] paramsClasses = new Class[] { Throwable.class };
michael@0: return Throwable.class.getMethod("initCause", paramsClasses);
michael@0: } catch (NoSuchMethodException e) {
michael@0: return null;
michael@0: }
michael@0: }
michael@0:
michael@0: /**
michael@0: * If we're running on JDK 1.4 or later, initialize the cause for the given throwable.
michael@0: *
michael@0: * @param throwable The throwable.
michael@0: * @param cause The cause of the throwable.
michael@0: */
michael@0: public static void initCause(Throwable throwable, Throwable cause) {
michael@0: if (INIT_CAUSE_METHOD != null) {
michael@0: try {
michael@0: INIT_CAUSE_METHOD.invoke(throwable, new Object[] { cause });
michael@0: } catch (Exception e) {
michael@0: // Well, with no logging, the only option is to munch the exception
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: private ExceptionUtils() {
michael@0: }
michael@0:
michael@0: }