Getting all the getter methods from a java bean

Hey,

I have a question. Suppose you have an unknown java bean given to you. Is there anyway by which you can add all these attributes to a label and without knowing the name of the attributes.

For example,


public class personBean
{
      String firstName;
      //... Other properties in the person Bean

      public void setFirstName(String firstName)
      {
            this.firstName = firstName;
      }
      
      public String getName()
      {
            return this.firstName; 
      }

       //...............More accessor methods for all other attributes included in personBean
}

Is there any way by which i can make a label containing all attributes/variables of an unknown java bean? Is there a particular method by which i can get a collection of all the methods used in the unknown bean object?

Any help would be greatly appreciated.

Cheers,
Rohith

You could use the bean methods provided in the java.beans package, for example Introspector.getBeanInfo(beanClass). But if you wanted to do it using some tools provided in Vaadin, you could use a BeanItem. Here is one solution:

StringBuffer beanValues = new StringBuffer();
BeanItem bi = new BeanItem(personBean);
for (Object propertyId : bi.getItemPropertyIds()) {
    beanValues.append(propertyId);
    beanValues.append("=");
    beanValues.append(bi.getItemProperty(propertyId).getValue());
    beanValues.append("\n");
}
Label values = new Label(beanValues.toString());

Be sure to check out the sources of MethodProperty, BeanItem and BeanItemContainer to see how it is done.

EDIT: I always forget that getValue() :slight_smile:

Thanks,

I will be sure to check that out.

Rohith