Whilst I was on holiday there was a bit of discussion about properties in Java.
Shannon Hickey announced Beans Binding version 1.0. This is the JSR that intends to develop an API to connect together swing components in a simple and easy to use manner. The idea is that you call a simple API to setup a binding between a GUI field and your data model.
During development, a lot of discussion on the JSR was about the interaction with the possible language proposal for properties.
As a result, the v1.0 API includes a
Property abstract class, with a subclasses for beans and the EL language from J2EE.
Per-class or per-instance
One key issue that has to be addressed with a property proposal is whether to go per-class or per-instance.
The per-class approach results in usage like
Field, where the bean must be passed in to obtain the value:
Person mother = ...; Property<Person, String> surnameProperty = BeanProperty.create("surname"); String surname = surnameProperty.getValue(mother);
Here, the surname property refers to the surname on all Person beans. The caller must pass in a specific person instance to extract the value of the surname.
On the other hand, the per-instance approach results in usage where the property itself knows what bean it is attached to:
Person mother = ...; Property<Person, String> surnameProperty = BeanProperty.create("surname", mother); String surname = surnameProperty.getValue();
Here, the surname property refers to the specific surname property on the mother instance. There is thus no need to pass in any object to extract the value of the surname, as the property knows that it is attached to mother.
Which is better? And which did Beans Binding adopt?
Well, its actually the case that for the swing use case, the per-class approach is really the only option. Consider a table where each row is the representation of a bean in a list. The columns of this table are clearly going to be properties, but which type? Well, when defining the binding, we want to do this without any reference to the beans in the list - after all, the beans may change and we don't want to have to redefine the binding if they do.
So, we naturally need the per-class rather than per-instance properties. And, that is what the Beans Binding project defines.
Is there ever a need for the per-instance approach? In my opinion yes, but that can be added to the per-class based property functionality easily.
Adding language support
Having seen the Beans Binding release, Remi Forax released a version of his property language compiler that integrates:
Person mother = ...; Property<Person, String> surnameProperty = LangProperty.create(Person#surname); String surname = surnameProperty.getValue(mother);
As can be seen, the main advantage is that we now have a type-safe, compile-time checked, refactorable property
This is a tremendous leap forward for Java, and one that shouldn't be underestimated.
And the # syntax can easily be extended to fields and methods (FCM).
But, why do we see this new class
Well, Remi's implementation returns an object of type
java.lang.Property, not a Beans Binding
This fustrating difference means that two classes named
Property exist, and conversion has to take place.
What this really demonstrates is the need for Sun to declare if Java 7 will or won't contain a language change for properties, and for a real effort to occur to unify the various different JSRs currently running in the beans/properties arena. It would be a real disaster for Java to end up with two similar solutions to the same problem (although I should at least say I'm very happy that the two parts are compatible :-).
Properties support in Java is progressing through a combination of JSR and individual work. Yet Sun is still silent on Java 7 language changes. Why is that?
Opinions welcomed as always!