Groovy, get enclosing function's name?

后端 未结 4 1994
你的背包
你的背包 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:32

    @CompileStatic
    class LogUtils {
        // can be called the Groovy or Java way
        public static String getCurrentMethodName(){
            StackTraceElement[] stackTrace = StackTraceUtils.sanitize(new Throwable()).stackTrace
            stackTrace[2].methodName != 'jlrMethodInvoke' ? stackTrace[2].methodName : stackTrace[3].methodName
        }
    }
    

提交回复
热议问题