001    /*
002     * Created on Mar 19, 2009
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005     * in compliance with the License. You may obtain a copy of the License at
006     *
007     * http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software distributed under the License
010     * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011     * or implied. See the License for the specific language governing permissions and limitations under
012     * the License.
013     *
014     * Copyright @2009 the original author or authors.
015     */
016    package org.fest.reflect.util;
017    
018    import java.lang.reflect.InvocationTargetException;
019    
020    /**
021     * Understands utility methods related to <code>{@link Throwable}</code>s.
022     *
023     * @author Alex Ruiz
024     * @since 1.2
025     */
026    public final class Throwables {
027    
028      /**
029       * Obtains the target of the given <code>{@link Throwable}</code>. If the <code>Throwable</code> is a
030       * <code>{@link InvocationTargetException}</code>, this method will return the "target exception" (not the cause.)
031       * For other <code>Throwable</code>s, the same instance is returned unmodified.
032       * @param t the given <code>Throwable</code>.
033       * @return the target exception, if applicable. Otherwise, this method returns the same <code>Throwable</code> passed
034       * as argument.
035       */
036      public static Throwable targetOf(Throwable t) {
037        if (t instanceof InvocationTargetException)
038          return ((InvocationTargetException)t).getTargetException();
039        return t;
040      }
041    
042      private Throwables() {}
043    }