Veja a seguir algumas formas de obter o nome do método corrente em Java.
public class MyClass { public void myMethod() { class Local {}; String methodName = Local.class.getEnclosingMethod().getName(); System.out.println(methodName); } }
public class MyClass { public void myMethod() { String methodName = Thread.currentThread().getStackTrace()[1].getMethodName(); System.out.println(methodName); } }
public class MyClass { public static String getMethodName() { return Thread.currentThread().getStackTrace()[2].getMethodName(); } public void myMethod() { System.out.println(getMethodName()); } }
O conteúdo desta página pode ajudar alguém? Compartilhe!