How can I get a constructor that assigns a copy of constructor arguments to the fields, to make the instance truly immutable? The below class has @Value annotation but this isn't immutable as its the properties are not: (The question is not specific to immutable Collections, I just used it to demo a mutable DS. I am looking for any mutable bean in place of List here, which, say is coming from a library and is not in my control)
@Value
class ImmutableBean {
List<String> props;
}
@Test
void checkImmutability() {
var props = new ArrayList<String>();
props.add("abc");
var immutableBean = new ImmutableBean(props);
System.out.println(immutableBean.getProps()); // abc
props.add("pqr");
System.out.println(immutableBean.getProps()); // abc pqr
}