BeanDescriptor in Java

BeanDescriptor class provides the detail of the bean and Class object for the respective bean class.

We can initialize a BeanDescriptor with the help of the method getBeanDescriptor() from BeanInfo interface.

The common methods available in BeanDescriptor class are:

MethodDescription
public String getName()It can be used to get the name of the bean. This method is an inherited method from the FeatureDescriptor class.
public Class getBeanClass()It can be used to get the java.lang.Class object from the respective bean class and when we are able to get the Class object then we can get all the detail of the bean class like access modifier, method names, variables, and so on.

Example

Student bean class.

public class Student {
    private int rollNo;
    private String name;
    private char gender;
    public int getRollNo() {
        return rollNo;
    }
    public void setRollNo(int rollNo) {
        this.rollNo = rollNo;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public char getGender() {
        return gender;
    }
    public void setGender(char gender) {
        this.gender = gender;
    }
}

Demo Class

public class BeanDescriptorDemo {

	public static void main(String[] args) throws IntrospectionException {

		BeanInfo beanInfo = Introspector.getBeanInfo(Student.class);
		BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor();
		System.out.println("Bean Name: "+beanDescriptor.getName());
		
		//Getting the Class object
		Class cls = beanDescriptor.getBeanClass();
		
		// Printing bean class name
		System.out.println("Bean Class name: "+ cls.getName());
		
		// Printing superclass name of the bean
		System.out.println("Super class name: "+ cls.getSuperclass().getName());
	}

}

Output

Bean Name: Student
Bean Class name: Student
Super class name: java.lang.Object

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments