Is there a way to get the name of the currently executing method in Java?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
January 2009:
A full code would be (to use with @Bombe's caveat in mind):
More in this question.
Update December 2011:
bluish comments:
virgo47's answer (upvoted) actually computes the right index to apply in order to get back the method name.
This can be done using
StackWalker
since Java 9.StackWalker
is designed to be lazy, so it's likely to be more efficient than, say,Thread.getStackTrace
which eagerly creates an array for the entire callstack. Also see the JEP for more information.The fastest way I found is that:
It accesses the native method getStackTraceElement(int depth) directly. And stores the accessible Method in a static variable.
Use the following Code :
Util.java:
SomeClass.java: