简体   繁体   中英

how to access groovy closure annotations?

Say that in my groovy code i've declared an closure with some annotation like this:

@GET('/heartbeat')
def myClosure = { req, res ->
  res.end('OK')
}

so now in my code I would like to extract the GET annotation out of the closure so i could create some automatic mapping:

public void doSomething(Closure closure) {
  closure.class.getAnnotations() // does not contain the GET annotation...
}

How can i get it?

So the full code would be:

@GET('/heartbeat')
def myClosure = { req, res ->
  res.end('OK')
}

public void doSomething(Closure closure) {
  closure.class.getAnnotations() // does not contain the GET annotation...
}

doSomething(myClosure)

You can't. The annotation is associated with the field, not the value of the field. The closure is the value of the field. When you do something like closure.class.getAnnotations() you are asking for the annotations that are on the groovy.lang.Closure class, not the object which the “closure” variable there refers to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM