How can a bean property type be null?

The book Thinking in Java has an example of how to get bean information through Reflection / Introspection.

BeanInfo bi = Introspector.getBeanInfo(Car.class, Object.class); for (PropertyDescriptor d: bi.getPropertyDescriptors()) { Class<?> p = d.getPropertyType(); if (p == null) continue; [...] } 

Line 4 of this example above checks to see if the PropertyType is null. When and under what circumstances does this happen? Can you give an example?

+8
java reflection javabeans
source share
3 answers

Javadoc for getPropertyType method of the PropertyDescriptor class:

The result may be "null" if it is an indexed property that does not support unindexed access.

Indexed properties are those that are supported by an array of values. In addition to standard JavaBean access methods, indexed properties can also have methods for getting / setting individual elements in an array by specifying an index. Therefore, a JavaBean can have indexed getters and setters:

 public PropertyElement getPropertyName(int index) public void setPropertyName(int index, PropertyElement element) 

optional standard receiver and setter for non-indexed access:

 public PropertyElement[] getPropertyName() public void setPropertyName(PropertyElement element[]) 

Moving on to the Javadoc description, if you omit unindexed accessors, you can get a null return value for the property descriptor property type.

So, if you have the following sort of JavaBean, you can 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; } } 

Please note that although you can only implement indexed property accessors, this is not recommended because standard accessors are used to read and write values ​​if you use the getReadMethod and getWriteMethod methods.

+2
source share

From JavaDoc :

Returns null if the type is an indexed property that does not support unindexed access.

Therefore, I assume that if the type of the property is an indexed property (like an array), it will return null .

+3
source share

This returns null if you have a method of type int getValue(int index) .

The following code prints

 double is null ints class [I 

Grade:

 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; } } 
+2
source share

All Articles