我试图通过替换该类中用于设置属性但未使用期望值设置属性的方法来更改groovy类的构造函数的行为。
class TestClass {
def noParam
def withParam
TestClass() {
noParam = noParam()
withParam = withParam('second test')
}
def noParam() {
return 'first test'
}
def withParam(param) {
return param
}
}
TestClass.metaClass.withParam = { param -> 'pass' }
TestClass.metaClass.noParam = {-> 'pass' }
def test = new TestClass()
assert test.withParam('dummy') == 'pass' //passes
assert test.withParam == 'pass' // fails
assert test.noParam() == 'pass' // passes
assert test.noParam == 'pass' // fails
答案 0 :(得分:1)
在执行noParam
和withParam
方法时,Groovy未在TestClass构造函数中使用您的元类覆盖。实际上,如果在withParam
方法中键入参数,则第二个断言也将失败。
class TestClass {
def noParam
def withParam
TestClass() {
noParam = noParam()
withParam = withParam('second test')
println "in the constructor: noParam = $noParam, withParam = $withParam"
}
def noParam() {
return 'first test'
}
def withParam(String param) {
return param
}
}
TestClass.metaClass.withParam = { String param -> 'pass' }
TestClass.metaClass.noParam = {-> 'pass' }
def test = new TestClass()
assert test.withParam('dummy') == 'pass'
assert test.withParam == 'pass' // this fails now too!
assert test.noParam() == 'pass'
assert test.noParam == 'pass' // this fails
这是输出:in the constructor: noParam = first test, withParam = second test
test.withParam
和test.noParam
实际上正在调用test.getWithParam()
和test.getNoParam()
- 他们正在返回构造函数中设置的属性值。
test.withParam('dummy')
和test.noParam()
正在调用您的元类方法并返回“pass”。
至于为什么 groovy在构造函数中不使用元类方法,我不确定...我在metaclass documentation中找不到任何内容... < / p>
也许您可以使用静态方法?
class TestClass {
def noParam
def withParam
TestClass() {
noParam = TestClass.noParam()
withParam = TestClass.withParam('second test')
println "in the constructor: noParam = $noParam, withParam = $withParam"
}
static def noParam() {
return 'first test'
}
static def withParam(String param) {
return param
}
}
TestClass.metaClass.'static'.withParam = { String param -> 'pass' }
TestClass.metaClass.'static'.noParam = {-> 'pass' }
def test = new TestClass()
assert test.withParam('dummy') == 'pass'
assert test.withParam == 'pass' // passes!
assert test.noParam() == 'pass'
assert test.noParam == 'pass' // passes!
这是输出:in the constructor: noParam = pass, withParam = pass