📄 stacktrace.java
字号:
package org.jutil.java.throwable;/** * <p>Utility methods to deal with the stack trace of throwables. This is nominally * only available as output on an OutputStream, and we want more.</p> * * @path $Source: /cvsroot/org-jutil/jutil.org/src/org/jutil/java/throwable/StackTrace.java,v $ * @version $Revision: 1.6 $ * @date $Date: 2002/05/20 15:01:36 $ * @state $State: Exp $ * @author Jan Dockx * @author Marko van Dooren * @release $Name: $ */public class StackTrace { /* The revision of this class */ public final static String CVS_REVISION ="$Revision: 1.6 $"; /** * <p>Converts the stack trace of <t> into a list of names of active methods.</p> * <p>This method was based on * <code>private String junit.framework.TestSuite.exceptionToString(Throwable t)</code> * and * <code>public static String junit.runner.BaseTestRunner.filterStack(String stack)</code> * from JUnit.</p>//JDJDJD check copyright * * @param t * The Throwable to get the stack trace of. * @return The stack trace of <t>, as a list of Strings. The Strings are the names * of the active methods. They are ordered in the list starting from main, * with the method during whose execution the throwable was generated the * last entry.// JDJDJD perhaps it would not be a bad idea to use new objects, holding a reference to the method and a string for the location in that file */ /*@ @ pre t != null; @ @ // The result is not null. @ // The result does not contain null references. @ post (\result != null) && @ (\forall Object o;\result.contains(o); o != null) && @ (\forall Object o;\result.contains(o); o instanceof String); @*/ public static java.util.List asList(Throwable t) { /* stupid Sun developers only allow to print the stack trace to a stream; there is no way to actually inspect it, not even as a String; a StringWriter emulates a writer we can write the stack trace to, and we can get the string from that */ java.io.StringWriter stringWriter= new java.io.StringWriter(); java.io.PrintWriter writer= new java.io.PrintWriter(stringWriter); t.printStackTrace(writer); /* now we are going to read the lines of the stack trace, each containing a method call, one by one; we use a BufferedReader for this; we simulate a stream from the above string with the StringReader */ //JDJDJD I think I don't understand PipedReaders/Writers, but those should help here. java.io.StringReader stringReader = new java.io.StringReader(stringWriter.toString()); java.io.BufferedReader reader = new java.io.BufferedReader(stringReader); java.util.ArrayList result = new java.util.ArrayList(); try { reader.readLine(); // skip the first line that contains the name and optional message of the Trowable String line = reader.readLine(); while (line != null) { line = line.substring(4); // get rit of the "\tat " stuff in front of each method in the stack trace result.add(line); line = reader.readLine(); } } catch (java.io.IOException ioExc) { /* I don't think this can happen, but we'll stop processing the trace if it does happen; this happens next */ } result.trimToSize(); /* the stack trace of a Throwable lists the calling methods, last one first; in the list we are producing, it makes more sense to have the main as the first entry */ java.util.Collections.reverse(result); return result; }}/*<copyright>Copyright (C) 1997-2001. This software is copyrighted by the people and entities mentioned after the "@author" tags above, on behalf of the JUTIL.ORG Project. The copyright is dated by the dates after the "@date" tags above. All rights reserved.This software is published under the terms of the JUTIL.ORG SoftwareLicense version 1.1 or later, a copy of which has been included withthis distribution in the LICENSE file, which can also be found athttp://org-jutil.sourceforge.net/LICENSE. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the JUTIL.ORG Software License for more details.For more information, please see http://org-jutil.sourceforge.net/</copyright>*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -