Interface Caller

  • All Known Implementing Classes:
    StackTraceCaller

    public interface Caller
    This utility class provides a way to determine which class call a function.
    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

      • getCallerClass

        @Pure
        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 (f<sub>0</sub>) that has called getCallerClass()
        • 1: the class where is defined the function (f<sub>1</sub>) that has called f<sub>0</sub>
        • 2: the class where is defined the function (f<sub>2</sub>) that has called f<sub>1</sub>
        • etc.
        Parameters:
        level - is the desired level of the class
        Returns:
        the class from the call stack according to the given level.
      • getCallerMethod

        @Pure
        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 (f<sub>0</sub>) that has called getCallerClass()
        • 1: the method where is defined the function (f<sub>1</sub>) that has called f<sub>0</sub>
        • 2: the method where is defined the function (f<sub>2</sub>) that has called f<sub>1</sub>
        • 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.
      • getCallerLine

        @Pure
        long getCallerLine​(int level)
        Replies the line number of the caller 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 (f<sub>0</sub>) that has called getCallerClass()
        • 1: the method where is defined the function (f<sub>1</sub>) that has called f<sub>0</sub>
        • 2: the method where is defined the function (f<sub>2</sub>) that has called f<sub>1</sub>
        • etc.

        The returned value is the line number of the calling method.

        Parameters:
        level - is the desired level of the class
        Returns:
        the line number of method from the call stack according to the given level.
      • getCallerFilename

        @Pure
        String getCallerFilename​(int level)
        Replies the filename of the method of the caller 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 (f<sub>0</sub>) that has called getCallerClass()
        • 1: the method where is defined the function (f<sub>1</sub>) that has called f<sub>0</sub>
        • 2: the method where is defined the function (f<sub>2</sub>) that has called f<sub>1</sub>
        • etc.
        Parameters:
        level - is the desired level of the class
        Returns:
        the filename of the method from the call stack according to the given level.