What features should be in scope and out of scope for a Beans specification v2.0?
Note: I am an independent blogger, not an Oracle employee, and as such this blog is my opinion. Oracle owns the JavaBeans trademark and controls what actually happens to the specification.
My last blog examined the contents of the current JavaBeans (TM) specification. The key feature elements can be summarized as follows:
- Properties - accessed using getter and setter methods
- Events - change events for properties
- Methods - all public methods
- BeanInfo - permits customization of properties, events and methods
- Beans - a utility class to access the features
- Property editor - a mechanism to edit properties, including conversion to and from a string format
- GUI support - allows a bean to be manipulated visually
These features were a good fit for 1997, given that the original goal was to be a component system, interoperating with other similar systems like COM/DCOM. These features are no longer what we need, so any Beans v2.0 is naturally going to look a lot different.
Beans v2.0 - Data abstraction
The key goal of a Beans v2.0 spec is to define an abstraction over the data in an object.
When such an abstraction exists, frameworks and libraries become far simpler to write and interact with. For example, a serialization framework at its most basic level simply needs to iterate over all the beans and properties in an object graph in order to perform its task.
In addition to frameworks, many ad-hoc coding tasks become simpler because it is much easier to walk over the object graph of beans and properties. For example, to find all instances of a particular type within an object graph starting from an arbitrary bean.
The data abstraction needs to be fully round-trip. The spec must allow for data to be read from a bean into another format. But it must also provide the tools to allow the original bean to be recreated from the data previously read.
Beans v2.0 - Mutable vs Immutable
The JavaBean v1.0 specification implicitly defines mutable objects. It requires all beans to have a no-args constructor, thus the only way for the properties to get populated is through the use of setters. This mutability is one of the key complaints that people have about JavaBeans, and often Java itself by association.
Any Beans v2.0 specification must support immutable objects.
Immutable classes are now a common design approach to systems and are increasingly popular. Supporting them is thus vital to encompass the range of data objects in use today.
Any Beans v2.0 specification must also still support mutable objects. While there are those that turn their nose up at any mutability, it is still a perfectly valid choice for an application. The decision between mutable and immutable data objects is a trade-off. The Beans v2.0 spec needs to support both options.
The implications of supporting immutable is that an alternative to a no-args constructor and public setters will be needed to support construction. This is likely to be a "bean builder" of some kind.
Beans v2.0 - Beans and Properties
The term "bean" is often associated with a specific Java class, such as
The term "property" is often associated with a single field on the class,
Any Beans v2.0 spec must not have a reflection-based view of beans and properties.
Firstly, a property is not the same as a field (instance variable). Fields hold the state of an object, but not necessarily in the form that the object wishes to expose (it might be stored in an optimised format for example). As such, it must be possible for a bean to control the set of properties that it publishes.
Secondly, it can be desirable to a bean with a dynamic set of properties. This might consist of a bean with a fixed set of base properties and an extensible map of additional ones, or a bean which is entirely dynamic, much closer to the data model of many dynamic languages.
The implications of this is that there is not a one-to-one mapping between a bean and a
nor is there a one-to-one mapping between a property and a
The JavaBeans v1.0 spec treats indexed properties in a special way. I can see no reason to do that in Beans v2.0, particularly given the range of collection types now in use.
Beans v2.0 - Getters and Setters
The current JavaBeans v1.0 spec requires there to be a physical
instance for both the getter and setter.
What Beans v2.0 needs to provide is an abstraction away from this, as it provides many more options.
Any Beans v2.0 spec must not require data access via a
Moving away from direct exposure of reflection raises the level of abstraction. It permits alternative object designs to be exposed as beans.
For example, consider a bean that has no public getters and setters. With the Beans v2.0 abstraction, this could still expose its data as properties, providing interoperation with frameworks, but encapsulating the data for most programmatic uses. This tackles another big criticism of JavaBeans, which is that they cause state to be exposed rather than hidden.
Another example is the
HashMap based bean.
In this case a
Method does not exist for each property, just a single get by name.
Being able to expose this as a bean provides the desired dynamic behaviour.
The implications of this are simply that the Beans v2.0 spec abstraction will simply be
long the lines of the
Beans v2.0 - Annotations
The current JavaBeans v1.0 spec predates annotations in Java. But annotations are now key to many programming practices in Java.
Any Beans v2.0 spec must provide access to property and bean annotations.
Annotations are represented by implementations of the
It so happens that the
Annotation interface is not part of reflection.
It is possible to implement an annotation yourself (apart from a compiler warning).
What is needed is for the Beans v2.0 spec abstraction to provide access to the set of annotations on the bean, and the set of annotations on each property without going via reflection. With this step, and the ones above, many direct uses of reflection will completely disappear, with the Beans v2.0 abstraction being good enough.
Beans v2.0 - Events
The current JavaBeans v1.0 spec considers events to be first class parts of the abstraction.
There is no good reason to include events in the Beans v2.0 spec.
The current JavaBeans v1.0 spec is GUI focussed, and as such events were an essential element. But the Beans v2.0 spec outlined here focuses on a different goal, abstracting over the data in the object. Round-tripping data from a bean to another format and back to a bean does not require events or a GUI.
There will be those reaching for the comments box right now, demanding that events must be included. But think carefully and you should realise that you don't need to.
None of what is proposed here for Beans v2.0 takes away what works today with JavaBeans v1.0.
GUI developers can still carry on using the same getters, setters and events that they do today,
including bound and constrained properties.
java.beans package will continue to exist and continue to be available.
In addition, JavaFX is the way forward for Java GUIs, and it has its own approach to properties.
Beans v2.0 - Conversion to/from String
The current JavaBeans v1.0 spec includes
One of the use cases of the class, notably in Spring framework, is to convert a simple object
(typically a VALJO) to and from a String.
For example, this includes
Any Beans v2.0 spec must tackle conversion to/from a String for simple types.
The most common use case for the Beans v2.0 spec will be to iterate over the object graph and process it in some way, such as to write out a JSON or XML message. In order for this to be practical, all objects in the object graph need to be either a bean, or a simple type convertible to a string. As such, simple type string conversion is very much needed to round out the spec.
The Joda-Convert project contains a simple set of interfaces and classes to tackle this problem, and I imagine any Beans v2.0 spec solution would be similar.
Beans v2.0 - But what does a bean actually look like?
Part of the goal of Beans v2.0 is to allow for different kinds of bean. Immutable as well as mutable. Private getters and setters as well as public. Dynamic map-based structures as well as field-based ones.
Any Beans v2.0 spec should not overly restrict how beans are implemented.
We know that Oracle has no current plans for properties at the language level. As such, it also makes little sense for Beans v2.0 spec to specify a single approach for how classes should be designed. Instead, the spec aims to allow classes that are designed in different ways to all agree on a common way to interact with frameworks and libraries that does not require getters, setters and a no-args constructor.
The Joda-Beans, Lombok, Immutables and Auto projects (and many others) all provide different ways to create beans without writing manual code. All could be adapted to work with the proposed Beans v2.0 spec, as could frameworks like Jackson, Hibernate, Dozer and Groovy (to name but a few).
The simple answer is that the bean could look just like it does today with getters and setters. The real answer is that the bean may look any way it likes so long as it can expose its data via the Beans v2.0 API.
The current JavaBeans v1.0 spec is very old and not that useful for the kinds of applications we build today. This blog has outlined my opinion on what direction a Beans v2.0 spec should take:
- Focussed on data abstraction
- Support for Mutable and Immutable beans
- Fully abstracted from reflection
- No requirement for getters and setters
- No requirement for a no-args constructor
- Support for dynamically extensible properties
- Access to bean and property annotations
- Conversion of simple types to and from a String
- No support for events
Feel free to comment on the high-level scope outlined above. In particular, if you develop a project that uses the JavaBeans spec (directly or indirectly), please comment to say whether the proposed spec is likely to be sufficient to cover your use cases.