Skip to main content

AbstractBean and Serialization

3 replies [Last post]
kschaefe
Offline
Joined: 2006-06-08

Bug 368 raises some issues with AbstractBean and serialization. AbstractBean is not serializable and as such any attempts to create serializable subclasses will fail to properly serialize any Serializable PropertyChangeListeners or VetoableChangeListeners.

There are two possible solutions to this problem:
1. Make AbstractBean Serializable and remove the transient markers from the PropertyChangeSupport and VetoableChangeSupport.
2. Update the documentation explaining that the listeners need to be manually serialized with readObject/writeObject methods, such as:

private void writeObject(ObjectOutputStream s) throws IOException {<br />
    s.defaultWriteObject();</p>
<p>    for (PropertyChangeListener l : getPropertyChangeListeners()) {<br />
        if (l instanceof Serializable) {<br />
            s.writeObject(l);<br />
        }<br />
    }</p>
<p>    for (VetoableChangeListener l : getVetoableChangeListeners()) {<br />
        if (l instanceof Serializable) {<br />
            s.writeObject(l);<br />
        }<br />
    }</p>
<p>    s.writeObject(null);<br />
}</p>
<p>private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {<br />
    s.defaultReadObject();</p>
<p>    Object listenerOrNull;<br />
    while (null != (listenerOrNull = s.readObject())) {<br />
        if (listenerOrNull instanceof PropertyChangeListener) {<br />
            addPropertyChangeListener((PropertyChangeListener)listenerOrNull);<br />
        } else if (listenerOrNull instanceof VetoableChangeListener) {<br />
                addVetoableChangeListener((VetoableChangeListener)listenerOrNull);<br />
        }<br />
    }<br />
}

Which direction do we want to take AbstractBean?

Karl

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Kleopatra

Karl,

how daring (this issue is a nasty one ;-) - I faintly remember we had
some debates about what/if/how to handle serialization and at some point
simply gave up.
> There are two possible solutions to this problem:
> 1. Make AbstractBean Serializable and remove the transient markers from the PropertyChangeSupport and VetoableChangeSupport.
>

that's not an option, IMO, because it'll effect existing subclasses:
it's adding a contract (support serialization) which they are not aware
of and consequently can't comply to. Plus the AbstractBean is one of the
few which have the reviewed tag (whatever that's worth for ;-)

> 2. Update the documentation explaining that the listeners need to be manually serialized with readObject/writeObject methods, such as:
>

that's a pain ... but doable. We could extract the actual write/read of
the listeners into protected methods to ease it a bit:

[code]

protected void writeListeners(ObjectOutputStream s) {
for (PropertyChangeListener l : getPropertyChangeListeners()) {
if (l instanceof Serializable) {
s.writeObject(l);
}
}

for (VetoableChangeListener l : getVetoableChangeListeners()) {
if (l instanceof Serializable) {
s.writeObject(l);
}
}

s.writeObject(null);

}

[/code]

this would reduce the private code in subclasses to, still not nice ...
but ... :

[code]

private void writeObject(ObjectOutputStream s) throws IOException {
s.defaultWriteObject();
writeListeners(s);
}

[/code]

CU
Jeanette

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

kschaefe
Offline
Joined: 2006-06-08

Jeanette,

There is another option: create AbstractSerializableBean. Extend AbstractBean add Serialization and perform all of the readObject/writeObject stuff in that class.

I think that I am favoring this option.

Karl

Kleopatra

sounds good to me.

Jeanette

jdnc-interest@javadesktop.org schrieb:
> Jeanette,
>
> There is another option: create AbstractSerializableBean. Extend AbstractBean add Serialization and perform all of the readObject/writeObject stuff in that class.
>
> I think that I am favoring this option.
>
> Karl
> [Message sent by forum member 'kschaefe' (kschaefe)]
>
> http://forums.java.net/jive/thread.jspa?messageID=266768
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
> For additional commands, e-mail: jdnc-help@jdnc.dev.java.net
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net