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. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@136985 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c838670a6f
commit
b34534b9d7
|
@ -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 <code>Throwable</code> 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 <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[] {});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
|
||||
* @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
|
||||
* @author <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
|
||||
* @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
|
||||
* @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 <code>PrintStream</code> 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 <code>PrintWriter</code> 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 <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 @@ 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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue