Skip to content

Instantly share code, notes, and snippets.

@JefStat
Created August 1, 2013 16:52
Show Gist options
  • Save JefStat/6133172 to your computer and use it in GitHub Desktop.
Save JefStat/6133172 to your computer and use it in GitHub Desktop.
getCurrentMethodName (wasn't quite working might need it to be static)
private static int getStackTraceIndexForObject(final Object object)
{
// Finds out the index of "this code" in the returned stack trace - funny but it differs in JDK 1.5 and 1.6
int i = 0;
for (final StackTraceElement ste : Thread.currentThread().getStackTrace()) {
++i;
if (ste.getClassName().equals(object.getClass().getName())) {
break;
}
}
return i;
}
public static String getCurrentMethodName(final Object object) {
return Thread.currentThread().getStackTrace()[getStackTraceIndexForObject(object)].getMethodName();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment