Java object any deep clone clone tool class share

Source: Internet
Author: User

Original: Java object any deep clone clone tool class share

Source code: Http://www.zuidaima.com/share/1550463408114688.htm

Java object any deep clone clone tool class share

Package com.zuidaima.n_app.util;  Import Java.lang.reflect.Constructor;  Import Java.lang.reflect.Field;  Import java.lang.reflect.InvocationTargetException;  Import Java.lang.reflect.Modifier;  Import Java.util.Arrays;  Import java.util.Collection;  Import Java.util.HashSet;  Import Java.util.Map;    Import Java.util.Set;      /** * Clone Tool class * @author [email protected] www.zuidaima.com * * * * public class Clone {/** * Special type array not required to be copied */static class[] needlesscloneclasses = new Class[]{string.class,boolean.class,character.class,byte.class,short      . class, Integer.class,long.class,float.class,double.class,void.class,object.class,class.class}; /** * Determines if the type object does not need to be copied * @param c specifies type * @return returns True if no replication is required, otherwise false */private static Boolean Isneedl          Essclone (Class c) {if (c.isprimitive ()) {//basic type return true;       } for (Class tmp:needlesscloneclasses) {//is not required to copy an array of type if (C.equals (TMP)) {           return true;      }} return false;      }/** * Try to create a new object * @param C Original Object * @return New Object * @throws illegalaccessexception */ Private Static Object CreateObject (object value) throws illegalaccessexception{try {Retu              RN Value.getclass (). newinstance ();              } catch (Instantiationexception e) {return null;              } catch (Illegalaccessexception e) {throw e; }}/** * Copy Object Data * @param value Original Object * @param level copy depth.      Less than 0 is an infinite depth, which is about to drill down to the most basic type and object class level of data replication; * Greater than 0 copies the data to the specified depth by its value, or 0 returns the object itself without any copying behavior. * @return Return the copied object * @throws illegalaccessexception * @throws instantiationexception * @throws Invocationta Rgetexception * @throws nosuchmethodexception */public static object Clone (object Value,int level) throws Illegalaccessexception, Instantiationexception, INVOcationtargetexception, nosuchmethodexception{if (value==null) {return null;          } if (level==0) {return value;          } Class C = Value.getclass ();          if (Isneedlessclone (c)) {return value;          } level--;              if (value instanceof Collection) {//Copy new collection Collection TMP = (Collection) c.newinstance ();          for (Object V: (Collection) value) {Tmp.add (Clone (V,level));//Deep copy} value = tmp;                  } else if (C.isarray ()) {//Copy new array//first determine if the base data type is (C.equals (Int[].class)) {                  Int[] Old = (int[]) value;              Value = (int[]) arrays.copyof (old, old.length);                  } else if (C.equals (Short[].class)) {short[] old = (short[]) value;              Value = (short[]) arrays.copyof (old, old.length); } else if (C.equals (CHAR[].CLASS) {char[] old = (char[]) value;              Value = (char[]) arrays.copyof (old, old.length);                  } else if (C.equals (Float[].class)) {float[] old = (float[]) value;              Value = (float[]) arrays.copyof (old, old.length);                  } else if (C.equals (Double[].class)) {double[] old = (double[]) value;              Value = (double[]) arrays.copyof (old, old.length);                  } else if (C.equals (Long[].class)) {long[] old = (long[]) value;              Value = (long[]) arrays.copyof (old, old.length);                  } else if (C.equals (Boolean[].class)) {boolean[] old = (boolean[]) value;              Value = (boolean[]) arrays.copyof (old, old.length);                  } else if (C.equals (Byte[].class)) {byte[] old = (byte[]) value;      Value = (byte[]) arrays.copyof (old, old.length);        } else {object[] old = (object[]) value;                  object[] tmp = (object[]) arrays.copyof (old, Old.length, Old.getclass ());                  for (int i = 0;i<old.length;i++) {Tmp[i] = Clone (Old[i],level);              } value = tmp;              }} else if (value instanceof map) {//Copy new map map tmp = (MAP) c.newinstance ();              Map org = (map) value;              For (Object Key:org.keySet ()) {Tmp.put (Key, Clone (Org.get (key), level));//Deep copy}          value = tmp;              } else {Object tmp = CreateObject (value);              if (tmp==null) {//Cannot create a new instance then returns the object itself without cloning return value;              } set<field> fields = new hashset<field> ();        while (C!=null&&!c.equals (Object.class)) {Fields.addall (Arrays.aslist (C.getdeclaredfields ()));          c = C.getsuperclass (); } for (Field field:fields) {if (!                      Modifier.isfinal (Field.getmodifiers ())) {//Copy only non-final fields field.setaccessible (true);          Field.set (TMP, Clone (Field.get (value), level));//Deep copy}} value = tmp;      } return value;      /** * Shallow Copy Object * @param value Original Object * @return copied object, copy only one layer * @throws illegalaccessexception * @throws instantiationexception * @throws invocationtargetexception * @throws nosuchmethodexception * /public static Object Clone (object value) throws Illegalaccessexception, Instantiationexception, Invocationtargetexc      Eption, nosuchmethodexception{return Clone (value,1);  }/** * Deep Copy Object * @param value Original Object * @return Copied Object * @throws illegalaccessexception * @throws instantiationexception * @throws invocationtargeTexception * @throws nosuchmethodexception */public static Object Deepclone (object value) throws Illegalac      Cessexception, Instantiationexception, InvocationTargetException, nosuchmethodexception{return Clone (value,-1);     }  }

Java object any deep clone clone tool class share

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.