【问题标题】:Get the name of the current invoking method at runtime [duplicate]在运行时获取当前调用方法的名称[重复]
【发布时间】:2025-12-28 06:40:11
【问题描述】:

能否在 Groovy 代码中获取当前方法的名称?

def myMethod() {
   // want to get the string myMethod here
}

谢谢

【问题讨论】:

    标签: groovy


    【解决方案1】:

    Tim_yates 为您提供通用解决方案的链接。

    这是获取方法名称的更短的方法。为了清楚起见,可能会进行优化,并按原样进行。

    class A {
        def myMethod() {
            for (m in new Throwable().stackTrace) {
                if (m.className == this.class.name)
                    return m.methodName
            }
        }
    }
    println new A().myMethod()
    

    【讨论】:

      最近更新 更多