Java-map and Object Changer

Source: Internet
Author: User

/*** Use org.apache.commons.beanutils for conversion*/  classA { Public StaticObject Maptoobject (map<string, object> Map, class<?> beanclass)throwsException {if(Map = =NULL)              return NULL; Object obj=beanclass.newinstance ();            Org.apache.commons.beanutils.BeanUtils.populate (obj, map); returnobj; }               Public Staticmap<?,? >objecttomap (Object obj) {if(obj = =NULL)              return NULL; return Neworg.apache.commons.beanutils.BeanMap (obj); }          }    /*** Use Introspector for conversion*/  classB { Public StaticObject Maptoobject (map<string, object> Map, class<?> beanclass)throwsException {if(Map = =NULL)               return NULL; Object obj=beanclass.newinstance (); BeanInfo BeanInfo=Introspector.getbeaninfo (Obj.getclass ()); Propertydescriptor[] PropertyDescriptors=beaninfo.getpropertydescriptors ();  for(PropertyDescriptor property:propertydescriptors) {Method setter=Property.getwritemethod (); if(Setter! =NULL) {Setter.invoke (obj, Map.get (Property.getname ())); }          }            returnobj; }               Public StaticMap<string, object> objecttomap (Object obj)throwsException {if(obj = =NULL)              return NULL; Map<string, object> map =NewHashmap<string, object>(); BeanInfo BeanInfo=Introspector.getbeaninfo (Obj.getclass ()); Propertydescriptor[] PropertyDescriptors=beaninfo.getpropertydescriptors ();  for(PropertyDescriptor property:propertydescriptors) {String key=Property.getname (); if(Key.comparetoignorecase ("class") = = 0) {                   Continue; } Method Getter=Property.getreadmethod (); Object value= getter!=NULL? Getter.invoke (obj):NULL;          Map.put (key, value); }              returnmap; }          }    /*** Use reflect for conversion*/  classC { Public StaticObject Maptoobject (map<string, object> Map, class<?> beanclass)throwsException {if(Map = =NULL)              return NULL; Object obj=beanclass.newinstance (); Field[] Fields=Obj.getclass (). Getdeclaredfields ();  for(Field field:fields) {intMoD =field.getmodifiers (); if(modifier.isstatic (mod) | |modifier.isfinal (MoD)) {                    Continue; } field.setaccessible (true);           Field.set (obj, Map.get (Field.getname ())); }             returnobj; }           Public StaticMap<string, object> objecttomap (Object obj)throwsException {if(obj = =NULL){                return NULL; } Map<string, object> map =NewHashmap<string, object>(); Field[] Declaredfields=Obj.getclass (). Getdeclaredfields ();  for(Field field:declaredfields) {field.setaccessible (true);          Map.put (Field.getname (), Field.get (obj)); }              returnmap; }   } 

Java-map and Object Changer

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.