这种行为是由于groovy.lang.Script http://docs.groovy-lang.org/latest/html/api/groovy/lang/Script.html类以及它重写以下方法的事实:
- Object getProperty(String property) http://docs.groovy-lang.org/latest/html/api/groovy/lang/Script.html#getProperty-java.lang.String-
- void setProperty(String property, Object newValue) https://github.com/apache/groovy/blob/master/src/main/groovy/lang/Script.java
您在示例中显示的闭包使用delegate
设置为脚本对象,这就是当您尝试访问或修改脚本中定义的字段时执行两个重写方法的原因。
现在让我们看看当您的示例结束时会发生什么
{ myField++ }
首先,getProperty("myField")
被调用以返回与该属性关联的值。该方法的实现如下:
public Object getProperty(String property) {
try {
return binding.getVariable(property);
} catch (MissingPropertyException e) {
return super.getProperty(property);
}
}
Source: https://github.com/apache/groovy/blob/GROOVY_2_4_X/src/main/groovy/lang/Script.java#L54 https://github.com/apache/groovy/blob/GROOVY_2_4_X/src/main/groovy/lang/Script.java#L54
binding
对象一开始只包含一个变量 - 闭包的args
大批。如果我们看一下实施binding.getVariable(property)
方法我们会看到:
public Object getVariable(String name) {
if (variables == null)
throw new MissingPropertyException(name, this.getClass());
Object result = variables.get(name);
if (result == null && !variables.containsKey(name)) {
throw new MissingPropertyException(name, this.getClass());
}
return result;
}
Source: https://github.com/apache/groovy/blob/GROOVY_2_4_X/src/main/groovy/lang/Binding.java#L56 https://github.com/apache/groovy/blob/GROOVY_2_4_X/src/main/groovy/lang/Binding.java#L56
在我们的例子中MissingPropertyException
正在被抛出,所以Script.getProperty(property)
方法返回字段的值myField
在我们的 Groovy 脚本中定义 -0
。然后 Groovy 将此值增加 1 并尝试将此新值设置为字段myField
。在这种情况下Script.setProperty(property, value)
正在被调用:
public void setProperty(String property, Object newValue) {
if ("binding".equals(property))
setBinding((Binding) newValue);
else if("metaClass".equals(property))
setMetaClass((MetaClass)newValue);
else
binding.setVariable(property, newValue);
}
Source: https://github.com/apache/groovy/blob/GROOVY_2_4_X/src/main/groovy/lang/Script.java#L62 https://github.com/apache/groovy/blob/GROOVY_2_4_X/src/main/groovy/lang/Script.java#L62
正如你所看到的,它使用以下命令设置了这个新值bindings
目的。如果我们显示binding.variables
我们将看到现在这个内部映射包含两个条目:args -> [:]
and myField -> 1
。它解释了为什么脚本中的断言总是失败。您定义的闭包主体永远不会到达myField
脚本类中的字段。
解决方法
如果您对以下事实不满意Script
类覆盖setProperty(property, value)
方法,您始终可以在脚本中手动覆盖它并使用与GroovyObjectSupport.setProperty(property, value)
。只需将以下方法添加到您的 Groovy 脚本中:
@Override
void setProperty(String property, Object newValue) {
getMetaClass().setProperty(this, property, newValue)
}
现在闭包定义在incrementField
将为类字段设置一个新值,而不是bindings
目的。当然,它可能会导致一些奇怪的副作用,你必须意识到这一点。我希望它有帮助。