[
https://issues.apache.org/jira/browse/BEANUTILS-198?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13601659#comment-13601659
]
Benedikt Ritter commented on BEANUTILS-198:
-------------------------------------------
Would be better to have something like
{code}
public interface PropertyMapper {
String getTarget(String sourcePropName);
}
{code}
And pass this to {{copyProperties}} instead of an array. A straight forward
implemenation that the existing {{copyProperties}} method could than delegate
to would be:
{code}
public void copyProperties(Object dest, Object orig) {
copyProperties(dest, orig, new PropertyMapper() {
public String getTarget(String sourcePropName) {
return sourcePropName;
}
}
}
{code}
> [beanutils] Copy bean properties with different names
> -----------------------------------------------------
>
> Key: BEANUTILS-198
> URL: https://issues.apache.org/jira/browse/BEANUTILS-198
> Project: Commons BeanUtils
> Issue Type: Improvement
> Components: Bean / Property Utils
> Affects Versions: Nightly Builds
> Environment: Operating System: All
> Platform: PC
> Reporter: Rafael U. C. Afonso
> Priority: Minor
> Fix For: LATER THAN 1.8.4
>
> Attachments: ASF.LICENSE.NOT.GRANTED--BeanFrom.java,
> ASF.LICENSE.NOT.GRANTED--BeanTo.java,
> ASF.LICENSE.NOT.GRANTED--CopyPropertiesWithDifferentNames.java
>
>
> 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);
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira