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

左心房为你撑大大i 提交于 2019-12-23 09:47:35

问题


I'm trying to implement a domain class that 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?


回答1:


Try:

this.@"$name" = value

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



来源:https://stackoverflow.com/questions/2075348/groovy-how-to-set-a-property-within-setproperty-and-avoid-infinite-recursion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!