Name: rmT116609 Date: 07/27/2001
java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)
While the StackTraceElement class reflects the data present in a printed stack
trace, it does not allow the programmer to determine, in the runtime system,
which method caused the Throwable. Additionally, if a class is compiled with
"javac -g:none", no line number or source file information will be available.
To illustrate this issue, consider the following class:
public class StackTest {
static StackTraceElement s1, s2;
public void test(int x) {
s1 = new Throwable().getStackTrace()[0];
}
public void test(String x) {
s2 = new Throwable().getStackTrace()[0];
}
public static void main(String[] argv) {
StackTest st = new StackTest();
st.test(1);
st.test("1");
System.out.println(s1.equals(s2));
}
}
Clearly, the stack traces s1 and s2 are being generated from different methods,
and when a class is compiled with debugging information, this program correctly
prints "false". However, if the class is compiled with "-g:none", the program
prints "true".
I would make the case that this behavior goes against a fundamental principle of
repeatable results.
The ideal enhancement to this feature would give the programmer enough
information to construct a reference (via reflection) to the Method object
relevant to the stack frame. The method I would propose would look like
Class[] StackTraceElement.getMethodParameters();
Optionally this could return String[] instead, if locating Class objects is too
involved a task.
With this data present in an instance of StackTraceElement, the .equals() method
could be extended to use this data, thus solving the repeatability issue noted
above.
There are numerous applications which could make good use of this additional
information for quality assurance, optimization and sundry purposes.
(Review ID: 128908)
======================================================================
- relates to
-
JDK-4942697 StackTraceElement doesn't contain Class objects but only the class names
-
- Closed
-