Using the java.beans package:
try {
String strVersion = new String("16");
Integer intVersion;
PropertyEditor pe = PropertyEditorManager.findEditor(Integer.class);
pe.setValueAsText(strVersion);
intVersion = (Integer) pe.getValue();
PropertyDescriptor pd = new PropertyDescriptor("age", Bean.class);
Method read = pd.getReadMethod();
Object [] params = new Object [] {intVersion};
read.invoke(bean, params);
} catch (IllegalArgumentException ie) {
...
}
Using the JBeans package:
try {
String strVersion = new String("16");
BeanProperty bp = new BeanProperty("age", Bean.class);
bp.setPropertyValue(bean, strVersion, /*convert=*/true);
} catch (BeanException ie) {
...
}
As you can see, the JBeans package makes life much easier by reducing the amount of code required
to access Java beans.
|