Java Copy object and copy object properties

Source: Internet
Author: User

    /*** Copy Object Properties (object type must be the same) * *@paramOrig Resource Object *@paramdest Target Object *@paramClazz Source Object class *@paramignorenull whether NULL is ignored (true: ignored, false: not ignored) *@return     */     Public Static<T> t Copyproperties (t orig, T dest, Class<?> Clazz,Booleanignorenull) {        if(Orig = =NULL|| Dest = =NULL)            return NULL; if(!Clazz.isassignablefrom (Orig.getclass ()))return NULL; Field[] Fields=Clazz.getdeclaredfields ();  for(Field field:fields) {Try{field.setaccessible (true); Object value=Field.get (orig); if(!java.lang.reflect.Modifier.isFinal (Field.getmodifiers ())) {                    if(! (ignorenull && Value = =NULL) {field.set (dest, value); }} field.setaccessible (false); } Catch(Exception e) {}}if(Clazz.getsuperclass () = = Object.class){            returndest; }                returncopyproperties (orig, dest, Clazz.getsuperclass (), ignorenull); }        /*** Copy Object Properties (object type must be the same) *@paramOrig Resource Object *@paramdest Target Object *@paramignorenull whether NULL is ignored (true: ignored, false: not ignored)*/     Public Static<T> t Copyproperties (t orig, T dest,Booleanignorenull) {        if(Orig = =NULL|| Dest = =NULL)            return NULL; returncopyproperties (orig, dest, Orig.getclass (), ignorenull); }    /*** Copy objects *@paramsrc Resource Object *@returnNew Object*/@SuppressWarnings ("Unchecked")     Public Static<T>t Clone (t src) {if(src = =NULL){            return NULL; } T NewObject=NULL; Try{newObject=(T) Src.getclass (). newinstance (); } Catch(Exception e) {e.printstacktrace (); }                returnCopyproperties (SRC, newObject,false); }

Java Copy object and copy object properties

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.