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
-
Constructor Summary
-
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.Definition
from the given binding, assuming the associatedBinder
was created using aSimplePropertySet
.
-
Constructor Details
-
SimplePropertySet
Constructor.- Parameters:
targetType
- object type that contains property values- Throws:
IllegalArgumentException
- iftargetType
is null
-
-
Method Details
-
getProperties
- Specified by:
getProperties
in interfacePropertySet<T>
-
getProperty
- Specified by:
getProperty
in 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 thansetter
is 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
propertyDescriptor
is compatible with the target object type.- Parameters:
propertyDescriptor
- property descriptor- Returns:
- newly created property definition
- Throws:
IllegalArgumentException
- ifpropertyDescriptor
is nullIllegalArgumentException
- ifpropertyDescriptor
is anIndexedPropertyDescriptor
IllegalArgumentException
- ifpropertyDescriptor
has 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.Definition
from the given binding, assuming the associatedBinder
was created using aSimplePropertySet
.- Parameters:
binding
-Binder
binding- Returns:
- binding's associated property definition
- Throws:
IllegalArgumentException
- if the associatedBinder
does not use aSimplePropertySet
IllegalArgumentException
- ifbinding
is 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
SimplePropertySet
just invokes theSimplePropertySet.Definition
constructor 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 thansetter
is null
-