commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Hanson Char" <hanson_c...@yahoo.com>
Subject JavaBean Deep copy
Date Sun, 13 Jun 2004 12:34:06 GMT
I see BeanUtils (package org.apache.commons.beanutils) currently has a
cloneBean utility method to do shallow bean cloning.  Sometimes it is useful
to do a deep clone.  Please find below a simple implementation.  Can similar
method be incorporated/provided in BeanUtils ?

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;
    }
}

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message