目前我正在使用以下代码来检查是否可以设置 bean 属性
class Foo {
def baz = 'baz'
String getBar() {
'bar'
}
}
def f = new Foo()
assert f.hasProperty('baz').setter
assert !f.hasProperty('bar').setter
getSetter()
我调用的方法定义于 MetaBeanProperty
,这是 MetaProperty
的运行时类型hasProperty
返回的实例。我确信一定有更好的方法来检查 bean 属性是否可写,有什么建议吗?
最佳答案
我建议使用respondsTo("setX")
:
class Foo {
def baz = 'baz'
String getBar() {
'bar'
}
}
foo = new Foo()
assert !foo.respondsTo('setBar')
assert foo.respondsTo('setBaz')
关于groovy - 如何测试 bean 属性是否可写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34396189/