org.springframework.expression.spel.support

## Class ReflectivePropertyAccessor

- java.lang.Object
- - org.springframework.expression.spel.support.ReflectivePropertyAccessor

- All Implemented Interfaces:
    PropertyAccessor

      public class ReflectivePropertyAccessor
      extends Object
      implements PropertyAccessor

  Simple `PropertyAccessor` that uses reflection to access properties for reading and writing.

  A property can be accessed through a public getter method (when being read) or a public setter method (when being written), and also as a public field.

  Since:
    3.0
  Author:
    Andy Clement, Juergen Hoeller, Phillip Webb

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `ReflectivePropertyAccessor()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `canRead(EvaluationContext context, Object target, String name)` Called to determine if a resolver instance is able to access a specified property on a specified target object.
    `boolean` | `canWrite(EvaluationContext context, Object target, String name)` Called to determine if a resolver instance is able to write to a specified property on a specified target object.
    `PropertyAccessor` | `createOptimalAccessor(EvaluationContext eContext, Object target, String name)` Attempt to create an optimized property accessor tailored for a property of a particular name on a particular class.
    `protected Field` | `findField(String name, Class<?> clazz, boolean mustBeStatic)` Find a field of a certain name on a specified class.
    `protected Method` | `findGetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic)` Find a getter method for the specified property.
    `protected Method` | `findSetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic)` Find a setter method for the specified property.
    `protected String` | `getPropertyMethodSuffix(String propertyName)` Return the method suffix for a given property name.
    `protected String[]` | `getPropertyMethodSuffixes(String propertyName)` Return the method suffixes for a given property name.
    `Class<?>[]` | `getSpecificTargetClasses()` Returns `null` which means this is a general purpose accessor.
    `TypedValue` | `read(EvaluationContext context, Object target, String name)` Called to read a property from a specified target object.
    `void` | `write(EvaluationContext context, Object target, String name, Object newValue)` Called to write to a property on a specified target object.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### ReflectivePropertyAccessor

          public ReflectivePropertyAccessor()

  - ### Method Detail

    - #### getSpecificTargetClasses

          public Class<?>[] getSpecificTargetClasses()

      Returns `null` which means this is a general purpose accessor.
      Specified by:
        `getSpecificTargetClasses` in interface `PropertyAccessor`
      Returns:
        an array of classes that this resolver is suitable for (or `null` if a general resolver)

    - #### canRead

          public boolean canRead(EvaluationContext context,
                                 Object target,
                                 String name)
                          throws AccessException

      Description copied from interface: `PropertyAccessor`
      Called to determine if a resolver instance is able to access a specified property on a specified target object.
      Specified by:
        `canRead` in interface `PropertyAccessor`
      Parameters:
        `context` - the evaluation context in which the access is being attempted
        `target` - the target object upon which the property is being accessed
        `name` - the name of the property being accessed
      Returns:
        true if this resolver is able to read the property
      Throws:
        `AccessException` - if there is any problem determining whether the property can be read

    - #### read

          public TypedValue read(EvaluationContext context,
                                 Object target,
                                 String name)
                          throws AccessException

      Description copied from interface: `PropertyAccessor`
      Called to read a property from a specified target object. Should only succeed if `PropertyAccessor.canRead(org.springframework.expression.EvaluationContext, java.lang.Object, java.lang.String)` also returns `true`.
      Specified by:
        `read` in interface `PropertyAccessor`
      Parameters:
        `context` - the evaluation context in which the access is being attempted
        `target` - the target object upon which the property is being accessed
        `name` - the name of the property being accessed
      Returns:
        a TypedValue object wrapping the property value read and a type descriptor for it
      Throws:
        `AccessException` - if there is any problem accessing the property value

    - #### canWrite

          public boolean canWrite(EvaluationContext context,
                                  Object target,
                                  String name)
                           throws AccessException

      Description copied from interface: `PropertyAccessor`
      Called to determine if a resolver instance is able to write to a specified property on a specified target object.
      Specified by:
        `canWrite` in interface `PropertyAccessor`
      Parameters:
        `context` - the evaluation context in which the access is being attempted
        `target` - the target object upon which the property is being accessed
        `name` - the name of the property being accessed
      Returns:
        true if this resolver is able to write to the property
      Throws:
        `AccessException` - if there is any problem determining whether the property can be written to

    - #### write

          public void write(EvaluationContext context,
                            Object target,
                            String name,
                            Object newValue)
                     throws AccessException

      Description copied from interface: `PropertyAccessor`
      Called to write to a property on a specified target object. Should only succeed if `PropertyAccessor.canWrite(org.springframework.expression.EvaluationContext, java.lang.Object, java.lang.String)` also returns `true`.
      Specified by:
        `write` in interface `PropertyAccessor`
      Parameters:
        `context` - the evaluation context in which the access is being attempted
        `target` - the target object upon which the property is being accessed
        `name` - the name of the property being accessed
        `newValue` - the new value for the property
      Throws:
        `AccessException` - if there is any problem writing to the property value

    - #### findGetterForProperty

          protected Method findGetterForProperty(String propertyName,
                                                 Class<?> clazz,
                                                 boolean mustBeStatic)

      Find a getter method for the specified property.

    - #### findSetterForProperty

          protected Method findSetterForProperty(String propertyName,
                                                 Class<?> clazz,
                                                 boolean mustBeStatic)

      Find a setter method for the specified property.

    - #### getPropertyMethodSuffixes

          protected String[] getPropertyMethodSuffixes(String propertyName)

      Return the method suffixes for a given property name. The default implementation uses JavaBean conventions with additional support for properties of the form 'xY' where the method 'getXY()' is used in preference to the JavaBean convention of 'getxY()'.

    - #### getPropertyMethodSuffix

          protected String getPropertyMethodSuffix(String propertyName)

      Return the method suffix for a given property name. The default implementation uses JavaBean conventions.

    - #### findField

          protected Field findField(String name,
                                    Class<?> clazz,
                                    boolean mustBeStatic)

      Find a field of a certain name on a specified class.

    - #### createOptimalAccessor

          public PropertyAccessor createOptimalAccessor(EvaluationContext eContext,
                                                        Object target,
                                                        String name)

      Attempt to create an optimized property accessor tailored for a property of a particular name on a particular class. The general ReflectivePropertyAccessor will always work but is not optimal due to the need to lookup which reflective member (method/field) to use each time read() is called. This method will just return the ReflectivePropertyAccessor instance if it is unable to build something more optimal.