How do I find the caller of a method using stacktrace or reflection?

后端 未结 12 1164
鱼传尺愫
鱼传尺愫 2020-11-21 13:26

I need to find the caller of a method. Is it possible using stacktrace or reflection?

12条回答
  •  -上瘾入骨i
    2020-11-21 13:45

    Oneliner:

    Thread.currentThread().getStackTrace()[2].getMethodName()
    

    Note that you might need to replace the 2 with 1.

提交回复
热议问题