Does anybody know of any existing support for modifying individual properties of immutable objects stored under a JavaBean compliant object?
For a trivial example:
For the given immutable value class and bean object (not worried about listeners for this):
public class ValueObject {
private final int value;
public ValueObject(int value) {
this.value = value;
}
public ValueObject withValue(int newValue) {
return new ValueObject(value);
}
}
public class Bean {
private ValueObject value;
public ValueObject getValue() {
return value;
}
public ValueObject setValue(ValueObject value) {
this.value = value;
}
}
It's already possible to view the property as bean.value.value
.
I'm looking to see if there's an existing way to say bean.value.value = 3
and basically have a call equivalent to bean.setValue(bean.getValue().withValue(3));
.
Note that the actual value object is significantly more complicated.
Thanks!
I would try creating a BeanInfo class for the bean class that specifies the property mutator method using setWriteMethod. This write method can take the primitive int value and create the immutable value object from it and assign that to the property field.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With