Java – a shorter method for dynamically obtaining the name of the current method
•
Java
See English answer > getting the name of the current executing method18
This is what we are doing now:
new Object() {}.getClass().getEnclosingMethod().getName(
Solution
Thread. currentThread(). getStackTrace()[1]?
Because of this, you can examine higher-level stack traces, which you can easily wrap in a helper (see below) It also allows you to select more information than the method name, such as file name, line number, etc
The edit assistant method might look like this (thanks @ esailija):
public static String getmethodName() { return Thread.currentThread().getStackTrace()[2].getmethodName(); }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码