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
I've got solution using this (In Android)
Thread
.currentThread()
.getStackTrace()
will usually contain the method you’re calling it from but there are pitfalls (see Javadoc):name will have value foo.
I don't know what is the intention behind getting the currently executed method's name, but if that's just for debugging purpose, then logging frameworks like "logback" can help here. For example, in logback, all you need to do is to use the pattern "%M" in your logging configuration. However, this should be used with caution as this may degrade performance.
This is an expansion on virgo47's answer (above).
It provides some static methods to get the current and invoking class / method names.