Groovy 版本:2.4.3 JVM:1.8.0_60 供应商:Oracle Corporation 操作系统:Mac OS X
import groovy.transform.*
@Canonical
class A {
def f() {
map = [:] //map is not null
3.times {
assert map != null // failed, map is null?!
}
}
@Delegate
Map map
}
new A().f()
当我调用 f() 时,我得到断言失败,这意味着映射为空。但是如果我删除注释“@Delegate”,那么就不会有任何问题。或者如果断言不在闭包中,也没有问题。我的问题是为什么委托(delegate)字段在闭包内或闭包外表现不同?如果是因为闭包中的map与A类中的对象不同,为什么在删除注释后它还能工作?
import groovy.transform.*
@Canonical
class A {
def f() {
map = [:]
3.times {
assert map != null // No problem, map is not null
}
}
Map map
}
new A().f()
或者
import groovy.transform.*
@Canonical
class A {
def f() {
map = [:]
assert map != null //no problem too
}
@Delegate
Map map
}
new A().f()
最佳答案
当您拥有@Delegate
注释时,您的类本质上成为java.util.Map
的实现,它将其调用委托(delegate)给它的字段map
.断言闭包中对 map
的引用被视为 this.getAt('map')
调用,而不是对 map
字段的引用。由于没有值映射到键 map
,因此断言失败。
也许这会让事情变得更清楚:
import groovy.transform.*
@Canonical
class A {
def f() {
map = [map:'not null'] // delegated map has key 'map'
3.times {
assert map == 'not null'
}
}
@Delegate
Map map
}
new A().f()
奇怪之处主要与断言是在闭包内完成的这一事实有关,该闭包具有 resolve strategy它规定了如何解析引用,其中包括元类的使用。删除闭包,@Delegate
注释不再产生影响。
import groovy.transform.*
@Canonical
class A {
def f() {
assert map != null
}
@Delegate
Map map = [:]
}
new A().f()
关于groovy - Groovy 中启动的委托(delegate)字段在闭包中变为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33035166/