Thoughts on adding getElementClass() method to StackTraceElement?

Nick Williams nicholas+openjdk at
Mon Jun 17 06:32:40 UTC 2013

On Jun 17, 2013, at 1:12 AM, Peter Levart wrote:

> On 06/17/2013 08:06 AM, Jeroen Frijters wrote:
>> Nick Williams wrote:
>>> What if we also added a getStackFrames() method to Throwable? That would
>>> meet my needs but it would also satisfy what I'm observing is a desire
>>> to have a new API for this (StackFrame) instead of adding it to
>>> StackTraceElement. I'm very open to how it's implemented, as long as it
>>> satisfies my use case. :-)
>>> The stack trace of a Throwable can be "filled in" on demand when
>>> getStackTrace() is called the first time, so that the overhead isn't
>>> incurred when creating and throwing the exception. Presumably, we would
>>> need to do something similar with getStackFrames(), especially since
>>> calling it would be less common.
>>> Thoughts on this?
>> Yes that is reasonable, but I'd add a static method to StackFrame instead. Something like StackFrame[] capture(Throwable).
> New API could be entirely unrelated to Throwable, if there was support for it in native code. Since there would have to be changes to the native code anyway to support this, why not create a separate API?

I'm not sure who's misunderstanding who. :-)

If some third party code that I have no control over throws an exception and I catch that exception, or some other code catches the exception and passes it to my code (because my code is a logging framework), I need to get the StackFrame[] for _when the exception was thrown_. Not the StackFrame[] related to my current method execution. How can that possibly be entirely unrelated to throwable?

The way I understand Jereon's suggestion, I'm thinking StackFrame would look like this:

public final class StackFrame {
 public Executable method();
 public String getFileName();
 public int getLineNumber();
 /** Shortcut for getModifiers() and Modifiers.NATIVE */
 public int isNativeMethod();
 /** Format exactly like StackTraceElement#toString() */
 public String toString();

 /** Gets current executing stack with number of frames skipped and max length */
 public static StackFrame[] capture(int skipFrames, int maxLength, boolean includeSourceInfo);
 /** Gets current executing stack with no frames skipped and no max length */
 public static StackFrame[] capture();
 /** Gets stack from when Throwable was created. */
 public static StackFrame[] capture(Throwable t);

But perhaps I am missing something.


More information about the core-libs-dev mailing list