Class ReflectivePropertyAccessor
- All Implemented Interfaces:
- PropertyAccessor,- TargetedAccessor
- Direct Known Subclasses:
- DataBindingPropertyAccessor
PropertyAccessor that uses reflection to access properties
 for reading and possibly also for writing on a target instance.
 A property can be referenced through a public getter method (when being read) or a public setter method (when being written), and also through a public field.
- Since:
- 3.0
- Author:
- Andy Clement, Juergen Hoeller, Phillip Webb, Sam Brannen, Sebastien Deleuze
- See Also:
- 
Constructor SummaryConstructorsConstructorDescriptionCreate a new property accessor for reading as well as writing.ReflectivePropertyAccessor(boolean allowWrite) Create a new property accessor for reading and possibly also writing.
- 
Method SummaryModifier and TypeMethodDescriptionbooleancanRead(EvaluationContext context, Object target, String name) Called to determine if this property accessor is able to read a specified property on a specified target object.booleancanWrite(EvaluationContext context, Object target, String name) Called to determine if this property accessor is able to write to a specified property on a specified target object.createOptimalAccessor(EvaluationContext context, Object target, String name) Attempt to create an optimized property accessor tailored for a property of a particular name on a particular class.protected FieldFind a field of a certain name on a specified class.protected MethodfindGetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic) Find a getter method for the specified property.protected MethodfindSetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic) Find a setter method for the specified property.protected StringgetPropertyMethodSuffix(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<?>[]Returnsnullwhich means this is a general purpose accessor.protected booleanisCandidateForProperty(Method method, Class<?> targetClass) Determine whether the givenMethodis a candidate for property access on an instance of the given target class.read(EvaluationContext context, Object target, String name) Called to read a property from a specified target object.voidwrite(EvaluationContext context, Object target, String name, Object newValue) Called to write to a property on a specified target object.
- 
Constructor Details- 
ReflectivePropertyAccessorpublic ReflectivePropertyAccessor()Create a new property accessor for reading as well as writing.- See Also:
 
- 
ReflectivePropertyAccessorpublic ReflectivePropertyAccessor(boolean allowWrite) Create a new property accessor for reading and possibly also writing.- Parameters:
- allowWrite- whether to allow write operations on a target instance
- Since:
- 4.3.15
- See Also:
 
 
- 
- 
Method Details- 
getSpecificTargetClassesReturnsnullwhich means this is a general purpose accessor.- Specified by:
- getSpecificTargetClassesin interface- TargetedAccessor
- Returns:
- an array of classes that this accessor is suitable for
 (or nullor an empty array if a generic accessor)
 
- 
canReadpublic boolean canRead(EvaluationContext context, @Nullable Object target, String name) throws AccessException Description copied from interface:PropertyAccessorCalled to determine if this property accessor is able to read a specified property on a specified target object.- Specified by:
- canReadin 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 property accessor is able to read the property
- Throws:
- AccessException- if there is any problem determining whether the property can be read
 
- 
readpublic TypedValue read(EvaluationContext context, @Nullable Object target, String name) throws AccessException Description copied from interface:PropertyAccessorCalled 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 returnstrue.- Specified by:
- readin 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 reading the property value
 
- 
canWritepublic boolean canWrite(EvaluationContext context, @Nullable Object target, String name) throws AccessException Description copied from interface:PropertyAccessorCalled to determine if this property accessor is able to write to a specified property on a specified target object.- Specified by:
- canWritein 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 property accessor is able to write to the property
- Throws:
- AccessException- if there is any problem determining whether the property can be written to
 
- 
writepublic void write(EvaluationContext context, @Nullable Object target, String name, @Nullable Object newValue) throws AccessException Description copied from interface:PropertyAccessorCalled 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 returnstrue.- Specified by:
- writein 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@Nullable protected Method findGetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic) Find a getter method for the specified property.
- 
findSetterForProperty@Nullable protected Method findSetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic) Find a setter method for the specified property.
- 
isCandidateForPropertyDetermine whether the givenMethodis a candidate for property access on an instance of the given target class.The default implementation considers any method as a candidate, even for non-user-declared properties on the Objectbase class.- Parameters:
- method- the Method to evaluate
- targetClass- the concrete target class that is being introspected
- Since:
- 4.3.15
 
- 
getPropertyMethodSuffixesReturn 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()'.
- 
getPropertyMethodSuffixReturn the method suffix for a given property name. The default implementation uses JavaBean conventions.
- 
findFieldFind a field of a certain name on a specified class.
- 
createOptimalAccessorpublic PropertyAccessor createOptimalAccessor(EvaluationContext context, @Nullable 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 ReflectivePropertyAccessorwill always work but is not optimal due to the need to look up which reflective member (method or field) to use each timeread(EvaluationContext, Object, String)is called.This method will return this ReflectivePropertyAccessorinstance if it is unable to build an optimized accessor.Note: An optimized accessor is currently only usable for read attempts. Do not call this method if you need a read-write accessor. 
 
-