- java.lang.Object
-
- org.arakhne.afc.vmutil.caller.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 Summary
Constructors Constructor Description StackTraceCaller()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Class<?>
getCallerClass(int level)
Replies the class from the stack according to its level.String
getCallerFilename(int level)
Replies the filename of the method of the caller from the stack according to its level.long
getCallerLine(int level)
Replies the line number of the caller from the stack according to its level.String
getCallerMethod(int level)
Replies the method from the stack according to its level.protected static StackTraceElement
getTraceElementAt(int level)
Replies the stack trace element for the given level.
-
-
-
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 ofCaller
1
: the class where is defined the function (f<sub>1</sub>
) that has calledf<sub>0</sub>
2
: the class where is defined the function (f<sub>2</sub>
) that has calledf<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 calledgetCallerClass()
1
: the class where is defined the function (f<sub>1</sub>
) that has calledf<sub>0</sub>
2
: the class where is defined the function (f<sub>2</sub>
) that has calledf<sub>1</sub>
- etc.
- Specified by:
getCallerClass
in interfaceCaller
- 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 calledgetCallerClass()
1
: the method where is defined the function (f<sub>1</sub>
) that has calledf<sub>0</sub>
2
: the method where is defined the function (f<sub>2</sub>
) that has calledf<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 prototypevoid 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 interfaceCaller
- 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 calledgetCallerClass()
1
: the method where is defined the function (f<sub>1</sub>
) that has calledf<sub>0</sub>
2
: the method where is defined the function (f<sub>2</sub>
) that has calledf<sub>1</sub>
- etc.
The returned value is the line number of the calling method.
- Specified by:
getCallerLine
in interfaceCaller
- 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 calledgetCallerClass()
1
: the method where is defined the function (f<sub>1</sub>
) that has calledf<sub>0</sub>
2
: the method where is defined the function (f<sub>2</sub>
) that has calledf<sub>1</sub>
- etc.
- Specified by:
getCallerFilename
in interfaceCaller
- Parameters:
level
- is the desired level of the class- Returns:
- the filename of the method from the call stack according to the given level.
-
-