Tuesday, June 5, 2012

JavaBeans Introspection

Java Bean are plain old java classes defined as per the patterns laid out by JavaBean Specification.

For an instance, consider the following class.It is a Java Bean with a property 'name';Similar conventions will be used to declare methods and events in Java Bean.

There is fat document that specifies guidelines on how to write compliant java beans.

public class MyJavaBean implements Serializable{
  private String name;
  public String getName(){

 }

 public void setName(){

 }

}

Java Bean came into existence with a sole purpose of allowing software component development, a reusable component.

Reflection is a way for a java programmer to reflect  a class definition programatically.Evey loaded class is associated with an object of type Class in the heap. class 'Class' has got methods that allow one to derive the methods , variables present in a class definition.

Introspection is for java Bean.There is a class 'Introspector' in java.beans package that given a java bean class will fetch JavaBeanInfo object with information like properties, method, events for given java bean class.

In essence it uses reflection api and the guidelines laid out by javabean spec to fetch the above information.