jakarta-jcs-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Hanson Char" <hanson_c...@yahoo.com>
Subject RE: Why the caching API should use Serializable rather than Object
Date Sat, 12 Jun 2004 15:46:18 GMT
Another interesting thing I find recently is that if a given object is a
Java Bean (according to Sun's java bean spec), it is possible to deeply
clone such object without the need for it to be serializable!  Below is a
simple class for such java bean cloning.

Cheers,
Hanson

import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.beans.XMLEncoder;
import java.beans.XMLDecoder;

/**
 * Used to clone java beans.
 */
public class JavaBeanCopier {
    /**
     *  Returns a deeply cloned java bean.
     *
     * @param fromBean java bean to be cloned.
     * @return a new java bean cloned from fromBean.
     */
    public static Object copy(Object fromBean) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        XMLEncoder out = new XMLEncoder(bos);
        out.writeObject(fromBean);
        out.close();
        ByteArrayInputStream bis = new
ByteArrayInputStream(bos.toByteArray());
        XMLDecoder in = new XMLDecoder(bis);
        Object toBean = in.readObject();
        in.close();
        return toBean;
    }
}

-----Original Message-----
From: Travis Savo [mailto:tsavo@IFILM.com]
Sent: Tuesday, 25 May 2004 3:43 AM
To: 'Turbine JCS Users List'
Subject: RE: Why the caching API should use Serializable rather than
Object


A very astute observation.   The option of 'copy key on put' might be
introduced in the future, which would require this.  I'm going to have to
meditate on the implications.  Thanks for the reply!  -Travis Savo
<tsavo@ifilm.com>

-----Original Message----- From: Hanson Char [mailto:hanson_char@yahoo.com]
<...Snip!...>

To my knowledge, the Serializable interface is left as the only means to
allow the caching system to ever possibly make such defensive copies.  The
Serializable interface certainly comes with it's own cost and cannot be
taken light-heartedly, but the alternative is a violation of Item 24.


---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-jcs-user-help@jakarta.apache.org


Mime
View raw message