JDK-8153912 : Reconsider StackFrame::getFileName and StackFrame::getLineNumber
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2016-04-09
  • Updated: 2017-05-17
  • Resolved: 2016-05-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b118Fixed
Related Reports
Relates :  
Relates :  
Description
StackFrame::getFileName and StackFrame::getLineNumber are originally proposed with  the view of any stack walking code can migrate to the StackWalker API without the use of StackTraceElement.

File name and line number are useful for debugging and troubleshooting purpose.  It has additional overhead to map from a method and BCI to look up the file name and line number. 

StackFrame::toStackTraceElement method returns StackTraceElement that includes the file name and line number.    There is no particular benefit to duplicate getFileName and getLineNumber methods in StackFrame.   It is equivalently convenient to call StackFrame.toStackTraceElement().getFileName() (or getLineNumber).


Comments
A stack frame is represented by a method and bci where bci is not exposed in the API that will be useful in the case of generated class file where line number is not available. Log4j plans to convert to use StackWalker and they will also capture filename and line number that are optional and may not be available. A logging facility has to handle the case when the information is unavailable. It would typically log some token to represent the unavailable case. StackTraceElement::getFileName and StackTraceElement::getLineNumber returns null and negative number if the information unavailable and the logging facility could simply log such return value to avoid calling Optional.get().orElse(-1) for example. StackFrame::getFileName and StackFrame::getLineNumber method are revised to drop Optional. Common use cases would directly use the returned value. This would also save the creation of short-lived Optional object that might help logging filename/linenumber case. A new method StackFrame::getByteCodeIndex is also added.
09-05-2016