利用反射將一個物件的值轉存到另一物件
阿新 • • 發佈:2019-02-17
工作要求,要將儲存在原物件中的值部分轉存到另一物件,沒有利用反射前的程式碼為:
public NewLCContTable replace(LCContTable mLcContTable){ NewLCContTable newlc=new NewLCContTable(); newlc.setAgentCode(mLcContTable.getAgentCode()); newlc.setAgentCom(mLcContTable.getAgentCom()); newlc.setCInValiDate(mLcContTable.getCInValiDate()); newlc.setCInValiTime(mLcContTable.getCInValiTime()); newlc.setContNo(mLcContTable.getContNo()); newlc.setCValiDate(mLcContTable.getCInValiDate()); newlc.setCValiTime(mLcContTable.getCInValiTime()); newlc.setManageCom(mLcContTable.getManageCom()); newlc.setPayIntv(mLcContTable.getPayIntv()); newlc.setPayMode(mLcContTable.getPayMode()); newlc.setPolApplyDate(mLcContTable.getPolApplyDate()); newlc.setPrtNo(mLcContTable.getPrtNo()); newlc.setSaleChnl(mLcContTable.getSaleChnl()); return newlc; }
利用反射節省了set get重複書寫:
public class Clone { public static void main(String[] args) { LCContSchema lcContSchema=new LCContSchema(); lcContSchema.setPrtNo("00000"); lcContSchema.setContNo("123456"); LCPolSchema lcPolSchema=new LCPolSchema(); lcPolSchema.setOperator("123"); clone(lcContSchema, lcPolSchema); System.out.println(lcPolSchema.getPrtNo()); System.out.println(lcPolSchema.getContNo()); System.out.println(lcPolSchema.getOperator()); System.out.println(lcContSchema.getOperator()); System.out.println(lcPolSchema); } public static Object clone(Object old,Object news){ Object o1=news; Method [] m1=news.getClass().getMethods(); for(Method m:m1){ String methodname=m.getName(); if(methodname.indexOf("set")!=-1){ String name=methodname.replaceAll("set", "get"); Method m2=null; try { m2=old.getClass().getMethod(name, null); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { continue; } System.out.println(m.getName()); System.out.println("old:"+m2.getName()); try { Object o2= m2.invoke(old, null); System.out.println(o2!=null); if(o2!=null){ m.invoke(news, m2.invoke(old, null)); } } catch (Exception e) { continue; } } } return o1; } }
05-27
開了下api修改了下程式碼
/** * * @param a news * @param b old * @return */ public static Object transFields(Object a, Object b) { Class c1 = a.getClass(); Class c2 = b.getClass(); Field[] fieldsDest = c1.getDeclaredFields(); Field[] fieldsOrg = c2.getDeclaredFields(); AccessibleObject.setAccessible(fieldsDest, true); //取消訪問控制檢查 AccessibleObject.setAccessible(fieldsOrg, true); for (int i = 0; i < fieldsDest.length; i++) { Field f = fieldsDest[i]; Class type = f.getType(); String name = f.getName(); String typeName = type.getName(); System.out.println("name:"+name); System.out.println("typename:"+typeName); if (name.equals("FIELDNUM") || name.equals("PK") || name.equals("mErrors") || name.equals("fDate")) { continue; } for (int j = 0; j < fieldsOrg.length; j++) { //得到資料來源的資料 Field f1 = fieldsOrg[j]; Class type1 = f1.getType(); String name1 = f1.getName(); String typeName1 = type.getName(); System.out.println("name1:"+name); System.out.println("typename1:"+typeName); if (name.equals("FIELDNUM") || name.equals("PK") || name.equals("mErrors") || name.equals("fDate")) { continue; } //賦值轉換 if ((typeName.equals(typeName1)) && (name1.equals(name))) { switch (transType(typeName)) { case 3: try { f.setDouble(a, f1.getDouble(b)); } catch (Exception e) { e.printStackTrace(); } break ; case 5: try { f.setInt(a, f1.getInt(b)); } catch (Exception e) { e.printStackTrace(); } break ; case 93: try { f.set(a, f1.get(b)); } catch (Exception e) { e.printStackTrace(); } break ; default: try { f.set(a, f1.get(b)); } catch (Exception e) { e.printStackTrace(); } } break; } } } return a; } private static int transType(Object type) { int typecode; typecode = 93; if (type.equals("java.lang.String")) { typecode = 93; } if (type.equals("double")) { typecode = 3; } if (type.equals("int")) { typecode = 5; } return typecode; }