Class StackTraceCaller

  • All Implemented Interfaces:
    Caller

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

    It inspirated from the Andriod API.

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

      • StackTraceCaller

        public StackTraceCaller()
    • Method Detail

      • getTraceElementAt

        protected static StackTraceElement getTraceElementAt​(int level)
        Replies the stack trace element for the given 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 one function of Caller
        • 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.
        Returns:
        the stack trace element; or null.
      • getCallerClass

        public Class<?> getCallerClass​(int level)
        Description copied from interface: Caller
        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.
        Specified by:
        getCallerClass in interface Caller
        Parameters:
        level - is the desired level of the class
        Returns:
        the class from the call stack according to the given level.
      • getCallerMethod

        public String getCallerMethod​(int level)
        Description copied from interface: Caller
        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)
             }
         }
         
        Specified by:
        getCallerMethod in interface Caller
        Parameters:
        level - is the desired level of the class
        Returns:
        the method from the call stack according to the given level.
      • getCallerLine

        public long getCallerLine​(int level)
        Description copied from interface: Caller
        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.

        Specified by:
        getCallerLine in interface Caller
        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

        public String getCallerFilename​(int level)
        Description copied from interface: Caller
        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.
        Specified by:
        getCallerFilename in interface Caller
        Parameters:
        level - is the desired level of the class
        Returns:
        the filename of the method from the call stack according to the given level.