Beanutils (Null empty collection empty list no longer replicated)

Source: Internet
Author: User

When Beanutils.copyproperties is used, the null value is also copied to make it a very chicken side.

He is extended to support a variety of null values are no longer copied

 Public Abstract classBeanutilsextendsOrg.springframework.beans.BeanUtils { Public Static voidCopyproperties (object source, object target)throwsbeansexception {assert.notnull (source,"Source must not being null"); Assert.notnull (Target,"Target must not being null"); Class<?> actualeditable =Target.getclass (); Propertydescriptor[] Targetpds=getpropertydescriptors (actualeditable);  for(PropertyDescriptor Targetpd:targetpds) {if(Targetpd.getwritemethod ()! =NULL) {PropertyDescriptor SOURCEPD=Getpropertydescriptor (Source.getclass (), Targetpd.getname ()); if(SOURCEPD! =NULL&& Sourcepd.getreadmethod ()! =NULL) {              Try{Method Readmethod=Sourcepd.getreadmethod (); if(!Modifier.ispublic (Readmethod.getdeclaringclass (). getmodifiers ())) {readmethod.setaccessible (true); } Object Value=Readmethod.invoke (source); //This determines whether the following value is empty, of course, there are some special requirements such as binding format conversion and so on              if(Value! =NULL) {                    BooleanIsEmpty =false; if(ValueinstanceofSet) {Set s=(Set) value; if(s = =NULL||S.isempty ()) {IsEmpty=true; }                    } Else if(ValueinstanceofMap) {Map m=(MAP) value; if(M = =NULL||M.isempty ()) {IsEmpty=true; }                    } Else if(ValueinstanceofList) {List L=(List) value; if(L = =NULL|| L.size () < 1) {IsEmpty=true; }                    } Else if(ValueinstanceofCollection) {Collection C=(Collection) value; if(c = =NULL|| C.size () < 1) {IsEmpty=true; }                    }                                  if(!isEmpty) {Method Writemethod=Targetpd.getwritemethod (); if(!Modifier.ispublic (Writemethod.getdeclaringclass (). getmodifiers ())) {writemethod.setaccessible (true);                } writemethod.invoke (target, value); }                }            } Catch(Throwable ex) {Throw NewFatalbeanexception ("Could not copy properties from source to target", ex); }            }          }        }      }    }  

Beanutils (Null empty collection empty list no longer replicated)

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.