View Javadoc
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.util.internal;
17  
18  import java.io.ByteArrayOutputStream;
19  import java.io.IOException;
20  import java.io.PrintStream;
21  import java.lang.reflect.InvocationTargetException;
22  import java.lang.reflect.Method;
23  import java.util.List;
24  
25  public final class ThrowableUtil {
26  
27      private static final Method addSupressedMethod = getAddSuppressed();
28  
29      private static Method getAddSuppressed() {
30          if (PlatformDependent.javaVersion() < 7) {
31              return null;
32          }
33          try {
34              // addSuppressed is final, so we only need to look it up on Throwable.
35              return Throwable.class.getDeclaredMethod("addSuppressed", Throwable.class);
36          } catch (NoSuchMethodException e) {
37              throw new RuntimeException(e);
38          }
39      }
40  
41      private ThrowableUtil() { }
42  
43      /**
44       * Set the {@link StackTraceElement} for the given {@link Throwable}, using the {@link Class} and method name.
45       */
46      public static <T extends Throwable> T unknownStackTrace(T cause, Class<?> clazz, String method) {
47          cause.setStackTrace(new StackTraceElement[] { new StackTraceElement(clazz.getName(), method, null, -1)});
48          return cause;
49      }
50  
51      /**
52       * Gets the stack trace from a Throwable as a String.
53       *
54       * @param cause the {@link Throwable} to be examined
55       * @return the stack trace as generated by {@link Throwable#printStackTrace(java.io.PrintWriter)} method.
56       */
57      public static String stackTraceToString(Throwable cause) {
58          ByteArrayOutputStream out = new ByteArrayOutputStream();
59          PrintStream pout = new PrintStream(out);
60          cause.printStackTrace(pout);
61          pout.flush();
62          try {
63              return new String(out.toByteArray());
64          } finally {
65              try {
66                  out.close();
67              } catch (IOException ignore) {
68                  // ignore as should never happen
69              }
70          }
71      }
72  
73      public static boolean haveSuppressed() {
74          return addSupressedMethod != null;
75      }
76  
77      public static void addSuppressed(Throwable target, Throwable suppressed) {
78          if (!haveSuppressed()) {
79              return;
80          }
81          try {
82              addSupressedMethod.invoke(target, suppressed);
83          } catch (IllegalAccessException e) {
84              throw new RuntimeException(e);
85          } catch (InvocationTargetException e) {
86              throw new RuntimeException(e);
87          }
88      }
89  
90      public static void addSuppressedAndClear(Throwable target, List<Throwable> suppressed) {
91          addSuppressed(target, suppressed);
92          suppressed.clear();
93      }
94  
95      public static void addSuppressed(Throwable target, List<Throwable> suppressed) {
96          for (Throwable t : suppressed) {
97              addSuppressed(target, t);
98          }
99      }
100 }