Groovy:从类内部方法动态添加Groovy类的属性 [英] Groovy: dynamically add properties to groovy classes from inside class methods
问题描述
class MyClass {
def myMethod(){
variable =我是一个变量
}
def propertyMissing(String name){
println缺少属性$ name
}
}
MyClass myClass = new MyClass();
myClass.myProperty
myClass.myMethod();
在myClass.myProperty中,缺少属性myProperty
然后在 myClass.myMethod()
中,groovy不会尝试去到 propertyMissing
,而只是抛出一个
groovy.lang.MissingPropertyException:没有这样的属性:类的变量:MyClass
一些在线搜索表明它是因为 myClass.myProperty
调用getter方法,该方法重定向到propertyMissing。
我猜测在类方法中,groovy不会去通过getter方法的变量,这就是为什么propertyMissing没有被调用?
有没有办法实现我想要做的动态 propertyMissing 或
getProperty
或类似的东西?
PS我不想在myMethod中执行 def variable = ...
或字符串变量= ...
。我希望 myMethod
中的语法保持为 variable = ...
,但在该方法之外添加任何内容是可以接受的。
你可以让你的类扩展Expando
a href =http://groovy.codehaus.org/api/groovy/util/Expando.html =noreferrer> Expando在这里描述)
class MyClass extends Expando {
def myMethod(){
variable =我是一个变量
}
def propertyMissing(String name){
printlnMissing property $ name
}
}
MyClass myClass = new MyClass()
myClass .myProperty
myClass.myMethod()
println myClass.variable
您可以通过为变量创建自己的备份映射并自己编写 get / setProperty
方法来手动执行类似的功能,即:
class MyClass {
de f myMethod(){
variable =我是一个变量
}
def propertyMissing(String name){
printlnMissing property $ name
$ b $ def backingMap = [:]
Object getProperty(String property){
if(backingMap [property] == null){
propertyMissing(property)
}
else {
backingMap [property]
}
}
void setProperty(String property,Object value ){
backingMap [property] = value
}
}
MyClass myClass = new MyClass()
myClass.myProperty
myClass。 myMethod()
println myClass.variable
虽然您可以从 Expando的源代码,this手卷版本的检查少了很多,我相信它少了; - )
I have the following code:
class MyClass {
def myMethod() {
variable = "I am a variable"
}
def propertyMissing(String name) {
println "Missing property $name"
}
}
MyClass myClass = new MyClass();
myClass.myProperty
myClass.myMethod();
At myClass.myProperty, Missing property myProperty
was printed out to the console.
But then in myClass.myMethod()
, groovy makes no attempt to go to propertyMissing
but instead just throws a
groovy.lang.MissingPropertyException: No such property: variable for class: MyClass
Some search online indicates that it is because myClass.myProperty
calls a getter method, which redirects to propertyMissing.
I am guessing that within class methods, groovy doesn't go through getter methods for variables and that's why propertyMissing is not getting called?
Is there a way to achieve what I want to do using the dynamic propertyMissing
, or getProperty
, or anything like that?
P.S. I don't want to do def variable = ...
or String variable = ...
in myMethod. I am hoping that the syntax within myMethod
will stay as variable = ...
, but adding anything outside of that method is acceptable.
You can make your class extend Expando
(Expando is described here)
class MyClass extends Expando {
def myMethod() {
variable = "I am a variable"
}
def propertyMissing(String name) {
println "Missing property $name"
}
}
MyClass myClass = new MyClass()
myClass.myProperty
myClass.myMethod()
println myClass.variable
You can hand-roll a similar functionality by creating your own backing map for variables, and writing the get/setProperty
methods yourself, ie:
class MyClass {
def myMethod() {
variable = "I am a variable"
}
def propertyMissing(String name) {
println "Missing property $name"
}
def backingMap = [:]
Object getProperty( String property ) {
if( backingMap[ property ] == null ) {
propertyMissing( property )
}
else {
backingMap[ property ]
}
}
void setProperty( String property, Object value ) {
backingMap[ property ] = value
}
}
MyClass myClass = new MyClass()
myClass.myProperty
myClass.myMethod()
println myClass.variable
Though as you can see from the source code for Expando, this hand-rolled version does a lot less checks and I'd trust it less ;-)
这篇关于Groovy:从类内部方法动态添加Groovy类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!