Class SimplePropertySet<T>
- Type Parameters:
T- underlying target type
- All Implemented Interfaces:
PropertySet<T>,Serializable
- Direct Known Subclasses:
MapPropertySet
PropertySet using caller-supplied getters and setters.
This class is useful for building arbitrary property sets, e.g., see MapPropertySet.
It's also useful when you need to detect Java bean properties defined by default interface methods.
Due to JDK-8071693, Vaadin's Binder
fails to detect such bean properties. To work around that bug, you can do something like this:
// Gather bean properties using Spring's BeanUtils to work around JDK-8071693
final SimplePropertySet<T> propertySet = new SimplePropertySet<>(beanType);
Stream.of(BeanUtils.getPropertyDescriptors(beanType))
.filter(pd -> !(pd instanceof IndexedPropertyDescriptor))
.filter(pd -> pd.getReadMethod() != null)
.filter(pd -> pd.getWriteMethod() != null)
.forEach(propertySet::addPropertyDefinition);
// Create binder
final Binder<T> binder = Binder.withPropertySet(propertySet);
This class allows you to recover the original property definition from
a Binder.Binding instance; see propertyDefinitionForBinding().
Does not support sub-properties.
- See Also:
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddPropertyDefinition(PropertyDescriptor propertyDescriptor) Add a new property to this instance corresponding to the given Java beanPropertyDescriptor.<V> SimplePropertySet<T>.Definition<V>addPropertyDefinition(String name, Class<V> type, String caption, ValueProvider<? super T, ? extends V> getter, Setter<? super T, ? super V> setter) Add a new property to this instance.protected <V> SimplePropertySet<T>.Definition<V>createDefinition(String name, Class<V> type, String caption, ValueProvider<? super T, ? extends V> getter, Setter<? super T, ? super V> setter) Create a newSimplePropertySet.Definition.getProperty(String name) Get the target object type associated with this instance.static SimplePropertySet<?>.Definition<?>propertyDefinitionForBinding(Binder.Binding<?, ?> binding) Recover theSimplePropertySet.Definitionfrom the given binding, assuming the associatedBinderwas created using aSimplePropertySet.
-
Constructor Details
-
SimplePropertySet
Constructor.- Parameters:
targetType- object type that contains property values- Throws:
IllegalArgumentException- iftargetTypeis null
-
-
Method Details
-
getProperties
- Specified by:
getPropertiesin interfacePropertySet<T>
-
getProperty
- Specified by:
getPropertyin interfacePropertySet<T>
-
getTargetType
Get the target object type associated with this instance.The target object stores the actual property values.
- Returns:
- target object type
-
addPropertyDefinition
public <V> SimplePropertySet<T>.Definition<V> addPropertyDefinition(String name, Class<V> type, String caption, ValueProvider<? super T, ? extends V> getter, Setter<? super T, ? super V> setter) Add a new property to this instance.- Parameters:
name- property nametype- property typecaption- property captiongetter- getter methodsetter- setter method, or null for none- Returns:
- newly created property definition
- Throws:
IllegalArgumentException- if any parameter other thansetteris nullIllegalArgumentException- if a property with the same name has already been added
-
addPropertyDefinition
public SimplePropertySet<T>.Definition<?> addPropertyDefinition(PropertyDescriptor propertyDescriptor) Add a new property to this instance corresponding to the given Java beanPropertyDescriptor.The caller is responsible for ensuring that
propertyDescriptoris compatible with the target object type.- Parameters:
propertyDescriptor- property descriptor- Returns:
- newly created property definition
- Throws:
IllegalArgumentException- ifpropertyDescriptoris nullIllegalArgumentException- ifpropertyDescriptoris anIndexedPropertyDescriptorIllegalArgumentException- ifpropertyDescriptorhas no getter methodIllegalArgumentException- if a property with the same name has already been added
-
propertyDefinitionForBinding
public static SimplePropertySet<?>.Definition<?> propertyDefinitionForBinding(Binder.Binding<?, ?> binding) Recover theSimplePropertySet.Definitionfrom the given binding, assuming the associatedBinderwas created using aSimplePropertySet.- Parameters:
binding-Binderbinding- Returns:
- binding's associated property definition
- Throws:
IllegalArgumentException- if the associatedBinderdoes not use aSimplePropertySetIllegalArgumentException- ifbindingis null
-
createDefinition
protected <V> SimplePropertySet<T>.Definition<V> createDefinition(String name, Class<V> type, String caption, ValueProvider<? super T, ? extends V> getter, Setter<? super T, ? super V> setter) Create a newSimplePropertySet.Definition.The implementation in
SimplePropertySetjust invokes theSimplePropertySet.Definitionconstructor directly.- Parameters:
name- property nametype- property typecaption- property captiongetter- getter methodsetter- setter method, or null for none- Returns:
- newly created property definition
- Throws:
IllegalArgumentException- if any parameter other thansetteris null
-