Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/java.base/share/classes/java/lang/StackTraceElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,16 @@ public String toString() {
+ methodName.length() + 1
+ Math.max(UNKNOWN_SOURCE.length(), length(fileName)) + 1
+ 12;
return appendTo(
new StringBuilder(estimatedLength))
.toString();
}

StringBuilder sb = new StringBuilder(estimatedLength);
/**
* Prints the toString result to the given buf, avoiding extra string allocations.
*/
StringBuilder appendTo(StringBuilder sb) {
int startingLength = sb.length();
if (!dropClassLoaderName() && classLoaderName != null && !classLoaderName.isEmpty()) {
sb.append(classLoaderName).append('/');
}
Expand All @@ -378,7 +386,7 @@ public String toString() {
}
}

if (sb.length() > 0) {
if (sb.length() != startingLength) {
sb.append('/');
}

Expand All @@ -393,9 +401,7 @@ public String toString() {
sb.append(':').append(lineNumber);
}
}
sb.append(')');

return sb.toString();
return sb.append(')');
}

private static int length(String s) {
Expand Down
79 changes: 55 additions & 24 deletions src/java.base/share/classes/java/lang/Throwable.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -687,26 +687,42 @@ public void printStackTrace(PrintStream s) {
}

private void printStackTrace(PrintStreamOrWriter s) {
// Guard against malicious overrides of Throwable.equals by
// using a Set with identity equality semantics.
Set<Throwable> dejaVu = Collections.newSetFromMap(new IdentityHashMap<>());
dejaVu.add(this);

synchronized(s.lock()) {
// Print our stack trace
s.println(this);
var lineBuffer = new StringBuilder(128); // arbitrary size
lineBuffer.append("\tat ");
int prefixLen = lineBuffer.length();

StackTraceElement[] trace = getOurStackTrace();
for (StackTraceElement traceElement : trace)
s.println("\tat " + traceElement);
for (int i = 0; i < trace.length; i++) {
if (i != 0) {
lineBuffer.setLength(prefixLen);
}
trace[i].appendTo(lineBuffer);
s.println(lineBuffer.toString());
}

// Print suppressed exceptions, if any
for (Throwable se : getSuppressed())
se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, "\t", dejaVu);
var suppressed = getSuppressed();
var ourCause = getCause();

// Print cause, if any
Throwable ourCause = getCause();
if (ourCause != null)
ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, "", dejaVu);
if (suppressed.length > 0 || ourCause != null) {
/*
* Guard against malicious overrides of Throwable.equals by
* using a Set with identity equality semantics.
*/
Set<Throwable> dejaVu = Collections.newSetFromMap(new IdentityHashMap<>());
dejaVu.add(this);

// Print suppressed exceptions, if any
for (Throwable se : suppressed)
se.printEnclosedStackTrace(s, lineBuffer, trace, SUPPRESSED_CAPTION, "\t", dejaVu);

// Print cause, if any
if (ourCause != null) {
ourCause.printEnclosedStackTrace(s, lineBuffer, trace, CAUSE_CAPTION, "", dejaVu);
}
}
}
}

Expand All @@ -715,13 +731,16 @@ private void printStackTrace(PrintStreamOrWriter s) {
* stack trace.
*/
private void printEnclosedStackTrace(PrintStreamOrWriter s,
StringBuilder lineBuffer,
StackTraceElement[] enclosingTrace,
String caption,
String prefix,
Set<Throwable> dejaVu) {
assert Thread.holdsLock(s.lock());
lineBuffer.setLength(0);
if (dejaVu.contains(this)) {
s.println(prefix + caption + "[CIRCULAR REFERENCE: " + this + "]");
s.println(
lineBuffer.append(prefix).append(caption).append("[CIRCULAR REFERENCE: ").append(this).append("]"));
} else {
dejaVu.add(this);
// Compute number of frames in common between this and enclosing trace
Expand All @@ -734,21 +753,33 @@ private void printEnclosedStackTrace(PrintStreamOrWriter s,
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");
s.println(
lineBuffer.append(prefix).append(caption).append(this));
lineBuffer.setLength(prefix.length());
lineBuffer.append("\tat ");
int indent = lineBuffer.length();
for (int i = 0; i <= m; i++) {
if (i != 0) {
lineBuffer.setLength(indent);
}
s.println(
trace[i].appendTo(lineBuffer));
}
if (framesInCommon != 0) {
lineBuffer.setLength(prefix.length());
s.println(
lineBuffer.append("\t... ").append(framesInCommon).append(" more"));
}

// Print suppressed exceptions, if any
for (Throwable se : getSuppressed())
se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION,
prefix +"\t", dejaVu);
se.printEnclosedStackTrace(s, lineBuffer, trace, SUPPRESSED_CAPTION,
prefix.concat("\t"), dejaVu);

// Print cause, if any
Throwable ourCause = getCause();
if (ourCause != null)
ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, prefix, dejaVu);
ourCause.printEnclosedStackTrace(s, lineBuffer, trace, CAUSE_CAPTION, prefix, dejaVu);
}
}

Expand Down
Loading