利用反射将一个对象的值转存到另一对象

来源:互联网 发布:json的解析方法 安卓 编辑:程序博客网 时间:2024/06/07 12:43

工作要求,要将存储在原对象中的值部分转存到另一对象,没有利用反射前的代码为:

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;      }


阅读全文
0 0
原创粉丝点击