Java Bean Introspection
Introspection
It is the self-examination of one’s conscious thoughts and feelings. [reference: Wikipedia]
Java Bean Introspection
It is the examination provided by a Java Bean class! But a class cannot speak. A developer has to write the description about the bean so that Other developers can understand the Bean properties/methods/events etc.
In short, The process to describe a Bean is known as Bean Introspection.
Which is done by two ways:
- Naming Conventions(What we did in previous tutorial – Link)
- By writing an additional class that extends the BeanInfo interface
In Brief:
1. Naming Conventions.
- Simple/Single Property:
private int id; public void setId(int id) { this.id = id; } public String getName() { return name; }
- Indexed Property:
private double data[ ]; public double getData(int index) { return data[index]; } public void setData(int index, double value) { data[index] = value; } public double[ ] getData( ) { return data; } public void setData(double[ ] values) { data = new double[values.length]; System.arraycopy(values, 0, data, 0, values.length); }
- Events:
public void addEventListener(EventListener e) { // code } public void removeEventListener(EventListener e) { // code }
2. By writing an additional class that extends the BeanInfo interface
The BeanInfo interface enables us to explicitly control what information is available in a Bean. The BeanInfo interface defines following:
PropertyDescriptor[ ] getPropertyDescriptors( ) EventSetDescriptor[ ] getEventSetDescriptors( ) MethodDescriptor[ ] getMethodDescriptors( )
Now we are going to describe all the properties of Employee.class by using java.beans.SimpleBeanInfo(Derived from BeanInfo Interface):
- Property Descriptor:
import java.beans.SimpleBeanInfo; public class EmployeeBeanInfo extends SimpleBeanInfo { public PropertyDescriptor[] getPropertyDescriptors() { try { PropertyDescriptor pdId = new PropertyDescriptor("id", Employee.class); PropertyDescriptor pdName = new PropertyDescriptor("name", Employee.class); PropertyDescriptor pdContact = new PropertyDescriptor("contact", Employee.class); PropertyDescriptor pd[] = {pdId, pdName, pdContact}; return pd; } catch(Exception e) { System.out.println("Exception caught. " + e); } return null; } }
Same way we can describe Events and Methods of the Bean class.
Recent Comments