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.lang.reflect.UndeclaredThrowableException;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  /**
30   * Helps with Throwable objects.
31   */
32  public final class Throwables {
33  
34      /**
35       * Has no effect on Java 6 and below.
36       *
37       * @param throwable a Throwable
38       * @param suppressedThrowable a suppressed Throwable
39       * @see Throwable#addSuppressed(Throwable)
40       * @deprecated If compiling on Java 7 and above use {@link Throwable#addSuppressed(Throwable)}. Marked as deprecated because Java 6 is
41       *             deprecated. Will be removed in 2.5.
42       */
43      @Deprecated
44      public static void addSuppressed(final Throwable throwable, final Throwable suppressedThrowable) {
45          throwable.addSuppressed(suppressedThrowable);
46      }
47  
48      /**
49       * Has no effect on Java 6 and below.
50       *
51       * @param throwable a Throwable
52       * @return see Java 7's {@link Throwable#getSuppressed()}
53       * @see Throwable#getSuppressed()
54       * @deprecated If compiling on Java 7 and above use {@link Throwable#getSuppressed()}. Marked as deprecated because Java 6 is
55       *             deprecated. Will be removed 2.5.
56       */
57      @Deprecated
58      public static Throwable[] getSuppressed(final Throwable throwable) {
59          return throwable.getSuppressed();
60      }
61  
62      /**
63       * Returns true if the getSuppressed method is available.
64       * 
65       * @return True if getSuppressed is available. As of 2.4, always returns true.
66       * @deprecated Will be removed in 2.5. As of 2.4, always returns true.
67       */
68      @Deprecated
69      public static boolean isGetSuppressedAvailable() {
70          return true;
71      }
72  
73      /**
74       * Converts a Throwable stack trace into a List of Strings
75       *
76       * @param throwable the Throwable
77       * @return a List of Strings
78       */
79      public static List<String> toStringList(final Throwable throwable) {
80          final StringWriter sw = new StringWriter();
81          final PrintWriter pw = new PrintWriter(sw);
82          try {
83              throwable.printStackTrace(pw);
84          } catch (final RuntimeException ex) {
85              // Ignore any exceptions.
86          }
87          pw.flush();
88          final List<String> lines = new ArrayList<>();
89          final LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));
90          try {
91              String line = reader.readLine();
92              while (line != null) {
93                  lines.add(line);
94                  line = reader.readLine();
95              }
96          } catch (final IOException ex) {
97              if (ex instanceof InterruptedIOException) {
98                  Thread.currentThread().interrupt();
99              }
100             lines.add(ex.toString());
101         } finally {
102             Closer.closeSilently(reader);
103         }
104         return lines;
105     }
106 
107     /**
108      * Rethrows a {@link Throwable}, wrapping checked exceptions into an {@link UndeclaredThrowableException}.
109      *
110      * @param t the Throwable to throw.
111      * @throws RuntimeException             if {@code t} is a RuntimeException
112      * @throws Error                        if {@code t} is an Error
113      * @throws UndeclaredThrowableException if {@code t} is a checked Exception
114      * @since 2.1
115      */
116     public static void rethrow(final Throwable t) {
117         if (t instanceof RuntimeException) {
118             throw (RuntimeException) t;
119         }
120         if (t instanceof Error) {
121             throw (Error) t;
122         }
123         throw new UndeclaredThrowableException(t);
124     }
125 
126     private Throwables() {
127     }
128 
129 }