You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dl...@apache.org on 2002/08/24 21:18:50 UTC
cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang/exception ExceptionUtils.java NestableDelegate.java
dlr 2002/08/24 12:18:50
Modified: lang/src/java/org/apache/commons/lang/exception
ExceptionUtils.java NestableDelegate.java
Log:
ExceptionUtils.getThrowables(Throwable): Switched from concrete
ArrayList local reference to List.
ExceptionUtils.getStackTrace(Throwable): New method for extracting the
text of a stack trace.
ExceptionUtils.getStackFrames(Throwable): Splits an exception's stace
trace into frames.
ExceptionUtils.getStackFrames(String): Splits a stace trace into frames.
NestableDelegate printStackTrace(): Delegate to
printStackTrace(PrintStream) using System.err rather than duplicating
its impl.
NestableDelegate printStackTrace(PrintWriter): Used new name
getStackFrames() method name.
NestableDelegate getStackFrames(Throwable): Renamed decompose() to
this and delegated to ExceptionUtils.getStackFrames(String) for half
of impl.
Revision Changes Path
1.14 +54 -7 jakarta-commons/lang/src/java/org/apache/commons/lang/exception/ExceptionUtils.java
Index: ExceptionUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/exception/ExceptionUtils.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -u -r1.13 -r1.14
--- ExceptionUtils.java 22 Aug 2002 00:04:42 -0000 1.13
+++ ExceptionUtils.java 24 Aug 2002 19:18:50 -0000 1.14
@@ -57,14 +57,13 @@
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 <code>Throwable</code> objects.
@@ -308,7 +307,7 @@
*/
public static Throwable[] getThrowables(Throwable t)
{
- ArrayList list = new ArrayList();
+ List list = new ArrayList();
while (t != null)
{
list.add(t);
@@ -366,5 +365,53 @@
}
}
return -1;
+ }
+
+ /**
+ * A convenient way of extracting the stack trace from an
+ * exception.
+ *
+ * @param t The <code>Throwable</code>.
+ * @return The stack trace as generated by the exception's
+ * <code>printStackTrace(PrintWriter)</code> 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
+ * <code>Throwable</code> object, decomposing it into a list of
+ * stack frames.
+ *
+ * @param t The <code>Throwable</code>.
+ * @return An array of strings describing each stack frame.
+ */
+ public static String[] getStackFrames(Throwable t)
+ {
+ return getStackFrames(getStackTrace(t));
+ }
+
+ /**
+ * Functionality shared between the
+ * <code>getStackFrames(Throwable)</code> 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[] {});
}
}
1.6 +14 -24 jakarta-commons/lang/src/java/org/apache/commons/lang/exception/NestableDelegate.java
Index: NestableDelegate.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/exception/NestableDelegate.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -u -r1.5 -r1.6
--- NestableDelegate.java 21 Aug 2002 23:52:02 -0000 1.5
+++ NestableDelegate.java 24 Aug 2002 19:18:50 -0000 1.6
@@ -59,8 +59,6 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
-import java.util.LinkedList;
-import java.util.StringTokenizer;
/**
* @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
@@ -258,16 +256,15 @@
*/
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 <code>PrintStream</code> to use for output.
+ * @see #printStackTrace(PrintWriter)
*/
public void printStackTrace(PrintStream out)
{
@@ -281,7 +278,8 @@
}
/**
- * 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 <code>PrintWriter</code> to use for output.
*/
@@ -289,7 +287,7 @@
{
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 @@
}
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 @@
}
/**
- * Captures the stack trace associated with a <code>Throwable</code>
- * object, decomposing it into a list of stack frames.
+ * Captures the stack trace associated with the specified
+ * <code>Throwable</code> object, decomposing it into a list of
+ * stack frames.
*
* @param t The <code>Throwable</code>.
* @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 @@
{
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());
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>