Java objects and map objects are converted to each other

Source: Internet
Author: User

1 /** 2 * Use org.apache.commons.beanutils for conversion3  */  4 classA {5       6      Public StaticObject Maptoobject (map<string, object> Map, class<?> beanclass)throwsException {7         if(Map = =NULL)  8             return NULL; 9   TenObject obj =beanclass.newinstance ();  One    A org.apache.commons.beanutils.BeanUtils.populate (obj, map);  -    -         returnobj;  the     }     -        -      Public Staticmap<?,? >objecttomap (Object obj) { -         if(obj = =NULL)   +             return NULL;  -    +         return Neworg.apache.commons.beanutils.BeanMap (obj);  A     }     at        - }   -    - /**  - * Use Introspector for conversion -  */   in classB { -    to      Public StaticObject Maptoobject (map<string, object> Map, class<?> beanclass)throwsException { +         if(Map = =NULL)    -             return NULL;  the    *Object obj =beanclass.newinstance ();  $   Panax NotoginsengBeanInfo BeanInfo =Introspector.getbeaninfo (Obj.getclass ());  -propertydescriptor[] PropertyDescriptors =beaninfo.getpropertydescriptors ();  the          for(PropertyDescriptor property:propertydescriptors) { +Method setter =Property.getwritemethod ();  A             if(Setter! =NULL) {   the setter.invoke (obj, Map.get (Property.getname ()));  +             }   -         }   $    $         returnobj;  -     }     -        the      Public StaticMap<string, object> objecttomap (Object obj)throwsException { -         if(obj = =NULL)  Wuyi             return NULL;  the    -map<string, object> map =NewHashmap<string, object>();  Wu    -BeanInfo BeanInfo =Introspector.getbeaninfo (Obj.getclass ());  Aboutpropertydescriptor[] PropertyDescriptors =beaninfo.getpropertydescriptors ();  $          for(PropertyDescriptor property:propertydescriptors) { -String key =Property.getname ();  -             if(Key.comparetoignorecase ("class") = = 0) {    -                 Continue;  A             }   +Method getter =Property.getreadmethod ();  theObject value = getter!=NULL? Getter.invoke (obj):NULL;  - map.put (key, value);  $         }     the    the         returnmap;  the     }     the        - }   in    the /**  the * Use reflect for conversion About  */   the classC { the    the      Public StaticObject Maptoobject (map<string, object> Map, class<?> beanclass)throwsException { +         if(Map = =NULL)   -             return NULL;  the   BayiObject obj =beanclass.newinstance ();  the    thefield[] Fields =Obj.getclass (). Getdeclaredfields ();  -          for(Field field:fields) { -             intMoD =field.getmodifiers ();  the             if(modifier.isstatic (mod) | |modifier.isfinal (MoD)) {     the                 Continue;  the             }     the    -Field.setaccessible (true);  the field.set (obj, Map.get (Field.getname ()));  the         }    the   94         returnobj;  the     }     the    the      Public StaticMap<string, object> objecttomap (Object obj)throwsException {98         if(obj = =NULL){     About             return NULL;  -         }   101   102map<string, object> map =NewHashmap<string, object>(); 103   104field[] Declaredfields =Obj.getclass (). Getdeclaredfields ();  the          for(Field field:declaredfields) {106Field.setaccessible (true); 107 Map.put (Field.getname (), Field.get (obj)); 108         }    109    the         returnmap; 111     }    the}

Java objects and map objects are converted to each other

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.