Skip to content

Instantly share code, notes, and snippets.

@jackrabb1t
Created November 28, 2011 17:59
Show Gist options
  • Save jackrabb1t/1401312 to your computer and use it in GitHub Desktop.
Save jackrabb1t/1401312 to your computer and use it in GitHub Desktop.
Get the method name from the stack trace
/**
* Get the method name for a depth in call stack. <br />
* Utility function
* @param depth depth in the call stack (0 means current method, 1 means call method, ...)
* @return method name
* http://stackoverflow.com/questions/421280/in-java-how-do-i-find-the-caller-of-a-method-using-stacktrace-or-reflection
*/
public static String getMethodName(final int depth)
{
final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
if ( ste.length - depth >= 1 ) {
return ste[ste.length - 1 - depth].getMethodName();
} else {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment