java深克隆有待测试。。。

来源:互联网 发布:万能导航端口检测工具 编辑:程序博客网 时间:2024/06/01 11:29
import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.lang.reflect.InvocationTargetException;import org.apache.commons.beanutils.BeanUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;public class BeanUtils implements java.io.Serializable {private static final longserialVersionUID= 2359458972081144630L;private static final Loglog= LogFactory.getLog( AHBeanUtils.class );private StringBuildersb= new StringBuilder();public static Object deepClone(Object fromDate){Object toDate = null;ByteArrayOutputStream bo = null;ObjectOutputStream oo = null;ByteArrayInputStream bi = null;ObjectInputStream oi = null;try {bo = new ByteArrayOutputStream();oo = new ObjectOutputStream( bo );oo.writeObject( fromDate );bi = new ByteArrayInputStream( bo.toByteArray() );oi = new ObjectInputStream( bi );toDate = oi.readObject();} catch ( IOException e ) {log.error( "Object com.utils.bean.BeanUtils.deepClone(Object fromDate)", e );} catch ( ClassNotFoundException e ) {log.error( "Object com.utils.bean.BeanUtils.deepClone(Object fromDate)", e );} finally {try {if ( oo != null )oo.close();if ( bo != null )bo.close();if ( bi != null )bi.close();if ( oi != null )oi.close();} catch ( IOException e ) {log.error( "Object com.utils.bean.BeanUtils.deepClone(Object fromDate)", e );}}return toDate;}public static void main(String abc[]){BeanUtils from = new BeanUtils();from.sb.append( "123456789" );BeanUtils deepTo = (BeanUtils) BeanUtils.deepClone( from );BeanUtils to = null;try {to = (BeanUtils) BeanUtils.cloneBean( from );} catch ( IllegalAccessException e ) {e.printStackTrace();} catch ( InstantiationException e ) {e.printStackTrace();} catch ( InvocationTargetException e ) {e.printStackTrace();} catch ( NoSuchMethodException e ) {e.printStackTrace();}System.out.println( "###############Before change####################" );System.out.println( "from:" + from.sb.toString() );System.out.println( "deepTo:" + deepTo.sb.toString() );System.out.println( "to:" + to.sb.toString() );from.sb.append( "!!!!!!!!!!!" );System.out.println( "###############After change####################" );System.out.println( "from:" + from.sb.toString() );System.out.println( "deepTo:" + deepTo.sb.toString() );System.out.println( "to:" + to.sb.toString() );}public StringBuilder getSb(){return sb;}public void setSb(StringBuilder sb){this.sb = sb;}}

0 0
原创粉丝点击