Class Caller


  • public final class Caller
    extends Object
    This utility class provides a way to determine which class call a function.

    It inspirated from the Sun's sun.reflect.Reflection class

    Version:
    17.0 2020-01-04 14:41:35
    Author:
    Stéphane GALLAND
    Maven Group Id:
    org.arakhne.afc.core
    Maven Artifact Id:
    vmutils
    • Method Detail

      • getCaller

        @Pure
        public static Caller getCaller()
        Replies the stack trace mamager used by this utility class.
        Returns:
        the stack trace mamager.
      • getCallerMethod

        @Pure
        public static String getCallerMethod()
        Replies the method of the caller that invoked the function from which getCallerMethod() was invoked.

        The returned value is the name of the method instead of a Method instance. It is due to JRE that does not store in the stack trace the complete prototype of the methods. So the following code failed: the stack contains the method name "test2", but no function has the prototype void test2().

         class Test {
             public void test1(int a) {
                 test2();
             }
             public void test2(int a) {
                   getCallerMethod(); // IllegalArgumentException because test1() not defined.
             }
         }
         

        Another failure example:

         class Test2 {
             public void test1(int a) {
                 test2();
             }
             public void test1() {
             }
             public void test2(int a) {
                   getCallerMethod(); // test1() is replied !!! not test1(int)
             }
         }
         
        Returns:
        the method of the caller that invoked the function from which getCallerMethod() was invoked.
      • getCallerMethod

        @Pure
        public static String getCallerMethod​(int level)
        Replies the method from the stack according to its level.

        The given level permits to specify which method to reply:

        • 0: the method where is defined the function (f0) that has called getCallerClass()
        • 1: the method where is defined the function (f1) that has called f0
        • 2: the method where is defined the function (f2) that has called f1
        • etc.

        The returned value is the name of the method instead of a Method instance. It is due to JRE that does not store in the stack trace the complete prototype of the methods. So the following code failed: the stack contains the method name "test2", but no function has the prototype void test2().

         class Test {
             public void test1(int a) {
                 test2();
             }
             public void test2(int a) {
                   getCallerMethod(); // IllegalArgumentException because test1() not defined.
             }
         }
         

        Another failure example:

         class Test2 {
             public void test1(int a) {
                 test2();
             }
             public void test1() {
             }
             public void test2(int a) {
                   getCallerMethod(); // test1() is replied !!! not test1(int)
             }
         }
         
        Parameters:
        level - is the desired level of the class
        Returns:
        the method from the call stack according to the given level.
      • getCallerClass

        @Pure
        public static Class<?> getCallerClass()
        Replies the class of the caller that invoked the function from which getCallerClass() was invoked.
        Returns:
        the class of the caller that invoked the function from which getCallerClass() was invoked.
      • getCallerClass

        @Pure
        public static Class<?> getCallerClass​(int level)
        Replies the class from the stack according to its level.

        The given level permits to specify which class to reply:

        • 0: the class where is defined the function (f0) that has called getCallerClass()
        • 1: the class where is defined the function (f1) that has called f0
        • 2: the class where is defined the function (f2) that has called f1
        • etc.
        Parameters:
        level - is the desired level of the class
        Returns:
        the class from the call stack according to the given level.
      • findClassForFirstCallerOutsideVmutilModule

        @Pure
        public static Class<?> findClassForFirstCallerOutsideVmutilModule()
        Replies the class of the first caller that invoked the function from which getCallerClass() was invoked and is outside the vmutils module.
        Returns:
        the class of the caller that invoked the function from which getCallerClass() was invoked and outside the current module. The null value may be replied if the caller class cannot be retrieved.
        Since:
        17.0