public class FluentPropertyBeanIntrospector extends java.lang.Object implements BeanIntrospector
An implementation of the BeanIntrospector
interface which can
detect write methods for properties used in fluent API scenario.
A fluent API allows setting multiple properties using a single statement by supporting so-called method chaining: Methods for setting a property value do not return void, but an object which can be called for setting another property. An example of such a fluent API could look as follows:
public class FooBuilder { public FooBuilder setFooProperty1(String value) { ... return this; } public FooBuilder setFooProperty2(int value) { ... return this; } }Per default,
PropertyUtils
does not detect methods like this
because, having a non-void return type, they violate the Java Beans
specification.
This class is more tolerant with regards to the return type of a set method.
It basically iterates over all methods of a class and filters them for a
configurable prefix (the default prefix is set
). It then
generates corresponding PropertyDescriptor
objects for the
methods found which use these methods as write methods.
An instance of this class is intended to collaborate with a
DefaultBeanIntrospector
object. So best results are achieved by
adding this instance as custom BeanIntrospector
after the
DefaultBeanIntrospector
object. Then default introspection finds
read-only properties because it does not detect the write methods with a
non-void return type. FluentPropertyBeanIntrospector
completes the descriptors for these properties by setting the correct write
method.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_WRITE_METHOD_PREFIX
The default prefix for write methods.
|
private org.apache.commons.logging.Log |
log
The logger.
|
private java.lang.String |
writeMethodPrefix
The prefix of write methods to search for.
|
Constructor and Description |
---|
FluentPropertyBeanIntrospector()
Creates a new instance of
FluentPropertyBeanIntrospector and
sets the default prefix for write methods. |
FluentPropertyBeanIntrospector(java.lang.String writePrefix)
Creates a new instance of
FluentPropertyBeanIntrospector and
initializes it with the prefix for write methods used by the classes to
be inspected. |
Modifier and Type | Method and Description |
---|---|
private java.beans.PropertyDescriptor |
createFluentPropertyDescritor(java.lang.reflect.Method m,
java.lang.String propertyName)
Creates a property descriptor for a fluent API property.
|
java.lang.String |
getWriteMethodPrefix()
Returns the prefix for write methods this instance scans for.
|
void |
introspect(IntrospectionContext icontext)
Performs introspection.
|
private java.lang.String |
propertyName(java.lang.reflect.Method m)
Derives the name of a property from the given set method.
|
public static final java.lang.String DEFAULT_WRITE_METHOD_PREFIX
private final org.apache.commons.logging.Log log
private final java.lang.String writeMethodPrefix
public FluentPropertyBeanIntrospector(java.lang.String writePrefix)
FluentPropertyBeanIntrospector
and
initializes it with the prefix for write methods used by the classes to
be inspected.writePrefix
- the prefix for write methods (must not be null)java.lang.IllegalArgumentException
- if the prefix is nullpublic FluentPropertyBeanIntrospector()
FluentPropertyBeanIntrospector
and
sets the default prefix for write methods.public java.lang.String getWriteMethodPrefix()
public void introspect(IntrospectionContext icontext) throws java.beans.IntrospectionException
introspect
in interface BeanIntrospector
icontext
- the introspection contextjava.beans.IntrospectionException
- if an error occursprivate java.lang.String propertyName(java.lang.reflect.Method m)
m
- the methodprivate java.beans.PropertyDescriptor createFluentPropertyDescritor(java.lang.reflect.Method m, java.lang.String propertyName) throws java.beans.IntrospectionException
m
- the set method for the fluent API propertypropertyName
- the name of the corresponding propertyjava.beans.IntrospectionException
- if an error occursCopyright (c) 2000-2008 - Apache Software Foundation