[英]groovy.lang.MissingPropertyException: No such property: owner for class: Mother
I'm working on the following code: 我正在处理以下代码:
caught exception at 'this.owner' which in the Closure of birth method. 在“关闭出生方法”中的“ this.owner”处捕获到异常。
class Mother {
int field = 1
int foo(){
return 2
}
Closure birth(param){
def local = 3
def closure = {caller -> [this,field,foo(),local,param,caller,this.owner]}
return closure
}
}
Mother m = new Mother();
closure = m.birth(4);
context = closure.call(this);
println context[0].class.name
assert context[1..4] == [1,2,3,4]
assert context[5] instanceof Script
assert context[6] instanceof Mother
fClosure = m.birth(4);
sClosure = m.birth(4);
assert false == fClosure.is(sClosure)
Which, when run finishes with: 当运行完成时:
'this.owner' caught groovy.lang.MissingPropertyException: No such property: owner for class: Mother 'this.owner'捕获到groovy.lang.MissingPropertyException:无此类属性:类的所有者:Mother
Why is that? 这是为什么?
It all works well, please have a look here , especially: 一切正常,请在此处查看 ,特别是:
this
corresponds to the enclosing class where the closure is definedthis
对应于定义闭包的封闭类
Since corresponding class is Mother
and it has no owner
field nor property defined it fails with MPE. 由于相应的类是Mother
,并且没有owner
字段或属性,因此MPE失败。 If you define owner
on Mother
class it will run (and possibly fail with another exception). 如果您在Mother
类上定义owner
,它将运行(并可能因另一个异常而失败)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.