Bean Manipulations

Bean Manipulations

In Bean Manipulation, we are able to perform the following actions.

  1. Getting Beans Information explicitly like properties and their setXXX() and getXXX() methods.
  2. Creating Java Bean Objects, checking bean property types, copying bean properties, etc.
  3. Accessing fields without standard getters and setters.
  4. Analyze and manipulate standard Java Beans like to get and set property values, get property descriptors, and query the readability/writability of properties and setting of index properties.

In Java, we are able to get beans descriptions like bean properties information like their names and the corresponding setXXX() and getXXX() methods information by using “Beans Introspection”. If we want to get beans data explicitly then we have to java.beans.BeanInfo interface, to get BeanInfo object then we have to use the following method from java.beans.Introspector class.

public BeanInfo getBeanInfo(Class bean_class_type)

E.g

BeanInfo beanInfo = Interospector.getBeanInfo(Employee.class);

To get All properties information of the Bean object we have to use “java.beans.PropertyDescriptor” class. To get all properties description in the form of PropertyDescriptor objects in an array then we have to use the following method from BeanInfo.

public PropertyDescriptor[] getPropertyDescriptors()

E.g

package com.ashok.spring.core.bean.manipulation.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Employee {
   private String empName;
   private String empId;
   private String empAddress;
   private double salary;

   public String getEmpName() {
      return empName;
   }

   public void setEmpName(String empName) {
      this.empName = empName;
   }

   public String getEmpId() {
      return empId;
   }

   public void setEmpId(String empId) {
      this.empId = empId;
   }

   public String getEmpAddress() {
      return empAddress;
   }

   public void setEmpAddress(String empAddress) {
      this.empAddress = empAddress;
   }

   public double getSalary() {
      return salary;
   }

   public void setSalary(double salary) {
      this.salary = salary;
   }

   @Override
   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + ", empAddress=" + empAddress + ", salary=" + salary
            + "]";
   }
}
package com.ashok.spring.core.bean.manipulation.test;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.MethodDescriptor;
import java.beans.PropertyDescriptor;

import org.springframework.beans.BeanInfoFactory;
import org.springframework.beans.ExtendedBeanInfoFactory;

import com.ashok.spring.core.bean.manipulation.beans.Employee;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class TestSpringApplication {
   public static void main(String[] args) throws IntrospectionException {
      BeanInfo beanInfo = Introspector.getBeanInfo(Employee.class);
      System.out.println(beanInfo);
      PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
      for (PropertyDescriptor p : props) {
         System.out.println(p);
      }
      MethodDescriptor[] meths = beanInfo.getMethodDescriptors();
      for (MethodDescriptor m : meths) {
         System.out.println(m.getName());
      }
   }
}

Output

java.beans.GenericBeanInfo@1b28cdfa
java.beans.PropertyDescriptor[name=class; propertyType=class java.lang.Class; readMethod=public final native java.lang.Class java.lang.Object.getClass()]
java.beans.PropertyDescriptor[name=empAddress; propertyType=class java.lang.String; readMethod=public java.lang.String com.ashok.spring.core.bean.manipulation.beans.Employee.getEmpAddress(); writeMethod=public void com.ashok.spring.core.bean.manipulation.beans.Employee.setEmpAddress(java.lang.String)]
java.beans.PropertyDescriptor[name=empId; propertyType=class java.lang.String; readMethod=public java.lang.String com.ashok.spring.core.bean.manipulation.beans.Employee.getEmpId(); writeMethod=public void com.ashok.spring.core.bean.manipulation.beans.Employee.setEmpId(java.lang.String)]
java.beans.PropertyDescriptor[name=empName; propertyType=class java.lang.String; readMethod=public java.lang.String com.ashok.spring.core.bean.manipulation.beans.Employee.getEmpName(); writeMethod=public void com.ashok.spring.core.bean.manipulation.beans.Employee.setEmpName(java.lang.String)]
java.beans.PropertyDescriptor[name=salary; propertyType=double; readMethod=public double com.ashok.spring.core.bean.manipulation.beans.Employee.getSalary(); writeMethod=public void com.ashok.spring.core.bean.manipulation.beans.Employee.setSalary(double)]
getClass
getSalary
getEmpId
setEmpId
getEmpAddress
wait
notifyAll
notify
wait
getEmpName
setEmpName
hashCode
setEmpAddress
setSalary
wait
equals
toString

In Spring framework, to create beans and to manipulate beans explicitly Spring Framework has provided predefined library in the form of “org.springframework.beans”.

In spring “org.springframework.beans” package has provided the following classes and interfaces to perform manipulations on beans.

BeanInfoFactory

It is an alternative to “Beans Introspection” provided by Spring Framework, it can be used to get details about the Bean objects like properties details, events details etc. by using java.beans.BeanInfo object internally. Spring Framework has provided a seperate predefined implementation class for BeanInfoFactory interface in the form of “org.springframework.beans. ExtendedBeanInfoFactory” class. To get BeanInfo object we have to use the following method from BeanInfoFactory interface.

public BeanInfo getBeanInfo(Class beanClassType)

Note

BeanInfoFactory implementation, org.springframework.beans. ExtendedBeanInfoFactory, accepts JavaBeans “non-standard” setter methods as ‘writable’ which returns some values instead of void.

package com.ashok.spring.core.bean.manipulation.test;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.MethodDescriptor;
import java.beans.PropertyDescriptor;

import org.springframework.beans.BeanInfoFactory;
import org.springframework.beans.ExtendedBeanInfoFactory;

import com.ashok.spring.core.bean.manipulation.beans.Employee;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class TestSpringApplication {
   public static void main(String[] args) throws IntrospectionException {
      BeanInfoFactory factory = new ExtendedBeanInfoFactory();
      BeanInfo beanInfo = factory.getBeanInfo(Employee.class);
      System.out.println(beanInfo);
      PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
      for (PropertyDescriptor p : props) {
         System.out.println(p);
      }
      MethodDescriptor[] meths = beanInfo.getMethodDescriptors();
      for (MethodDescriptor m : meths) {
         System.out.println(m.getName());
      }
   }
}

Output

java.beans.GenericBeanInfo@1b28cdfa
java.beans.PropertyDescriptor[name=class; propertyType=class java.lang.Class; readMethod=public final native java.lang.Class java.lang.Object.getClass()]
java.beans.PropertyDescriptor[name=empAddress; propertyType=class java.lang.String; readMethod=public java.lang.String com.ashok.spring.core.bean.manipulation.beans.Employee.getEmpAddress(); writeMethod=public void com.ashok.spring.core.bean.manipulation.beans.Employee.setEmpAddress(java.lang.String)]
java.beans.PropertyDescriptor[name=empId; propertyType=class java.lang.String; readMethod=public java.lang.String com.ashok.spring.core.bean.manipulation.beans.Employee.getEmpId(); writeMethod=public void com.ashok.spring.core.bean.manipulation.beans.Employee.setEmpId(java.lang.String)]
java.beans.PropertyDescriptor[name=empName; propertyType=class java.lang.String; readMethod=public java.lang.String com.ashok.spring.core.bean.manipulation.beans.Employee.getEmpName(); writeMethod=public void com.ashok.spring.core.bean.manipulation.beans.Employee.setEmpName(java.lang.String)]
java.beans.PropertyDescriptor[name=salary; propertyType=double; readMethod=public double com.ashok.spring.core.bean.manipulation.beans.Employee.getSalary(); writeMethod=public void com.ashok.spring.core.bean.manipulation.beans.Employee.setSalary(double)]
getClass
getSalary
getEmpId
setEmpId
getEmpAddress
wait
notifyAll
notify
wait
getEmpName
setEmpName
hashCode
setEmpAddress
setSalary
wait
equals
toString
Bean Manipulations


Scroll to top