Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Existing Javabean support for changing immutable subproperties

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!

like image 765
deterb Avatar asked Nov 14 '22 03:11

deterb


1 Answers

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.

like image 153
Will Avatar answered Dec 05 '22 08:12

Will