views:

123

answers:

1

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  A: 

Try:

this.@"$name" = value

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

John Stoneham
this.@name would access a specific property, but I need generic access based on property name. I've changed your answer to what I now got to work. Strangely, I had already found and tried the .@ operator. I suspect that Netbeans was still running the previous code (it really sucks that way).
Michael Borgwardt
Excellent. Glad it worked for you.
John Stoneham