Class MethodProperty<T>
- All Implemented Interfaces:
Property<T>,Property.ReadOnlyStatusChangeNotifier,Property.ValueChangeNotifier,Serializable
Proxy class for creating Properties from pairs of getter and setter methods of a Bean property. An instance of this class can be thought as having been attached to a field of an object. Accessing the object through the Property interface directly manipulates the underlying field.
It's assumed that the return value returned by the getter method is assignable to the type of the property, and the setter method parameter is assignable to that value.
A valid getter method must always be available, but instance of this class
can be constructed with a null setter method in which case the
resulting MethodProperty is read-only.
MethodProperty implements Property.ValueChangeNotifier, but does not automatically know whether or not the getter method will actually return a new value - value change listeners are always notified when setValue is called, without verifying what the getter returns.
- Since:
- 3.0
- Author:
- Vaadin Ltd.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classExceptionobject that signals that there were problems calling or finding the specified getter or setter methods of the property.Nested classes/interfaces inherited from class com.vaadin.featurepack.data.util.AbstractProperty
AbstractProperty.ReadOnlyStatusChangeEventNested classes/interfaces inherited from interface com.vaadin.featurepack.data.Property
Property.Editor, Property.ReadOnlyException, Property.ReadOnlyStatusChangeListener, Property.ReadOnlyStatusChangeNotifier, Property.Transactional<T>, Property.ValueChangeListener, Property.ValueChangeNotifier, Property.Viewer -
Constructor Summary
ConstructorsConstructorDescriptionMethodProperty(Class<?> type, Object instance, Method getMethod, Method setMethod, Object[] getArgs, Object[] setArgs, int setArgumentIndex) Creates a new instance ofMethodPropertyfrom the getter and setter methods, and argument lists.Creates a new instance ofMethodPropertywith the getter and setter methods.MethodProperty(Class<? extends T> type, Object instance, String getMethodName, String setMethodName) Creates a new instance ofMethodPropertyfrom named getter and setter methods.MethodProperty(Class<? extends T> type, Object instance, String getMethodName, String setMethodName, Object[] getArgs, Object[] setArgs, int setArgumentIndex) Creates a new instance ofMethodPropertyfrom named getter and setter methods and argument lists.MethodProperty(Object instance, String beanPropertyName) Creates a new instance ofMethodPropertyfrom a named bean property. -
Method Summary
Modifier and TypeMethodDescriptionvoidSends a value change event to all registered listeners.The instance used by this propertygetType()Returns the type of the Property.getValue()Gets the value stored in the Property.protected voidinvokeSetMethod(T value) Internal method to actually call the setter method of the wrapped property.booleanTests if the object is in read-only mode.voidsetArguments(Object[] getArgs, Object[] setArgs, int setArgumentIndex) Sets the setter method and getter method argument lists.voidsetInstance(Object instance) Sets the instance used by this property.voidSets the value of the property.Methods inherited from class com.vaadin.featurepack.data.util.AbstractProperty
addReadOnlyStatusChangeListener, addValueChangeListener, fireReadOnlyStatusChange, getListeners, removeReadOnlyStatusChangeListener, removeValueChangeListener, setPropertyReadOnly
-
Constructor Details
-
MethodProperty
Creates a new instance of
MethodPropertyfrom a named bean property. This constructor takes an object and the name of a bean property and initializes itself with the accessor methods for the property.The getter method of a
MethodPropertyinstantiated with this constructor will be called with no arguments, and the setter method with only the new value as the sole argument.If the setter method is unavailable, the resulting
MethodPropertywill be read-only, otherwise it will be read-write.Method names are constructed from the bean property by adding get/is/are/set prefix and capitalising the first character in the name of the given bean property.
- Parameters:
instance- the object that includes the property.beanPropertyName- the name of the property to bind to.
-
MethodProperty
public MethodProperty(Class<? extends T> type, Object instance, String getMethodName, String setMethodName) Creates a new instance of
MethodPropertyfrom named getter and setter methods. The getter method of aMethodPropertyinstantiated with this constructor will be called with no arguments, and the setter method with only the new value as the sole argument.If the setter method is
null, the resultingMethodPropertywill be read-only, otherwise it will be read-write.- Parameters:
type- the type of the property.instance- the object that includes the property.getMethodName- the name of the getter method.setMethodName- the name of the setter method.
-
MethodProperty
Creates a new instance of
MethodPropertywith the getter and setter methods. The getter method of aMethodPropertyinstantiated with this constructor will be called with no arguments, and the setter method with only the new value as the sole argument.If the setter method is
null, the resultingMethodPropertywill be read-only, otherwise it will be read-write.- Parameters:
type- the type of the property.instance- the object that includes the property.getMethod- the getter method.setMethod- the setter method.
-
MethodProperty
public MethodProperty(Class<? extends T> type, Object instance, String getMethodName, String setMethodName, Object[] getArgs, Object[] setArgs, int setArgumentIndex) Creates a new instance of
MethodPropertyfrom named getter and setter methods and argument lists. The getter method of aMethodPropertyinstantiated with this constructor will be called with the getArgs as arguments. The setArgs will be used as the arguments for the setter method, though the argument indexed by the setArgumentIndex will be replaced with the argument passed to thesetValue(Object newValue)method.For example, if the
setArgscontainsA,BandC, andsetArgumentIndex = 1, the callmethodProperty.setValue(X)would result in the setter method to be called with the parameter set of{A, X, C}- Parameters:
type- the type of the property.instance- the object that includes the property.getMethodName- the name of the getter method.setMethodName- the name of the setter method.getArgs- the fixed argument list to be passed to the getter method.setArgs- the fixed argument list to be passed to the setter method.setArgumentIndex- the index of the argument insetArgsto be replaced withnewValuewhensetValue(Object newValue)is called.
-
MethodProperty
public MethodProperty(Class<?> type, Object instance, Method getMethod, Method setMethod, Object[] getArgs, Object[] setArgs, int setArgumentIndex) Creates a new instance of
MethodPropertyfrom the getter and setter methods, and argument lists.This constructor behaves exactly like
MethodProperty(Class type, Object instance, String getMethodName, String setMethodName, Object [] getArgs, Object [] setArgs, int setArgumentIndex)except that instead of names of the getter and setter methods this constructor is given the actual methods themselves.- Parameters:
type- the type of the property.instance- the object that includes the property.getMethod- the getter method.setMethod- the setter method.getArgs- the fixed argument list to be passed to the getter method.setArgs- the fixed argument list to be passed to the setter method.setArgumentIndex- the index of the argument insetArgsto be replaced withnewValuewhensetValue(Object newValue)is called.
-
-
Method Details
-
getType
Returns the type of the Property. The methodsgetValueandsetValuemust be compatible with this type: one must be able to safely cast the value returned fromgetValueto the given type and pass any variable assignable to this type as an argument tosetValue.- Returns:
- type of the Property
-
isPropertyReadOnly
public boolean isPropertyReadOnly()Tests if the object is in read-only mode. In read-only mode calls tosetValuewill throwReadOnlyExceptionand will not modify the value of the Property.- Specified by:
isPropertyReadOnlyin interfaceProperty<T>- Overrides:
isPropertyReadOnlyin classAbstractProperty<T>- Returns:
trueif the object is in read-only mode,falseif it's not
-
getValue
Gets the value stored in the Property. The value is resolved by calling the specified getter method with the argument specified at instantiation.- Returns:
- the value of the Property
-
setArguments
Sets the setter method and getter method argument lists.
- Parameters:
getArgs- the fixed argument list to be passed to the getter method.setArgs- the fixed argument list to be passed to the setter method.setArgumentIndex- the index of the argument insetArgsto be replaced withnewValuewhensetValue(Object newValue)is called.
-
setValue
Sets the value of the property. Note that since Vaadin 7, no conversions are performed and the value must be of the correct type.- Parameters:
newValue- the New value of the property.- Throws:
Property.ReadOnlyException- if the object is in read-only mode- See Also:
-
invokeSetMethod
Internal method to actually call the setter method of the wrapped property.- Parameters:
value-
-
fireValueChange
public void fireValueChange()Sends a value change event to all registered listeners. Public for backwards compatibility, visibility may be reduced in future versions.- Overrides:
fireValueChangein classAbstractProperty<T>
-
getInstance
The instance used by this property- Returns:
- the instance used for fetching the property value
- Since:
- 7.7.7
-
setInstance
Sets the instance used by this property.The new instance must be of the same type as the old instance
To be consistent with
setValue(Object), this method will fire a value change event even if the value stays the same- Parameters:
instance- the instance to use- Since:
- 7.7.7
-