Sometimes it is required to list entire attributes of a bean. They are usually fields of a bean with setters and getters. but this isn’t a requirement to list the attributes of a bean using Java Bean’s Introspection API. Setters and getters define the attributes of a bean and not actual attributes. The mapping utilities use Java Bean’s Introspection API to list attributes of bean using setters and getters.
Listing attributes of a bean: Here is an example to show how to list properties of DashBoard bean.
1.Java Bean
package com.jminded.beanInfo; import java.io.Serializable; /** * * @author Umashankar * https://jminded.com */ public class DashBoard implements Serializable { /** * */ private static final long serialVersionUID = 1L; private String taskId; private String taskName; private String teamName; private int qaDefects; private String projectStatus; public String getTaskId() { return taskId; } public void setTaskId(String taskId) { this.taskId = taskId; } public String getTaskName() { return taskName; } public void setTaskName(String taskName) { this.taskName = taskName; } public String getTeamName() { return teamName; } public void setTeamName(String teamName) { this.teamName = teamName; } public int getQaDefects() { return qaDefects; } public void setQaDefects(int qaDefects) { this.qaDefects = qaDefects; } public String getProjectStatus() { return projectStatus; } public void setProjectStatus(String projectStatus) { this.projectStatus = projectStatus; } public String getResource() { return null; } }
2. Listing Attributes of Bean
package com.jminded.beanInfo; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.util.ArrayList; import java.util.List; /** * * @author Umashankar * https://jminded.com */ public class ListAttributesofBean { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { BeanInfo bi = Introspector.getBeanInfo(DashBoard.class); PropertyDescriptor[] pds = bi.getPropertyDescriptors(); List<String> beanAttributes=new ArrayList<String>(); for (int i=0; i<pds.length; i++) { // Get property name String propName = pds[i].getName(); beanAttributes.add(propName); } for(String beanAttr:beanAttributes){ System.out.print(":"+beanAttr+":"); } } catch (java.beans.IntrospectionException e) { System.err.println(e.getMessage()); } } }
3.Output
:class::projectStatus::qaDefects::resource::taskId::taskName::teamName: