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<STRING,&NBSP;STRING>&NBSP;MAP,&NBSP;CLASS<?>&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); &