2012-10-06 114 views
2

我可以以某种方式在参数化构造函数中混入类吗?有没有一种方法可以在Groovy中对mixin进行参数化?

事情是这样的:

class RenderedWithTemplates { 
    def templates = [] 

    RenderedWithTemplates(templates) { ... } 

    ... 

} 

@Mixin(RenderedWithTemplates(show: "showAddress.gsp", add: "addAddress.gsp") 
class Address { ... } 
+1

你尝试[验证码](http://docs.codehaus.org/display/GroovyJSR/Mixins#Mixins-ParamaterizedMixing)? –

回答

0

我发现混入建议从2007年[0],但GroovyDefaultMethods#混入[1]的方法对参数混入不支持,并且都没有@Mixin。

据我可以从代码示例告诉上面,你需要找到一种方法来混入被绑定到你的(域)班GSP视图的信息。的替代方案(和略微更巧妙的)对于这种情况下)的方法将是实现RenderedWithTemplates与单个闭合参数保持GSP视图信息注释:

import java.lang.annotation.* 

@Retention(RetentionPolicy.RUNTIME) 
@interface RenderedWithTemplates { 
    Class value() 
} 

@RenderedWithTemplates({ [show: "showAddress.gsp", add: "addAddress.gsp"] }) 
class Address {} 

// shows how to read the map with all the GSP infos 

def templateInfo = Address.getAnnotation(RenderedWithTemplates) 
def gspMap = templateInfo.value().newInstance(this, this).call() 

[0] http://docs.codehaus.org/display/GroovyJSR/Mixins

[1] http://groovy.codehaus.org/api/org/codehaus/groovy/runtime/DefaultGroovyMethods.html

相关问题