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.util.List;
22  
23  public final class ThrowableUtil {
24  
25      private ThrowableUtil() { }
26  
27      /**
28       * Set the {@link StackTraceElement} for the given {@link Throwable}, using the {@link Class} and method name.
29       */
30      public static <T extends Throwable> T unknownStackTrace(T cause, Class<?> clazz, String method) {
31          cause.setStackTrace(new StackTraceElement[] { new StackTraceElement(clazz.getName(), method, null, -1)});
32          return cause;
33      }
34  
35      /**
36       * Gets the stack trace from a Throwable as a String.
37       *
38       * @param cause the {@link Throwable} to be examined
39       * @return the stack trace as generated by {@link Throwable#printStackTrace(java.io.PrintWriter)} method.
40       */
41      public static String stackTraceToString(Throwable cause) {
42          ByteArrayOutputStream out = new ByteArrayOutputStream();
43          PrintStream pout = new PrintStream(out);
44          cause.printStackTrace(pout);
45          pout.flush();
46          try {
47              return new String(out.toByteArray());
48          } finally {
49              try {
50                  out.close();
51              } catch (IOException ignore) {
52                  // ignore as should never happen
53              }
54          }
55      }
56  
57      public static boolean haveSuppressed() {
58          return PlatformDependent.javaVersion() >= 7;
59      }
60  
61      @SuppressJava6Requirement(reason = "Throwable addSuppressed is only available for >= 7. Has check for < 7.")
62      public static void addSuppressed(Throwable target, Throwable suppressed) {
63          if (!haveSuppressed()) {
64              return;
65          }
66          target.addSuppressed(suppressed);
67      }
68  
69      public static void addSuppressedAndClear(Throwable target, List<Throwable> suppressed) {
70          addSuppressed(target, suppressed);
71          suppressed.clear();
72      }
73  
74      public static void addSuppressed(Throwable target, List<Throwable> suppressed) {
75          for (Throwable t : suppressed) {
76              addSuppressed(target, t);
77          }
78      }
79  }