John Olmstead wrote:
Ladies and Gentlemen;
I have a requirement to develope a custom tag that will have passed to it a
collection of
beans, object type unknown, and render the bean fields as a header in an html
table and the
bean parameters in the rest of the table. This tag would have many
oppertunities for use
throughout the application, hence the desire to write the code in a reuseable
class.
Of coarse , this is trivial in JSTL if I know the bean object type in the collection that is
passed, but not knowing the object type certainly makes it more challenging.
Clearly, reflection would be involved to get the parameters and the field names, but, If I
can't cast the object being retrieved from the collection, I don't think using reflection on a
generic Object instance is going to help me.
Hmmm, I'm not quite sure what you're asking. However, this kind of
thing is fairly easy with the standard java.beans package. For example:
Object bean = ...;
BeanInfo beanInfo = Introspector.getBeanInfo( bean.getClass() );
PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
for ( int i = 0; i < properties.length; i++ )
{
Class propertyType =
properties[i].getPropertyType();
Object propertyValue =
properties[i].getReadMethod().invoke( bean, new Object[0] );
}
Hope that helps!
Thanks,
Eric
|