I'm searching for something that works like BeanUtils.describe, but working on .class
, not object ? Anybody help ? Currently i'm working on list of objects class with default getHeaders method like below.
public class SimpleList<E> {
protected final Class<E> clazz;
SimpleList(Class<E> clazz) {
this.clazz = clazz;
}
public String[] getHeaders() {
Map props = BeanUtils.describe(clazz); // replace this with something
return (String[]) props.keySet().toArray();
}
}
Use the Introspector
API:
PropertyDescriptor[] propertyDescriptors =
Introspector.getBeanInfo(beanClass).getPropertyDescriptors();
List<String> propertyNames = new ArrayList<String>(propertyDescriptors.length);
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
propertyNames.add(propertyDescriptor.getName());
}
You pretty much want PropertyUtils.getPropertyDescriptors()
. It returns an array of PropertyDescriptor
objects, of which you'd need to extract the names.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With