Search code examples
javaintrospection

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?


Solution

  • 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();