001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.util; 018 019import java.io.IOException; 020import java.io.InterruptedIOException; 021import java.io.LineNumberReader; 022import java.io.PrintWriter; 023import java.io.StringReader; 024import java.io.StringWriter; 025import java.lang.reflect.UndeclaredThrowableException; 026import java.util.ArrayList; 027import java.util.List; 028 029/** 030 * Helps with Throwable objects. 031 */ 032public final class Throwables { 033 034 /** 035 * Has no effect on Java 6 and below. 036 * 037 * @param throwable a Throwable 038 * @param suppressedThrowable a suppressed Throwable 039 * @see Throwable#addSuppressed(Throwable) 040 * @deprecated If compiling on Java 7 and above use {@link Throwable#addSuppressed(Throwable)}. Marked as deprecated because Java 6 is 041 * deprecated. Will be removed in 2.5. 042 */ 043 @Deprecated 044 public static void addSuppressed(final Throwable throwable, final Throwable suppressedThrowable) { 045 throwable.addSuppressed(suppressedThrowable); 046 } 047 048 /** 049 * Has no effect on Java 6 and below. 050 * 051 * @param throwable a Throwable 052 * @return see Java 7's {@link Throwable#getSuppressed()} 053 * @see Throwable#getSuppressed() 054 * @deprecated If compiling on Java 7 and above use {@link Throwable#getSuppressed()}. Marked as deprecated because Java 6 is 055 * deprecated. Will be removed 2.5. 056 */ 057 @Deprecated 058 public static Throwable[] getSuppressed(final Throwable throwable) { 059 return throwable.getSuppressed(); 060 } 061 062 /** 063 * Returns true if the getSuppressed method is available. 064 * 065 * @return True if getSuppressed is available. As of 2.4, always returns true. 066 * @deprecated Will be removed in 2.5. As of 2.4, always returns true. 067 */ 068 @Deprecated 069 public static boolean isGetSuppressedAvailable() { 070 return true; 071 } 072 073 /** 074 * Converts a Throwable stack trace into a List of Strings 075 * 076 * @param throwable the Throwable 077 * @return a List of Strings 078 */ 079 public static List<String> toStringList(final Throwable throwable) { 080 final StringWriter sw = new StringWriter(); 081 final PrintWriter pw = new PrintWriter(sw); 082 try { 083 throwable.printStackTrace(pw); 084 } catch (final RuntimeException ex) { 085 // Ignore any exceptions. 086 } 087 pw.flush(); 088 final List<String> lines = new ArrayList<>(); 089 final LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString())); 090 try { 091 String line = reader.readLine(); 092 while (line != null) { 093 lines.add(line); 094 line = reader.readLine(); 095 } 096 } catch (final IOException ex) { 097 if (ex instanceof InterruptedIOException) { 098 Thread.currentThread().interrupt(); 099 } 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}