commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Frank W. Zammetti" <fzli...@omnytex.com>
Subject Re: [BeanUtils] Handling array creation?
Date Thu, 08 Sep 2005 14:26:05 GMT
On Thu, September 8, 2005 12:16 am, Niall Pemberton said:
> Sorry Frank, but I don't think you have to worry about casting at all. Say
> you've collected all your values into a List (myList), as Thomas suggested
> -
> then all you need to do is something like the following:

Why are you sorry?!?  This is great! :)  I never mind someone pointing out
a better way to do something to me, believe me :)

Although, I tried something startingly similar last night and found it to
not work...  But I could easily have missed one step here, or otherwise
done something wrong, so I'll be off to try this in a little while.  I'll
thank you very much in advance and just assume this is going to work, I
have no doubt it will :)

Frank

> Class type = PropertyUtils.getPropertyType(obj, "myArray");
>
> // Array Processing
> if (type.isArray()) {
>
>     Class componentType = type.getComponentType();
>
>     // Create new instance of the array
>     Object myArray = Array.newInstance(componentType, myList.size());
>
>     // Get the converter
>     Converter converter = ConvertUtils.lookup(componentType);
>
>     // populate array values
>     for (int i = 0; i < myList.size(); i++) {
>
>         // Get & Convert the value
>         Object value = converter.convert(componentType,  myList.get(i));
>
>         // Set the value in the array
>         Array.set(myArray, i, value);
>
>     }
>
>     // set the beans property
>     BeanUtils.setProperty(obj, "myArray", myArray);
>
> } else {
>
>     // ? handle error ?
>
> }
>
> ----- Original Message -----
> From: "Frank W. Zammetti" <fzlists@omnytex.com>
> Sent: Thursday, September 08, 2005 4:33 AM
>
> <snip>
>> Yep, knew about these already... unfortunately, the one thing that would
>> make this a piece of cake is not available here, or in Java t all:
>> dynamic casting.  If you could do that, it would be trivial to do
>> something like:
>>
>> Class type = PropertyUtils.getPropertyType(obj, "myArray");
>> Object v = Array.newInstance(type, values.size());
>> BeanUtils.setProperty(obj, "myArray",
>> values.toArray((type.getName())v));
>
>>
>> That still assumes that getName() returned the type in a useable form,
>> which of course it doesn't anyway (which complicates my above if block,
>> but I digress), so my pipe dream is even more so :)
> </snip>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>


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


Mime
View raw message