diff --git a/src/java/org/apache/commons/lang/exception/ExceptionUtils.java b/src/java/org/apache/commons/lang/exception/ExceptionUtils.java
index 2e6cf62de..f0dc75054 100644
--- a/src/java/org/apache/commons/lang/exception/ExceptionUtils.java
+++ b/src/java/org/apache/commons/lang/exception/ExceptionUtils.java
@@ -57,14 +57,13 @@ package org.apache.commons.lang.exception;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.io.PrintWriter;
+import java.io.StringWriter;
import java.sql.SQLException;
import java.util.ArrayList;
-
-/*
-TODO: Refactor code from NestableDelegate to ExceptionUtils.
-
-printStackTrace(Throwable, PrintWriter)
-*/
+import java.util.LinkedList;
+import java.util.List;
+import java.util.StringTokenizer;
/**
* Utility routines for manipulating Throwable
objects.
@@ -308,7 +307,7 @@ public class ExceptionUtils
*/
public static Throwable[] getThrowables(Throwable t)
{
- ArrayList list = new ArrayList();
+ List list = new ArrayList();
while (t != null)
{
list.add(t);
@@ -367,4 +366,52 @@ public class ExceptionUtils
}
return -1;
}
+
+ /**
+ * A convenient way of extracting the stack trace from an
+ * exception.
+ *
+ * @param t The Throwable
.
+ * @return The stack trace as generated by the exception's
+ * printStackTrace(PrintWriter)
method.
+ */
+ public static String getStackTrace(Throwable t)
+ {
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw, true);
+ t.printStackTrace(pw);
+ return sw.getBuffer().toString();
+ }
+
+ /**
+ * Captures the stack trace associated with the specified
+ * Throwable
object, decomposing it into a list of
+ * stack frames.
+ *
+ * @param t The Throwable
.
+ * @return An array of strings describing each stack frame.
+ */
+ public static String[] getStackFrames(Throwable t)
+ {
+ return getStackFrames(getStackTrace(t));
+ }
+
+ /**
+ * Functionality shared between the
+ * getStackFrames(Throwable)
methods of this and the
+ * {@link org.apache.commons.lang.exception.NestableDelegate}
+ * classes.
+ */
+ static String[] getStackFrames(String stackTrace)
+ {
+ // TODO: Use constant from org.apache.commons.lang.SystemUtils.
+ String linebreak = System.getProperty("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[] {});
+ }
}
diff --git a/src/java/org/apache/commons/lang/exception/NestableDelegate.java b/src/java/org/apache/commons/lang/exception/NestableDelegate.java
index 729092aaa..1db15d59b 100644
--- a/src/java/org/apache/commons/lang/exception/NestableDelegate.java
+++ b/src/java/org/apache/commons/lang/exception/NestableDelegate.java
@@ -59,15 +59,13 @@ import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
-import java.util.LinkedList;
-import java.util.StringTokenizer;
/**
* @author Rafal Krzewski
* @author Daniel Rall
* @author Kasper Nielsen
* @author Steven Caswell
- * @version $Id: NestableDelegate.java,v 1.5 2002/08/21 23:52:02 dlr Exp $
+ * @version $Id: NestableDelegate.java,v 1.6 2002/08/24 19:18:50 dlr Exp $
*/
public class NestableDelegate
{
@@ -258,16 +256,15 @@ public class NestableDelegate
*/
public void printStackTrace()
{
- synchronized (System.err)
- {
- printStackTrace(System.err);
- }
+ printStackTrace(System.err);
}
/**
- * Prints the stack trace of this exception to the specified print stream.
+ * Prints the stack trace of this exception to the specified
+ * stream.
*
* @param out PrintStream
to use for output.
+ * @see #printStackTrace(PrintWriter)
*/
public void printStackTrace(PrintStream out)
{
@@ -281,7 +278,8 @@ public class NestableDelegate
}
/**
- * Prints the stack trace of this exception to the specified print writer.
+ * Prints the stack trace of this exception to the specified
+ * writer.
*
* @param out PrintWriter
to use for output.
*/
@@ -289,7 +287,7 @@ public class NestableDelegate
{
synchronized (out)
{
- String[] st = decompose(this.cause);
+ String[] st = getStackFrames(this.cause);
Throwable nestedCause = ExceptionUtils.getCause(this.cause);
if (nestedCause != null)
{
@@ -300,7 +298,7 @@ public class NestableDelegate
}
else
{
- String[] nst = decompose(nestedCause);
+ String[] nst = getStackFrames(nestedCause);
for (int i = 0; i < nst.length; i++)
{
out.println(nst[i]);
@@ -318,13 +316,14 @@ public class NestableDelegate
}
/**
- * Captures the stack trace associated with a Throwable
- * object, decomposing it into a list of stack frames.
+ * Captures the stack trace associated with the specified
+ * Throwable
object, decomposing it into a list of
+ * stack frames.
*
* @param t The Throwable
.
* @return An array of strings describing each stack frame.
*/
- private String[] decompose(Throwable t)
+ private String[] getStackFrames(Throwable t)
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
@@ -338,15 +337,6 @@ public class NestableDelegate
{
t.printStackTrace(pw);
}
-
- String linebreak = System.getProperty("line.separator");
- StringTokenizer st = new StringTokenizer(sw.getBuffer().toString(),
- linebreak);
- LinkedList list = new LinkedList();
- while (st.hasMoreTokens())
- {
- list.add(st.nextToken());
- }
- return (String []) list.toArray(new String[] {});
+ return ExceptionUtils.getStackFrames(sw.getBuffer().toString());
}
}