New Features in JavaBeans
Mark Davidson
Lead Engineer, JavaBeans
Sun Microsystems, Inc
JavaBeans Changes For J2SE 1.4
JSR 57: Long Term Persistence for JavaBeans
Beans Spec Amendment: Getting listeners
Introspector changes
Bean Builder Demo: Component Assembly
JSR 57: Long Term Persistence
Included in J2SE 1.4 (Merlin) in the java.beans package
New way of Serializing graphs of JavaBeans to an XML format
Version resilience to changes in the VM
Archives public state of the bean
Tolerance to damaged archives.
Mechanics of Long Term Persistence
XMLEncoder - Encodes an Object graph to XML
- Takes an OutputStream as constructor argument
- writeObject will encode the Object to the stream
XMLDecoder - Decodes an Object graph from XML
- Takes an InputStream as the construct argument
- readObject will return the root of the Object graph
Requires a nullary constructor and properties which conform to the JavaBeans Specification
Use a subclass of PersistenceDelegate to encode JavaBeans which are not well formed.
Getting Listeners from JavaBeans
EventSets are defined by the existence of the addFooListener(FooListener) and removeFooListener(FooListener) methods.
There is no access to the JavaBeans listener lists.
1.3 introduced the method: Component.getListeners(Class)
Beans Spec has been amended to support FooListeners[] getFooListeners()
New Design Patterns for Listeners
public void add<ListenerType>(<ListenerType l)
public void remove<ListenerType>(<ListenerType l)
public <ListenerType>[] get<ListenerType>s()
The existence of the the
ListenerType[] get<ListenerType>s()
method is completely optional
The Introspector will look for this method when EventSetDescriptors are created
Support for Named Properties
For PropertyChangeListeners and VetoableChangeListeners
void addPropertyChangeListener(String name, PropertyChangeListener l)
void removePropertyChangeListener(String name, PropertyChangeListener l)
PropertyChangeListener[] getPropertyChangeListeners(String name)
Affect of Named Properties on getPropertyChangeListeners()
New class: PropertyChangeListenerProxy
Returned listener array will contain a mix of PropertyChangeListeners and Proxies.
get<Foo>Listeners Conclusion
All Swing and AWT classes have getFooListeners() methods added for 1.4
The Introspector will use the getFooListeners() method if it exists to build the EventSets.
EventSetDescriptor.getGetListenerMethod() will return the method to access the array of listeners or null if it doesnt exist.
Changes to the Introspector
Uses new 1.2 Collections classes
Descriptors are sorted by the key FeatureDescriptor.getName(). However, this is not part of the Beans Spec.
getBeanInfo() no longer returns a copy
Bean Builder Demo
Showing how all the pieces fit together
Conclusion
Long term persistence is finally available!
Add getFooListeners() methods to your Beans
Introspector is faster and more deterministic.
Watch the JavaBeans site for the Bean Builder and other emerging technologies
http://java.sun.com/products/javabeans