BDK Introspection

A bean builder usually uses Java core reflection API to discover methods of a bean. It then applies the design pattern to discover other bean features (such as properties and events). This procedure is known as introspection.

1. Design Patterns

The JavaBeans framework introduces a lot of rules for names to be used for classes and methods. They are collectively called design patterns.

Note that the conventions and design patterns are all optional. However, by following those conventions, we can create really useful beans that can be used within a builder tool as well as in other JavaBean-enabled environments. Builder tools such as BeanBox, NetBeans, and JBuilder use these conventions and design patterns to introspect a bean’s properties, methods, and events. Consequently, they can provide an environment where we can customize bean features.

Suppose, a bean has a property called “xxx”. JavaBean technology encourages us to use “getXxx” as the name of the reader method and “setXxx” as the name of the writer method. If the property happens to be a Boolean, the reader method could be named “isXxx”. For example, our State bean has the property state. So, the reader method is “getState” and the writer method is “setState”.

A Beaninfo class is a class that is used to provide infor^ration about a bean explicitly. To do this, a class is created implementing the Beaninfo interface. The name of this class should also follow a naming rule. The name of the Beaninfo class must be the name of the target bean, followed by the string “Beaninfo”. For example, the name of the Beaninfo class for the bean Person must be “PersonBeanInfo”.

Similarly, the name of the Customizer class (see Section 26.9) for the bean MyBean must be MyBeanCustomizer.

JavaBean API provides interfaces and classes that can be used to discover properties and methods of a bean dynamically. The following is a brief description of those interfaces and classes:

Beaninfo

The java.beans.Beaninfo interface defines a set of methods that allow bean developers to provide information about their beans explicitly. By specifying Beaninfo for a bean component, a developer can hide methods, specify an icon for the toolbox, provide descriptive names for properties, define which properties are bound, etc.

Introspector

The java.beans.introspector class provides descriptor classes (see Section 26.7) with information about properties, methods, and events of a bean.

The getBeaninfo() method of the Introspector class can be used by builder tools and other automated environments to provide detailed information about a bean. The getBeaninfo() method relies on the naming conventions for the bean’s properties, events, and methods. A call to getBeaninfo() results in the introspection process analyzing the bean’s classes and super classes. The following example finds the properties and methods of our Factorial bean.

//IntrospectionDemo.java

import java.beans.*;

public class IntrospectionDemo {

public static void main( String[] args ) throws IntrospectionException {

Beaninfo info = Introspector.getBeanInfo( Factorial.class,

Object.class );

System.out.println(”properties: ”);

for ( PropertyDescriptor pd : info.getPropertyDescriptors() )

System.out.println(” ” + pd.getName());

System.out.println(”methods: ”);

for ( MethodDescriptor pd : info.getMethodDescriptors() ) System.out.println(” ” + pd.getName());

}

}

Figure 26.1: Bean Introspection

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *