Autobeans 旨在成为 JSON/XML/任何格式的 Java 皮肤 - 它们并不是真正设计用于保存其他数据。也就是说,一些想法几乎可以使用开箱即用的工具回答您的问题,或者可能会激发一些其他想法来解决您的问题。
您应该能够通过省略 setter 来构建只读属性。这不是您所要求的,但仍然可能很方便。
按照这些思路,@PropertyName 注释的 JavaDoc 似乎暗示了这个可能的特性:
/**
* An annotation that allows inferred property names to be overridden.
* <p>
* This annotation is asymmetric, applying it to a getter will not affect the
* setter. The asymmetry allows existing users of an interface to read old
* {@link AutoBeanCodex} messages, but write new ones.
*/
阅读旧消息但编写新消息似乎可能更接近您所追求的,并且仍然允许您使用看起来像 bean 的东西。
真正的答案似乎是 AutoBean.setTag 和 getTag 方法:
/**
* A tag is an arbitrary piece of external metadata to be associated with the
* wrapped value.
*
* @param tagName the tag name
* @param value the wrapped value
* @see #getTag(String)
*/
void setTag(String tagName, Object value);
...
/**
* Retrieve a tag value that was previously provided to
* {@link #setTag(String, Object)}.
*
* @param tagName the tag name
* @return the tag value
* @see #setTag(String, Object)
*/
<Q> Q getTag(String tagName);
从AbstractAutoBean 中这些方法的实现可以看出,它们将数据存储在与通过网络发送的完全不同的对象中。缺点是您需要获取底层 AutoBean 对象(请参阅com.google.web.bindery.autobean.shared.AutoBeanUtils.getAutoBean(U) 了解执行此操作的一种方法)才能调用这些方法。