紀念SUN

          Powered By Andy

          測試clone

          package cn.xx

          import java.io.ByteArrayInputStream;
          import java.io.ByteArrayOutputStream;
          import java.io.IOException;
          import java.io.ObjectInputStream;
          import java.io.ObjectOutputStream;
          import java.io.Serializable;
          import java.lang.reflect.InvocationTargetException;
          import java.lang.reflect.Method;
          import java.util.Arrays;

          /**
           * <pre>
           * @Types   role : 測試clone

           * @Create  time : 2011-12-26 : 上午10:49:39
           * @ver     curr : 1.0
           * </pre>
           */
          public class TestClone implements Serializable{

              private static final long serialVersionUID = -7340886443308126418L;

              /**
               * <pre>
               * @Methods role : 對象的Clone
               * 注: 要clone對象必須要實現Serializable接口, 不然拋NoSerializableException
               * </pre>
               * @param obj
               * @return
               * @throws IOException
               * @throws ClassNotFoundException
               * @Create  time : 2011-12-23 : 下午5:26:57
               * @ver     curr : 1.0
               */
              @SuppressWarnings("unchecked")
              public static <T> T invokeCopy(T obj) throws IOException, ClassNotFoundException{
                  //T newObj = null;
                  // write object to memory
                  ByteArrayOutputStream bos = new ByteArrayOutputStream();
                  ObjectOutputStream oos = new ObjectOutputStream(bos);
                  oos.writeObject(obj);
                  oos.flush();
                  
                  // read object come from memory
                  ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
                  ObjectInputStream ois = new ObjectInputStream(bis);
                  //newObj = (T)ois.readObject();
                  
                  //bis.close();
                  //oos.close();
                  // return clone object
                  return (T)ois.readObject();
                  
                  
              }
              
              /**
               * <pre>
               * @Methods role : 實現對象的clone
               * 兩種方案(二選一實現都可以):
               * 1: 實現Cloneable接口, 深度clone自己重寫clone()實現,此方法只實現淺度clone
               * 2: 實現Serializable接口
               * @param obj
               * @return
               * @throws NoSuchMethodException
               * @throws SecurityException
               * @throws IllegalAccessException
               * @throws IllegalArgumentException
               * @throws InvocationTargetException
               * @throws ClassNotFoundException
               * @throws IOException
               * @throws CloneNotSupportedException
               * @Create  time : 2011-12-26 : 上午10:31:37
               * @ver     curr : 1.0
               * </pre>
               */
              public static<T> T clone(T obj) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, IOException, CloneNotSupportedException{
                  // is null
                  if(null == obj) return null;
                  
                  // is instanceof Cloneable
                  if(obj instanceof Cloneable){
                      return invokeClone(obj);
                  }
                  // is instanceof Serializable
                  else if(obj instanceof Serializable){
                      return invokeCopy(obj);
                  }
                  
                  // is not supported clone
                  throw new java.lang.CloneNotSupportedException();
              }

              @SuppressWarnings("unchecked")
              public static <T> T invokeClone(T obj) throws NoSuchMethodException,
                      IllegalAccessException, InvocationTargetException {
                  Class<? extends Object> classObject = obj.getClass();
                  Method method = classObject.getDeclaredMethod("clone");
                  return (T)method.invoke(obj);
              }
              
              public static void main(String[] args) throws ClassNotFoundException, IOException {
                  String x = "123";
                  String v = invokeCopy(x);
                  
                  String[][] array = {{"1", "2"},{ "3", "2","2","3"},{"V","3"}}, array2;
                  
                  
                  System.out.println(x);
                  System.out.println(v);
                  array2 = invokeCopy(array);
                  System.out.println(Arrays.deepToString(array2));
                  
                  int[] a = {1,48,2}, b = {1, 20, 19};
                  System.out.println("a --- hashCode: " + a.hashCode() + "---b: hashCode:" + b.hashCode());
                  b = invokeCopy(a);
                  System.out.println(b.hashCode());
                  System.out.println(Arrays.toString(b));
                  short age = 25;
                  Person p = new TestClone(). new Person(1l, "andy", age), ps, pe;
                  
                  ps = invokeCopy(p);
                  
                  System.out.println(ps);
                  
                  try {
                      //pe = clone(p);
                      pe = invokeClone(p);
                      System.out.println(pe);
                  } catch (NoSuchMethodException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (SecurityException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (IllegalAccessException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (IllegalArgumentException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (InvocationTargetException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
              
              class Person implements Serializable{//, Cloneable{
                  private static final long serialVersionUID = 7605971168826706980L;
                  private Long id;
                  private String name;
                  private short age;
                  
                  
                  public Person() {
                  }
                  
                  
                  public Person(Long id, String name, short age) {
                      super();
                      this.id = id;
                      this.name = name;
                      this.age = age;
                  }


                  public Long getId() {
                      return id;
                  }
                  public void setId(Long id) {
                      this.id = id;
                  }
                  public String getName() {
                      return name;
                  }
                  public void setName(String name) {
                      this.name = name;
                  }
                  public short getAge() {
                      return age;
                  }
                  public void setAge(short age) {
                      this.age = age;
                  }


                  public String toString() {
                      return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
                  }
                  
                  /*protected Object clone() throws CloneNotSupportedException {
                      // TODO Auto-generated method stub
                      return super.clone();
                  }*/
                  
              }
          }

          posted on 2011-12-26 10:54 Powered By Andy 閱讀(264) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          <2011年12月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 岳池县| 剑阁县| 南郑县| 兴仁县| 读书| 钟祥市| 丰顺县| 安仁县| 灵璧县| 诸城市| 葫芦岛市| 九龙城区| 吴堡县| 汝南县| 漾濞| 雅江县| 瑞金市| 印江| 铜鼓县| 塘沽区| 利辛县| 栾城县| 闵行区| 凤山县| 尼玛县| 库伦旗| 平潭县| 汕头市| 大理市| 临夏县| 巴林右旗| 淮安市| 梨树县| 朝阳县| 凤山市| 永昌县| 荣昌县| 汶上县| 行唐县| 应城市| 柳河县|