Java Reflection Beans Property API
You question is very unclear, but if I get it:
Yes. The java.beans
package has the so called Introspector
. There you can read the properties of a bean.
BeanInfo info = Introspector.getBeanInfo(Bean.class);
PropertyDescriptor[] pds = info.getPropertyDescriptors();
You can find the desired PropertyDescriptor
by its name and you can call getReadMethod().invoke(..)
What you need is the BeanInfo / Introspector mechanism (see Bozho's answer). However, it's hell to use this directly, so you can use one of the Libraries that offer property-based access. The best-known is probably Apache Commons / BeanUtils (another one is Spring's BeanWrapper
abstraction)
Example code:
A someBean = new A();
// access properties as Map
Map<String, Object> properties = BeanUtils.describe(someBean);
properties.set("name","Fred");
BeanUtils.populate(someBean, properties);
// access individual properties
String oldname = BeanUtils.getProperty(someBean,"name");
BeanUtils.setProperty(someBean,"name","Barny");