View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.util;
18  
19  import java.io.IOException;
20  import java.io.InterruptedIOException;
21  import java.io.LineNumberReader;
22  import java.io.PrintWriter;
23  import java.io.StringReader;
24  import java.io.StringWriter;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  /**
29   * Helps with Throwable objects.
30   */
31  public final class Throwables {
32  
33      private Throwables() {
34      }
35  
36      /**
37       * Returns the deepest cause of the given {@code throwable}.
38       *
39       * @param throwable the throwable to navigate
40       * @return the deepest throwable or the given throwable
41       */
42      public static Throwable getRootCause(final Throwable throwable) {
43          Throwable cause;
44          Throwable root = throwable;
45          while ((cause = root.getCause()) != null) {
46              root = cause;
47          }
48          return root;
49      }
50  
51      /**
52       * Converts a Throwable stack trace into a List of Strings.
53       *
54       * @param throwable the Throwable
55       * @return a List of Strings
56       */
57      public static List<String> toStringList(final Throwable throwable) {
58          final StringWriter sw = new StringWriter();
59          final PrintWriter pw = new PrintWriter(sw);
60          try {
61              throwable.printStackTrace(pw);
62          } catch (final RuntimeException ex) {
63              // Ignore any exceptions.
64          }
65          pw.flush();
66          final List<String> lines = new ArrayList<>();
67          final LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));
68          try {
69              String line = reader.readLine();
70              while (line != null) {
71                  lines.add(line);
72                  line = reader.readLine();
73              }
74          } catch (final IOException ex) {
75              if (ex instanceof InterruptedIOException) {
76                  Thread.currentThread().interrupt();
77              }
78              lines.add(ex.toString());
79          } finally {
80              Closer.closeSilently(reader);
81          }
82          return lines;
83      }
84  
85      /**
86       * Rethrows a {@link Throwable}.
87       *
88       * @param t the Throwable to throw.
89       * @since 2.1
90       */
91      public static void rethrow(final Throwable t) {
92          Throwables.<RuntimeException>rethrow0(t);
93      }
94  
95      @SuppressWarnings("unchecked")
96      private static <T extends Throwable> void rethrow0(final Throwable t) throws T {
97          throw (T) t;
98      }
99  }