PropertyAccessor class abstract interface

A plug-in that tells expressions how to read and write the properties of some kind of object, so that point.x works.

Java's SpEL reads any public getter by reflection; Dart (in Flutter and AOT builds) has no reflection, so an expression can only see a property some accessor knows. The contexts already bring accessors for maps (MapAccessor), @Introspected classes (IntrospectionPropertyAccessor) and core Dart types (BuiltinPropertyAccessor). For a class that is not @Introspected, a FunctionPropertyAccessor is usually enough; implement this interface for anything more dynamic (a JSON document, a record from a database). For each property the context asks its accessors in order and the first whose canRead (or canWrite) returns true handles it.

// A Point class from another package, without introspection data.
final class PointAccessor implements PropertyAccessor {
  @override
  bool canRead(EvaluationContext c, Object target, String name) =>
      target is Point && (name == 'x' || name == 'y');

  @override
  Object? read(EvaluationContext c, Object target, String name) =>
      name == 'x' ? (target as Point).x : (target as Point).y;

  @override
  bool canWrite(EvaluationContext c, Object target, String name) =>
      false;

  @override
  void write(EvaluationContext c, Object t, String n, Object? v) =>
      throw UnsupportedError('read-only');
}

final context = StandardEvaluationContext(const Point(1, 2))
  ..addPropertyAccessor(PointAccessor());

Spring (Java):

public class PointAccessor implements PropertyAccessor {
  public Class<?>[] getSpecificTargetClasses() {
    return new Class<?>[] {Point.class};
  }
  public boolean canRead(EvaluationContext c, Object t, String name) {
    return name.equals("x") || name.equals("y");
  }
  public TypedValue read(EvaluationContext c, Object t, String name) {
    Point p = (Point) t;
    return new TypedValue(name.equals("x") ? p.x : p.y);
  }
  // canWrite / write ...
}
context.addPropertyAccessor(new PointAccessor());

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.
canWrite(EvaluationContext context, Object target, String name) → bool
Whether this accessor handles writing property name of target; return false for read-only properties.
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.
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.

Operators

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