Reflection assigns the values of all fields of an object to another similar object, and the other field
In our work, we need to accept messages from the front end, convert the messages to objects in our own format, process them in the background, convert them to objects in the front-end format, and return them to them, because the set and get between objects feel that the code is too redundant, I want to write a tool class for future development. I checked a lot of information at night and found that only one object is converted, if there are other objects in the object, this conversion method is not seen. After two days of research, I finally wrote the final improved version, but there are still many defects, the method for determining whether it is a basic data type is still not perfect. If you want to use Array [String] or other methods, it cannot be completely matched. Therefore, this method can only be used for conversion between simple objects, but I will post it first. I will try to optimize it the next time I am free, or share some good suggestions from Boyou.
Public class TestFinal {public static void main (String [] args) throws InstantiationException, InvocationTargetException {MtUserInfoRule mt = new MtUserInfoRule (); mt. setName ("1"); mt. setPassport ("1"); mt. setAddress ("1"); mt. setCredentials ("2"); mt. setEmail ("2"); mt. setHkmlp ("2"); mt. setId ("2"); mt. setMobile ("1"); mt. setMtp ("1"); mt. setTlp ("1"); mt. setPinyin ("1"); Person p = new Person (); p. setId _ P (1); p. setName_p ("personName"); p. setMen_p (true); p. setCh_p ('A'); p. setFloat_p (1f); p. setDouble_p (2.0); p. setLong_p (2l); p. setShort_p (short) 3); p. setByte_p (byte) 4); Student s = new Student (); s. setId_s (11); s. setName_s ("stuName"); p. setStudent_p (s); mt. setPerson (p); UserInfoRule info = new UserInfoRule (); doField (mt, info);} private static void doField (Object obj, Object obj2) throws Instant IationException, InvocationTargetException {try {Field [] properties = obj. getClass (). getDeclaredFields (); Field [] field2 = obj2.getClass (). getDeclaredFields (); for (Field p: properties) {boolean accessible = p. isAccessible (); p. setAccessible (true); String fieldName1 = p. getName (); // The get Method Name of the boolean Type isMen is isMen String strGet = p. getType (). equals (Boolean. class) | p. getType (). equals (boolean. c Lass )? FieldName1: ("get" + fieldName1.substring (0, 1 ). toUpperCase () + fieldName1.substring (1, fieldName1.length (); Method methodGet = obj. getClass (). getDeclaredMethod (strGet); Object object = methodGet. invoke (obj); for (Field p2: field2) {p2.setAccessible (true); // The two objects have the same name (if the two objects have different names, they can be converted for matching) if (p. getName (). equals (p2.getName () {if (returnBoolean (p) {doField (object, p2.getType (). newInstance ();} Else {if (boolean. class. equals (p2.getType () | Boolean. class. equals (p2.getType () {p2.set (obj2, "1 ". equals (object )? True: false);} else {p2.set (obj2, object);} System. out. println (p2.getName () + ":" + p2.get (obj2);} p. setAccessible (accessible) ;}} catch (SecurityException e) {e. printStackTrace ();} catch (IllegalArgumentException e) {e. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace ();} catch (NoSuchMethodException e) {e. printStackTrace () ;}/// determines whether the data type is basic or String or Date (which may be incomplete or may be of another type) public static boolean returnBoolean (Field f) {Class t = f. getType (); if (String. class. equals (t) | Date. class. equals (t) | byte. class. equals (t) | Byte. class. equals (t) | short. class. equals (t) | Short. class. equals (t) | float. class. equals (t) | Float. class. equals (t) | long. class. equals (t) | Long. class. equals (t) | int. class. equals (t) | Integer. class. equals (t) | double. class. equals (t) | Double. class. equals (t) | boolean. class. equals (t) | Boolean. class. equals (t) | char. class. equals (t) | Character. class. equals (t) {return false;} return true ;}}