Class VarHandleAccessor<T>

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

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

High-performance property access using VarHandle. Provides atomic operations and memory fence semantics.

使用VarHandle的高性能属性访问。 提供原子操作和内存屏障语义。

Features | 主要功能:

  • VarHandle-based high-performance access - 基于VarHandle的高性能访问
  • Atomic operations (CAS, getAndSet) - 原子操作(CAS, getAndSet)
  • Volatile and acquire/release semantics - Volatile和获取/释放语义

Usage Examples | 使用示例:

VarHandleAccessor<Counter> accessor = VarHandleAccessor.of(Counter.class, "count");
accessor.compareAndSet(counter, 0, 1);
Object value = accessor.getVolatile(counter);

Security | 安全性:

  • Thread-safe: Yes (VarHandle operations are atomic) - 线程安全: 是(VarHandle操作是原子的)
  • Null-safe: No (caller must ensure non-null target for instance fields) - 空值安全: 否(实例字段须确保非空目标)
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • fromField

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

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

      public VarHandle getVarHandle()
      Gets the underlying VarHandle 获取底层VarHandle
      Returns:
      the VarHandle | VarHandle
    • 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
    • isStatic

      public boolean isStatic()
      Checks if this is a static field 检查是否为静态字段
      Returns:
      true if static | 如果是静态返回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 | 值
    • getVolatile

      public Object getVolatile(T target)
      Gets value with volatile semantics 以volatile语义获取值
      Parameters:
      target - the target object | 目标对象
      Returns:
      the value | 值
    • setVolatile

      public void setVolatile(T target, Object value)
      Sets value with volatile semantics 以volatile语义设置值
      Parameters:
      target - the target object | 目标对象
      value - the value | 值
    • getAndSet

      public Object getAndSet(T target, Object newValue)
      Atomically gets and sets value 原子获取并设置值
      Parameters:
      target - the target object | 目标对象
      newValue - the new value | 新值
      Returns:
      the previous value | 之前的值
    • compareAndSet

      public boolean compareAndSet(T target, Object expectedValue, Object newValue)
      Atomically compares and sets value 原子比较并设置值
      Parameters:
      target - the target object | 目标对象
      expectedValue - the expected value | 期望值
      newValue - the new value | 新值
      Returns:
      true if successful | 如果成功返回true
    • compareAndExchange

      public Object compareAndExchange(T target, Object expectedValue, Object newValue)
      Atomically compares and exchanges value 原子比较并交换值
      Parameters:
      target - the target object | 目标对象
      expectedValue - the expected value | 期望值
      newValue - the new value | 新值
      Returns:
      the witness value | 见证值
    • getAndAdd

      public Object getAndAdd(T target, Object delta)
      Atomically adds to value (for numeric types) 原子加法(用于数值类型)
      Parameters:
      target - the target object | 目标对象
      delta - the delta | 增量
      Returns:
      the previous value | 之前的值
    • getAcquire

      public Object getAcquire(T target)
      Gets value with acquire semantics 以获取语义获取值
      Parameters:
      target - the target object | 目标对象
      Returns:
      the value | 值
    • setRelease

      public void setRelease(T target, Object value)
      Sets value with release semantics 以释放语义设置值
      Parameters:
      target - the target object | 目标对象
      value - the value | 值
    • 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