One of the most critical requirements of JavaBeans is that it provide a way for Beans to expose information about themselves.
This is important because application builder tools and developers need to be able to take a prepackaged Bean and have a consistent and preferably automated way to find out how it works.
The mechanism that exposes the structure and functionality of a Bean to the outside world is called introspection.
There are two ways that Beans support introspection:
By conforming to JavaBeans design patterns
By explicitly providing information about themselves
Definition of Introspection
In the next lesson, you learn how design patterns are used to provide automatic introspection capabilities.
The BeanInfo Interface
Introspection is a mechanism for providing information about a Bean explicitly. This is done by creating a class that exists specifically to describe the Bean. This class implements the java.beans.BeanInfo interface, which specifies a set of methods that can be used to gather various pieces of information about a Bean. The methods defined by this interface are shown in Table 5-2.
Reflection versus Introspection
Everything about our beans has been determined by low-level reflection, that is, by looking at the methods of our classes.
The java.Beans.Introspector class gathers information on a bean using reflection, then analyzes and describes a bean to any tool that wants to know about it.
The introspection process works only if the class follows the JavaBeans naming conventions for its methods; furthermore, it gives us little control over exactly what properties and events appear in NetBeans menus.
For example, we have seen that NetBeans by default shows all the stuff we inherit from the base Swing component.
We can change that by creating BeanInfo classes for our beans. A BeanInfo class provides the JavaBeans introspector with explicit information about the properties, methods, and events of a bean; we can even use it to customize the text that appears in menus in NetBeans (and in other IDEs).
A BeanInfo class implements the BeanInfo interface. That is a complicated proposition; in most situations, the introspector's default behavior is reasonable. Instead of
implementing the BeanInfo interface, we extend the SimpleBeanInfo class, which implements all of BeanInfo's methods.
We can override specific methods to provide the information we want; when we do not override a method, we will get the introspector's default behavior.
Introspection: the mechanism that exposes the structure and functionality of a Bean to the outside world.