Java simple application of reflection, the object is simply converted into a map

Source: Internet
Author: User

Turn from: http://blog.csdn.net/tarrant1/article/details/10954633

[Java]  View plain copy package com.appdev.bsf.server.common;      import  java.lang.reflect.field;   import java.lang.reflect.method;   import  java.util.hashmap;   import java.util.map;      public class  objectdynamiccreator {       /**       *  Returns a map set        *        * that has the property of the object as key and value as map   @param  obj       *             Object       *  @return  mapValue Map< string,string>       *  @throws  Exception        */       public static Map<String, String>  Getfieldvlaue (object obj)  throws exception {           Map<String,  String> mapvalue = new hashmap<string, string> ();            class<?> cls = obj.getclass ();            field[] fields = cls.getdeclaredfields ();           for  (Field field : fields)  {                String name =  Field.getname ();               string  strGet =  "Get"  + name.substring (0, 1). toUpperCase ()  + name.substring ( 1, name.length ());                Method methodget&nbsP;= cls.getdeclaredmethod (strget);                object object = methodget.invoke (obj);                string value = object != null ?  object.tostring ()  :  "";                mapvalue.put (name, value);           }            return mapValue;        }          /**       *  Returns the corresponding        *        * of the key pairs attribute, value corresponding value of map   @param  map       *             map<String,String>       *  @param  cls        *            Class        *  @return  obj Object       *  @throws   exception       */       public static  Object setfieldvalue (MAP&LT;STRING,&NBSP;STRING&GT;&NBSP;MAP,&NBSP;CLASS&LT;?&GT;&NBSP;CLS)  throws  Exception {           field[] fields =  cls.getdeclaredfields ();           object obj  = cls.newinstance ();           for  (Field  field : fields)  {                Class<?> clstype = field.gettype ();                string name = field.getname ();                String strSet =  "Set"  + name.substring (0,  1). toUpperCase ()  + name.substring (1, name.length ());                method methodset = cls.getdeclaredmethod (StrSet ,  clstype);               if  ( Map.containskey (name))  {                    object objvalue = typeconversion (ClsType, map.get (name));                     Methodset.invoke (OBJ,&NBsp;objvalue);         &

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.