0
是否可以在Groovy代碼中獲取當前方法的名稱?在運行時獲取當前調用方法的名稱
def myMethod() {
// want to get the string myMethod here
}
感謝
是否可以在Groovy代碼中獲取當前方法的名稱?在運行時獲取當前調用方法的名稱
def myMethod() {
// want to get the string myMethod here
}
感謝
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()