Groovy,获取封闭函数的名字? [英] Groovy, get enclosing function's name?

查看:148
本文介绍了Groovy,获取封闭函数的名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  def myFunction 

我使用Groovy 1.8.4,尝试获取封闭函数的名称...

(){
println functionName ??
}

我试过委托这个 owner ,Groovy抱怨找不到这样的对象。



我也尝试了Java hack new Exception()。getStackTrace()[0] .getMethodName(),但只是打印 newInstance0

解决方案

  import org.codehaus.groovy.runtime .StackTraceUtils 
$ b $ def getCurrentMethodName(){
def marker = new Throwable()
return StackTraceUtils.sanitize(marker).stackTrace [1] .methodName
}

def helloFun(){
println(getCurrentMethodName())
}

helloFun()

输出:

  helloFun 
code>


I'm using Groovy 1.8.4, trying to get the name of the enclosing function...

def myFunction() {
  println functionName??
}

I've tried delegate, this, owner, Groovy complains no such objects found.

I also tried the Java hack new Exception().getStackTrace()[0].getMethodName(), but that just prints newInstance0

解决方案

import org.codehaus.groovy.runtime.StackTraceUtils

def getCurrentMethodName(){
  def marker = new Throwable()
  return StackTraceUtils.sanitize(marker).stackTrace[1].methodName
}

def helloFun(){
   println( getCurrentMethodName() )
}

helloFun()

output:

helloFun

这篇关于Groovy,获取封闭函数的名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆