Java map and the transformation before the bean

Source: Internet
Author: User

 Map --> Bean 2:  using the org.apache.commons.beanutils  tool class for  Map -->  bean      public static void transmap2bean2 (Map<String,  object> map, object obj)  {           if  (map == null | |  obj == null)  {               return;          }           try {               beanutils.populate (Obj, map);           } catch  (exception e)  {               system.out.println ("transmap2bean2 error "  + e);          }      }         // Map --> Bean 1:  using Introspector, PropertyDescriptor Implementation  map --> bean      public static  void transmap2bean (map<string, object> map, object obj)  {             try {               BeanInfo beanInfo =  Introspector.getbeaninfo (Obj.getclass ());               propertydescriptor[] propertydescriptors = beaninfo.getpropertydescriptors ();                 for  ( Propertydescriptor property : propertydescriptors)  {                   string key = property.getname ();                     if  (Map.containskey (key))  {                        object value = map.get (Key);                       //  Get the property corresponding setter method                        method setter = property.getwritemethod ();                        setter.invoke (Obj,&nbsP;value);                   }                }             } catch  (Exception e)  {              system.out.println ( "transmap2bean error "  + e);           }             return;         }        // bean --> map 1:   using Introspector and propertydescriptor  will bean --> map       Public static map<string, object> transbean2map (object obj)  {       &Nbsp;     if (obj == null) {               return null;           }                   Map<String, Object> map = new HashMap<String, Object> ();           try {               beaninfo beaninfo = introspector.getbeaninfo ( Obj.getclass ());               Propertydescriptor[] propertydescriptors = beaninfo.getpropertydescriptors ();               for  (Propertydescriptor property  : propertydescriptors)  {                   String key = property.getname ();                     //  Filter Class Properties                    if  (!key.equals ("Class"))  {                       //  get the property corresponding getter method                        method getter =  property.getreadmethod ();                       object value = getter.invoke (obj);                          map.put (Key, value);                   }                 }          } catch  (Exception  e)  {               System.out.println ("transbean2map error "  + e);           }            return map;         }


This article is from "Avatar" blog, please make sure to keep this source http://shamrock.blog.51cto.com/2079212/1686654

Java map and the transformation before the bean

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.