Intro
This examples will demonstrates how to display all property names of a bean using BeanInfo class.
Examples
Employee.java
package com.freesamplecode.java.bean;
import java.io.Serializable;
import java.util.Date;
public class Employee implements Serializable{
/**
*
*/
private static final long serialVersionUID = 8836890329708390627L;
private long id;
private String name;
private String sex;
private String placeOfBirth;
private Date dateOfBirth;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPlaceOfBirth() {
return placeOfBirth;
}
public void setPlaceOfBirth(String placeOfBirth) {
this.placeOfBirth = placeOfBirth;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
}
GetListPropertyNameBeanDemo.javapackage com.freesamplecode.java.bean;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
public class GetListPropertyNameBeanDemo {
public static void main(String[] args){
BeanInfo beanInfo;
try {
beanInfo = Introspector.getBeanInfo(Employee.class);
PropertyDescriptor[] propertyDescriptor = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor pd : propertyDescriptor){
System.out.println("Property Name : "+pd.getName());
}
} catch (IntrospectionException e) {
e.printStackTrace();
}
}
}
Output
Property Name : class Property Name : dateOfBirth Property Name : id Property Name : name Property Name : placeOfBirth Property Name : sex

0 comments: