staticvoid notifyRecursionEnded() { // We moved this to a method instead of doing it directly in the // exception handler for the StackOverflowError, since we don't // need to know the line number (which might change later) when // requesting the breakpoint. Additionally this method is // used as a marker method to check for the correct top of stack // in the stack trace queried by JDI.
System.out.println("SOE occurred as expected");
}
staticint recurse(int depth) { if (depth == 0) { // Should have seen a stack overflow by now.
System.out.println("Exited without creating SOE");
System.exit(0);
}
try { int newDepth = recurse(depth - 1);
if (newDepth == -1_000) { // Pop some frames so there is room on the stack for the // call (including println()).
notifyRecursionEnded();
}
return newDepth - 1;
} catch (StackOverflowError e) { // Use negative depth to indicate the recursion has ended. return -1;
}
}
}
/********** test program **********/
publicclass Frames2Test extends TestScaffold {
public Frames2Test(String args[]) { super(args);
}
publicstaticvoid main(String[] args) throws Exception { new Frames2Test(args).startTests();
}
// Check that the stack looks as follows: // notifyRecursionEnded() // recurse() // .... // recurse() // main() // <whatever the VM likes to add>
// Check the bottom of the stack. for (int i = 0; i < frames1.size(); ++i) { int i2 = frames2.size() - frames1.size() + i; if (!frames1.get(i).equals(frames2.get(i2))) {
failure("Bottom methods do not match: " +
frames1.get(i) + " vs " + frames2.get(i2));
}
}
// Check the recurse() calls on the stack. for (int i = 1; i < frames2.size() - frames1.size(); ++i) { if (!frames2.get(i).name().equals("recurse")) {
failure("Expected recurse() but got " + frames2.get(i));
}
}
// Check the top method of the stack. if (!frames2.get(0).name().equals("notifyRecursionEnded")) {
failure("Expected notifyRecursionEnded() but got " + frames2.get(0));
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.