Java – how do I get the method name from this method?

I tried to create a function that returns the method name from this method:

public static String getmethodName(final int depth)
  {
    final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
    return ste[ste.length - 1 - depth].getmethodName();
  }

But when I get from activity When oncreate() calls this method, it returns "main" instead of "oncreate"

How do I get the actual method name from this method?

Solution

return ste[1+depth].getmethodName();
return ste[1+depth].getmethodName();

If you change the return statement as described above, you will get the immediate call method name, and the course depth should be zero

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
分享
二维码
< <上一篇
下一篇>>