我在使用propertyMissing()和GroovyShell时遇到了问题
我有文件
/**
* @file FooScript.groovy
*/
abstract class FooScript extends Script {
def propertyMissing(String name) {
"This is the property '$name'"
}
def propertyMissing(String name, value) {
println "You tried to set property '$name' to '$value'"
}
}和
/**
* @file FooScriptTest.groovy
*/
import org.codehaus.groovy.control.*
def fooScript = """\
foo = 'bar'
println foo"""
def conf = new CompilerConfiguration()
conf.setScriptBaseClass("FooScript")
def sh = new GroovyShell(conf)
sh.evaluate fooScript当我运行FooScriptTest.groovy时,我期望输出
您试图将属性“foo”设置为“bar”
这是财产'foo‘
我得到的是:
棒
我的propertyMissing()似乎被默认的一个覆盖了。我怎么才能防止这件事?
发布于 2011-05-11 22:58:55
用这个代替
abstract class BarScript extends Script {
def getProperty(String name) {
"This is the property '$name'"
}
void setProperty(String name, value) {
println "You tried to set property '$name' to '$value'"
}
}missingProperty方法是捕捉属性访问的最后一个资源,只有在其他所有操作都失败时才进行测试。
但是groovy.lang.Script已经实现了更高优先级的方法get/setProperty。
因此,要捕获缺少的属性,必须在子类中重写以下方法
https://stackoverflow.com/questions/5937942
复制相似问题