How to get the name of the current method in Java

  Uncategorized

There is a simple solution, to get the name of the current method in Java. It uses the stack trace to get the name of the mehod. It works with different virtual machines like oracle, dalvik (Android), Open JDK, etc.

String name = Thread.currentThread().getStackTrace()[1].getMethodName();

But don’t use this in time-critical code, because working with the StackTrace is not recommended for high performcnce activities.

Source: http://stackoverflow.com/