Class FluentPropertyDescriptor

java.lang.Object
java.beans.FeatureDescriptor
java.beans.PropertyDescriptor
com.norconex.commons.lang.bean.FluentPropertyDescriptor

public class FluentPropertyDescriptor extends PropertyDescriptor

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" instance
  • void foo(Foo foo)
  • Self foo(Foo foo) //Self is "this" instance
Version:
2.0.0
  • 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 is null or blank, it will try to derive it using a few method name variations, as described in this class documentation.
      Parameters:
      propertyName - the bean property name
      beanClass - the bean class name
      readMethodName - 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 name
      beanClass - 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 be null to have it automatically detected (if it exists). If both methods are provided, this constructor behaves the same as PropertyDescriptor(String, Method, Method).
      Parameters:
      propertyName - the bean property name
      readMethod - read method
      writeMethod - write method
      Throws:
      IntrospectionException - problem creating property descriptor
      NullPointerException - if both read and write methods are null
    • FluentPropertyDescriptor

      public FluentPropertyDescriptor(PropertyDescriptor propertyDescriptor) throws IntrospectionException
      Copy constructor. If the supplied property descriptor has a null read 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:
      true if 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:
      true if it can be written
      Since:
      3.0.0
    • readValue

      public <T> T readValue(Object bean)
      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

      public void writeValue(Object bean, Object value)
      Writes the bean value matching the property descriptor.
      Parameters:
      bean - bean to write property to
      value - the value to write
      Since:
      3.0.0