Created
November 28, 2011 17:59
-
-
Save jackrabb1t/1401312 to your computer and use it in GitHub Desktop.
Get the method name from the stack trace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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