现有 Javabean 支持更改不可变子属性
有谁知道是否存在对修改存储在 JavaBean 兼容对象下的不可变对象的各个属性的支持?
举一个简单的例子:
对于给定的不可变值类和 bean 对象(不用担心监听器):
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;
}
}
已经可以将属性视为 bean.value.value
。
我正在寻找是否有一种现有的方式来表示 bean.value.value = 3
并且基本上有一个相当于 bean.setValue(bean.getValue().withValue(3) 的调用));
。
请注意,实际值对象要复杂得多。
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尝试为 bean 类创建一个 BeanInfo 类,该类使用 setWriteMethod 指定属性变换器方法。此 write 方法可以获取原始 int 值并从中创建不可变值对象并将其分配给属性字段。
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.