Introspection in Java Beans

Introspection:

The Introspector class provides several static methods that support introspection. Of most interest is getBeanInfo(). This method returns a BeanInfo object that can be used to obtain information about the Bean. It has the following syntax:

static BeanInfo getBeanInfo(Class<?> bean) throws IntrospectionException

PropertyDescriptor: This class describes a Bean property. It supports several methods that manage and describe properties. For example, you can determine if a property is bound by calling [java]isBound()[/java]. To determine if a property is constrained, call [java]isConstrained()[/java]. You can obtain the name of the property by calling [java]getName()[/java].

EventSetDescriptor: This class represents a Bean event. It supports several methods that obtain the methods that a Bean uses to add or remove event listeners, and to otherwise manage events. For example, to obtain the method used to add listeners, call [java]getAddListenerMethod()[/java]. To obtain the method used to remove listeners, call [java]getRemoveListenerMethod()[/java]. To obtain the type of a listener, call [java]getListenerType()[/java]. You can obtain the name of an event by calling [java]getName()[/java].

MethodDescriptor: This class represents a Bean method. To obtain the name of the method, call [java]getName()[/java]. You can obtain information about the method by calling [java]getMethod()[/java], shown here:
[java]Method getMethod()
[/java]

Program:


import java.awt.*;
import java.awt.event.*;
import java.io.Serializable;
public class Colors extends Canvas implements Serializable
{
transient private Color color;
private boolean rectangular;
public Colors() {
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
change();
}
});
rectangular = false;
setSize(200, 100);
change();
}
public boolean getRectangular() {
return rectangular;
}
public void setRectangular(boolean flag) {
this.rectangular = flag;
repaint();
}
public void change() {
color = randomColor();
repaint();
}
private Color randomColor() {
int r = (int)(255*Math.random());
int g = (int)(255*Math.random());
int b = (int)(255*Math.random());
return new Color(r, g, b);
}
public void paint(Graphics g)
{
Dimension d = getSize();
int h = d.height;
int w = d.width;
g.setColor(color);
if(rectangular) {
g.fillRect(0, 0, w-1, h-1);
}
else
{
g.fillOval(0, 0, w-1, h-1);
}
}
}