Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 715
Michael Borgwardt Avatar asked Jan 15 '10 23:01

Michael Borgwardt


1 Answers

Try:

this.@"$name" = value

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

like image 61
John Stoneham Avatar answered Oct 21 '22 07:10

John Stoneham