2016-06-14 5 views
0

ここでコードをGroovy DSL Docに変更すると@DelegatesToアノテーションを使用する場合、どのようにクロージャにパラメータを渡すのですか?

この

email('hello world') { // change here 
    from '[email protected]' 
    to '[email protected]' 
    subject 'The pope has resigned!' 
    body { 
     p 'Really, the pope has resigned!' 
    } 
} 

のように、電子メールにいくつかの文字列 'Hello Worldの' を追加し、そう

def email(def name, @DelegatesTo(EmailSpec) Closure cl) { // change here 
    def email = new EmailSpec() 
    def code = cl.rehydrate(email, this, this) 
    code.resolveStrategy = Closure.DELEGATE_ONLY 
    code.call(name) // change here 
} 

を変更、どのクラスEmailSpec 'Hello World' 文字列を取得するために修正します?

答えて

0

はい、私は方法を見つけましたが、完璧ではありません。

シンプル

new EmailSpec(name) // change to 

しかし、私は本当に閉鎖はあなたが追加する必要があるパラメータで呼び出される、コンパイルを言うと、それを

0

を解決するためにグルーヴィー関数呼び出し(名)を使用したいですClosureParams注釈。あなたの例に固執する

def email(def name, 
     @ClosureParams(value = SimpleType, options = "java.lang.String") 
     @DelegatesTo(EmailSpec) Closure cl) { 
    def email = new EmailSpec() 
    def code = cl.rehydrate(email, this, this) 
    code.resolveStrategy = Closure.DELEGATE_ONLY 
    code.call(name) // change here 
} 

は、最初のパラメータがStringあるコンパイラを教えてくれます。

詳しくは、groovyのドキュメントのThe @ClosureParams annotationセクションを参照してください。

関連する問題