package org.codefx.demo.java9.internal.stack;

import jdk.internal.vm.annotation.ReservedStackAccess;

public class ReservingStackAreas {

	private static int depth = 0;

	public static void main(String[] args) {
		System.out.println("\nDEPTH & CALLS USING REGULAR STACK");
		determineDepth();
		recurseThenGreet();

		System.out.println("\nDEPTH USING REGULAR STACK & CALLS USING RESERVED STACK");
		determineDepth();
		recurseWithReservedStackThenGreet();

		System.out.println("\nDEPTH USING RESERVED STACK");
		determineDepthWithReservedStack();
	}

	private static void determineDepth() {
		depth = 0;
		try {
			recurseToDetermineMaxDepth();
		} catch (StackOverflowError err) { }
		System.out.printf("Depth: %d%n", depth);
	}

	private static void recurseToDetermineMaxDepth() {
		depth++;
		recurseToDetermineMaxDepth();
	}

	private static void recurseThenGreet() {
		try {
			recurseToDepth();
			System.out.println("Hi!");
		} catch (StackOverflowError err) {
			System.out.println("Stack Overflow");
		}
	}

	private static void recurseToDepth() {
		depth--;
		if (depth > 0)
			recurseToDepth();
	}

	@ReservedStackAccess
	private static void determineDepthWithReservedStack() {
		determineDepth();
	}

	@ReservedStackAccess
	private static void recurseWithReservedStackThenGreet() {
		recurseThenGreet();
	}

}

@@ -40,16 +40,267 @@
 stack;%0A%0A
+// this import may lead to errors in your IDE - two choices:%0A//  - put the import and the two uses below in comments%0A//  - configure your IDE to add the following compiler argument:%0A//    --add-exports=java.base/jdk.internal.vm.annotation=ALL-UNNAMED%0A
 import j

Add note addressing IDE problems with 'ReservingStackAreas'