How can the type of a bean property be null?
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?
From the JavaDoc:
Returns null if the type is an indexed property that does not support non-indexed access.
So I guess if the property type is an indexed property(like an array) it will return null
.
The Javadoc for the getPropertyType
method of the PropertyDescriptor
class states:
The result may be "null" if this is an indexed property that does not support non-indexed access.
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:
public PropertyElement getPropertyName(int index)
public void setPropertyName(int index, PropertyElement element)
in addition the standard getter and setter for non-indexed access:
public PropertyElement[] getPropertyName()
public void setPropertyName(PropertyElement element[])
Going by the Javadoc description, if you omit the non-indexed accessors, you can obtain a return value of null
for the property type of the property descriptor.
So, if you have a JavaBean of the following variety, you could get a null return value:
class ExampleBean
{
ExampleBean()
{
this.elements = new String[10];
}
private String[] elements;
// standard getters and setters for non-indexed access. Comment the lines in the double curly brackets, to have getPropertyType return null.
// {{
public String[] getElements()
{
return elements;
}
public void setElements(String[] elements)
{
this.elements = elements;
}
// }}
// indexed getters and setters
public String getElements(int index) {
return this.elements[index];
}
public void setElements(int index, String[] elements)
{
this.elements[index] = elements;
}
}
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 getReadMethod
and getWriteMethod
methods of the PropertyDescriptor
.
This returns null when you have a method like int getValue(int index)
.
The following code prints
double is null
ints class [I
The class:
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
public class BeanInfos {
public static void main(String[] args) {
try {
BeanInfo bi = Introspector.getBeanInfo(ClassA.class, Object.class);
for (PropertyDescriptor d : bi.getPropertyDescriptors()) {
Class<?> p = d.getPropertyType();
if (p == null)
System.out.println(d.getName() + " is null" );
else
System.out.println(d.getName() + " " + p);
}
} catch (IntrospectionException e) {
e.printStackTrace();
}
}
}
class ClassA {
private int[] ints;
private double[] doubles;
public int[] getInts() {
return ints;
}
public void setInts(int[] ints) {
this.ints = ints;
}
public double getDouble(int idx) {
return doubles[idx];
}
public void setDoubles(double val, int idx) {
this.doubles[idx] = val;
}
}
精彩评论