Although the NestedBeanProperty differs greatly in the goals it accomplishs, it behaves identical to
the BeanProperty and IndexedBeanProperty classes in that it provides access to Java bean properties.
This class even uses the same methods to change and retrieve the property's value (ie getPropertyValue
and setPropertyValue). The difference between the last two classes and this one is that the
NestedBeanProperty is designed to provide access to nested properties. Of course, it can also
provide access to local properties, but it handles indexed properties quite differently.
First we will start off with examples of nested properties and how to change and retrieve them using
the NestedBeanProperty class. When the class is constructed, you must provide it ALL the information
that it needs to change and retrieve the property values. Since a nested property can be composed of
both normal and indexed properties, this means that when constructed, all indexes for indexed
properties in the nesting must be provided. Confused? Well, here's an example of using the constuctor:
NestedBeanProperty property = new NestedBeanProperty("foo[1].bar.value", Foo.class);
Although this example does nothing useful, it shows that in constructing the NestedBeanProperty, you
must provide all the indexes required to change and retrieve the property. This is invalid:
NestedBeanProperty property = new NestedBeanProperty("foo.bar.value", Foo.class);
The reason is that foo is an indexed property and the NestedBeanProperty class will assume that since
no index was specified that it is a normal property. It will be unable to find the normal property
named foo and an exception will be thrown.
Here are the constructor signatures for the NestedBeanProperty. They are identical to the BeanProperty
and IndexedBeanProperty with respect to the list of parameters but as shown above, the propertyName
parameter behaves quite differently.
public NestedBeanProperty(String propertyName, Class beanClass)
throws BeanException;
public NestedBeanProperty(String propertyName, Class beanClass, boolean strict)
throws BeanException;
The second constructor is a bit different. It provides a third parameter named strict. This parameter
sets the strictness of the NestedBeanProperty class. The strictness determines how the class behaves
when null values are encountered. If you recall from our definitions, auto-generation, you can
probably deduce that the strictness parameter controls whether or not auto-generation takes place
when changing nested properties. But, in addition to this, the strictness controls something else. It
also controls how the NestedBeanProperty class behaves when non-leaf properties in the nesting return
null and the property's value is being retrieved. In this case, auto-generation doesn't make sense
because it is overhead that will just slow things down. Instead, the strictness determines whether or
not an exception is thrown or whether null is returned. If the strictness is set to false (off), then
null is returned. If the strictness is set to true (on), then an exception is thrown.
|