📄 exceptionutils.java
字号:
for ( int i = 0, isize = CAUSE_METHOD_NAMES.length; i < isize; i++ ) { try { Method method = cls.getMethod( CAUSE_METHOD_NAMES[i], null ); if ( method != null && Throwable.class.isAssignableFrom( method.getReturnType() ) ) { return true; } } catch ( NoSuchMethodException ignored ) { } catch ( SecurityException ignored ) { } } try { Field field = cls.getField( "detail" ); if ( field != null ) { return true; } } catch ( NoSuchFieldException ignored ) { } catch ( SecurityException ignored ) { } return false; } //----------------------------------------------------------------------- /** * <p>Counts the number of <code>Throwable</code> objects in the * exception chain.</p> * <p/> * <p>A throwable without cause will return <code>1</code>. * A throwable with one cause will return <code>2</code> and so on. * A <code>null</code> throwable will return <code>0</code>.</p> * * @param throwable the throwable to inspect, may be null * @return the count of throwables, zero if null input */ public static int getThrowableCount(Throwable throwable) { int count = 0; while ( throwable != null ) { count++; throwable = ExceptionUtils.getCause( throwable ); } return count; } /** * <p>Returns the list of <code>Throwable</code> objects in the * exception chain.</p> * <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 = ExceptionUtils.getCause( throwable ); } return ( Throwable[] ) list.toArray( new Throwable[list.size()] ); } //----------------------------------------------------------------------- /** * <p>Returns the (zero based) index of the first <code>Throwable</code> * that matches the specified type in the exception chain.</p> * <p/> * <p>A <code>null</code> throwable returns <code>-1</code>. * A <code>null</code> type returns <code>-1</code>. * No match in the chain returns <code>-1</code>.</p> * * @param throwable the throwable to inspect, may be null * @param type the type to search for * @return the index into the throwable chain, -1 if no match or null input */ public static int indexOfThrowable(Throwable throwable, Class type) { return indexOfThrowable( throwable, type, 0 ); } /** * <p>Returns the (zero based) index of the first <code>Throwable</code> * that matches the specified type in the exception chain from * a specified index.</p> * <p/> * <p>A <code>null</code> throwable returns <code>-1</code>. * A <code>null</code> type returns <code>-1</code>. * No match in the chain returns <code>-1</code>. * A negative start index is treated as zero. * A start index greater than the number of throwables returns <code>-1</code>.</p> * * @param throwable the throwable to inspect, may be null * @param type the type to search for * @param fromIndex the (zero based) index of the starting position, * negative treated as zero, larger than chain size returns -1 * @return the index into the throwable chain, -1 if no match or null input */ public static int indexOfThrowable(Throwable throwable, Class type, int fromIndex) { if ( throwable == null ) { return -1; } if ( fromIndex < 0 ) { fromIndex = 0; } Throwable[] throwables = ExceptionUtils.getThrowables( throwable ); if ( fromIndex >= throwables.length ) { return -1; } for ( int i = fromIndex; i < throwables.length; i++ ) { if ( throwables[i].getClass().equals( type ) ) { return i; } } return -1; } //----------------------------------------------------------------------- /** * <p>Prints a compact stack trace for the root cause of a throwable * to <code>System.err</code>.</p> * <p/> * <p>The compact stack trace starts with the root cause and prints * stack frames up to the place where it was caught and wrapped. * Then it prints the wrapped exception and continues with stack frames * until the wrapper exception is caught and wrapped again, etc.</p> * <p/> * <p>The method is equivalent to <code>printStackTrace</code> for throwables * that don't have nested causes.</p> * * @param throwable the throwable to output * @since 2.0 */ public static void printRootCauseStackTrace(Throwable throwable) { printRootCauseStackTrace( throwable, System.err ); } /** * <p>Prints a compact stack trace for the root cause of a throwable.</p> * <p/> * <p>The compact stack trace starts with the root cause and prints * stack frames up to the place where it was caught and wrapped. * Then it prints the wrapped exception and continues with stack frames * until the wrapper exception is caught and wrapped again, etc.</p> * <p/> * <p>The method is equivalent to <code>printStackTrace</code> for throwables * that don't have nested causes.</p> * * @param throwable the throwable to output, may be null * @param stream the stream to output to, may not be null * @throws IllegalArgumentException if the stream is <code>null</code> * @since 2.0 */ public static void printRootCauseStackTrace(Throwable throwable, PrintStream stream) { if ( throwable == null ) { return; } if ( stream == null ) { throw new IllegalArgumentException( "The PrintStream must not be null" ); } String trace[] = getRootCauseStackTrace( throwable ); for ( int i = 0; i < trace.length; i++ ) { stream.println( trace[i] ); } stream.flush(); } /** * <p>Prints a compact stack trace for the root cause of a throwable.</p> * <p/> * <p>The compact stack trace starts with the root cause and prints * stack frames up to the place where it was caught and wrapped. * Then it prints the wrapped exception and continues with stack frames * until the wrapper exception is caught and wrapped again, etc.</p> * <p/> * <p>The method is equivalent to <code>printStackTrace</code> for throwables * that don't have nested causes.</p> * * @param throwable the throwable to output, may be null * @param writer the writer to output to, may not be null * @throws IllegalArgumentException if the writer is <code>null</code> * @since 2.0 */ public static void printRootCauseStackTrace(Throwable throwable, PrintWriter writer) { if ( throwable == null ) { return; } if ( writer == null ) { throw new IllegalArgumentException( "The PrintWriter must not be null" ); } String trace[] = getRootCauseStackTrace( throwable ); for ( int i = 0; i < trace.length; i++ ) { writer.println( trace[i] ); } writer.flush(); } //----------------------------------------------------------------------- /** * <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 ArrayHelper.EMPTY_STRING_ARRAY; } 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>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--; } } //----------------------------------------------------------------------- /** * <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>A way to get the entire nested stack-trace of an throwable.</p> * * @param throwable the <code>Throwable</code> to be examined * @return the nested stack trace, with the root cause first * @since 2.0 */ public static String getFullStackTrace(Throwable throwable) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter( sw, true ); Throwable[] ts = getThrowables( throwable ); for ( int i = 0; i < ts.length; i++ ) { ts[i].printStackTrace( pw ); if ( isNestedThrowable( ts[i] ) ) { break; } } return sw.getBuffer().toString(); } //----------------------------------------------------------------------- /** * <p>Captures the stack trace associated with the specified * <code>Throwable</code> object, decomposing it into a list of * stack frames.</p> * * @param throwable the <code>Throwable</code> to exaamine, may be null * @return an array of strings describing each stack frame, never null */ public static String[] getStackFrames(Throwable throwable) { if ( throwable == null ) { return ArrayHelper.EMPTY_STRING_ARRAY; } return getStackFrames( getStackTrace( throwable ) ); } /** * <p>Functionality shared between the * <code>getStackFrames(Throwable)</code> methods of this and the * {@link org.apache.commons.lang.exception.NestableDelegate} * classes.</p> */ static String[] getStackFrames(String stackTrace) { String linebreak = LINE_SEPARATOR; StringTokenizer frames = new StringTokenizer( stackTrace, linebreak ); List list = new LinkedList(); while ( frames.hasMoreTokens() ) { list.add( frames.nextToken() ); } return ( String[] ) list.toArray( new String[list.size()] ); } /** * <p>Produces a <code>List</code> of stack frames - the message * is not included.</p> * <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 */ static List getStackFrameList(Throwable t) { String stackTrace = getStackTrace( t ); String linebreak = 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; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -