In C++, you can use __FUNCTION_NAME__
to get the name of the function that contains __FUNCTION_NAME__
.
Is there an equivalent in Java? It could, in Java, be possible to do something with this
and reflection. Is there something simpler though?
In C++, you can use __FUNCTION_NAME__
to get the name of the function that contains __FUNCTION_NAME__
.
Is there an equivalent in Java? It could, in Java, be possible to do something with this
and reflection. Is there something simpler though?
Thread.currentThread().getStackTrace() will usually contain the method you’re calling it from but there are pitfalls
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StackTraceElement.html
Thread.currentThread().getStackTrace()[ste.length - 1 - depth].getMethodName();
depth = 0 (zero) will give current method
also
System.out.println((new Throwable()).getStackTrace()[0].toString());
Sample output:
com.junk.Junk3.main(Junk3.java:12)