Package org.codehaus.groovy.reflection

# Class CachedField

java.lang.Object
groovy.lang.MetaProperty
org.codehaus.groovy.reflection.CachedField

All Implemented Interfaces:
  `MetaMember`

public class CachedField extends MetaProperty
Caches reflection information about a single field for efficient access and modification.

Extends `MetaProperty` to integrate with the meta-programming framework. Handles lazy field accessibility and provides property-like access to field values.

- ## Field Summary

  ### Fields inherited from class groovy.lang.MetaProperty

  `name, PROPERTY_SET_PREFIX, type`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `CachedField(Field field)`
  Constructs a `CachedField` for the given Java field.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `MethodHandle`
  `asAccessMethod(MethodHandles.Lookup lookup)`
  Creates a method handle that provides getter access to this field via MethodHandles API.
  `MethodHandle`
  `asWriteAccessMethod(MethodHandles.Lookup lookup)`
  Creates a method handle that writes this field via the MethodHandles API, the mutating counterpart of `asAccessMethod(MethodHandles.Lookup)`, with the same caching policy.
  `boolean`
  `canAccessViaReflection()`
  Determines whether the reflective access path (`Field.get`/`set`) can reach this field: deep access has been, or can now be, established.
  `Field`
  `getCachedField()`
  Returns the underlying Java `Field` object, making it accessible if necessary.
  `Class`
  `getDeclaringClass()`
  Returns the class that declares this field.
  `int`
  `getModifiers()`
  Returns the access modifiers.
  `Object`
  `getProperty(Object object)`
  `boolean`
  `isAnnotationPresent(Class<? extends Annotation> annotationType)`
  Checks whether the underlying field has the specified annotation.
  `void`
  `setProperty(Object object, Object newValue)`
  Sets the property on the given object to the new value.

  ### Methods inherited from class groovy.lang.MetaProperty

  `getGetterName, getName, getSetterName, getType`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface groovy.lang.MetaMember

  `isFinal, isPackagePrivate, isPrivate, isProtected, isPublic, isStatic, isSynthetic`

- ## Constructor Details

  - ### CachedField

    public CachedField(Field field)
    Constructs a `CachedField` for the given Java field.
    Parameters:
      `field` - the field to cache reflection information for

- ## Method Details

  - ### canAccessViaReflection

    public boolean canAccessViaReflection()
    Determines whether the reflective access path (`Field.get`/`set`) can reach this field: deep access has been, or can now be, established. The reflective path's callers use this to treat an unreachable field (strongly encapsulated declaring class) as absent instead of failing when it is read or written. It says nothing about what a caller's own `Lookup` may reach -- see `asAccessMethod(MethodHandles.Lookup)` -- and so must not be used to decide member selection or call-site linkage.
    Since:
      6.0.0

  - ### getCachedField

    public Field getCachedField()
    Returns the underlying Java `Field` object, making it accessible if necessary.
    Returns:
      the cached field with accessibility ensured

  - ### getDeclaringClass

    public Class getDeclaringClass()
    Returns the class that declares this field.
    Returns:
      the declaring class

  - ### isAnnotationPresent

    public boolean isAnnotationPresent(Class<? extends Annotation> annotationType)
    Checks whether the underlying field has the specified annotation. Unlike `getCachedField()`, this does not trigger accessibility changes.
    Since:
      6.0.0

  - ### getModifiers

    public int getModifiers()
    Returns the access modifiers.
    Specified by:
      `getModifiers` in interface `MetaMember`
    Overrides:
      `getModifiers` in class `MetaProperty`
    Returns:
      `Modifier.PUBLIC`

  - ### getProperty

    public Object getProperty(Object object)
    Specified by:
      `getProperty` in class `MetaProperty`
    Returns:
      the property of the given object

  - ### setProperty

    public void setProperty(Object object, Object newValue)
    Sets the property on the given object to the new value.
    Specified by:
      `setProperty` in class `MetaProperty`
    Parameters:
      `object` - on which to set the property
      `newValue` - the new value of the property

  - ### asAccessMethod

    public MethodHandle asAccessMethod(MethodHandles.Lookup lookup) throws IllegalAccessException
    Creates a method handle that provides getter access to this field via MethodHandles API. When deep-reflection access can be established (`setAccessible` on the field), the resulting handle is caller-independent and is created once and cached; otherwise the given caller lookup decides, and its handle -- which carries that caller's access rights -- is never cached.
    Parameters:
      `lookup` - the method handles lookup context of the caller
    Returns:
      a method handle providing getter access to this field
    Throws:
      `IllegalAccessException` - if neither deep reflection nor the caller's lookup can access the field

  - ### asWriteAccessMethod

    public MethodHandle asWriteAccessMethod(MethodHandles.Lookup lookup) throws IllegalAccessException
    Creates a method handle that writes this field via the MethodHandles API, the mutating counterpart of `asAccessMethod(MethodHandles.Lookup)`, with the same caching policy. No deep-reflection handle is created for a final field: writing it must keep failing the caller's access check.
    Parameters:
      `lookup` - the method handles lookup context of the caller
    Returns:
      a setter handle of type `(declaringClass, fieldType)void`
    Throws:
      `IllegalAccessException` - if neither deep reflection nor the caller's lookup can write the field
    Since:
      6.0.0