java对象属性复制

来源:互联网 发布:百度的算法 编辑:程序博客网 时间:2024/05/17 06:24
public static void copy(Object source, Object dest)    {        Method s[] = source.getClass().getMethods();        Method d[] = dest.getClass().getMethods();        try        {            for (int i = 0; i < d.length; i++)            {                String dname = d[i].getName();                String dformat = dname.substring(3);                if (dname.startsWith("set"))                {                    for (int j = 0; j < s.length; j++)                    {                        String sname = s[j].getName();                        String sformat = sname.substring(3);                        if                        (sname.startsWith("get") && sformat.equals(dformat))                        {                            Object value = null;                            value = s[j].invoke(source);                            Object[] args = new Object[1];                            args[0] = value;                            d[i].invoke(dest, args);                        }                    }                }            }        }        catch (IllegalAccessException e)        {        log.debug(e.getMessage(),e);            // TODO        }        catch (InvocationTargetException e)        {            // TODO        }    }

原创粉丝点击