开发者

Groovy: How to set a property within setProperty() and avoid infinite recursion?

开发者 https://www.devze.com 2022-12-17 01:18 出处:网络
I\'m trying to implement a domain class tha开发者_如何转开发t records when any property\'s value was changed, but my setProperty() call results in infinite recursion when setting the actual value.

I'm trying to implement a domain class tha开发者_如何转开发t records when any property's value was changed, but my setProperty() call results in infinite recursion when setting the actual value.

This is how it looks right now:

void setProperty(String name, value)
{
    if(name == "modified")
    {
        this.modified = value
        return
    }
    else
    {
        if(this[name]==value)
        {
            return
        }
        this.modified = true
        this[name]=value
    }
}

So how can I access a property given its name without triggering a recursive setProperty() call? Or is there a different way to achieve my goal?


Try:

this.@"$name" = value

(see http://groovy.codehaus.org/Operators#Operators-Javafield%28.@%29)

0

精彩评论

暂无评论...
验证码 换一张
取 消