Virtual Beans
The {@link oaj.BeanContext#BEAN_useInterfaceProxies} setting (enabled by default) allows
the Juneau parsers to parse content into virtual beans (bean interfaces without implementation classes).
For example, the following code creates an instance of the specified unimplemented interface:
// Our unimplemented interface
public interface Address {
String getStreet();
void setStreet(String x);
String getCity();
void setCity(String x);
StateEnum getState();
void setState(StateEnum x);
int getZip();
void setZip(int zip);
}
// Our code
Address address = JsonParser.DEFAULT.parse(
"{street:'123 Main St', city:'Anywhere', state:'PR', zip:12345}",
Address.class
);
int zip = address.getZip();
address.setState(StateEnum.NY);
Getter and setter values can be any {@doc PojoCategories parsable} values, even other virtual beans.
Under-the-covers, a virtual bean is simply a proxy interface on top of an existing BeanMap
instance. From a programmatic point-of-view, they're indistinguishable from real beans, and can be
manipulated and serialized like any other bean.
Virtual beans can also be created programmatically using the BeanContext class:
Address address = BeanContext.DEFAULT.createSession().newBean(Address.class);