Simple application of Reflection in Java, simple conversion of an object into a map

Source: Internet
Author: User

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 the map collection of value for map with the object's property as key * * @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 = Cls.getdeclaredmethod (strget);            Object object = Methodget.invoke (obj); String Value = Object! = null?            Object.ToString (): ""; Mapvalue.put(name, value);    } return mapvalue; }/** * Returns the corresponding value of the key pair attribute of the map, which consists of the values corresponding to * * @param map * map<string,string> * @param CLS * Class * @return obj Object * @throws Exception */public static Object SetFieldValue (M        Ap<string, string> map, class<?> 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, objvalue);    }} return obj; }/** * Set some values inside map to existing objects through reflectionGo * * @param obj * Object * @param data * map<string,string> * @retu RN obj Object * @throws Exception */public static object Setobjectfilevalue (Object obj, map<string, string& Gt        Data) throws Exception {class<?> cls = Obj.getclass ();        field[] fields = Cls.getdeclaredfields ();            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 (Data.containskey (name)) {Object ObjValue = typeconversion (Clstype, Data.get (name));            Methodset.invoke (obj, objvalue);    }} return obj;  }/** * Put the value of the object in map. * @param map * map<string,string> * @param obj *          Object * @return Map map<string,string> */public static map<string, string> Comp Aremap (map<string, string> Map, Object obj) {map<string, string> mapvalue = new hashmap<string, Str        Ing> ();        field[] fields = Obj.getclass (). Getdeclaredfields ();            for (Field field:fields) {String name = Field.getname ();            if (Map.containskey (name)) {mapvalue.put (name, Map.get (name));    }} return mapvalue;            /** * Copy the value of the temporary object to the persisted object * * @param oldobject * Object persisted objects * @param newObject * Object Temporary Object * @return persisted Object * @throws Exception */public static object Mergedobject (Object Oldob        Ject, Object newObject) throws Exception {class<?> cls = Newobject.getclass ();        field[] fields = Cls.getdeclaredfields (); for (Field field:fields) {class<?> Clstype = Field.gettYpe ();            String name = Field.getname ();            String method = name.substring (0, 1). toUpperCase () + name.substring (1, Name.length ());            String strget = "Get" + method;            Method methodget = Cls.getdeclaredmethod (strget);            Object object = Methodget.invoke (NewObject);                if (Object! = null) {String Strset = "Set" + method;                Method Methodset = Cls.getdeclaredmethod (Strset, Clstype);                Object ObjValue = typeconversion (Clstype, object.tostring ());            Methodset.invoke (Oldobject, ObjValue);    }} return oldobject;        public static Object Typeconversion (class<?> cls, String str) {object obj = null;        String nametype = Cls.getsimplename ();        if ("Integer". Equals (NameType)) {obj = integer.valueof (str);        } if ("String". Equals (NameType)) {obj = str; } if ("Float". Equals (NameType)) {OBJ = float.valueof (str);        } if ("Double". Equals (NameType)) {obj = double.valueof (str);        } if ("Boolean". Equals (NameType)) {obj = boolean.valueof (str);        } if ("Long". Equals (NameType)) {obj = long.valueof (str);        } if ("Short". Equals (NameType)) {obj = short.valueof (str);        } if ("Character". Equals (NameType)) {obj = Str.charat (1);    } return obj; }}



Simple application of Reflection in Java, simple conversion of an object into a map

Related Article

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.