technocrat
technocrat

Reputation: 35

How do I get the PropertyDescriptor for the elements of an array?

I have the PropertyDescriptor of an array field that looks like below.

Foo[] fooArray;

How can I get the PropertyDescriptor for Foo, so that I am able to get the getter and setter methods of the class?

Upvotes: 0

Views: 1909

Answers (2)

wero
wero

Reputation: 33000

You can access the component type of an array class via Class.getComponentType().

Therefore to create a BeanDescriptor for Foo given fooArray write

BeanDescriptor d = new BeanDescriptor(fooArray.getClass().getComponentType());

The BeanDescriptor then allows you to retrieve the PropertyDescriptor for all Foo bean properties.

Upvotes: 2

If a Class object represents an array type, you can ask for the element type:

Class<?> clazz = Foo[].class;
assert (clazz.isArray());
assert Foo.class.equals(clazz.getComponentType());

Asking for "the PropertyDescriptor" for a class doesn't really make sense, but perhaps you're looking for the BeanInfo:

BeanInfo infoAboutFoo = Introspector.getBeanInfo(clazz.getComponentType());
PropertyDescriptor[] fooDescriptors = infoAboutFoo.getPropertyDescriptors();

Upvotes: 4

Related Questions