O'Reilly logo

Java 9: Building Robust Modular Applications by Jason Lee, Peter Verhas, Dr. Edward Lavieri

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Example - Restricting callers

In this section we develop a sample library with two methods. The hello() method prints hello to the standard output. The callMe() method accepts a Runnable as an argument and runs it. The first method however is restricted. It executes only if the caller is purely outside of the library. It throws an IllegalCallerException if the caller obtained the control in a way that the library was calling out, presumably via the second method invoking the passed Runnable. The implementation of the API is simple:

 package packt.java9.deep.stackwalker.myrestrictivelibrary; public class RestrictedAPI { public void hello(){ CheckEligibility.itIsNotCallBack(); System.out.println("hello"); } public void callMe(Runnable cb){ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required