public class MethodProperty<T> extends AbstractProperty<T>
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.
Modifier and Type | Class and Description |
---|---|
static class |
MethodProperty.MethodException
Exception object that signals that there were problems
calling or finding the specified getter or setter methods of the
property. |
AbstractProperty.ReadOnlyStatusChangeEvent
Property.Editor, Property.ReadOnlyException, Property.ReadOnlyStatusChangeListener, Property.ReadOnlyStatusChangeNotifier, Property.Transactional<T>, Property.ValueChangeListener, Property.ValueChangeNotifier, Property.Viewer
Constructor and Description |
---|
MethodProperty(Class<?> type,
Object instance,
Method getMethod,
Method setMethod,
Object[] getArgs,
Object[] setArgs,
int setArgumentIndex)
Creates a new instance of
MethodProperty from the getter and
setter methods, and argument lists. |
MethodProperty(Class<? extends T> type,
Object instance,
Method getMethod,
Method setMethod)
Creates a new instance of
MethodProperty with the getter and
setter methods. |
MethodProperty(Class<? extends T> type,
Object instance,
String getMethodName,
String setMethodName)
Creates a new instance of
MethodProperty from 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 of
MethodProperty from named getter
and setter methods and argument lists. |
MethodProperty(Object instance,
String beanPropertyName)
Creates a new instance of
MethodProperty from a named bean
property. |
Modifier and Type | Method and Description |
---|---|
void |
fireValueChange()
Sends a value change event to all registered listeners.
|
Object |
getInstance()
The instance used by this property
|
Class<? extends T> |
getType()
Returns the type of the Property.
|
T |
getValue()
Gets the value stored in the Property.
|
protected void |
invokeSetMethod(T value)
Internal method to actually call the setter method of the wrapped
property.
|
boolean |
isReadOnly()
Tests if the object is in read-only mode.
|
void |
setArguments(Object[] getArgs,
Object[] setArgs,
int setArgumentIndex)
Sets the setter method and getter method argument lists.
|
void |
setInstance(Object instance)
Sets the instance used by this property.
|
void |
setValue(T newValue)
Sets the value of the property.
|
addListener, addListener, addReadOnlyStatusChangeListener, addValueChangeListener, fireReadOnlyStatusChange, getListeners, removeListener, removeListener, removeReadOnlyStatusChangeListener, removeValueChangeListener, setReadOnly, toString
public MethodProperty(Object instance, String beanPropertyName)
Creates a new instance of MethodProperty
from 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 MethodProperty
instantiated 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
MethodProperty
will 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.
instance
- the object that includes the property.beanPropertyName
- the name of the property to bind to.public MethodProperty(Class<? extends T> type, Object instance, String getMethodName, String setMethodName)
Creates a new instance of MethodProperty
from named getter
and setter methods. The getter method of a MethodProperty
instantiated 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 resulting
MethodProperty
will be read-only, otherwise it will be
read-write.
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.public MethodProperty(Class<? extends T> type, Object instance, Method getMethod, Method setMethod)
Creates a new instance of MethodProperty
with the getter and
setter methods. The getter method of a MethodProperty
instantiated 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 resulting
MethodProperty
will be read-only, otherwise it will be
read-write.
type
- the type of the property.instance
- the object that includes the property.getMethod
- the getter method.setMethod
- the setter method.public MethodProperty(Class<? extends T> type, Object instance, String getMethodName, String setMethodName, Object[] getArgs, Object[] setArgs, int setArgumentIndex)
Creates a new instance of MethodProperty
from named getter
and setter methods and argument lists. The getter method of a
MethodProperty
instantiated 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 the
setValue(Object newValue)
method.
For example, if the setArgs
contains A
,
B
and C
, and setArgumentIndex =
1
, the call methodProperty.setValue(X)
would result
in the setter method to be called with the parameter set of
{A, X, C}
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 in setArgs
to be
replaced with newValue
when
setValue(Object newValue)
is called.public MethodProperty(Class<?> type, Object instance, Method getMethod, Method setMethod, Object[] getArgs, Object[] setArgs, int setArgumentIndex)
Creates a new instance of MethodProperty
from 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.
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 in setArgs
to be
replaced with newValue
when
setValue(Object newValue)
is called.public final Class<? extends T> getType()
getValue
and
setValue
must be compatible with this type: one must be able
to safely cast the value returned from getValue
to the given
type and pass any variable assignable to this type as an argument to
setValue
.public boolean isReadOnly()
setValue
will throw ReadOnlyException
and will
not modify the value of the Property.isReadOnly
in interface Property<T>
isReadOnly
in class AbstractProperty<T>
true
if the object is in read-only mode,
false
if it's notpublic T getValue()
public void setArguments(Object[] getArgs, Object[] setArgs, int setArgumentIndex)
Sets the setter method and getter method argument lists.
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 in setArgs
to be
replaced with newValue
when
setValue(Object newValue)
is called.public void setValue(T newValue) throws Property.ReadOnlyException
newValue
- the New value of the property.Property.ReadOnlyException
- if the object is in read-only mode.Property.ReadOnlyException
- if the object is in read-only modeinvokeSetMethod(Object)
protected void invokeSetMethod(T value)
value
- public void fireValueChange()
fireValueChange
in class AbstractProperty<T>
public Object getInstance()
public void setInstance(Object instance)
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
instance
- the instance to useCopyright © 2019 Vaadin Ltd. All rights reserved.