在工作中需要接受來自前置的消息,然後將其消息轉化為我們自己格式的對象,然後經由後台處理,最後再轉化為前置格式的對象並返回給他們,由於對象之間set、get感覺代碼太冗余,所以想寫個工具類來作為後續發展,晚上查了很多資料,發現只有單個對象的轉化,對象中又有其他對象的話,這種轉化方式沒看到,研究了兩天,終於將最終完善版本寫出來了,不過還是有很多缺陷的,判斷是否是基本數據類型那個方法感覺還是不夠完善,想Array[String]之類的這些還是不能夠完全匹配,所以這個方法也只能用於簡單的對象之間的轉化,不過還是先貼出來,等下次有空的時候在進行優化,或者博友有啥好的建議盡情發表啊。
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 InstantiationException, 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();
// boolean類型的isMen的get方法名就是isMen
String strGet = p.getType().equals(Boolean.class) || p.getType().equals(boolean.class) ? 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);
//兩個對象名字相同(如果兩個對象名字有差異,則可以轉換進行匹配)
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();
}
}
//判斷是否是基本數據類型或者是String、Date(可能不全面,可能還有其他類型)
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;
}
}