Module aeonics.core
Package aeonics.util

Class CheckCaller


  • public class CheckCaller
    extends java.lang.Object
    This class offers simple methods to protect a method based on the calling method. You can either require or prevent another class to call a your function.

    Example to make sure the specified method is in the call stack:

    public void foo() {
         CheckCaller.require(Example.class, "bar");
     }

    While this is not a bulletproof security system, it is safe enough to be used as intended. However, in case unsafe reflection, bytecode manipulations, java agents, or debuggers are into play, the call stack trace can be manipulated or spoofed to mislead this implementation.

    • Constructor Summary

      Constructors 
      Constructor Description
      CheckCaller()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void prevent​(java.lang.Class<?> clazz, java.lang.String method)
      Calls prevent(Class, String, int, boolean) with an unlimited depth and not strict check.
      static void prevent​(java.lang.Class<?> clazz, java.lang.String method, boolean strict)
      Calls prevent(Class, String, int, boolean) with an unlimited depth.
      static void prevent​(java.lang.Class<?> clazz, java.lang.String method, int maxDepth)
      Calls prevent(Class, String, int, boolean) in non strict mode.
      static void prevent​(java.lang.Class<?> clazz, java.lang.String method, int maxDepth, boolean strict)
      Checks that the current method has NOT been called as a result of the specified class and method.
      static void require​(java.lang.Class<?> clazz, java.lang.String method)
      Calls require(Class, String, int, boolean) with an unlimited depth and not strict check.
      static void require​(java.lang.Class<?> clazz, java.lang.String method, boolean strict)
      Calls require(Class, String, int, boolean) with an unlimited depth.
      static void require​(java.lang.Class<?> clazz, java.lang.String method, int maxDepth)
      Calls require(Class, String, int, boolean) in non strict mode.
      static void require​(java.lang.Class<?> clazz, java.lang.String method, int maxDepth, boolean strict)
      Checks that the current method has been called as a result of the specified class and method.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CheckCaller

        public CheckCaller()
    • Method Detail

      • require

        public static void require​(java.lang.Class<?> clazz,
                                   java.lang.String method,
                                   int maxDepth,
                                   boolean strict)
        Checks that the current method has been called as a result of the specified class and method.

        In other words:

         class Test {
             public void foo() {
                 CheckCaller.require(Example.class, "bar");
             }
         }
         

        This code sample will check that the foo() method has been called from Example.bar() somewhere in the hyerarchy, it may be a direct call or an indirect call.

        If the method is null, then it is not checked and any method of the specified class is considered.

        If the strict mode is enabled, then only the specified class is valid. Otherwise any other subclass (as defined by Class.isAssignableFrom(Class)) is also valid.

        The maxDepth parameted defines the number of intermediate method calls allowed. In other words, the depth of the stack trace. The depth count starts as 0 for the direct caller:

         public void foo() {
             
             // this will check that the direct caller (depth 0) 
             // is exactly the Example class and no other subclass.
             
             CheckCaller.require(Example.class, "bar", 0, true);
         }
         
        Parameters:
        clazz - the class to check
        method - the method to check (may be null)
        maxDepth - the maximum intermediate method calls
        strict - whether or not to check in strict mode
      • prevent

        public static void prevent​(java.lang.Class<?> clazz,
                                   java.lang.String method,
                                   int maxDepth,
                                   boolean strict)
        Checks that the current method has NOT been called as a result of the specified class and method.

        In other words:

         class Test {
             public void foo() {
                 CheckCaller.prevent(Example.class, "bar");
             }
         }
         

        This code sample will check that the foo() method has not been called from Example.bar() somewhere in the hyerarchy, either by a direct call or an indirect call.

        If the method is null, then it is not checked and any method of the specified class is considered.

        If the strict mode is enabled, then only the specified class is valid. Otherwise any other subclass (as defined by Class.isAssignableFrom(Class)) is also valid.

        The maxDepth parameted defines the number of intermediate method calls allowed. In other words, the depth of the stack trace. The depth count starts as 0 for the direct caller:

         public void foo() {
             
             // this will check that the direct caller (depth 0) 
             // is not the Example class nor any of its subclasses.
             // Although it would allow Example to be further in the hyerarchy.
             
             CheckCaller.prevent(Example.class, "bar", 0, false);
         }
         
        Parameters:
        clazz - the class to check
        method - the method to check (may be null)
        maxDepth - the maximum intermediate method calls
        strict - whether or not to check in strict mode