Contents
This document
What is JBeans?
Why use JBeans?
What does JBeans replace?
Making life easier
Installation
Building
Case studies/scenarios
First steps/definitions
Using BeanProperty
Using IndexedBeanProperty
Using NestedBeanProperty
Using DynamicNestedBeanProperty
Using JavaBean
Events and conversion
Using DynamicNestedBeanProperty

DynamicNestedBeanProperty is very similar to the NestedBeanProperty class just discussed and is also the newest addition to the JBeans family of classes. This class provide a dynamic method for accessing nested properties. When we talk about dynamic, we are only referring to indexed properties within the nesting.

When we used NestedBeanProperty before, any indexed properties were setup using the cosntructor. In order to do this, we provided the constructor with index notation for each of the indexed properties in the nesting. When updating and retrieving the properties, the indices setup during construction were then used while traversing the nesting. This meant that the indices used to construct the NestedBeanProperty were the indices used when updating and retrieving. It was impossible to change these indices. Using the DynamicNestedBeanProperty, this is not the case. The indices are supplied when updating and retrieving the property, which allows the indices to be changed each time.

We won't go into detail as we did with NestedBeanProperty, because for the most part, the DynamicNestedBeanProperty and NestedBeanProperty behave the same. Instead, we'll only show you the parts that differ between the two. The first of which is the constructors.

If you recall, using a NestedBeanProperty with an indexed property, you might call the constructor like this:

    NestedBeanProperty property = new NestedBeanProperty("foo[1].bar.value", Foo.class);
    

However, when using the DynamicNestedBeanProperty for the exact same property you would call the constructor like this:

    DynamicNestedBeanProperty property = new DynamicNestedBeanProperty("foo.bar.value", Foo.class);
    

The reason is that DynamicNestedBeanProperty does not statically store the indices during construction like the NestedBeanProperty did. Instead, you simply tell it which properties are in the nesting and it determines which are indexed and which are not. Then when updating are retrieving the property you can specify the indices. That is the other difference between the two classes.

Continuing with the example above, using the NestedBeanProperty you would retrieve the property's value by doing something like this:

    try {
        Foo foo = new Foo();
        NestedBeanProperty property = new NestedBeanProperty("foo[1].bar.value", Foo.class);
        
        Object value = property.getPropertyValue(foo);
    } catch (BeanException be) {
        be.printStackTrace();
    }
    

Since the indices were setup during construction, the property can simply be retrieve without worry. However, when using the DynamicNestedBeanProperty to retrieve the property's value, you would do something like this:

    try {
        Foo foo = new Foo();
        DynamicNestedBeanProperty property = new DynamicNestedBeanProperty("foo.bar.value", Foo.class);
        int [] indices = new int[] {1}
        
        Object value = property.getPropertyValue(foo, indices);
    } catch (BeanException be) {
        be.printStackTrace();
    }
    

As you can see, the indices are supplied when retrieving. The same is done when updating. Here are the new methods in DynamicNestedBeanProperty the allow for this dynamic indexing behavior.

    public Object getPropertyValue(Object beanInstance, int [] indices);

    public Object getPropertyValue(Object beanInstance, List indices);
    
    public void setPropertyValue(Object beanInstance, int [] indices, Object value, boolean convert);
    
    public void setPropertyValue(Object beanInstance, List indices, Object value, boolean convert);
    

There are two versions of the methods. The first version takes an integer array, which is expected. The second version takes a java.util.List type. This is a list of indices to be used. The only restriction is that the List contains only java.lang.Integer objects. These objects are then converted to int values for use while indexing.

The DynamicNestedBeanProperty contains all the same methods as the NestedBeanProperty as well as these extra methods. As something to explore that is out of the scope of this document, DynamicNestedBeanProperty is the parent class of NestedBeanProperty. This means that the four methods listed above can be called on instances of NestedBeanProperty. In fact they function correctly when used on instances of NestedBeanProperty and effectively change the indices for a single method call.