簡體   English   中英

如何訪問 groovy 閉包注釋?

[英]how to access groovy closure annotations?

假設在我的 groovy 代碼中,我已經聲明了一個帶有一些注釋的閉包,如下所示:

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

所以現在在我的代碼中,我想從閉包中提取GET注釋,以便我可以創建一些自動映射:

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

我怎么才能得到它?

所以完整的代碼是:

@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)

你不能。 注釋與字段相關聯,而不是字段的值。 閉包是該字段的值。 當您執行諸如closure.class.getAnnotations() 之類的操作時,您需要的是 groovy.lang.Closure 類上的注釋,而不是那里的“closure”變量所指的對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM