FunctionPropertyAccessor<T extends Object> class final

A PropertyAccessor for objects of type T, built from one getter (and optionally one setter) function per property name.

Use it to make a class readable in expressions when it is not @Introspected, for instance a class from another package: list the properties expressions may see, nothing else is exposed. IntrospectionPropertyAccessor does the same for @Introspected classes from generated metadata.

// `name` can be read and assigned on a User.
final context = StandardEvaluationContext(user)
  ..addPropertyAccessor(FunctionPropertyAccessor<User>(
    getters: {'name': (u) => u.name},
    setters: {'name': (u, v) => u.name = v! as String},
  ));
SpelExpressionParser().parseExpression('name').getValue(context);
SpelExpressionParser()
    .parseExpression("name = 'Ada'")
    .getValue(context);

Spring (Java):

// Spring reads getName()/setName() by reflection
// (ReflectivePropertyAccessor), so no registration is needed:
parser.parseExpression("name")
    .getValue(new StandardEvaluationContext(user));
Implemented types

Constructors

FunctionPropertyAccessor({Map<String, Object? Function(T target)> getters = const {}, Map<String, void Function(T target, Object? value)> setters = const {}})
Exposes getters (readable properties) and setters (writable properties) of T, keyed by property name. A setter receives the value unconverted, so it casts it itself.

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

canRead(EvaluationContext context, Object target, String name) → bool
Whether this accessor handles reading property name of target; false lets the next accessor try. target is never null.
override
canWrite(EvaluationContext context, Object target, String name) → bool
Whether this accessor handles writing property name of target; return false for read-only properties.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
read(EvaluationContext context, Object target, String name) → Object?
The value of property name of target; only called after canRead returned true.
override
toString() → String
A string representation of this object.
inherited
write(EvaluationContext context, Object target, String name, Object? newValue) → void
Stores newValue in property name of target; only called after canWrite returned true.
override

Operators

operator ==(Object other) → bool
The equality operator.
inherited