Skip to content

Instantly share code, notes, and snippets.

@siosio
Created July 11, 2012 09:28
Show Gist options
  • Save siosio/3089274 to your computer and use it in GitHub Desktop.
Save siosio/3089274 to your computer and use it in GitHub Desktop.
クロージャの名前
class Hogehoge {
def cls = {}
static hogefuga = {}
}
cls = {}
def cls2 = {}
def method(Closure c) {
println getDeclaredName(c)
}
def getDeclaredName(Closure c) {
def clazz = c.class
def field = clazz.declaredFields.find {it.type == Reference.class}
if (field) {
return field.name
} else {
def method = clazz.declaredMethods.find {it.name =~ /getCallSiteArray/}
method.accessible = true
return method.invoke(null)[0].name
}
}
method {cls}
method {cls2}
method {new Hogehoge().cls}
method {Hogehoge.hogefuga}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment