[ 
https://issues.apache.org/jira/browse/BEANUTILS-526?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Marco Janc updated BEANUTILS-526:
---------------------------------
    Description: 
The function _BeanUtils.copyProperty(bean, name, value)_ shall instantiate 
nested properties before copying them.

 

*Example:*

 
{code:java}
class Foo
{
 Bar bar;
}

class Bar
{
 int id;
}
{code}
 

 

We now have an instance of _Foo_ where _bar_ property is null and call:

 
{code:java}
Foo foo = new Foo();
BeanUtils.copyProperty(foo, "bar.id", 3);
{code}
This will throw an exception currently.

 

*Workaround*

In my generic copy method, i instantiate all required nested properties before. 
Here my implementation. In BeanUtils this could be done more lazy when the 
target is null.

 
{code:java}
        /**
         * Instantiates the nested properties of the given Object with proxy 
Objects
         * with the given property fieldname.
         *
         * @param obj
         * @param fieldName
         *
         * @throws NoSuchMethodException
         * @throws InvocationTargetException
         * @throws IllegalAccessException
         * @throws InstantiationException
         */
        public static void instantiateNestedProperties(final Object obj, final 
String fieldName)
                throws IllegalAccessException, InvocationTargetException, 
NoSuchMethodException, InstantiationException
        {
                final String[] fieldNames = fieldName.split("\\.");
                if (fieldNames.length == 1)
                        return;         final StringBuffer nestedProperty = new 
StringBuffer();         // outsource temporary variables to optimize performance
                String fn = null;
                Object value = null;
                PropertyDescriptor propertyDescriptor = null;
                Class<?> propertyType = null;
                Object newInstance = null;              for (int i = 0; i < 
fieldNames.length - 1; i++)
                {
                        fn = fieldNames[i];
                        if (i != 0)
                                nestedProperty.append(".");
                        nestedProperty.append(fn);                      value = 
PropertyUtils.getProperty(obj, nestedProperty.toString());                      
if (value == null)
                        {
                                propertyDescriptor = 
PropertyUtils.getPropertyDescriptor(obj, nestedProperty.toString());
                                propertyType = 
propertyDescriptor.getPropertyType();
                                newInstance = propertyType.newInstance();
                                PropertyUtils.setProperty(obj, 
nestedProperty.toString(), newInstance);
                        }
                }
        }
{code}
 

 

 

 

  was:
The function _BeanUtils.copyProperty(bean, name, value)_ shall instantiate 
nested properties before copying them.

 

*Example:*

 

 
{code:java}

{code}
*class Foo*
*{*
    *Bar bar;*
*}*

*class Bar*
*{*
    *int id;*
*}*

 

We now have an instance of _Foo_ where _bar_ property is null and call:

 
{code:java}
Foo foo = new Foo();
BeanUtils.copyProperty(foo, "bar.id", 3);
{code}
This will throw an exception currently.

 

*Workaround*

In my generic copy method, i instantiate all required nested properties before.

 

 


> copyProperty should instantiate nested properties
> -------------------------------------------------
>
>                 Key: BEANUTILS-526
>                 URL: https://issues.apache.org/jira/browse/BEANUTILS-526
>             Project: Commons BeanUtils
>          Issue Type: Bug
>          Components: Bean / Property Utils
>    Affects Versions: 1.9.4
>            Reporter: Marco Janc
>            Priority: Major
>
> The function _BeanUtils.copyProperty(bean, name, value)_ shall instantiate 
> nested properties before copying them.
>  
> *Example:*
>  
> {code:java}
> class Foo
> {
>  Bar bar;
> }
> class Bar
> {
>  int id;
> }
> {code}
>  
>  
> We now have an instance of _Foo_ where _bar_ property is null and call:
>  
> {code:java}
> Foo foo = new Foo();
> BeanUtils.copyProperty(foo, "bar.id", 3);
> {code}
> This will throw an exception currently.
>  
> *Workaround*
> In my generic copy method, i instantiate all required nested properties 
> before. Here my implementation. In BeanUtils this could be done more lazy 
> when the target is null.
>  
> {code:java}
>       /**
>        * Instantiates the nested properties of the given Object with proxy 
> Objects
>        * with the given property fieldname.
>        *
>        * @param obj
>        * @param fieldName
>        *
>        * @throws NoSuchMethodException
>        * @throws InvocationTargetException
>        * @throws IllegalAccessException
>        * @throws InstantiationException
>        */
>       public static void instantiateNestedProperties(final Object obj, final 
> String fieldName)
>               throws IllegalAccessException, InvocationTargetException, 
> NoSuchMethodException, InstantiationException
>       {
>               final String[] fieldNames = fieldName.split("\\.");
>               if (fieldNames.length == 1)
>                       return;         final StringBuffer nestedProperty = new 
> StringBuffer();         // outsource temporary variables to optimize 
> performance
>               String fn = null;
>               Object value = null;
>               PropertyDescriptor propertyDescriptor = null;
>               Class<?> propertyType = null;
>               Object newInstance = null;              for (int i = 0; i < 
> fieldNames.length - 1; i++)
>               {
>                       fn = fieldNames[i];
>                       if (i != 0)
>                               nestedProperty.append(".");
>                       nestedProperty.append(fn);                      value = 
> PropertyUtils.getProperty(obj, nestedProperty.toString());                    
>   if (value == null)
>                       {
>                               propertyDescriptor = 
> PropertyUtils.getPropertyDescriptor(obj, nestedProperty.toString());
>                               propertyType = 
> propertyDescriptor.getPropertyType();
>                               newInstance = propertyType.newInstance();
>                               PropertyUtils.setProperty(obj, 
> nestedProperty.toString(), newInstance);
>                       }
>               }
>       }
> {code}
>  
>  
>  
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to