Accessing the BCI of Throwable's StackTraceElement

Eirik Bjørsnøs eirbjo at gmail.com
Fri Feb 18 11:42:28 UTC 2022


Hi,

Currently, StackWalker.StackFrame::getByteCodeIndex provides a way to get
the BCI of stack frames during a stack walk.

Similarly, it might be useful to get the BCI when inspecting a Throwable's
StackTraceElements. This does however not seem possible, given that
StackTraceElement currently does not expose the BCI. (It exposes the
lineNumber, but that's not useful for my current use case).

On another note, it seems like calling Throwable.getStackTrace() would be a
wasteful way to just find the BCI of the top stack frame. Would be really
nice if one could do a StackWalk with a Throwable as input, which I imagine
should work a lot more efficiently.

(My use case here is to attribute any exception to a BCI in the same method
such that I don't need to instrument code to track the BCI myself).

So I guess I have two questions:

1: Would it be reasonable to add a getByteCodeIndex method to
StackTraceElement? Or perhaps even make StackTraceElement implement
StackWalker.StackFrame?

2: Could the StackWalker API be suitable for walking not just the stack of
current threads, but also the stacks of Throwables? Was this discussed
during the development of StackWalker for Java 9?

Cheers,
Eirik.


More information about the core-libs-dev mailing list