Class FluentPropertyDescriptor
An implementation of PropertyDescriptor that is more relaxed
when deriving method names from property names.
Unless a method or method name is explicitly passed in a constructor,
this class will attempt to detect read and write methods with different
style variations (e.g., fluent or builder-style).
In addition, it supports read-only and write-only methods by default
with the short constructor form (no need to pass null
as constructor arguments for method or method names).
Extra methods are also provided to check for the existence of read or
write methods: isReadable() and isWritable().
Shortcut methods are also provided to set or get a bean value:
readValue(Object bean) and
writeValue(Object bean, Object value).
If a property has no read method (write-only), the property type will be
derived from the first matching setter encountered with a single parameter.
The following are examples of supported variations, in order of precedence (for cases where their might be more than one variation on a class for the same property).
Supported readable method styles (getters)
Foo getFoo()boolean isFoo()Foo foo()
Supported writable method styles (setters)
void setFoo(Foo foo)Self setFoo(Foo foo) //Self is "this" instancevoid foo(Foo foo)Self foo(Foo foo) //Self is "this" instance
- Version:
- 2.0.0
-
Constructor Summary
ConstructorsConstructorDescriptionFluentPropertyDescriptor(PropertyDescriptor propertyDescriptor) Copy constructor.FluentPropertyDescriptor(String propertyName, Class<?> beanClass) Creates a new property descriptor, trying to derive the reader and writer methods using a few naming variations, as described in this class documentation.FluentPropertyDescriptor(String propertyName, Class<?> beanClass, String readMethodName, String writeMethodName) Creates a new property descriptor with the specified write and read method names.FluentPropertyDescriptor(String propertyName, Method readMethod, Method writeMethod) Creates a new property descriptor with the specified write and read method. -
Method Summary
Modifier and TypeMethodDescriptionbooleanGets whether this described property can be read (has a reader method).booleanGets whether this described property can be written to (has a writer method).<T> TReads the bean value matching the property descriptor.voidwriteValue(Object bean, Object value) Writes the bean value matching the property descriptor.Methods inherited from class java.beans.PropertyDescriptor
createPropertyEditor, equals, getPropertyEditorClass, getPropertyType, getReadMethod, getWriteMethod, hashCode, isBound, isConstrained, setBound, setConstrained, setPropertyEditorClass, setReadMethod, setWriteMethodMethods inherited from class java.beans.FeatureDescriptor
attributeNames, getDisplayName, getName, getShortDescription, getValue, isExpert, isHidden, isPreferred, setDisplayName, setExpert, setHidden, setName, setPreferred, setShortDescription, setValue, toString
-
Constructor Details
-
FluentPropertyDescriptor
public FluentPropertyDescriptor(String propertyName, Class<?> beanClass, String readMethodName, String writeMethodName) throws IntrospectionException Creates a new property descriptor with the specified write and read method names. When a supplied method name isnullor blank, it will try to derive it using a few method name variations, as described in this class documentation.- Parameters:
propertyName- the bean property namebeanClass- the bean class namereadMethodName- optional read method name (getter)writeMethodName- optional write method name (setter)- Throws:
IntrospectionException- problem creating property descriptor
-
FluentPropertyDescriptor
public FluentPropertyDescriptor(String propertyName, Class<?> beanClass) throws IntrospectionException Creates a new property descriptor, trying to derive the reader and writer methods using a few naming variations, as described in this class documentation.- Parameters:
propertyName- the bean property namebeanClass- the bean class name- Throws:
IntrospectionException- problem creating property descriptor
-
FluentPropertyDescriptor
public FluentPropertyDescriptor(String propertyName, Method readMethod, Method writeMethod) throws IntrospectionException Creates a new property descriptor with the specified write and read method. One (and only one) of the methods can benullto have it automatically detected (if it exists). If both methods are provided, this constructor behaves the same asPropertyDescriptor(String, Method, Method).- Parameters:
propertyName- the bean property namereadMethod- read methodwriteMethod- write method- Throws:
IntrospectionException- problem creating property descriptorNullPointerException- if both read and write methods arenull
-
FluentPropertyDescriptor
public FluentPropertyDescriptor(PropertyDescriptor propertyDescriptor) throws IntrospectionException Copy constructor. If the supplied property descriptor has anullread or write method, it will try to detect one using a few method name variations.- Parameters:
propertyDescriptor- the property descriptor to copy- Throws:
IntrospectionException- problem creating property descriptor
-
-
Method Details
-
isReadable
public boolean isReadable()Gets whether this described property can be read (has a reader method).- Returns:
trueif it can be read- Since:
- 3.0.0
-
isWritable
public boolean isWritable()Gets whether this described property can be written to (has a writer method).- Returns:
trueif it can be written- Since:
- 3.0.0
-
readValue
Reads the bean value matching the property descriptor.- Type Parameters:
T- returned value type- Parameters:
bean- bean to get property value from- Returns:
- a value
- Since:
- 3.0.0
-
writeValue
Writes the bean value matching the property descriptor.- Parameters:
bean- bean to write property tovalue- the value to write- Since:
- 3.0.0
-