Class AccessibleFieldHandler

java.lang.Object
io.github.arasdenizhan.core.handler.AccessibleFieldHandler

public final class AccessibleFieldHandler extends Object
Utility class to efficiently retrieve the value of a field via reflection.

Instead of blindly calling setAccessible(true) on each access, this class first checks if the field is already accessible using canAccess(). If not accessible, it enables access explicitly. This avoids unnecessary reflection overhead.

Note: In some JVM implementations, repeated setAccessible(true) calls are cached, which can improve performance slightly, though this behavior is not guaranteed in all Java versions.

  • Method Details

    • getFieldValue

      public static Object getFieldValue(Field field, Object target) throws IllegalAccessException
      Securely retrieves the value of a field using reflection, enabling access only if necessary.
      Parameters:
      field - The field to read from
      target - The object instance
      Returns:
      The value of the field
      Throws:
      IllegalAccessException - if the field is not accessible and cannot be made accessible