请参阅下面的代码。在使用 metaClass 将方法添加到类之前创建的类的旧实例不应该理解该方法吗?'PROBLEMATIC LINE' 注释下方的断言语句在我认为不应该执行时执行,因为旧的 parentDir 实例不应该理解 blech() 消息。
// derived from http://ssscripting.wordpress.com/2009/10/20/adding-methods-to-singular-objects-in-groovy/
// Adding a method to a single instance of a class
def thisDir = new File('.')
def parentDir = new File('..')
thisDir.metaClass.bla = { -> "bla: ${File.separator}" }
assert thisDir.bla() == "bla: ${File.separator}" : 'thisDir should understand how to respond to bla() message'
try {
parentDir.bla()
assert false : 'parentDir should NOT understand bla() message'
} catch (MissingMethodException mmex) {
// do nothing : this is expected
}
// Adding a method to all instances of a class
File.metaClass.blech = { -> "blech: ${File.separator}" }
try {
thisDir.blech()
assert false : 'old instance thisDir should NOT understand blech() message'
} catch (MissingMethodException mmex) {
// do nothing : this is expected
}
try {
parentDir.blech()
// PROBLEMATIC LINE BELOW - THE LINE IS EXECUTED WHEN
// I THINK AN EXCEPTION SHOULD HAVE BEEN THROWN
assert false : 'old instance parentDir should NOT understand blech() message'
} catch (MissingMethodException mmex) {
// do nothing : this is expected
}
thisDir = new File('.')
parentDir = new File('..')
try {
thisDir.bla()
assert false : 'new instance thisDir should NOT understand bla() message'
} catch (MissingMethodException mmex) {
// do nothing : this is expected
}
assert "blech: ${File.separator}" == thisDir.blech() : 'new instance thisDir should understand blech() message'
assert "blech: ${File.separator}" == parentDir.blech() : 'new instance parentDir should understand blech() message'