Tuesday, January 20, 2009

Know the name of currently executing method in Java

This method is used to get the name of method currently executing.

protected String getCurrentlyExecutingMethodName() {
  Throwable t = new Throwable(); 
  StackTraceElement[] elements = t.getStackTrace(); 
  if (elements.length <= 0) return "[No Stack Information Available]";
  // elements[0] is this method
  if (elements.length < 2) return null;
  return elements[1].getMethodName();
}

No comments:

Post a Comment