commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 33109] New: - [BeanUtils]Copy bean properties with different names
Date Sat, 15 Jan 2005 01:34:04 GMT
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG·
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=33109>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND·
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=33109

           Summary: [BeanUtils]Copy bean properties with different names
           Product: Commons
           Version: Nightly Builds
          Platform: PC
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Bean Utilities
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: r.u.c.afonso@uol.com.br


In avaliable BeanUtils.copyProperties() implementation it is necessary that
properties names of origin and destiny bean must be equals. My propose is
include a overloading method in BeanUtils that receives - beyond destiny and
origin beans - a two dimensional String array with two columns. This array will
have name of properties to be copied - destiny property in first column, origin
property in second column.  

Code Implementation:

    private static final int TO = 0;

    private static final int FROM = 1;

    /**<p>Copy property values from the origin bean to the destination bean 
     * for cases where properties names are different. A bidimentional array 
     * provides properties names: destiny bean properties in first column and
     * origin bean properties in secon column. To be added to 
     * org.apache.commons.beanutils.BeanUtils class.  
     * 
     * @param dest
     * @param orig
     * @param toFrom
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     */
    public static void copyProperties(Object dest, Object orig,
            String[][] toFrom) throws IllegalAccessException,
            InvocationTargetException, NoSuchMethodException {
        BeanUtilsBean beanUtilsBean = BeanUtilsBean.getInstance();
        for (int i = 0; i < toFrom.length; i++) {
            String[] strings = toFrom[i];
            Object value = beanUtilsBean.getProperty(orig, strings[FROM]);
            beanUtilsBean.setProperty(dest, strings[TO], value);
        }
    }

Usage:

        BeanFrom from = new BeanFrom(10, "my value"); // properties: keyFrom
(int), valueFrom (String)
        BeanTo to = new BeanTo(); // properties: keyTo (int), valueTo (String) 
        String[][] table = { { "keyTo", "keyFrom" }, { "valueTo", "valueFrom" } };
       copyProperties(b, a, table);

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message