0

Is it possible to get the name of the current method in Groovy code?

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

Thanks

Breako Breako
  • 6,353
  • 14
  • 40
  • 59

1 Answers1

0

Tim_yates gives you a link to general solution.

This is shorter way to get method name. May be optimized, leawed as is for the sake of clarity.

class A {
    def myMethod() {
        for (m in new Throwable().stackTrace) {
            if (m.className == this.class.name)
                return m.methodName
        }
    }
}
println new A().myMethod()
Seagull
  • 13,484
  • 2
  • 33
  • 45