org.arakhne.vmutil
Class Caller

java.lang.Object
  extended by org.arakhne.vmutil.Caller

public 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:
6.3 (rev:285) 2011-10-14 12:02:41
Author:
Stéphane GALLAND
Maven Group Id:
org.arakhne.afc
Maven Artifact Id:
arakhneVmutils

Constructor Summary
Caller()
           
 
Method Summary
static Class<?> getCallerClass()
          Replies the class of the caller that invoked the function from which GetCallerClass() 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 which GetCallerClass() was invoked.
static String getCallerMethod(int level)
          Replies the method from the stack according to its level.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Caller

public Caller()
Method Detail

getCallerMethod

public static String getCallerMethod()
Replies the method of the caller that invoked the function from which GetCallerClass() 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 GetCallerClass() was invoked.

getCallerClass

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

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:

Parameters:
level - is the desired level of the class
Returns:
the class from the call stack according to the given level.

getCallerMethod

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:

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.


Copyright © 2006-2011 Arakhnê.org Consortium. All rights reserved under LGPL license terms.