Interface PropertyAccessor


public interface PropertyAccessor
Property Accessor SPI 属性访问器SPI

Provides a service provider interface for property access.

为属性访问提供服务提供者接口。

Features | 主要功能:

  • SPI for custom property access on any object type - 用于任意对象类型自定义属性访问的SPI
  • Read and optional write support - 读取和可选写入支持
  • Target type filtering via getSpecificTargetClasses - 通过getSpecificTargetClasses进行目标类型过滤

Usage Examples | 使用示例:

public class JsonPropertyAccessor implements PropertyAccessor {
    @Override
    public Class<?>[] getSpecificTargetClasses() {
        return new Class<?>[]{ JsonNode.class };
    }

    @Override
    public boolean canRead(Object target, String name) {
        return target instanceof JsonNode jn && jn.has(name);
    }

    @Override
    public Object read(Object target, String name) {
        return ((JsonNode) target).get(name);
    }
}

Security | 安全性:

  • Thread-safe: Depends on implementation - 线程安全: 取决于实现
  • Null-safe: Depends on implementation - 空值安全: 取决于实现
Since:
JDK 25, opencode-base-expression V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    canRead(Object target, String name)
    Check if this accessor can read the property 检查此访问器是否可以读取属性
    default boolean
    canWrite(Object target, String name)
    Check if this accessor can write the property 检查此访问器是否可以写入属性
    Class<?>[]
    Get the target types this accessor supports 获取此访问器支持的目标类型
    read(Object target, String name)
    Read the property value 读取属性值
    default void
    write(Object target, String name, Object value)
    Write the property value 写入属性值
  • Method Details

    • getSpecificTargetClasses

      Class<?>[] getSpecificTargetClasses()
      Get the target types this accessor supports 获取此访问器支持的目标类型
      Returns:
      the supported types, or null for all types | 支持的类型,null表示所有类型
    • canRead

      boolean canRead(Object target, String name)
      Check if this accessor can read the property 检查此访问器是否可以读取属性
      Parameters:
      target - the target object | 目标对象
      name - the property name | 属性名
      Returns:
      true if readable | 如果可读返回true
    • read

      Object read(Object target, String name)
      Read the property value 读取属性值
      Parameters:
      target - the target object | 目标对象
      name - the property name | 属性名
      Returns:
      the property value | 属性值
    • canWrite

      default boolean canWrite(Object target, String name)
      Check if this accessor can write the property 检查此访问器是否可以写入属性
      Parameters:
      target - the target object | 目标对象
      name - the property name | 属性名
      Returns:
      true if writable | 如果可写返回true
    • write

      default void write(Object target, String name, Object value)
      Write the property value 写入属性值
      Parameters:
      target - the target object | 目标对象
      name - the property name | 属性名
      value - the value to write | 要写入的值