diff --git a/src/java/org/apache/commons/lang/exception/ExceptionUtils.java b/src/java/org/apache/commons/lang/exception/ExceptionUtils.java index ac742291f..66884366a 100644 --- a/src/java/org/apache/commons/lang/exception/ExceptionUtils.java +++ b/src/java/org/apache/commons/lang/exception/ExceptionUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2005 The Apache Software Foundation. + * Copyright 2002-2006 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,13 +29,13 @@ import java.util.StringTokenizer; import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.NullArgumentException; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.SystemUtils; -import org.apache.commons.lang.NullArgumentException; /** *

Provides utilities for manipulating and examining -Throwable objects.

+ * Throwable objects.

* * @author Daniel Rall * @author Dmitri Plotnikov @@ -74,16 +74,12 @@ public class ExceptionUtils { }; /** - *

- * The Method object for Java 1.4 getCause. - *

+ *

The Method object for Java 1.4 getCause.

*/ private static final Method THROWABLE_CAUSE_METHOD; /** - *

- * The Method object for Java 1.4 initCause. - *

+ *

The Method object for Java 1.4 initCause.

*/ private static final Method THROWABLE_INITCAUSE_METHOD; @@ -149,16 +145,12 @@ public static void removeCauseMethodName(String methodName) { } /** - *

- * Sets the cause of a Throwable using introspection, allowing source code compatibility between - * pre-1.4 and post-1.4 Java releases. - *

- * - *

- * The typical use of this method is inside a constructor as in the following example: - *

- * - *

+ *

Sets the cause of a Throwable using introspection, allowing + * source code compatibility between pre-1.4 and post-1.4 Java releases.

+ * + *

The typical use of this method is inside a constructor as in + * the following example:

+ * *
      * import org.apache.commons.lang.exception.ExceptionUtils;
      *  
@@ -167,20 +159,16 @@ public static void removeCauseMethodName(String methodName) {
      *    public MyException(String msg) {
      *       super(msg);
      *    }
-     *   
+     *
      *    public MyException(String msg, Throwable cause) {
      *       super(msg);
      *       ExceptionUtils.setCause(this, cause);
      *    }
-     * 
-     * }           
+     * }
      * 
- *

- * - * @param target - * the target Throwable - * @param cause - * the Throwable to set in the target + * + * @param target the target Throwable + * @param cause the Throwable to set in the target * @return a true if the target has been modified * @since 2.2 */ @@ -225,6 +213,7 @@ private static String[] toArray(List list) { /** * Returns {@link #CAUSE_METHOD_NAMES} as a List. + * * @return {@link #CAUSE_METHOD_NAMES} as a List. */ private static ArrayList getCauseMethodNameList() { @@ -246,7 +235,7 @@ public static boolean isCauseMethodName(String methodName) { /** *

Introspects the Throwable to obtain the cause.

- * + * *

The method searches for methods with specific names that return a * Throwable object. This will pick up most wrapping exceptions, * including those from JDK 1.4, and @@ -267,7 +256,7 @@ public static boolean isCauseMethodName(String methodName) { * *

In the absence of any such method, the object is inspected for a * detail field assignable to a Throwable.

- * + * *

If none of the above is found, returns null.

* * @param throwable the throwable to introspect for a cause, may be null @@ -281,13 +270,13 @@ public static Throwable getCause(Throwable throwable) { /** *

Introspects the Throwable to obtain the cause.

- * + * *
    *
  1. Try known exception types.
  2. *
  3. Try the supplied array of method names.
  4. *
  5. Try the field 'detail'.
  6. *
- * + * *

A null set of method names means use the default set. * A null in the set of method names will be ignored.

* @@ -325,7 +314,7 @@ public static Throwable getCause(Throwable throwable, String[] methodNames) { /** *

Introspects the Throwable to obtain the root cause.

- * + * *

This method walks through the exception chain to the last element, * "root" of the tree, using {@link #getCause(Throwable)}, and * returns that exception.

@@ -369,7 +358,7 @@ private static Throwable getCauseUsingWellKnownTypes(Throwable throwable) { /** *

Finds a Throwable by method name.

- * + * * @param throwable the exception to examine * @param methodName the name of the method to find and invoke * @return the wrapped exception, or null if not found @@ -400,7 +389,7 @@ private static Throwable getCauseUsingMethodName(Throwable throwable, String met /** *

Finds a Throwable by field name.

- * + * * @param throwable the exception to examine * @param fieldName the name of the attribute to examine * @return the wrapped exception, or null if not found @@ -430,9 +419,9 @@ private static Throwable getCauseUsingFieldName(Throwable throwable, String fiel //----------------------------------------------------------------------- /** *

Checks if the Throwable class has a getCause method.

- * + * *

This is true for JDK 1.4 and above.

- * + * * @return true if Throwable is nestable * @since 2.0 */ @@ -442,7 +431,7 @@ public static boolean isThrowableNested() { /** *

Checks whether this Throwable class can store a cause.

- * + * *

This method does not check whether it actually does store a cause.

* * @param throwable the Throwable to examine, may be null @@ -496,11 +485,11 @@ public static boolean isNestedThrowable(Throwable throwable) { /** *

Counts the number of Throwable objects in the * exception chain.

- * + * *

A throwable without cause will return 1. * A throwable with one cause will return 2 and so on. * A null throwable will return 0.

- * + * * @param throwable the throwable to inspect, may be null * @return the count of throwables, zero if null input */ @@ -516,7 +505,7 @@ public static int getThrowableCount(Throwable throwable) { /** *

Returns the list of Throwable objects in the * exception chain.

- * + * *

A throwable without cause will return an array containing * one element - the input throwable. * A throwable with one cause will return an array containing @@ -541,7 +530,7 @@ public static Throwable[] getThrowables(Throwable throwable) { * that matches the specified class (exactly) in the exception chain. * Subclasses of the specified class do not match - see * {@link #indexOfType(Throwable, Class)} for the opposite.

- * + * *

A null throwable returns -1. * A null type returns -1. * No match in the chain returns -1.

@@ -560,7 +549,7 @@ public static int indexOfThrowable(Throwable throwable, Class clazz) { * a specified index. * Subclasses of the specified class do not match - see * {@link #indexOfType(Throwable, Class, int)} for the opposite.

- * + * *

A null throwable returns -1. * A null type returns -1. * No match in the chain returns -1. @@ -583,7 +572,7 @@ public static int indexOfThrowable(Throwable throwable, Class clazz, int fromInd * that matches the specified class or subclass in the exception chain. * Subclasses of the specified class do match - see * {@link #indexOfThrowable(Throwable, Class)} for the opposite.

- * + * *

A null throwable returns -1. * A null type returns -1. * No match in the chain returns -1.

@@ -603,7 +592,7 @@ public static int indexOfType(Throwable throwable, Class type) { * a specified index. * Subclasses of the specified class do match - see * {@link #indexOfThrowable(Throwable, Class)} for the opposite.

- * + * *

A null throwable returns -1. * A null type returns -1. * No match in the chain returns -1. @@ -623,7 +612,7 @@ public static int indexOfType(Throwable throwable, Class type, int fromIndex) { /** *

Worker method for the indexOfType methods.

- * + * * @param throwable the throwable to inspect, may be null * @param type the type to search for, subclasses match, null returns -1 * @param fromIndex the (zero based) index of the starting position, @@ -663,15 +652,18 @@ private static int indexOf(Throwable throwable, Class type, int fromIndex, boole /** *

Prints a compact stack trace for the root cause of a throwable * to System.err.

- * + * *

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 output of this method is consistent across JDK versions. + * Note that this is the opposite order to the JDK1.4 display.

+ * *

The method is equivalent to printStackTrace for throwables * that don't have nested causes.

- * + * * @param throwable the throwable to output * @since 2.0 */ @@ -687,9 +679,12 @@ public static void printRootCauseStackTrace(Throwable throwable) { * Then it prints the wrapped exception and continues with stack frames * until the wrapper exception is caught and wrapped again, etc.

* + *

The output of this method is consistent across JDK versions. + * Note that this is the opposite order to the JDK1.4 display.

+ * *

The method is equivalent to printStackTrace for throwables * that don't have nested causes.

- * + * * @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 null @@ -717,9 +712,12 @@ public static void printRootCauseStackTrace(Throwable throwable, PrintStream str * Then it prints the wrapped exception and continues with stack frames * until the wrapper exception is caught and wrapped again, etc.

* + *

The output of this method is consistent across JDK versions. + * Note that this is the opposite order to the JDK1.4 display.

+ * *

The method is equivalent to printStackTrace for throwables * that don't have nested causes.

- * + * * @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 null @@ -743,7 +741,12 @@ public static void printRootCauseStackTrace(Throwable throwable, PrintWriter wri /** *

Creates a compact stack trace for the root cause of the supplied * Throwable.

- * + * + *

The output of this method is consistent across JDK versions. + * It consists of the root exception followed by each of its wrapping + * exceptions separated by '[wrapped]'. Note that this is the opposite + * order to the JDK1.4 display.

+ * * @param throwable the throwable to examine, may be null * @return an array of stack trace frames, never null * @since 2.0 @@ -776,7 +779,7 @@ public static String[] getRootCauseStackTrace(Throwable throwable) { /** *

Removes common frames from the cause trace given the two stack traces.

- * + * * @param causeFrames stack trace of a cause throwable * @param wrapperFrames stack trace of a wrapper throwable * @throws IllegalArgumentException if either argument is null @@ -805,6 +808,11 @@ public static void removeCommonFrames(List causeFrames, List wrapperFrames) { /** *

Gets the stack trace from a Throwable as a String.

* + *

The result of this method vary by JDK version as this method + * uses {@link Throwable#printStackTrace(java.io.PrintWriter)}. + * On JDK1.3 and earlier, the cause exception will not be shown + * unless the specified throwable alters printStackTrace.

+ * * @param throwable the Throwable to be examined * @return the stack trace as generated by the exception's * printStackTrace(PrintWriter) method @@ -819,6 +827,9 @@ public static String getStackTrace(Throwable throwable) { /** *

A way to get the entire nested stack-trace of an throwable.

* + *

The result of this method is highly dependent on the JDK version + * and whether the exceptions override printStackTrace or not.

+ * * @param throwable the Throwable to be examined * @return the nested stack trace, with the root cause first * @since 2.0 @@ -842,6 +853,11 @@ public static String getFullStackTrace(Throwable throwable) { * Throwable object, decomposing it into a list of * stack frames.

* + *

The result of this method vary by JDK version as this method + * uses {@link Throwable#printStackTrace(java.io.PrintWriter)}. + * On JDK1.3 and earlier, the cause exception will not be shown + * unless the specified throwable alters printStackTrace.

+ * * @param throwable the Throwable to examine, may be null * @return an array of strings describing each stack frame, never null */ @@ -853,21 +869,16 @@ public static String[] getStackFrames(Throwable throwable) { } /** - *

- * Returns an array where each element is a line from the argument. - *

- *

- * The end of line is determined by the value of {@link SystemUtils#LINE_SEPARATOR}. - *

- * - *

- * Functionality shared between the getStackFrames(Throwable) methods of this and the - * {@link org.apache.commons.lang.exception.NestableDelegate} classes. - *

- * - * @param stackTrace - * A stack trace String. - * @return an array where each element is a line from the argument. + *

Returns an array where each element is a line from the argument.

+ * + *

The end of line is determined by the value of {@link SystemUtils#LINE_SEPARATOR}.

+ * + *

Functionality shared between the + * getStackFrames(Throwable) methods of this and the + * {@link org.apache.commons.lang.exception.NestableDelegate} classes.

+ * + * @param stackTrace a stack trace String + * @return an array where each element is a line from the argument */ static String[] getStackFrames(String stackTrace) { String linebreak = SystemUtils.LINE_SEPARATOR; @@ -881,7 +892,8 @@ static String[] getStackFrames(String stackTrace) { /** *

Produces a List of stack frames - the message - * is not included.

+ * is not included. Only the trace of the specified exception is + * returned, any caused by trace is stripped.

* *

This works in most cases - it will only fail if the exception * message contains a line that starts with: @@ -909,5 +921,5 @@ static List getStackFrameList(Throwable t) { } return list; } - + }