Groovy, get enclosing function's name?

后端 未结 4 2004
你的背包
你的背包 2021-01-11 15:37

I\'m using Groovy 1.8.4, trying to get the name of the enclosing function...

def myFunction() {
  println functionName??
}

I\'ve tried

4条回答
  •  灰色年华
    2021-01-11 16:25

    import org.codehaus.groovy.runtime.StackTraceUtils
    
    def getCurrentMethodName(){
      def marker = new Throwable()
      return StackTraceUtils.sanitize(marker).stackTrace[1].methodName
    }
    
    def helloFun(){
       println( getCurrentMethodName() )
    }
    
    helloFun()
    

    output:

    helloFun
    

提交回复
热议问题