Get current method name in Java

Description

The following code shows how to get current method name.

Example


/*from   w  w  w .  ja  v a  2  s . c om*/
public class Main{

  public static void main(java.lang.String[] args) {
    System.out.println("in " + new CurrentClassGetter().getClassName() + " class");
  }
}

class CurrentClassGetter extends SecurityManager {
  public String getClassName() {
    return getClassContext()[1].getName();
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Reflection »




Annotation
Array
Class
Constructor
Field
Generics
Interface
Method
Modifier
Package
Proxy