Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
org.codehaus.plexus.util.ExceptionUtils
public class ExceptionUtils
extends java.lang.Object
ExceptionUtils
provides utilities for manipulating
Throwable
objects.
Field Summary | |
protected static String[] |
|
(package private) static String |
|
Constructor Summary | |
|
Method Summary | |
static void |
|
static Throwable |
|
static Throwable |
|
private static Throwable |
|
private static Throwable |
|
private static Throwable |
|
static String |
|
static Throwable |
|
static String[] |
|
(package private) static List |
|
(package private) static String[] |
|
static String[] |
|
static String |
|
static int |
|
static Throwable[] |
|
static int |
|
static int |
|
static boolean |
|
static void |
|
static void |
|
static void |
|
private static void |
|
protected static String[] CAUSE_METHOD_NAMES
The names of methods commonly used to access a wrapped exception.
(package private) static final String WRAPPED_MARKER
Used when printing stack frames to denote the start of a wrapped exception. Package private for accessibility by test suite.
protected ExceptionUtils()
Constructs a newExceptionUtils
. Protected to discourage instantiation.
public static void addCauseMethodName(String methodName)
Adds to the list of method names used in the search forThrowable
objects.
- Parameters:
methodName
- the methodName to add to the list, null and empty strings are ignored
public static Throwable getCause(Throwable throwable)
Introspects the specifiedThrowable
to obtain the cause. The method searches for methods with specific names that return aThrowable
object. This will pick up most wrapping exceptions, including those from JDK 1.4, and The method names can be added to usingaddCauseMethodName(String)
. The default list searched for are:In the absence of any such method, the object is inspected for a
getCause()
getNextException()
getTargetException()
getException()
getSourceException()
getRootCause()
getCausedByException()
getNested()
detail
field assignable to aThrowable
. If none of the above is found, returnsnull
.
- Parameters:
throwable
- The exception to introspect for a cause.
- Returns:
- The cause of the
Throwable
.
public static Throwable getCause(Throwable throwable, String[] methodNames)
Introspects the specifiedThrowable
to obtain the cause using a supplied array of method names.
- Parameters:
throwable
- The exception to introspect for a cause.
- Returns:
- The cause of the
Throwable
.
private static Throwable getCauseUsingFieldName(Throwable throwable, String fieldName)
Find a throwable by field name.
- Parameters:
throwable
- the exception to examinefieldName
- the name of the attribute to examine
- Returns:
- The wrapped exception, or
null
if not found.
private static Throwable getCauseUsingMethodName(Throwable throwable, String methodName)
Find a throwable by method name.
- Parameters:
throwable
- the exception to examinemethodName
- the name of the method to find and invoke
- Returns:
- The wrapped exception, or
null
if not found.
private static Throwable getCauseUsingWellKnownTypes(Throwable throwable)
Usesinstanceof
checks to examine the exception, looking for well known types which could contain chained or wrapped exceptions.
- Parameters:
throwable
- the exception to examine
- Returns:
- The wrapped exception, or
null
if not found.
public static String getFullStackTrace(Throwable t)
A way to get the entire nested stack-trace of an throwable.
- Parameters:
t
- TheThrowable
.
- Returns:
- The nested stack trace, with the root cause first.
public static Throwable getRootCause(Throwable throwable)
Walks through the exception chain to the last element -- the "root" of the tree -- usinggetCause(Throwable)
, and returns that exception.
- Parameters:
throwable
- the throwable to get the root cause for
- Returns:
- The root cause of the
Throwable
.
public static String[] getRootCauseStackTrace(Throwable t)
Creates a compact stack trace for the root cause of the supplied throwable. SeeprintRootCauseStackTrace(Throwable t, PrintStream s)
(package private) static List getStackFrameList(Throwable t)
Produces a List of stack frames - the message is not included. This works in most cases - it will only fail if the exception message contains a line that starts with: " at".
- Parameters:
t
- is any throwable
- Returns:
- List of stack frames
(package private) static String[] getStackFrames(String stackTrace)
Functionality shared between thegetStackFrames(Throwable)
methods of this and the classes.
public static String[] getStackFrames(Throwable t)
Captures the stack trace associated with the specifiedThrowable
object, decomposing it into a list of stack frames.
- Parameters:
t
- TheThrowable
.
- Returns:
- An array of strings describing each stack frame.
public static String getStackTrace(Throwable t)
A convenient way of extracting the stack trace from an exception.
- Parameters:
t
- TheThrowable
.
- Returns:
- The stack trace as generated by the exception's
printStackTrace(PrintWriter)
method.
public static int getThrowableCount(Throwable throwable)
Returns the number ofThrowable
objects in the exception chain.
- Parameters:
throwable
- the exception to inspect
- Returns:
- The throwable count.
public static Throwable[] getThrowables(Throwable throwable)
Returns the list ofThrowable
objects in the exception chain.
- Parameters:
throwable
- the exception to inspect
- Returns:
- The list of
Throwable
objects.
public static int indexOfThrowable(Throwable throwable, Class type)
Delegates toindexOfThrowable(Throwable,Class,int)
, starting the search at the beginning of the exception chain.
public static int indexOfThrowable(Throwable throwable, Class type, int fromIndex)
Returns the (zero based) index, of the firstThrowable
that matches the specified type in the exception chain ofThrowable
objects with an index greater than or equal to the specified index, or-1
if the type is not found.
- Parameters:
throwable
- the exception to inspecttype
-Class
to look forfromIndex
- the (zero based) index of the starting position in the chain to be searched
- Returns:
- the first occurrence of the type in the chain, or
-1
if the type is not found
public static boolean isNestedThrowable(Throwable throwable)
Whether an Throwable is considered nested or not.
- Parameters:
throwable
- TheThrowable
.
- Returns:
- boolean true/false
public static void printRootCauseStackTrace(Throwable t)
Equivalent to printRootCauseStackTrace(t, System.err)
public static void printRootCauseStackTrace(Throwable t, PrintStream stream)
Prints a compact stack trace for the root cause of a throwable. 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. The method is equivalent to t.printStackTrace() for throwables that don't have nested causes.
public static void printRootCauseStackTrace(Throwable t, PrintWriter writer)
Same as printRootCauseStackTrace(t, stream), except it takes a PrintWriter as an argument.
private static void removeCommonFrames(List causeFrames, List wrapperFrames)
Given two stack traces, removes common frames from the cause trace.
- Parameters:
causeFrames
- stack trace of a cause throwablewrapperFrames
- stack trace of a wrapper throwable