Introspection is the automatic process of analyzing a bean's design patterns to reveal the bean's properties, events, and methods. This process controls the publishing and discovery of bean operations and properties. This lesson explains the purpose of introspection, introduces the Introspection API, and gives an example of introspection code.
A growing number of Java object repository sites exist on the Internet in answer to the demand for centralized deployment of applets, classes, and source code in general. Any developer who has spent time hunting through these sites for licensable Java code to incorporate into a program has undoubtedly struggled with issues of how to quickly and cleanly integrate code from one particular source into an application.
The way in which introspection is implemented provides great advantages, including:
The
BeanInfo
(in the API reference documentation) interface of the java.beans
package
defines a set of methods that allow bean implementors to
provide explicit information about their beans. 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 properties, and
much more.
The
getBeanInfo(beanName)
(in the API reference documentation) of the
Introspector
(in the API reference documentation) 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 beans classes and superclasses.
The Introspector
class provides descriptor classes with information about
properties, events, and methods of a bean. Methods of this class locate any
descriptor information that has been explicitly supplied by the developer through
BeanInfo
classes. Then the Introspector
class applies the naming conventions to determine
what properties the bean has, the events to which it can listen, and those which
it can send.
The following figure represents a hierarchy of the FeatureDescriptor
classes:
Each class represented in this group describes a particular attribute of the bean.
For example, the isBound
method of the
PropertyDescriptor
class indicates whether a PropertyChangeEvent
event is fired when
the value of this property changes.
To open the BeanInfo dialog box, expand the appropriate class hierarchy to the bean Patterns node. Right-click the bean Patterns node and choose BeanInfo Editor from the pop-up menu. All elements of the selected class that match bean-naming conventions will be displayed at the left in the BeanInfo Editor dialog box as shown in the following figure:
Select one of the following nodes to view and edit its properties at the right of the dialog box:
Special symbols (green and red) appear next to the subnode to indicate whether an element will be
included or excluded from the BeanInfo
class.
If the Get From Introspection option is not selected, the node's subnodes are
available for inclusion in the BeanInfo
class. To include all subnodes,
right-click a node and choose Include All. You can also include each element individually
by selecting its subnode and setting the Include in BeanInfo property.
If the Get From Introspection option is selected, the setting the properties of
subnodes has no effect in the generated BeanInfo
code.
The following attributes are available for the nodes for each bean, property, event sources, and method:
BeanInfo
class.
For Event Source nodes, the following Expert properties are available:
import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; public class SimpleBean { private final String name = "SimpleBean"; private int size; public String getName() { return this.name; } public int getSize() { return this.size; } public void setSize( int size ) { this.size = size; } public static void main( String[] args ) throws IntrospectionException { BeanInfo info = Introspector.getBeanInfo( SimpleBean.class ); for ( PropertyDescriptor pd : info.getPropertyDescriptors() ) System.out.println( pd.getName() ); } }
This example creates a non-visual bean and displays the following properties derived
from the BeanInfo
object:
class
name
size
Note that a class
property was not
defined in the SimpleBean
class. This property was inherited from the
Object
class. To get properties defined only in the SimpleBean
class, use the following form of the getBeanInfo
method:
Introspector.getBeanInfo( SimpleBean.class, Object.class );