1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.lang.reflect.UndeclaredThrowableException;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.apache.logging.log4j.status.StatusLogger;
32
33
34
35
36 public final class Throwables {
37
38 private static final Method ADD_SUPPRESSED;
39
40 private static final Method GET_SUPPRESSED;
41
42 static {
43 Method getSuppressed = null, addSuppressed = null;
44 final Method[] methods = Throwable.class.getMethods();
45 for (final Method method : methods) {
46 if (method.getName().equals("getSuppressed")) {
47 getSuppressed = method;
48 } else if (method.getName().equals("addSuppressed")) {
49 addSuppressed = method;
50 }
51 }
52 GET_SUPPRESSED = getSuppressed;
53 ADD_SUPPRESSED = addSuppressed;
54 }
55
56
57
58
59
60
61
62
63
64
65 @Deprecated
66 public static void addSuppressed(final Throwable throwable, final Throwable suppressedThrowable) {
67 if (ADD_SUPPRESSED != null) {
68 try {
69 ADD_SUPPRESSED.invoke(throwable, suppressedThrowable);
70 } catch (final IllegalAccessException e) {
71
72 StatusLogger.getLogger().error(e);
73 } catch (final IllegalArgumentException e) {
74
75 StatusLogger.getLogger().error(e);
76 } catch (final InvocationTargetException e) {
77
78 StatusLogger.getLogger().error(e);
79 }
80 }
81
82 }
83
84
85
86
87
88
89
90
91
92
93 @Deprecated
94 public static Throwable[] getSuppressed(final Throwable throwable) {
95 if (GET_SUPPRESSED != null) {
96 try {
97 return (Throwable[]) GET_SUPPRESSED.invoke(throwable);
98 } catch (final Exception e) {
99
100 StatusLogger.getLogger().error(e);
101 return null;
102 }
103 }
104 return null;
105 }
106
107
108
109
110
111
112 public static boolean isGetSuppressedAvailable() {
113 return GET_SUPPRESSED != null;
114 }
115
116
117
118
119
120
121
122 public static List<String> toStringList(final Throwable throwable) {
123 final StringWriter sw = new StringWriter();
124 final PrintWriter pw = new PrintWriter(sw);
125 try {
126 throwable.printStackTrace(pw);
127 } catch (final RuntimeException ex) {
128
129 }
130 pw.flush();
131 final List<String> lines = new ArrayList<String>();
132 final LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));
133 try {
134 String line = reader.readLine();
135 while (line != null) {
136 lines.add(line);
137 line = reader.readLine();
138 }
139 } catch (final IOException ex) {
140 if (ex instanceof InterruptedIOException) {
141 Thread.currentThread().interrupt();
142 }
143 lines.add(ex.toString());
144 } finally {
145 Closer.closeSilently(reader);
146 }
147 return lines;
148 }
149
150
151
152
153
154
155
156
157
158
159 public static void rethrow(final Throwable t) {
160 if (t instanceof RuntimeException) {
161 throw (RuntimeException) t;
162 }
163 if (t instanceof Error) {
164 throw (Error) t;
165 }
166 throw new UndeclaredThrowableException(t);
167 }
168
169 private Throwables() {
170 }
171
172 }