2006-07-07 01:26:24 +02:00
|
|
|
// Check that stack trace's work, and stack trace line numbers, if available,
|
|
|
|
// are correct.
|
|
|
|
|
2006-07-07 21:33:02 +02:00
|
|
|
public class StackTrace2
|
2006-07-07 01:26:24 +02:00
|
|
|
{
|
|
|
|
public static void main(String[] args)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2008-08-07 16:19:15 +02:00
|
|
|
new StackTrace2().a();
|
2006-07-07 01:26:24 +02:00
|
|
|
}
|
|
|
|
catch (Exception x)
|
|
|
|
{
|
|
|
|
StackTraceElement[] trace = x.getStackTrace();
|
|
|
|
checkTrace(trace);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-07 16:19:15 +02:00
|
|
|
void a()
|
2006-07-07 01:26:24 +02:00
|
|
|
{
|
2008-08-07 16:19:15 +02:00
|
|
|
new Inner().foo();
|
2006-07-07 01:26:24 +02:00
|
|
|
}
|
|
|
|
|
2008-08-07 16:19:15 +02:00
|
|
|
class Inner
|
2006-07-07 01:26:24 +02:00
|
|
|
{
|
2008-08-07 16:19:15 +02:00
|
|
|
public void foo()
|
2006-07-07 01:26:24 +02:00
|
|
|
{
|
|
|
|
doCrash(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void doCrash(Object o)
|
|
|
|
{
|
|
|
|
o.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void checkTrace(StackTraceElement[] trace)
|
|
|
|
{
|
|
|
|
System.out.println("Trace length = " + trace.length);
|
2006-07-07 21:33:02 +02:00
|
|
|
checkLine(trace[0], "StackTrace2$Inner", "doCrash", 33);
|
2008-08-07 16:19:15 +02:00
|
|
|
checkLine(trace[1], "StackTrace2$Inner", "foo", 28);
|
2006-07-07 21:33:02 +02:00
|
|
|
checkLine(trace[2], "StackTrace2", "a", 21);
|
|
|
|
checkLine(trace[3], "StackTrace2", "main", 10);
|
2006-07-07 01:26:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void checkLine(StackTraceElement frame, String expected_cl,
|
|
|
|
String expected_method, int expected_line)
|
|
|
|
{
|
|
|
|
if (frame.getClassName().equals(expected_cl))
|
|
|
|
System.out.print(expected_cl);
|
|
|
|
else
|
|
|
|
System.out.print("FAIL - expected " + expected_cl + ", got: " +
|
|
|
|
frame.getClassName());
|
|
|
|
|
|
|
|
System.out.print(".");
|
|
|
|
|
|
|
|
if (frame.getMethodName().equals(expected_method))
|
|
|
|
System.out.print(expected_method);
|
|
|
|
else
|
|
|
|
System.out.print("FAIL - expected " + expected_method + ", got: " +
|
|
|
|
frame.getMethodName());
|
|
|
|
|
|
|
|
System.out.print(":");
|
|
|
|
|
|
|
|
// Permit either the correct line number or no line number. This is so
|
|
|
|
// we don't fail on platforms that don't yet support reading debug info
|
|
|
|
// for stack traces, or when no debug info is available.
|
|
|
|
if (frame.getLineNumber() < 0
|
|
|
|
|| (frame.getLineNumber() == expected_line
|
2006-07-07 21:33:02 +02:00
|
|
|
&& frame.getFileName().equals("StackTrace2.java")))
|
2006-07-07 01:26:24 +02:00
|
|
|
System.out.println("OK");
|
|
|
|
else
|
2007-09-06 20:58:20 +02:00
|
|
|
System.out.println("FAIL - expected " + expected_line + ", got: "
|
|
|
|
+ frame.getLineNumber() + ", in file "
|
|
|
|
+ frame.getFileName());
|
2006-07-07 01:26:24 +02:00
|
|
|
}
|
|
|
|
}
|