1 // Check that stack trace's work, and stack trace line numbers, if available,
4 public class StackTrace2
6 public static void main(String
[] args
)
10 new StackTrace2().a();
14 StackTraceElement
[] trace
= x
.getStackTrace();
31 public void doCrash(Object o
)
37 static void checkTrace(StackTraceElement
[] trace
)
39 System
.out
.println("Trace length = " + trace
.length
);
40 checkLine(trace
[0], "StackTrace2$Inner", "doCrash", 33);
41 checkLine(trace
[1], "StackTrace2$Inner", "foo", 28);
42 checkLine(trace
[2], "StackTrace2", "a", 21);
43 checkLine(trace
[3], "StackTrace2", "main", 10);
46 static void checkLine(StackTraceElement frame
, String expected_cl
,
47 String expected_method
, int expected_line
)
49 if (frame
.getClassName().equals(expected_cl
))
50 System
.out
.print(expected_cl
);
52 System
.out
.print("FAIL - expected " + expected_cl
+ ", got: " +
53 frame
.getClassName());
55 System
.out
.print(".");
57 if (frame
.getMethodName().equals(expected_method
))
58 System
.out
.print(expected_method
);
60 System
.out
.print("FAIL - expected " + expected_method
+ ", got: " +
61 frame
.getMethodName());
63 System
.out
.print(":");
65 // Permit either the correct line number or no line number. This is so
66 // we don't fail on platforms that don't yet support reading debug info
67 // for stack traces, or when no debug info is available.
68 if (frame
.getLineNumber() < 0
69 || (frame
.getLineNumber() == expected_line
70 && frame
.getFileName().equals("StackTrace2.java")))
71 System
.out
.println("OK");
73 System
.out
.println("FAIL - expected " + expected_line
+ ", got: "
74 + frame
.getLineNumber() + ", in file "
75 + frame
.getFileName());