001/*
002 * Copyright (C) 2007 The Guava Authors
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
015package com.google.common.base;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018import static java.util.Arrays.asList;
019import static java.util.Collections.unmodifiableList;
020
021import com.google.common.annotations.Beta;
022import com.google.common.annotations.GwtCompatible;
023import com.google.common.annotations.GwtIncompatible;
024import com.google.common.annotations.VisibleForTesting;
025import com.google.errorprone.annotations.CanIgnoreReturnValue;
026import java.io.IOException;
027import java.io.PrintWriter;
028import java.io.StringWriter;
029import java.lang.reflect.InvocationTargetException;
030import java.lang.reflect.Method;
031import java.util.AbstractList;
032import java.util.ArrayList;
033import java.util.Collections;
034import java.util.List;
035import org.checkerframework.checker.nullness.qual.Nullable;
036
037/**
038 * Static utility methods pertaining to instances of {@link Throwable}.
039 *
040 * <p>See the Guava User Guide entry on <a
041 * href="https://github.com/google/guava/wiki/ThrowablesExplained">Throwables</a>.
042 *
043 * @author Kevin Bourrillion
044 * @author Ben Yu
045 * @since 1.0
046 */
047@GwtCompatible(emulated = true)
048public final class Throwables {
049  private Throwables() {}
050
051  /**
052   * Throws {@code throwable} if it is an instance of {@code declaredType}. Example usage:
053   *
054   * <pre>
055   * for (Foo foo : foos) {
056   *   try {
057   *     foo.bar();
058   *   } catch (BarException | RuntimeException | Error t) {
059   *     failure = t;
060   *   }
061   * }
062   * if (failure != null) {
063   *   throwIfInstanceOf(failure, BarException.class);
064   *   throwIfUnchecked(failure);
065   *   throw new AssertionError(failure);
066   * }
067   * </pre>
068   *
069   * @since 20.0
070   */
071  @GwtIncompatible // Class.cast, Class.isInstance
072  public static <X extends Throwable> void throwIfInstanceOf(
073      Throwable throwable, Class<X> declaredType) throws X {
074    checkNotNull(throwable);
075    if (declaredType.isInstance(throwable)) {
076      throw declaredType.cast(throwable);
077    }
078  }
079
080  /**
081   * Propagates {@code throwable} exactly as-is, if and only if it is an instance of {@code
082   * declaredType}. Example usage:
083   *
084   * <pre>
085   * try {
086   *   someMethodThatCouldThrowAnything();
087   * } catch (IKnowWhatToDoWithThisException e) {
088   *   handle(e);
089   * } catch (Throwable t) {
090   *   Throwables.propagateIfInstanceOf(t, IOException.class);
091   *   Throwables.propagateIfInstanceOf(t, SQLException.class);
092   *   throw Throwables.propagate(t);
093   * }
094   * </pre>
095   *
096   * @deprecated Use {@link #throwIfInstanceOf}, which has the same behavior but rejects {@code
097   *     null}.
098   */
099  @Deprecated
100  @GwtIncompatible // throwIfInstanceOf
101  public static <X extends Throwable> void propagateIfInstanceOf(
102      @Nullable Throwable throwable, Class<X> declaredType) throws X {
103    if (throwable != null) {
104      throwIfInstanceOf(throwable, declaredType);
105    }
106  }
107
108  /**
109   * Throws {@code throwable} if it is a {@link RuntimeException} or {@link Error}. Example usage:
110   *
111   * <pre>
112   * for (Foo foo : foos) {
113   *   try {
114   *     foo.bar();
115   *   } catch (RuntimeException | Error t) {
116   *     failure = t;
117   *   }
118   * }
119   * if (failure != null) {
120   *   throwIfUnchecked(failure);
121   *   throw new AssertionError(failure);
122   * }
123   * </pre>
124   *
125   * @since 20.0
126   */
127  public static void throwIfUnchecked(Throwable throwable) {
128    checkNotNull(throwable);
129    if (throwable instanceof RuntimeException) {
130      throw (RuntimeException) throwable;
131    }
132    if (throwable instanceof Error) {
133      throw (Error) throwable;
134    }
135  }
136
137  /**
138   * Propagates {@code throwable} exactly as-is, if and only if it is an instance of {@link
139   * RuntimeException} or {@link Error}. Example usage:
140   *
141   * <pre>
142   * try {
143   *   someMethodThatCouldThrowAnything();
144   * } catch (IKnowWhatToDoWithThisException e) {
145   *   handle(e);
146   * } catch (Throwable t) {
147   *   Throwables.propagateIfPossible(t);
148   *   throw new RuntimeException("unexpected", t);
149   * }
150   * </pre>
151   *
152   * @deprecated Use {@link #throwIfUnchecked}, which has the same behavior but rejects {@code
153   *     null}.
154   */
155  @Deprecated
156  @GwtIncompatible
157  public static void propagateIfPossible(@Nullable Throwable throwable) {
158    if (throwable != null) {
159      throwIfUnchecked(throwable);
160    }
161  }
162
163  /**
164   * Propagates {@code throwable} exactly as-is, if and only if it is an instance of {@link
165   * RuntimeException}, {@link Error}, or {@code declaredType}. Example usage:
166   *
167   * <pre>
168   * try {
169   *   someMethodThatCouldThrowAnything();
170   * } catch (IKnowWhatToDoWithThisException e) {
171   *   handle(e);
172   * } catch (Throwable t) {
173   *   Throwables.propagateIfPossible(t, OtherException.class);
174   *   throw new RuntimeException("unexpected", t);
175   * }
176   * </pre>
177   *
178   * @param throwable the Throwable to possibly propagate
179   * @param declaredType the single checked exception type declared by the calling method
180   */
181  @GwtIncompatible // propagateIfInstanceOf
182  public static <X extends Throwable> void propagateIfPossible(
183      @Nullable Throwable throwable, Class<X> declaredType) throws X {
184    propagateIfInstanceOf(throwable, declaredType);
185    propagateIfPossible(throwable);
186  }
187
188  /**
189   * Propagates {@code throwable} exactly as-is, if and only if it is an instance of {@link
190   * RuntimeException}, {@link Error}, {@code declaredType1}, or {@code declaredType2}. In the
191   * unlikely case that you have three or more declared checked exception types, you can handle them
192   * all by invoking these methods repeatedly. See usage example in {@link
193   * #propagateIfPossible(Throwable, Class)}.
194   *
195   * @param throwable the Throwable to possibly propagate
196   * @param declaredType1 any checked exception type declared by the calling method
197   * @param declaredType2 any other checked exception type declared by the calling method
198   */
199  @GwtIncompatible // propagateIfInstanceOf
200  public static <X1 extends Throwable, X2 extends Throwable> void propagateIfPossible(
201      @Nullable Throwable throwable, Class<X1> declaredType1, Class<X2> declaredType2)
202      throws X1, X2 {
203    checkNotNull(declaredType2);
204    propagateIfInstanceOf(throwable, declaredType1);
205    propagateIfPossible(throwable, declaredType2);
206  }
207
208  /**
209   * Propagates {@code throwable} as-is if it is an instance of {@link RuntimeException} or {@link
210   * Error}, or else as a last resort, wraps it in a {@code RuntimeException} and then propagates.
211   *
212   * <p>This method always throws an exception. The {@code RuntimeException} return type allows
213   * client code to signal to the compiler that statements after the call are unreachable. Example
214   * usage:
215   *
216   * <pre>
217   * T doSomething() {
218   *   try {
219   *     return someMethodThatCouldThrowAnything();
220   *   } catch (IKnowWhatToDoWithThisException e) {
221   *     return handle(e);
222   *   } catch (Throwable t) {
223   *     throw Throwables.propagate(t);
224   *   }
225   * }
226   * </pre>
227   *
228   * @param throwable the Throwable to propagate
229   * @return nothing will ever be returned; this return type is only for your convenience, as
230   *     illustrated in the example above
231   * @deprecated Use {@code throw e} or {@code throw new RuntimeException(e)} directly, or use a
232   *     combination of {@link #throwIfUnchecked} and {@code throw new RuntimeException(e)}. For
233   *     background on the deprecation, read <a href="https://goo.gl/Ivn2kc">Why we deprecated
234   *     {@code Throwables.propagate}</a>.
235   */
236  @CanIgnoreReturnValue
237  @GwtIncompatible
238  @Deprecated
239  public static RuntimeException propagate(Throwable throwable) {
240    throwIfUnchecked(throwable);
241    throw new RuntimeException(throwable);
242  }
243
244  /**
245   * Returns the innermost cause of {@code throwable}. The first throwable in a chain provides
246   * context from when the error or exception was initially detected. Example usage:
247   *
248   * <pre>
249   * assertEquals("Unable to assign a customer id", Throwables.getRootCause(e).getMessage());
250   * </pre>
251   *
252   * @throws IllegalArgumentException if there is a loop in the causal chain
253   */
254  public static Throwable getRootCause(Throwable throwable) {
255    // Keep a second pointer that slowly walks the causal chain. If the fast pointer ever catches
256    // the slower pointer, then there's a loop.
257    Throwable slowPointer = throwable;
258    boolean advanceSlowPointer = false;
259
260    Throwable cause;
261    while ((cause = throwable.getCause()) != null) {
262      throwable = cause;
263
264      if (throwable == slowPointer) {
265        throw new IllegalArgumentException("Loop in causal chain detected.", throwable);
266      }
267      if (advanceSlowPointer) {
268        slowPointer = slowPointer.getCause();
269      }
270      advanceSlowPointer = !advanceSlowPointer; // only advance every other iteration
271    }
272    return throwable;
273  }
274
275  /**
276   * Gets a {@code Throwable} cause chain as a list. The first entry in the list will be {@code
277   * throwable} followed by its cause hierarchy. Note that this is a snapshot of the cause chain and
278   * will not reflect any subsequent changes to the cause chain.
279   *
280   * <p>Here's an example of how it can be used to find specific types of exceptions in the cause
281   * chain:
282   *
283   * <pre>
284   * Iterables.filter(Throwables.getCausalChain(e), IOException.class));
285   * </pre>
286   *
287   * @param throwable the non-null {@code Throwable} to extract causes from
288   * @return an unmodifiable list containing the cause chain starting with {@code throwable}
289   * @throws IllegalArgumentException if there is a loop in the causal chain
290   */
291  @Beta // TODO(kevinb): decide best return type
292  public static List<Throwable> getCausalChain(Throwable throwable) {
293    checkNotNull(throwable);
294    List<Throwable> causes = new ArrayList<>(4);
295    causes.add(throwable);
296
297    // Keep a second pointer that slowly walks the causal chain. If the fast pointer ever catches
298    // the slower pointer, then there's a loop.
299    Throwable slowPointer = throwable;
300    boolean advanceSlowPointer = false;
301
302    Throwable cause;
303    while ((cause = throwable.getCause()) != null) {
304      throwable = cause;
305      causes.add(throwable);
306
307      if (throwable == slowPointer) {
308        throw new IllegalArgumentException("Loop in causal chain detected.", throwable);
309      }
310      if (advanceSlowPointer) {
311        slowPointer = slowPointer.getCause();
312      }
313      advanceSlowPointer = !advanceSlowPointer; // only advance every other iteration
314    }
315    return Collections.unmodifiableList(causes);
316  }
317
318  /**
319   * Returns {@code throwable}'s cause, cast to {@code expectedCauseType}.
320   *
321   * <p>Prefer this method instead of manually casting an exception's cause. For example, {@code
322   * (IOException) e.getCause()} throws a {@link ClassCastException} that discards the original
323   * exception {@code e} if the cause is not an {@link IOException}, but {@code
324   * Throwables.getCauseAs(e, IOException.class)} keeps {@code e} as the {@link
325   * ClassCastException}'s cause.
326   *
327   * @throws ClassCastException if the cause cannot be cast to the expected type. The {@code
328   *     ClassCastException}'s cause is {@code throwable}.
329   * @since 22.0
330   */
331  @Beta
332  @GwtIncompatible // Class.cast(Object)
333  public static <X extends Throwable> X getCauseAs(
334      Throwable throwable, Class<X> expectedCauseType) {
335    try {
336      return expectedCauseType.cast(throwable.getCause());
337    } catch (ClassCastException e) {
338      e.initCause(throwable);
339      throw e;
340    }
341  }
342
343  /**
344   * Returns a string containing the result of {@link Throwable#toString() toString()}, followed by
345   * the full, recursive stack trace of {@code throwable}. Note that you probably should not be
346   * parsing the resulting string; if you need programmatic access to the stack frames, you can call
347   * {@link Throwable#getStackTrace()}.
348   */
349  @GwtIncompatible // java.io.PrintWriter, java.io.StringWriter
350  public static String getStackTraceAsString(Throwable throwable) {
351    StringWriter stringWriter = new StringWriter();
352    throwable.printStackTrace(new PrintWriter(stringWriter));
353    return stringWriter.toString();
354  }
355
356  /**
357   * Returns the stack trace of {@code throwable}, possibly providing slower iteration over the full
358   * trace but faster iteration over parts of the trace. Here, "slower" and "faster" are defined in
359   * comparison to the normal way to access the stack trace, {@link Throwable#getStackTrace()
360   * throwable.getStackTrace()}. Note, however, that this method's special implementation is not
361   * available for all platforms and configurations. If that implementation is unavailable, this
362   * method falls back to {@code getStackTrace}. Callers that require the special implementation can
363   * check its availability with {@link #lazyStackTraceIsLazy()}.
364   *
365   * <p>The expected (but not guaranteed) performance of the special implementation differs from
366   * {@code getStackTrace} in one main way: The {@code lazyStackTrace} call itself returns quickly
367   * by delaying the per-stack-frame work until each element is accessed. Roughly speaking:
368   *
369   * <ul>
370   *   <li>{@code getStackTrace} takes {@code stackSize} time to return but then negligible time to
371   *       retrieve each element of the returned list.
372   *   <li>{@code lazyStackTrace} takes negligible time to return but then {@code 1/stackSize} time
373   *       to retrieve each element of the returned list (probably slightly more than {@code
374   *       1/stackSize}).
375   * </ul>
376   *
377   * <p>Note: The special implementation does not respect calls to {@link Throwable#setStackTrace
378   * throwable.setStackTrace}. Instead, it always reflects the original stack trace from the
379   * exception's creation.
380   *
381   * @since 19.0
382   */
383  // TODO(cpovirk): Say something about the possibility that List access could fail at runtime?
384  @Beta
385  @GwtIncompatible // lazyStackTraceIsLazy, jlaStackTrace
386  // TODO(cpovirk): Consider making this available under GWT (slow implementation only).
387  public static List<StackTraceElement> lazyStackTrace(Throwable throwable) {
388    return lazyStackTraceIsLazy()
389        ? jlaStackTrace(throwable)
390        : unmodifiableList(asList(throwable.getStackTrace()));
391  }
392
393  /**
394   * Returns whether {@link #lazyStackTrace} will use the special implementation described in its
395   * documentation.
396   *
397   * @since 19.0
398   */
399  @Beta
400  @GwtIncompatible // getStackTraceElementMethod
401  public static boolean lazyStackTraceIsLazy() {
402    return getStackTraceElementMethod != null && getStackTraceDepthMethod != null;
403  }
404
405  @GwtIncompatible // invokeAccessibleNonThrowingMethod
406  private static List<StackTraceElement> jlaStackTrace(final Throwable t) {
407    checkNotNull(t);
408    /*
409     * TODO(cpovirk): Consider optimizing iterator() to catch IOOBE instead of doing bounds checks.
410     *
411     * TODO(cpovirk): Consider the UnsignedBytes pattern if it performs faster and doesn't cause
412     * AOSP grief.
413     */
414    return new AbstractList<StackTraceElement>() {
415      @Override
416      public StackTraceElement get(int n) {
417        return (StackTraceElement)
418            invokeAccessibleNonThrowingMethod(getStackTraceElementMethod, jla, t, n);
419      }
420
421      @Override
422      public int size() {
423        return (Integer) invokeAccessibleNonThrowingMethod(getStackTraceDepthMethod, jla, t);
424      }
425    };
426  }
427
428  @GwtIncompatible // java.lang.reflect
429  private static Object invokeAccessibleNonThrowingMethod(
430      Method method, Object receiver, Object... params) {
431    try {
432      return method.invoke(receiver, params);
433    } catch (IllegalAccessException e) {
434      throw new RuntimeException(e);
435    } catch (InvocationTargetException e) {
436      throw propagate(e.getCause());
437    }
438  }
439
440  /** JavaLangAccess class name to load using reflection */
441  @GwtIncompatible // not used by GWT emulation
442  private static final String JAVA_LANG_ACCESS_CLASSNAME = "sun.misc.JavaLangAccess";
443
444  /** SharedSecrets class name to load using reflection */
445  @GwtIncompatible // not used by GWT emulation
446  @VisibleForTesting
447  static final String SHARED_SECRETS_CLASSNAME = "sun.misc.SharedSecrets";
448
449  /** Access to some fancy internal JVM internals. */
450  @GwtIncompatible // java.lang.reflect
451  private static final @Nullable Object jla = getJLA();
452
453  /**
454   * The "getStackTraceElementMethod" method, only available on some JDKs so we use reflection to
455   * find it when available. When this is null, use the slow way.
456   */
457  @GwtIncompatible // java.lang.reflect
458  private static final @Nullable Method getStackTraceElementMethod =
459      (jla == null) ? null : getGetMethod();
460
461  /**
462   * The "getStackTraceDepth" method, only available on some JDKs so we use reflection to find it
463   * when available. When this is null, use the slow way.
464   */
465  @GwtIncompatible // java.lang.reflect
466  private static final @Nullable Method getStackTraceDepthMethod =
467      (jla == null) ? null : getSizeMethod();
468
469  /**
470   * Returns the JavaLangAccess class that is present in all Sun JDKs. It is not whitelisted for
471   * AppEngine, and not present in non-Sun JDKs.
472   */
473  @GwtIncompatible // java.lang.reflect
474  private static @Nullable Object getJLA() {
475    try {
476      /*
477       * We load sun.misc.* classes using reflection since Android doesn't support these classes and
478       * would result in compilation failure if we directly refer to these classes.
479       */
480      Class<?> sharedSecrets = Class.forName(SHARED_SECRETS_CLASSNAME, false, null);
481      Method langAccess = sharedSecrets.getMethod("getJavaLangAccess");
482      return langAccess.invoke(null);
483    } catch (ThreadDeath death) {
484      throw death;
485    } catch (Throwable t) {
486      /*
487       * This is not one of AppEngine's whitelisted classes, so even in Sun JDKs, this can fail with
488       * a NoClassDefFoundError. Other apps might deny access to sun.misc packages.
489       */
490      return null;
491    }
492  }
493
494  /**
495   * Returns the Method that can be used to resolve an individual StackTraceElement, or null if that
496   * method cannot be found (it is only to be found in fairly recent JDKs).
497   */
498  @GwtIncompatible // java.lang.reflect
499  private static @Nullable Method getGetMethod() {
500    return getJlaMethod("getStackTraceElement", Throwable.class, int.class);
501  }
502
503  /**
504   * Returns the Method that can be used to return the size of a stack, or null if that method
505   * cannot be found (it is only to be found in fairly recent JDKs). Tries to test method {@link
506   * sun.misc.JavaLangAccess#getStackTraceDepth(Throwable)} getStackTraceDepth} prior to return it
507   * (might fail some JDKs).
508   *
509   * <p>See <a href="https://github.com/google/guava/issues/2887">Throwables#lazyStackTrace throws
510   * UnsupportedOperationException</a>.
511   */
512  @GwtIncompatible // java.lang.reflect
513  private static @Nullable Method getSizeMethod() {
514    try {
515      Method getStackTraceDepth = getJlaMethod("getStackTraceDepth", Throwable.class);
516      if (getStackTraceDepth == null) {
517        return null;
518      }
519      getStackTraceDepth.invoke(getJLA(), new Throwable());
520      return getStackTraceDepth;
521    } catch (UnsupportedOperationException | IllegalAccessException | InvocationTargetException e) {
522      return null;
523    }
524  }
525
526  @GwtIncompatible // java.lang.reflect
527  private static @Nullable Method getJlaMethod(String name, Class<?>... parameterTypes)
528      throws ThreadDeath {
529    try {
530      return Class.forName(JAVA_LANG_ACCESS_CLASSNAME, false, null).getMethod(name, parameterTypes);
531    } catch (ThreadDeath death) {
532      throw death;
533    } catch (Throwable t) {
534      /*
535       * Either the JavaLangAccess class itself is not found, or the method is not supported on the
536       * JVM.
537       */
538      return null;
539    }
540  }
541}