< prev index next >
src/java.base/share/classes/java/lang/Throwable.java
Print this page
@@ -209,10 +209,15 @@
* @serial
* @since 1.4
*/
private StackTraceElement[] stackTrace = UNASSIGNED_STACK;
+ /**
+ * Native code sets the depth of the backtrace for later retrieval
+ */
+ private transient int depth;
+
// Setting this static field introduces an acceptable
// initialization dependency on a few java.util classes.
private static final List<Throwable> SUPPRESSED_SENTINEL = Collections.emptyList();
/**
@@ -826,14 +831,15 @@
if (stackTrace == UNASSIGNED_STACK ||
(stackTrace == null && backtrace != null) /* Out of protocol state */) {
if (backtrace instanceof StackStreamFactory.StackTrace) {
stackTrace = ((StackStreamFactory.StackTrace)backtrace).getStackTraceElements();
} else {
- int depth = getStackTraceDepth();
stackTrace = new StackTraceElement[depth];
- for (int i = 0; i < depth; i++)
- stackTrace[i] = getStackTraceElement(i);
+ for (int i = 0; i < depth; i++) {
+ stackTrace[i] = new StackTraceElement();
+ }
+ getStackTraceElements(stackTrace);
}
} else if (stackTrace == null) {
return UNASSIGNED_STACK;
}
return stackTrace;
@@ -882,27 +888,14 @@
this.stackTrace = defensiveCopy;
}
}
/**
- * Returns the number of elements in the stack trace (or 0 if the stack
- * trace is unavailable).
- *
- * package-protection for use by SharedSecrets.
- */
- native int getStackTraceDepth();
-
- /**
- * Returns the specified element of the stack trace.
- *
- * package-protection for use by SharedSecrets.
- *
- * @param index index of the element to return.
- * @throws IndexOutOfBoundsException if {@code index < 0 ||
- * index >= getStackTraceDepth() }
+ * @param elements
+ * @throws IndexOutOfBoundsException if {@code elements.length != depth }
*/
- native StackTraceElement getStackTraceElement(int index);
+ private native void getStackTraceElements(StackTraceElement[] elements);
/**
* Reads a {@code Throwable} from a stream, enforcing
* well-formedness constraints on fields. Null entries and
* self-pointers are not allowed in the list of {@code
< prev index next >