- java.lang.Object
-
- org.arakhne.afc.vmutil.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
-
-
Field Summary
Fields Modifier and Type Field Description static String
MODULE_NAME
Name of thevmutils
module that is considered byfindClassForFirstCallerOutsideVmutilModule()
.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Class<?>
findClassForFirstCallerOutsideVmutilModule()
Replies the class of the first caller that invoked the function from whichgetCallerClass()
was invoked and is outside the vmutils module.static Caller
getCaller()
Replies the stack trace mamager used by this utility class.static Class<?>
getCallerClass()
Replies the class of the caller that invoked the function from whichgetCallerClass()
was invoked.static Class<?>
getCallerClass(int level)
Replies the class from the stack according to its level.static String
getCallerMethod()
Replies the method of the caller that invoked the function from whichgetCallerMethod()
was invoked.static String
getCallerMethod(int level)
Replies the method from the stack according to its level.
-
-
-
Field Detail
-
MODULE_NAME
public static final String MODULE_NAME
Name of thevmutils
module that is considered byfindClassForFirstCallerOutsideVmutilModule()
.- Since:
- 17.0
- See Also:
- Constant Field Values
-
-
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 whichgetCallerMethod()
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 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) } }
- 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 calledgetCallerClass()
1
: the method where is defined the function (f1
) that has calledf0
2
: the method where is defined the function (f2
) that has calledf1
- 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) } }
- 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 whichgetCallerClass()
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 calledgetCallerClass()
1
: the class where is defined the function (f1
) that has calledf0
2
: the class where is defined the function (f2
) that has calledf1
- 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 whichgetCallerClass()
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. Thenull
value may be replied if the caller class cannot be retrieved. - Since:
- 17.0
-
-