Is there a way to bind properties from one instance of a class to the properties of an instance of another class (the common fields between the two). See the example below:
class One {
String foo
String bar
}
class Two {
String foo
String bar
String baz
}
def one = new One(foo:'one-foo', bar:'one-bar')
def two = new Two()
two.properties = one.properties
assert "one-foo" == two.foo
assert "one-bar" == two.bar
assert !two.baz
The result is an error: Cannot set readonly property: properties for class: Two
The problem is that for every object,
.propertiesincludes two built-in Groovy-defined properties, these are themetaClassandclass. What you want to do is only set the user-defined properties. You can easily do this using code such as that shown below: