When working with Java Beans you may sometimes encounter a problem of inspecting a bean that you do not know. Suppose you wanted to find all of a bean’s properties. One way to do it is directly use Java Reflection API, searching for all the getter and setter methods, parsing their names for the property name, getting its type and so on. It can be however tedious and error-prone. Instead, you could use classes available in java.beans package and make them scan the bean class for you.

Without further ado, let’s see a simple example of using java.beans.Introspector class for scanning Java Beans. We will use following Java Bean:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
public class TestBean {
    private String name;
    private int beanId;
    public TestBean (int beanId, String name) {
        this.beanId=beanId;
        this.name=name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public int getBeanId() {
        return beanId;
    }
}

It isn’t actually a ‘real’ Java Bean (it does not implement Serializable for example) but for our needs it’s enough that it declares a few getter and setter methods. It has both a setter and a getter for field ‘name’, and only a getter for field ‘beanId’, which makes it a read-only field.

We will inspect this bean in the main method below, printing all of its properties. Inspected bean’s information will be stored in an object of class java.bean.BeanInfo:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
public class BeanInspection {
    public static void main(String[] args)
        throws IntrospectionException {

        //We use Inspector class to get a BeanInfo object
        //containing informations about the Java Bean class
        BeanInfo testBeanInfo =
            Introspector.getBeanInfo(TestBean.class);

        //Here we will print out all the discovered
        //property names of our bean
        for (PropertyDescriptor propertyDescriptor :
            testBeanInfo.getPropertyDescriptors()) {
            System.out.println("Property: " +
                propertyDescriptor.getName());
        }
    }
}

It will print the following output:

Property: beanId
Property: class
Property: name

What happened here? Why is there some ‘class’ property that we did not declare in our bean? TestBean, like any other class, extends java.lang.Object, which declares getClass() method. Introspector checks for all properties available in the class, also those declared in super-classes. But there is a way to tell the Introspector to search for properties in our bean, ignoring at the same time all the properties found in some super-class of the bean. We do it by specifying the super-class as the second parameter to the getBeanInfo method:

7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
        //This time we call getBeanInfo method with
        //two arguments. The second one is class
        //whose properties should be ignored during inspection.
        BeanInfo testBeanInfo =
            Introspector.getBeanInfo(TestBean.class, Object.class);
        for (PropertyDescriptor propertyDescriptor :
            testBeanInfo.getPropertyDescriptors()) {
            System.out.println("Property: " +
                propertyDescriptor.getName());
        }

It will ignore the ‘class’ property found in Object, hence printing out only ‘beanId’ and ‘name’.

Java Beans API is much more powerful than just finding Java Bean’s properties. To find out more about how it can be used, check out for example this page.