public class ReflectivePropertyAccessor extends java.lang.Object implements PropertyAccessor
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 as a public field.
StandardEvaluationContext
,
SimpleEvaluationContext
,
DataBindingPropertyAccessor
Modifier and Type | Class and Description |
---|---|
private static class |
ReflectivePropertyAccessor.InvokerPair
Captures the member (method/field) to call reflectively to access a property value
and the type descriptor for the value returned by the reflective call.
|
static class |
ReflectivePropertyAccessor.OptimalPropertyAccessor
An optimized form of a PropertyAccessor that will use reflection but only knows
how to access a particular property on a particular class.
|
private static class |
ReflectivePropertyAccessor.PropertyCacheKey |
Modifier and Type | Field and Description |
---|---|
private boolean |
allowWrite |
private static java.util.Set<java.lang.Class<?>> |
ANY_TYPES |
private static java.util.Set<java.lang.Class<?>> |
BOOLEAN_TYPES |
private ReflectivePropertyAccessor.InvokerPair |
lastReadInvokerPair |
private java.util.Map<ReflectivePropertyAccessor.PropertyCacheKey,ReflectivePropertyAccessor.InvokerPair> |
readerCache |
private java.util.Map<java.lang.Class<?>,java.lang.reflect.Method[]> |
sortedMethodsCache |
private java.util.Map<ReflectivePropertyAccessor.PropertyCacheKey,TypeDescriptor> |
typeDescriptorCache |
private java.util.Map<ReflectivePropertyAccessor.PropertyCacheKey,java.lang.reflect.Member> |
writerCache |
Constructor and Description |
---|
ReflectivePropertyAccessor()
Create a new property accessor for reading as well writing.
|
ReflectivePropertyAccessor(boolean allowWrite)
Create a new property accessor for reading and possibly also writing.
|
Modifier and Type | Method and Description |
---|---|
boolean |
canRead(EvaluationContext context,
java.lang.Object target,
java.lang.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,
java.lang.Object target,
java.lang.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 context,
java.lang.Object target,
java.lang.String name)
Attempt to create an optimized property accessor tailored for a property of a
particular name on a particular class.
|
protected java.lang.reflect.Field |
findField(java.lang.String name,
java.lang.Class<?> clazz,
boolean mustBeStatic)
Find a field of a certain name on a specified class.
|
private java.lang.reflect.Field |
findField(java.lang.String name,
java.lang.Class<?> clazz,
java.lang.Object target) |
protected java.lang.reflect.Method |
findGetterForProperty(java.lang.String propertyName,
java.lang.Class<?> clazz,
boolean mustBeStatic)
Find a getter method for the specified property.
|
private java.lang.reflect.Method |
findGetterForProperty(java.lang.String propertyName,
java.lang.Class<?> clazz,
java.lang.Object target) |
private java.lang.reflect.Method |
findMethodForProperty(java.lang.String[] methodSuffixes,
java.lang.String prefix,
java.lang.Class<?> clazz,
boolean mustBeStatic,
int numberOfParams,
java.util.Set<java.lang.Class<?>> requiredReturnTypes) |
protected java.lang.reflect.Method |
findSetterForProperty(java.lang.String propertyName,
java.lang.Class<?> clazz,
boolean mustBeStatic)
Find a setter method for the specified property.
|
private java.lang.reflect.Method |
findSetterForProperty(java.lang.String propertyName,
java.lang.Class<?> clazz,
java.lang.Object target) |
java.lang.reflect.Member |
getLastReadInvokerPair()
Deprecated.
as of 4.3.15 since it is not used within the framework anymore
|
protected java.lang.String |
getPropertyMethodSuffix(java.lang.String propertyName)
Return the method suffix for a given property name.
|
protected java.lang.String[] |
getPropertyMethodSuffixes(java.lang.String propertyName)
Return the method suffixes for a given property name.
|
private java.lang.reflect.Method[] |
getSortedMethods(java.lang.Class<?> clazz)
Return class methods ordered with non-bridge methods appearing higher.
|
java.lang.Class<?>[] |
getSpecificTargetClasses()
Returns
null which means this is a general purpose accessor. |
private TypeDescriptor |
getTypeDescriptor(EvaluationContext context,
java.lang.Object target,
java.lang.String name) |
protected boolean |
isCandidateForProperty(java.lang.reflect.Method method,
java.lang.Class<?> targetClass)
Determine whether the given
Method is a candidate for property access
on an instance of the given target class. |
TypedValue |
read(EvaluationContext context,
java.lang.Object target,
java.lang.String name)
Called to read a property from a specified target object.
|
void |
write(EvaluationContext context,
java.lang.Object target,
java.lang.String name,
java.lang.Object newValue)
Called to write to a property on a specified target object.
|
private static final java.util.Set<java.lang.Class<?>> ANY_TYPES
private static final java.util.Set<java.lang.Class<?>> BOOLEAN_TYPES
private final boolean allowWrite
private final java.util.Map<ReflectivePropertyAccessor.PropertyCacheKey,ReflectivePropertyAccessor.InvokerPair> readerCache
private final java.util.Map<ReflectivePropertyAccessor.PropertyCacheKey,java.lang.reflect.Member> writerCache
private final java.util.Map<ReflectivePropertyAccessor.PropertyCacheKey,TypeDescriptor> typeDescriptorCache
private final java.util.Map<java.lang.Class<?>,java.lang.reflect.Method[]> sortedMethodsCache
private volatile ReflectivePropertyAccessor.InvokerPair lastReadInvokerPair
public ReflectivePropertyAccessor()
ReflectivePropertyAccessor(boolean)
public ReflectivePropertyAccessor(boolean allowWrite)
allowWrite
- whether to allow write operations on a target instancecanWrite(org.springframework.expression.EvaluationContext, java.lang.Object, java.lang.String)
public java.lang.Class<?>[] getSpecificTargetClasses()
null
which means this is a general purpose accessor.getSpecificTargetClasses
in interface PropertyAccessor
null
if a general resolver)public boolean canRead(EvaluationContext context, java.lang.Object target, java.lang.String name) throws AccessException
PropertyAccessor
canRead
in interface PropertyAccessor
context
- the evaluation context in which the access is being attemptedtarget
- the target object upon which the property is being accessedname
- the name of the property being accessedAccessException
- if there is any problem determining whether the property can be readpublic TypedValue read(EvaluationContext context, java.lang.Object target, java.lang.String name) throws AccessException
PropertyAccessor
PropertyAccessor.canRead(org.springframework.expression.EvaluationContext, java.lang.Object, java.lang.String)
also returns true
.read
in interface PropertyAccessor
context
- the evaluation context in which the access is being attemptedtarget
- the target object upon which the property is being accessedname
- the name of the property being accessedAccessException
- if there is any problem accessing the property valuepublic boolean canWrite(EvaluationContext context, java.lang.Object target, java.lang.String name) throws AccessException
PropertyAccessor
canWrite
in interface PropertyAccessor
context
- the evaluation context in which the access is being attemptedtarget
- the target object upon which the property is being accessedname
- the name of the property being accessedAccessException
- if there is any problem determining whether the
property can be written topublic void write(EvaluationContext context, java.lang.Object target, java.lang.String name, java.lang.Object newValue) throws AccessException
PropertyAccessor
PropertyAccessor.canWrite(org.springframework.expression.EvaluationContext, java.lang.Object, java.lang.String)
also returns true
.write
in interface PropertyAccessor
context
- the evaluation context in which the access is being attemptedtarget
- the target object upon which the property is being accessedname
- the name of the property being accessednewValue
- the new value for the propertyAccessException
- if there is any problem writing to the property value@Deprecated public java.lang.reflect.Member getLastReadInvokerPair()
private TypeDescriptor getTypeDescriptor(EvaluationContext context, java.lang.Object target, java.lang.String name)
private java.lang.reflect.Method findGetterForProperty(java.lang.String propertyName, java.lang.Class<?> clazz, java.lang.Object target)
private java.lang.reflect.Method findSetterForProperty(java.lang.String propertyName, java.lang.Class<?> clazz, java.lang.Object target)
protected java.lang.reflect.Method findGetterForProperty(java.lang.String propertyName, java.lang.Class<?> clazz, boolean mustBeStatic)
protected java.lang.reflect.Method findSetterForProperty(java.lang.String propertyName, java.lang.Class<?> clazz, boolean mustBeStatic)
private java.lang.reflect.Method findMethodForProperty(java.lang.String[] methodSuffixes, java.lang.String prefix, java.lang.Class<?> clazz, boolean mustBeStatic, int numberOfParams, java.util.Set<java.lang.Class<?>> requiredReturnTypes)
private java.lang.reflect.Method[] getSortedMethods(java.lang.Class<?> clazz)
protected boolean isCandidateForProperty(java.lang.reflect.Method method, java.lang.Class<?> targetClass)
Method
is 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 Object
base class.
method
- the Method to evaluatetargetClass
- the concrete target class that is being introspectedprotected java.lang.String[] getPropertyMethodSuffixes(java.lang.String propertyName)
protected java.lang.String getPropertyMethodSuffix(java.lang.String propertyName)
private java.lang.reflect.Field findField(java.lang.String name, java.lang.Class<?> clazz, java.lang.Object target)
protected java.lang.reflect.Field findField(java.lang.String name, java.lang.Class<?> clazz, boolean mustBeStatic)
public PropertyAccessor createOptimalAccessor(EvaluationContext context, java.lang.Object target, java.lang.String name)
Note: An optimal accessor is currently only usable for read attempts. Do not call this method if you need a read-write accessor.