diff options
Diffstat (limited to 'server/src/com/vaadin/data/util/NestedMethodProperty.java')
-rw-r--r-- | server/src/com/vaadin/data/util/NestedMethodProperty.java | 59 |
1 files changed, 58 insertions, 1 deletions
diff --git a/server/src/com/vaadin/data/util/NestedMethodProperty.java b/server/src/com/vaadin/data/util/NestedMethodProperty.java index b62ecfbfc3..7a3963c17e 100644 --- a/server/src/com/vaadin/data/util/NestedMethodProperty.java +++ b/server/src/com/vaadin/data/util/NestedMethodProperty.java @@ -32,7 +32,7 @@ import com.vaadin.data.util.MethodProperty.MethodException; * can contain multiple levels of nesting. * * When accessing the property value, all intermediate getters must return - * non-null values. + * non-null values or the <code>nullBeansAllowed</code> must be set to true. * * @see MethodProperty * @@ -55,6 +55,15 @@ public class NestedMethodProperty<T> extends AbstractProperty<T> { */ private Object instance; + /** + * a boolean flag indicating whether intermediate getters may return null + * values. If the flag is set to true, calling getValue will return null if + * the property or any of its intermediate getters returns null. If set to + * false, intermediate getters returning null value will throw Exception. + * The default value is false to ensure backwards compatibility. + */ + private boolean nullBeansAllowed = false; + private Class<? extends T> type; /* Special serialization to handle method references */ @@ -85,7 +94,33 @@ public class NestedMethodProperty<T> extends AbstractProperty<T> { * if the property name is invalid */ public NestedMethodProperty(Object instance, String propertyName) { + this(instance, propertyName, false); + } + + /** + * Constructs a nested method property for a given object instance. The + * property name is a dot separated string pointing to a nested property, + * e.g. "manager.address.street". The <code>nullBeansAllowed</code> controls + * the behavior in cases where the intermediate getters may return null + * values. If the flag is set to true, calling getValue will return null if + * the property or any of its intermediate getters returns null. If set to + * false, null values returned by intermediate getters will cause + * NullPointerException. The default value is false to ensure backwards + * compatibility. + * + * @param instance + * top-level bean to which the property applies + * @param propertyName + * dot separated nested property name + * @param nullBeansAllowed + * set true to allow null values from intermediate getters + * @throws IllegalArgumentException + * if the property name is invalid + */ + public NestedMethodProperty(Object instance, String propertyName, + boolean nullBeansAllowed) { this.instance = instance; + this.nullBeansAllowed = nullBeansAllowed; initialize(instance.getClass(), propertyName); } @@ -104,6 +139,25 @@ public class NestedMethodProperty<T> extends AbstractProperty<T> { } /** + * For internal use to deduce property type etc. without a bean instance. + * Calling {@link #setValue(Object)} or {@link #getValue()} on properties + * constructed this way is not supported. + * + * @param instanceClass + * class of the top-level bean + * @param propertyName + * dot separated nested property name + * @param nullBeansAllowed + * set true to allow null values from intermediate getters + */ + NestedMethodProperty(Class<?> instanceClass, String propertyName, + boolean nullBeansAllowed) { + instance = null; + this.nullBeansAllowed = nullBeansAllowed; + initialize(instanceClass, propertyName); + } + + /** * Initializes most of the internal fields based on the top-level bean * instance and property name (dot-separated string). * @@ -199,6 +253,9 @@ public class NestedMethodProperty<T> extends AbstractProperty<T> { Object object = instance; for (Method m : getMethods) { object = m.invoke(object); + if (object == null && nullBeansAllowed) { + return null; + } } return (T) object; } catch (final Throwable e) { |