让我们考虑一个简单的Groovy DSL使用 '所有者' 在Groovy DSL财产
execute {
sendNotification owner
sendNotification payee
}
执行的实施是
public static void execute(Closure dslCode) {
Closure clonedCode = dslCode.clone()
def dslDelegate = new MyDslDelegate(owner: 'IncCorp', payee: 'TheBoss')
clonedCode.delegate = dslDelegate
clonedCode.call()
}
和定制委托
public static class MyDslDelegate {
def owner
def payee
void sendNotification(to) {
println "Notification sent to $to"
}
}
预期的结果运行execute
区块是
Notification sent to IncCorp
Notification sent to TheBoss
实际一个是
Notification sent to class package.OwnerClassName
Notification sent to TheBoss
的问题是owner
是在Groovy中Closure
本身保留财产,没有resolveStrategy
选项有助于更换owner
值和自定义值从代表由于Groovy的getProperty
实施Closure
public Object getProperty(final String property) {
if ("delegate".equals(property)) {
return getDelegate();
} else if ("owner".equals(property)) {
return getOwner();
...
} else {
switch(resolveStrategy) {
case DELEGATE_FIRST:
...
}
我的问题是如何有人可以得出这个限制和使用自定义DSL中的10个属性名称?