publicfinalclassStackTraceElementimplementsjava.io.Serializable{ // Normally initialized by VM (public constructor added in 1.5) private String declaringClass; private String methodName; private String fileName; privateint lineNumber;
/** * 获取执行点所属源文件名称 * @return the name of the file containing the execution point * represented by this stack trace element, or {@code null} if * this information is unavailable. */ public String getFileName(){ return fileName; }
/** * 获取执行点在源文件中的行号 * @return the line number of the source line containing the execution * point represented by this stack trace element, or a negative * number if this information is unavailable. */ publicintgetLineNumber(){ return lineNumber; }
/** * 获取执行点所属类的全限定名 * @return the fully qualified name of the {@code Class} containing * the execution point represented by this stack trace element. */ public String getClassName(){ return declaringClass; }
/** * 获取执行点所属方法名称 * @return the name of the method containing the execution point * represented by this stack trace element. */ public String getMethodName(){ return methodName; }
publicclassThrowableimplementsSerializable{ private Throwable cause = this; publicsynchronized Throwable getCause(){ return (cause==this ? null : cause); } protectedThrowable(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace){ if (writableStackTrace) { fillInStackTrace(); } else { stackTrace = null; } detailMessage = message; this.cause = cause; if (!enableSuppression) suppressedExceptions = null; } private StackTraceElement[] stackTrace = UNASSIGNED_STACK; /** * A shared value for an empty stack. */ privatestaticfinal StackTraceElement[] UNASSIGNED_STACK = new StackTraceElement[0]; publicsynchronized Throwable fillInStackTrace(){ if (stackTrace != null || backtrace != null/* Out of protocol state */ ) { fillInStackTrace(0); stackTrace = UNASSIGNED_STACK; } returnthis; }
public StackTraceElement[] getStackTrace() { return getOurStackTrace().clone(); } privatesynchronized StackTraceElement[] getOurStackTrace() { // Initialize stack trace field with information from // backtrace if this is the first call to this method if (stackTrace == UNASSIGNED_STACK || (stackTrace == null && backtrace != null) /* Out of protocol state */) { int depth = getStackTraceDepth(); stackTrace = new StackTraceElement[depth]; for (int i=0; i < depth; i++) stackTrace[i] = getStackTraceElement(i); } elseif (stackTrace == null) { return UNASSIGNED_STACK; } return stackTrace; }
privatevoidprintStackTrace(PrintStreamOrWriter s){ // Guard against malicious overrides of Throwable.equals by // using a Set with identity equality semantics. Set<Throwable> dejaVu = Collections.newSetFromMap(new IdentityHashMap<Throwable, Boolean>()); dejaVu.add(this);
// Print suppressed exceptions, if any for (Throwable se : getSuppressed()) se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, "\t", dejaVu);
// Print cause, if any Throwable ourCause = getCause(); if (ourCause != null) ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, "", dejaVu); } }
privatevoidprintEnclosedStackTrace(PrintStreamOrWriter s, StackTraceElement[] enclosingTrace, String caption, String prefix, Set<Throwable> dejaVu){ assert Thread.holdsLock(s.lock()); if (dejaVu.contains(this)) { s.println("\t[CIRCULAR REFERENCE:" + this + "]"); } else { dejaVu.add(this); // Compute number of frames in common between this and enclosing trace StackTraceElement[] trace = getOurStackTrace(); int m = trace.length - 1; int n = enclosingTrace.length - 1; while (m >= 0 && n >=0 && trace[m].equals(enclosingTrace[n])) { m--; n--; } int framesInCommon = trace.length - 1 - m;
// Print our stack trace s.println(prefix + caption + this); for (int i = 0; i <= m; i++) s.println(prefix + "\tat " + trace[i]); if (framesInCommon != 0) s.println(prefix + "\t... " + framesInCommon + " more");
// Print suppressed exceptions, if any for (Throwable se : getSuppressed()) se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, prefix +"\t", dejaVu);
// Print cause, if any Throwable ourCause = getCause(); if (ourCause != null) ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, prefix, dejaVu); } } }