Here you can find the source of getRootCauseStackTrace(Throwable throwable)
Creates a compact stack trace for the root cause of the supplied Throwable
.
Parameter | Description |
---|---|
throwable | the throwable to examine, may be null |
public static String[] getRootCauseStackTrace(Throwable throwable)
//package com.java2s; /******************************************************************************* * Copyright 2012 Internet2// ww w . j a v a 2 s. c o m * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; import java.lang.reflect.Array; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.StringTokenizer; public class Main { /** * The name says it all. */ private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; /** * <p>Used when printing stack frames to denote the start of a * wrapped exception.</p> * * <p>Package private for accessibility by test suite.</p> */ static final String WRAPPED_MARKER = " [wrapped] "; /** * <p>The names of methods commonly used to access a wrapped exception.</p> */ private static String[] CAUSE_METHOD_NAMES = { "getCause", "getNextException", "getTargetException", "getException", "getSourceException", "getRootCause", "getCausedByException", "getNested", "getLinkedException", "getNestedException", "getLinkedCause", "getThrowable", }; /** * <p>Creates a compact stack trace for the root cause of the supplied * <code>Throwable</code>.</p> * * @param throwable the throwable to examine, may be null * @return an array of stack trace frames, never null * @since 2.0 */ public static String[] getRootCauseStackTrace(Throwable throwable) { if (throwable == null) { return new String[0]; } Throwable throwables[] = getThrowables(throwable); int count = throwables.length; ArrayList frames = new ArrayList(); List nextTrace = getStackFrameList(throwables[count - 1]); for (int i = count; --i >= 0;) { List trace = nextTrace; if (i != 0) { nextTrace = getStackFrameList(throwables[i - 1]); removeCommonFrames(trace, nextTrace); } if (i == count - 1) { frames.add(throwables[i].toString()); } else { frames.add(WRAPPED_MARKER + throwables[i].toString()); } for (int j = 0; j < trace.size(); j++) { frames.add(trace.get(j)); } } return (String[]) frames.toArray(new String[0]); } /** * <p>Returns the list of <code>Throwable</code> objects in the * exception chain.</p> * * <p>A throwable without cause will return an array containing * one element - the input throwable. * A throwable with one cause will return an array containing * two elements. - the input throwable and the cause throwable. * A <code>null</code> throwable will return an array size zero.</p> * * @param throwable the throwable to inspect, may be null * @return the array of throwables, never null */ public static Throwable[] getThrowables(Throwable throwable) { List list = new ArrayList(); while (throwable != null) { list.add(throwable); throwable = getCause(throwable); } return (Throwable[]) list.toArray(new Throwable[list.size()]); } /** * <p>Produces a <code>List</code> of stack frames - the message * is not included.</p> * * <p>This works in most cases - it will only fail if the exception * message contains a line that starts with: * <code>" at".</code></p> * * @param t is any throwable * @return List of stack frames */ public static List getStackFrameList(Throwable t) { String stackTrace = getStackTrace(t); String linebreak = getSystemProperty("line.separator"); StringTokenizer frames = new StringTokenizer(stackTrace, linebreak); List list = new LinkedList(); boolean traceStarted = false; while (frames.hasMoreTokens()) { String token = frames.nextToken(); // Determine if the line starts with <whitespace>at int at = token.indexOf("at"); if (at != -1 && token.substring(0, at).trim().length() == 0) { traceStarted = true; list.add(token); } else if (traceStarted) { break; } } return list; } /** * <p>Removes common frames from the cause trace given the two stack traces.</p> * * @param causeFrames stack trace of a cause throwable * @param wrapperFrames stack trace of a wrapper throwable * @throws IllegalArgumentException if either argument is null * @since 2.0 */ public static void removeCommonFrames(List causeFrames, List wrapperFrames) { if (causeFrames == null || wrapperFrames == null) { throw new IllegalArgumentException("The List must not be null"); } int causeFrameIndex = causeFrames.size() - 1; int wrapperFrameIndex = wrapperFrames.size() - 1; while (causeFrameIndex >= 0 && wrapperFrameIndex >= 0) { // Remove the frame from the cause trace if it is the same // as in the wrapper trace String causeFrame = (String) causeFrames.get(causeFrameIndex); String wrapperFrame = (String) wrapperFrames.get(wrapperFrameIndex); if (causeFrame.equals(wrapperFrame)) { causeFrames.remove(causeFrameIndex); } causeFrameIndex--; wrapperFrameIndex--; } } /** * Get the contents of an <code>InputStream</code> as a String. * @param input the <code>InputStream</code> to read from * @param encoding The name of a supported character encoding. See the * <a href="http://www.iana.org/assignments/character-sets">IANA * Charset Registry</a> for a list of valid encoding types. * @return the requested <code>String</code> * @throws IOException In case of an I/O problem */ public static String toString(InputStream input, String encoding) throws IOException { StringWriter sw = new StringWriter(); copy(input, sw, encoding); return sw.toString(); } /** * Get a specific index of an array or collection (note for collections and iterating, * it is more efficient to get an iterator and iterate * @param arrayOrCollection * @param index * @return the object at that index */ public static Object get(Object arrayOrCollection, int index) { if (arrayOrCollection == null) { if (index == 0) { return null; } throw new RuntimeException("Trying to access index " + index + " of null"); } //no need to iterator an arraylist (NOT LINKED LIST) if (arrayOrCollection instanceof ArrayList) { return ((ArrayList) arrayOrCollection).get(index); } if (arrayOrCollection instanceof Collection) { Iterator iterator = iterator(arrayOrCollection); for (int i = 0; i < index; i++) { next(arrayOrCollection, iterator, i); } return next(arrayOrCollection, iterator, index); } if (arrayOrCollection.getClass().isArray()) { return Array.get(arrayOrCollection, index); } if (index == 0) { return arrayOrCollection; } throw new RuntimeException("Trying to access index " + index + " of and object: " + arrayOrCollection); } /** * Returns the given list as a <code>String[]</code>. * @param list a list to transform. * @return the given list as a <code>String[]</code>. */ private static String[] toArray(List list) { return (String[]) list.toArray(new String[list.size()]); } /** * <p>Introspects the <code>Throwable</code> to obtain the cause.</p> * * <p>The method searches for methods with specific names that return a * <code>Throwable</code> object. This will pick up most wrapping exceptions, * including those from JDK 1.4, and * The method names can be added to using {@link #addCauseMethodName(String)}.</p> * * <p>The default list searched for are:</p> * <ul> * <li><code>getCause()</code></li> * <li><code>getNextException()</code></li> * <li><code>getTargetException()</code></li> * <li><code>getException()</code></li> * <li><code>getSourceException()</code></li> * <li><code>getRootCause()</code></li> * <li><code>getCausedByException()</code></li> * <li><code>getNested()</code></li> * </ul> * * <p>In the absence of any such method, the object is inspected for a * <code>detail</code> field assignable to a <code>Throwable</code>.</p> * * <p>If none of the above is found, returns <code>null</code>.</p> * * @param throwable the throwable to introspect for a cause, may be null * @return the cause of the <code>Throwable</code>, * <code>null</code> if none found or null throwable input * @since 1.0 */ public static Throwable getCause(Throwable throwable) { return getCause(throwable, CAUSE_METHOD_NAMES); } /** * <p>Introspects the <code>Throwable</code> to obtain the cause.</p> * * <ol> * <li>Try known exception types.</li> * <li>Try the supplied array of method names.</li> * <li>Try the field 'detail'.</li> * </ol> * * <p>A <code>null</code> set of method names means use the default set. * A <code>null</code> in the set of method names will be ignored.</p> * * @param throwable the throwable to introspect for a cause, may be null * @param methodNames the method names, null treated as default set * @return the cause of the <code>Throwable</code>, * <code>null</code> if none found or null throwable input * @since 1.0 */ public static Throwable getCause(Throwable throwable, String[] methodNames) { if (throwable == null) { return null; } Throwable cause = getCauseUsingWellKnownTypes(throwable); if (cause == null) { if (methodNames == null) { methodNames = CAUSE_METHOD_NAMES; } for (int i = 0; i < methodNames.length; i++) { String methodName = methodNames[i]; if (methodName != null) { cause = getCauseUsingMethodName(throwable, methodName); if (cause != null) { break; } } } if (cause == null) { cause = getCauseUsingFieldName(throwable, "detail"); } } return cause; } /** * <p>Gets the stack trace from a Throwable as a String.</p> * * @param throwable the <code>Throwable</code> to be examined * @return the stack trace as generated by the exception's * <code>printStackTrace(PrintWriter)</code> method */ public static String getStackTrace(Throwable throwable) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw, true); throwable.printStackTrace(pw); return sw.getBuffer().toString(); } /** * <p>Gets a System property, defaulting to <code>null</code> if the property * cannot be read.</p> * * <p>If a <code>SecurityException</code> is caught, the return * value is <code>null</code> and a message is written to <code>System.err</code>.</p> * * @param property the system property name * @return the system property value or <code>null</code> if a security problem occurs */ private static String getSystemProperty(String property) { try { return System.getProperty(property); } catch (SecurityException ex) { // we are not allowed to look at this property System.err.println("Caught a SecurityException reading the system property '" + property + "'; the SystemUtils property value will default to null."); return null; } } /** * * @param throwable * @param type * @param fromIndex * @param subclass * @return the index */ private static int indexOf(Throwable throwable, Class type, int fromIndex, boolean subclass) { if (throwable == null || type == null) { return -1; } if (fromIndex < 0) { fromIndex = 0; } Throwable[] throwables = getThrowables(throwable); if (fromIndex >= throwables.length) { return -1; } if (subclass) { for (int i = fromIndex; i < throwables.length; i++) { if (type.isAssignableFrom(throwables[i].getClass())) { return i; } } } else { for (int i = fromIndex; i < throwables.length; i++) { if (type.equals(throwables[i].getClass())) { return i; } } } return -1; } /** * <p>Find the index of the given object in the array.</p> * * <p>This method returns <code>-1</code> if <code>null</code> array input.</p> * * @param array the array to search through for the object, may be <code>null</code> * @param objectToFind the object to find, may be <code>null</code> * @return the index of the object within the array, * <code>-1</code> if not found or <code>null</code> array input */ public static int indexOf(Object[] array, Object objectToFind) { return indexOf(array, objectToFind, 0); } /** * <p>Find the index of the given object in the array starting at the given index.</p> * * <p>This method returns <code>-1</code> if <code>null</code> array input.</p> * * <p>A negative startIndex is treated as zero. A startIndex larger than the array * length will return <code>-1</code>.</p> * * @param array the array to search through for the object, may be <code>null</code> * @param objectToFind the object to find, may be <code>null</code> * @param startIndex the index to start searching at * @return the index of the object within the array starting at the index, * <code>-1</code> if not found or <code>null</code> array input */ public static int indexOf(Object[] array, Object objectToFind, int startIndex) { if (array == null) { return -1; } if (startIndex < 0) { startIndex = 0; } if (objectToFind == null) { for (int i = startIndex; i < array.length; i++) { if (array[i] == null) { return i; } } } else { for (int i = startIndex; i < array.length; i++) { if (objectToFind.equals(array[i])) { return i; } } } return -1; } /** * Null safe array length or map * @param arrayOrCollection * @return the length of the array (0 for null) */ public static int length(Object arrayOrCollection) { if (arrayOrCollection == null) { return 0; } if (arrayOrCollection.getClass().isArray()) { return Array.getLength(arrayOrCollection); } if (arrayOrCollection instanceof Collection) { return ((Collection) arrayOrCollection).size(); } if (arrayOrCollection instanceof Map) { return ((Map) arrayOrCollection).size(); } //simple non array non collection object return 1; } /** * <p>Removes control characters (char <= 32) from both * ends of this String, handling <code>null</code> by returning * <code>null</code>.</p> * * <p>The String is trimmed using {@link String#trim()}. * Trim removes start and end characters <= 32. * To strip whitespace use strip(String)</p> * * <p>To trim your choice of characters, use the * strip(String, String) methods.</p> * * <pre> * StringUtils.trim(null) = null * StringUtils.trim("") = "" * StringUtils.trim(" ") = "" * StringUtils.trim("abc") = "abc" * StringUtils.trim(" abc ") = "abc" * </pre> * * @param str the String to be trimmed, may be null * @return the trimmed string, <code>null</code> if null String input */ public static String trim(String str) { return str == null ? null : str.trim(); } /** * <p>Compares two Strings, returning <code>true</code> if they are equal.</p> * * <p><code>null</code>s are handled without exceptions. Two <code>null</code> * references are considered to be equal. The comparison is case sensitive.</p> * * <pre> * StringUtils.equals(null, null) = true * StringUtils.equals(null, "abc") = false * StringUtils.equals("abc", null) = false * StringUtils.equals("abc", "abc") = true * StringUtils.equals("abc", "ABC") = false * </pre> * * @see java.lang.String#equals(Object) * @param str1 the first String, may be null * @param str2 the second String, may be null * @return <code>true</code> if the Strings are equal, case sensitive, or * both <code>null</code> */ public static boolean equals(String str1, String str2) { return str1 == null ? str2 == null : str1.equals(str2); } /** * Copy and convert bytes from an <code>InputStream</code> to chars on a * <code>Writer</code>, using the specified encoding. * @param input the <code>InputStream</code> to read from * @param output the <code>Writer</code> to write to * @param encoding The name of a supported character encoding. See the * <a href="http://www.iana.org/assignments/character-sets">IANA * Charset Registry</a> for a list of valid encoding types. * @throws IOException In case of an I/O problem */ public static void copy(InputStream input, Writer output, String encoding) throws IOException { InputStreamReader in = new InputStreamReader(input, encoding); copy(in, output); } /** * Copy chars from a <code>Reader</code> to a <code>Writer</code>. * @param input the <code>Reader</code> to read from * @param output the <code>Writer</code> to write to * @return the number of characters copied * @throws IOException In case of an I/O problem */ public static int copy(Reader input, Writer output) throws IOException { char[] buffer = new char[DEFAULT_BUFFER_SIZE]; int count = 0; int n = 0; while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, n); count += n; } return count; } /** * null safe iterator getter if the type if collection * @param collection * @return the iterator */ public static Iterator iterator(Object collection) { if (collection == null) { return null; } //array list doesnt need an iterator if (collection instanceof Collection && !(collection instanceof ArrayList)) { return ((Collection) collection).iterator(); } return null; } /** * If array, get the element based on index, if Collection, get it based on iterator. * @param arrayOrCollection * @param iterator * @param index * @return the object */ public static Object next(Object arrayOrCollection, Iterator iterator, int index) { if (arrayOrCollection.getClass().isArray()) { return Array.get(arrayOrCollection, index); } if (arrayOrCollection instanceof ArrayList) { return ((ArrayList) arrayOrCollection).get(index); } if (arrayOrCollection instanceof Collection) { return iterator.next(); } //simple object if (0 == index) { return arrayOrCollection; } throw new RuntimeException("Invalid class type: " + arrayOrCollection.getClass().getName()); } /** * <p>Finds a <code>Throwable</code> for known types.</p> * * <p>Uses <code>instanceof</code> checks to examine the exception, * looking for well known types which could contain chained or * wrapped exceptions.</p> * * @param throwable the exception to examine * @return the wrapped exception, or <code>null</code> if not found */ private static Throwable getCauseUsingWellKnownTypes(Throwable throwable) { if (throwable instanceof SQLException) { return ((SQLException) throwable).getNextException(); } else if (throwable instanceof InvocationTargetException) { return ((InvocationTargetException) throwable).getTargetException(); } else { return null; } } /** * <p>Finds a <code>Throwable</code> by method name.</p> * * @param throwable the exception to examine * @param methodName the name of the method to find and invoke * @return the wrapped exception, or <code>null</code> if not found */ private static Throwable getCauseUsingMethodName(Throwable throwable, String methodName) { Method method = null; try { method = throwable.getClass().getMethod(methodName, (Class[]) null); } catch (NoSuchMethodException ignored) { } catch (SecurityException ignored) { } if (method != null && Throwable.class.isAssignableFrom(method.getReturnType())) { try { return (Throwable) method.invoke(throwable, new Object[0]); } catch (IllegalAccessException ignored) { } catch (IllegalArgumentException ignored) { } catch (InvocationTargetException ignored) { } } return null; } /** * <p>Finds a <code>Throwable</code> by field name.</p> * * @param throwable the exception to examine * @param fieldName the name of the attribute to examine * @return the wrapped exception, or <code>null</code> if not found */ private static Throwable getCauseUsingFieldName(Throwable throwable, String fieldName) { Field field = null; try { field = throwable.getClass().getField(fieldName); } catch (NoSuchFieldException ignored) { } catch (SecurityException ignored) { } if (field != null && Throwable.class.isAssignableFrom(field.getType())) { try { return (Throwable) field.get(throwable); } catch (IllegalAccessException ignored) { } catch (IllegalArgumentException ignored) { } } return null; } }