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] Determine type of bean field
Date Fri, 02 Jun 2006 03:13:11 GMT
Ugh, what a pain in the butt... I *finally* got it working though.  See, 
give me enough time and I eventually stumble the right way :)  I make no 
claim that this is the most elegant approach, but it does have the 
virtue of actually *working*...

List fieldValues = new ArrayList();
fieldValues.add("test");
Object theArray = PropertyUtils.getProperty(bean, fieldName);
List fv = (List)fieldValues;
Class arrayClass = theArray.getClass();
Class componentClass = arrayClass.getComponentType();
theArray = Array.newInstance(componentClass, fv.size());
int i = 0;
for (Iterator fvit = fv.iterator(); fvit.hasNext();) {
   Array.set(theArray, i, fvit.next());
   i++;
}
PropertyUtils.setProperty(bean, fieldName, theArray);

Hope that helps someone in the future, after all the banging my head 
against the wall I did :)

Frank

Frank W. Zammetti wrote:
> On Thu, June 1, 2006 4:54 pm, Craig McClanahan wrote:
>>> Ok, I'm with you so far... but what if you don't know the type of the
>>> target array until runtime?  I need to dynamically determine the element
>>> type of the target array, then construct a new array instance to pass to
>>> toArray() here.  I'm messing around right now, but haven't stumbled on
>>> the
>>> right answer yet...
>>
>> I imagine you would have to synthesize the class for "array of Foo", and
>> then ask it for a newInstance() to create the calling argument ... check
>> methods in the java.util.Arrays class for useful ways to do that.
>>
>> Of course, you won't be able to cast the actual result instance (defaults
>> to
>> Object[]) if you don't know the type at compile time.
> 
> Well, *that* doesn't sound like much fun! :(  Nah, actually, in a way it
> does!  I'm playing with it right now.  Thanks again, everyone!
> 
>> Craig
> 
> Frank
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 
> 
> 

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM: fzammetti
Yahoo: fzammetti
MSN: fzammetti@hotmail.com
Java Web Parts -
http://javawebparts.sourceforge.net
Supplying the wheel, so you don't have to reinvent it!

---------------------------------------------------------------------
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