Sorry Bob,  I didn't look at your code in enough detail!

Ok, I've had a bit more of an investigation, and i think i can see where it
is falling down based on the message you're getting in the log, here is the
snippet out of BeanUtils.copyProperty():

lines 393-411:

            PropertyDescriptor descriptor = null;
            try {
                descriptor =
                    PropertyUtils.getPropertyDescriptor(target, name);
                if (descriptor == null) {
                    return; // Skip this property setter
                }
            } catch (NoSuchMethodException e) {
                return; // Skip this property setter
            }
            type = descriptor.getPropertyType();
            if (type == null) {
                // Most likely an indexed setter on a POJB only
                if (log.isTraceEnabled()) {
                    log.trace("    target type for property '" +
                              propName + "' is null, so skipping ths
setter");
                }
                return;
            }

Therefore, it appears that the property descriptor is saying that there is
no property type for the nominated property.  After looking up the
java.beans.PropertyDescriptor classes' javadoc for getPropertyType(), I
think things are becoming a little clearer:

getPropertyType

public Class getPropertyType()

    Gets the Class object for the property.

    Returns:
        The Java type info for the property. Note that the "Class" object
may describe a built-in Java type such as "int". The result may be "null"
if this is an indexed property that does not support non-indexed access.

        This is the type that will be returned by the ReadMethod.

So, indexed properties do no return a property type if the non-indexed
getter method does not exist.  It actually says that indexed properties can
be overloaded in the java.beans.IndexedPropertyDescriptor constructor, but
I don't think that this is supported in BeanUtils (but I can't find where i
read that now!).

So, long story short, I think that it brings us back to the documentation
of the Beanutils.copyProperty() method:

* Does not support destination properties that are indexed, but only an
indexed setter (as opposed to an array setter) is available.

I was mis-interpretting this to mean that indexed properties are only
copied if they use the indexed property methods, i.e.Object getProp(int)
and setProp(int, Object).  However, after reading this other information, I
think what it is saying is that indexed properties will ONLY be copied if
you provide straight non-indexed property methods, i.e. Object[]
getProp(int) and setProp(Object[]).  It is clearer I think if the
documentation read:

* Does not support destination properties that are indexed AND ONLY an
indexed setter (as opposed to an array setter) is available.

Therefore, change the getListValues methods to use arrays as opposed to
Lists and you should be right to go using the property name "listValues[i]"
as opposed to "listValue[i]".

Cheers!

Trent McClenahan



|---------+---------------------------->
|         |           "Butash, Bob"    |
|         |           <[EMAIL PROTECTED]|
|         |           om>              |
|         |                            |
|         |           14/05/2004 21:16 |
|         |           Please respond to|
|         |           "Jakarta Commons |
|         |           Users List"      |
|         |                            |
|---------+---------------------------->
  
>-------------------------------------------------------------------------------------------------------------------------------|
  |                                                                                    
                                           |
  |       To:       "'Jakarta Commons Users List'" <[EMAIL PROTECTED]>                 
                             |
  |       cc:                                                                          
                                           |
  |       Subject:  RE: Question: BeanUtils.copyProperty(Object bean, String name, Ob  
     ject value)                           |
  
>-------------------------------------------------------------------------------------------------------------------------------|




Trent,

Thanks but I was not overloading originally... I was using a getListValues
&
getListValue.  I know this is not best practice, it is hard to see the
difference, this code is more a derivative of trying to get it to work than
it is in a final state.

Any other pointers would be welcomed.

Thanks

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Thursday, May 13, 2004 8:16 PM
To: Jakarta Commons Users List
Subject: Re: Question: BeanUtils.copyProperty(Object bean, String name,
Object value)






G'day Bob,

I think I see what your problem is.  You have overloaded the ListValue
property getters and setters.  This is not supported in BeanUtils.  Instead
you should structure your class like this:

public class IndexedBean {

    private List items;

    public IndexedBean()
    {
        this.items = new ArrayList();
    }

    // simple getter and setter for list - these are only necessary if you
    // plan on copying the entire bean using BeanUtils.copyProperties(src,
dest)

    public List getItems()
    {
        return this.items;
    }

    public void setItems(List items)
    {
        this.items = items;
    }

    // indexed getter and setter - different name - these methods are used
by
    // BeanUtils.copyProperty(obj, name, value) so that it can determine
what
    // converter to apply to the value nominated so that it fits the
indexed
property

    public Object getItemValue(int index)
    {
        return getItems().get(index);
    }

    public void setItemValue(int index, Object object)
    {
        getItems().add(index, object);
    }
}

Call BeanUtils.copyProperty() like you had before, ensuring that you use
the
"itemValue" property, not the "items" property:

BeanUtils.copyProperty(toBean, "itemValue" + "[" +  i + "]",
Array.get(indexedObject, i));

Hope this helps!

Cheers,

Trent McClenahan

Enterprise Web Services
Phone: (02) 9466 7763
Email: [EMAIL PROTECTED]



|---------+---------------------------->
|         |           "Butash, Bob"    |
|         |           <[EMAIL PROTECTED]|
|         |           om>              |
|         |                            |
|         |           14/05/2004 00:02 |
|         |           Please respond to|
|         |           "Jakarta Commons |
|         |           Users List"      |
|         |                            |
|---------+---------------------------->

>---------------------------------------------------------------------------

----------------------------------------------------|
  |
|
  |       To:       "'[EMAIL PROTECTED]'"
<[EMAIL PROTECTED]>                                         |
  |       cc:
|
  |       Subject:  Question: BeanUtils.copyProperty(Object bean, String
name, Object        value)                               |

>---------------------------------------------------------------------------

----------------------------------------------------|




Looking for help, I'm trying to copy a property from an indexed value to an
indexed value.  The snippet of code that I'm trying to do this with is:

BeanUtils.copyProperty(toBean, fieldName + "[" +  i + "]",
Array.get(indexedObject, i));

Now looking at the JavaDoc I do see the warning:
    Does not support destination properties that are indexed, but only an
indexed setter
    (as opposed to an array setter) is available.

This warning is not clear to me, not sure what they are trying to say.  I
do
notice in the parameter description it states that the name Property name
(can be nested/indexed/mapped/combo).

I'm not getting an error, the property is just skipped, the info in the log
is:
copyProperty([EMAIL PROTECTED]


3c, listValue[0], 1)
DEBUG [main] (BeanUtils.java:407) -     target type for property
'listValue'
is null, so skipping ths setter

I have the following accessors for the bean in question:

             private List listValues;

             public List getListValues()
             {
                         if(listValues == null)
                         {
                                     listValues = new ArrayList();
                         }
                         return listValues;
             }

             public Object getListValue(int index)
             {
                         return getListValues().get(index);
             }

             public void setListValues(List list)
             {
                         listValues = list;
             }

             public void setListValue(int index, Object object)
             {
                         getListValues().add(index, object);
             }


Am I calling this method incorrectly for an indexed field?  Does this
method
not support indexed fields at all???


Any help would be greatly appreciated....Thanks



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to