Class MethodHandleAccessor<T>

java.lang.Object
cloud.opencode.base.reflect.accessor.MethodHandleAccessor<T>
Type Parameters:
T - the target type | 目标类型
All Implemented Interfaces:
PropertyAccessor<T>

public class MethodHandleAccessor<T> extends Object implements PropertyAccessor<T>
MethodHandle-based Property Accessor 基于MethodHandle的属性访问器

High-performance property access using MethodHandle. Faster than reflection after warmup.

使用MethodHandle的高性能属性访问。 预热后比反射更快。

Features | 主要功能:

  • MethodHandle-based high-performance access - 基于MethodHandle的高性能访问
  • Field and method-based accessor creation - 基于字段和方法的访问器创建
  • Faster than reflection after JIT warmup - JIT预热后比反射更快

Usage Examples | 使用示例:

MethodHandleAccessor<User> accessor = MethodHandleAccessor.of(User.class, "name");
String name = (String) accessor.get(user);
accessor.set(user, "Alice");

Security | 安全性:

  • Thread-safe: Yes (MethodHandle is immutable and thread-safe) - 线程安全: 是(MethodHandle不可变且线程安全)
  • Null-safe: No (caller must ensure non-null target) - 空值安全: 否(调用方须确保非空目标)
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • fromField

      public static <T> MethodHandleAccessor<T> fromField(Field field)
      Creates a MethodHandleAccessor from a field 从字段创建MethodHandleAccessor
      Type Parameters:
      T - the target type | 目标类型
      Parameters:
      field - the field | 字段
      Returns:
      the accessor | 访问器
    • fromMethods

      public static <T> MethodHandleAccessor<T> fromMethods(String name, Class<T> declaringClass, Method getter, Method setter)
      Creates a MethodHandleAccessor from getter/setter methods 从getter/setter方法创建MethodHandleAccessor
      Type Parameters:
      T - the target type | 目标类型
      Parameters:
      name - the property name | 属性名
      declaringClass - the declaring class | 声明类
      getter - the getter method (can be null) | getter方法(可为null)
      setter - the setter method (can be null) | setter方法(可为null)
      Returns:
      the accessor | 访问器
    • of

      public static <T> MethodHandleAccessor<T> of(Class<T> clazz, String fieldName)
      Creates a MethodHandleAccessor for a field by name 按名称为字段创建MethodHandleAccessor
      Type Parameters:
      T - the target type | 目标类型
      Parameters:
      clazz - the class | 类
      fieldName - the field name | 字段名
      Returns:
      the accessor | 访问器
    • getName

      public String getName()
      Description copied from interface: PropertyAccessor
      Gets the property name 获取属性名
      Specified by:
      getName in interface PropertyAccessor<T>
      Returns:
      the property name | 属性名
    • getType

      public Class<?> getType()
      Description copied from interface: PropertyAccessor
      Gets the property type 获取属性类型
      Specified by:
      getType in interface PropertyAccessor<T>
      Returns:
      the property type | 属性类型
    • getGenericType

      public Type getGenericType()
      Description copied from interface: PropertyAccessor
      Gets the generic property type 获取泛型属性类型
      Specified by:
      getGenericType in interface PropertyAccessor<T>
      Returns:
      the generic type | 泛型类型
    • getDeclaringClass

      public Class<T> getDeclaringClass()
      Description copied from interface: PropertyAccessor
      Gets the declaring class 获取声明类
      Specified by:
      getDeclaringClass in interface PropertyAccessor<T>
      Returns:
      the declaring class | 声明类
    • isReadable

      public boolean isReadable()
      Description copied from interface: PropertyAccessor
      Checks if property is readable 检查属性是否可读
      Specified by:
      isReadable in interface PropertyAccessor<T>
      Returns:
      true if readable | 如果可读返回true
    • isWritable

      public boolean isWritable()
      Description copied from interface: PropertyAccessor
      Checks if property is writable 检查属性是否可写
      Specified by:
      isWritable in interface PropertyAccessor<T>
      Returns:
      true if writable | 如果可写返回true
    • get

      public Object get(T target)
      Description copied from interface: PropertyAccessor
      Gets the property value 获取属性值
      Specified by:
      get in interface PropertyAccessor<T>
      Parameters:
      target - the target object | 目标对象
      Returns:
      the value | 值
    • set

      public void set(T target, Object value)
      Description copied from interface: PropertyAccessor
      Sets the property value 设置属性值
      Specified by:
      set in interface PropertyAccessor<T>
      Parameters:
      target - the target object | 目标对象
      value - the value | 值
    • getGetterHandle

      public MethodHandle getGetterHandle()
      Gets the getter MethodHandle 获取getter MethodHandle
      Returns:
      the getter handle or null | getter句柄或null
    • getSetterHandle

      public MethodHandle getSetterHandle()
      Gets the setter MethodHandle 获取setter MethodHandle
      Returns:
      the setter handle or null | setter句柄或null
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object