Groovy, get enclosing function's name?

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

    You can get to it via the stacktrace, I've been able to get it via:

    groovy:000> def foo() { println Thread.currentThread().stackTrace[10].methodName }
    ===> true
    groovy:000> foo()
    foo
    groovy:000> class Foo {                                                             
    groovy:001>   def bar() { println Thread.currentThread().stackTrace[10].methodName }
    groovy:002> }
    ===> true
    groovy:000> new Foo().bar()
    bar
    

提交回复
热议问题