|
How to list names of the properties of a Bean |
|
|
This Java tip demonstrates a method of listing names of various properties of a bean.
The bean properties are entities that participate to the mapping process. They are
usually the fields of the data bean. However, this isn't a requirement since the get
& set methods of the data bean define the bean properties. The mapping utilities use
the JavaBeans' introspection API to get the names and the types of the properties of
a data bean.
try {
BeanInfo bi = Introspector.getBeanInfo(MyBean.class);
PropertyDescriptor[] pds = bi.getPropertyDescriptors();
for (int i=0; i<pds.length; i++) {
// Get property name
String propName = pds[i].getName();
}
// class, prop1, prop2, PROP3
} catch (java.beans.IntrospectionException e) {
}
public class MyBean {
// Property prop1
public String getProp1() {
return null;
}
public void setProp1(String s) {
}
// Property prop2
public int getProp2() {
return 0;
}
public void setProp2(int i) {
}
// Property PROP
public byte[] getPROP3() {
return null;
}
public void setPROP3(byte[] bytes) {
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.