In the book “Thinking in Java” there is an example of how to get information for a bean via Reflection/Introspection.
BeanInfo bi = Introspector.getBeanInfo(Car.class, Object.class);
for (PropertyDescriptor d: bi.getPropertyDescriptors()) {
Class<?> p = d.getPropertyType();
if (p == null) continue;
[...]
}
In line 4 of that sample above there is a check if the PropertyType is null. When and under what circumstances does that happen? Can you give an example?
The Javadoc for the
getPropertyTypemethod of thePropertyDescriptorclass states:Indexed properties are those that are backed by an array of values. In addition to the standard JavaBean accessor methods, indexed properties may also have methods to get/set individual elements in the array, by specifying an index. The JavaBean, may therefore, have the indexed getters and setters:
in addition the standard getter and setter for non-indexed access:
Going by the Javadoc description, if you omit the non-indexed accessors, you can obtain a return value of
nullfor the property type of the property descriptor.So, if you have a JavaBean of the following variety, you could get a null return value:
Note, while that you can implement the indexed property accessors alone, it is not recommended to do so, as the standard accessors are used to read and write values, if you happen to use the
getReadMethodandgetWriteMethodmethods of thePropertyDescriptor.