Class ObjectAccessUtils

java.lang.Object
org.daiitech.naftah.utils.reflect.ObjectAccessUtils

public final class ObjectAccessUtils extends Object
Utility class for reflective access to object fields and properties.

This class provides methods to get and set field values on Java objects, using either direct field access or getter/setter methods if available. It is designed to work with JavaBeans-style properties as well as raw fields.

All methods are static, and the class cannot be instantiated. Attempting to instantiate it will throw a NaftahBugError.

Typical usage examples:


 Object value = ObjectAccessUtils.get(person, "name", null);
 boolean updated = ObjectAccessUtils.set(person, "age", null, 30);
 
*
Author:
Chakib Daii
  • Field Details

    • BUILD_SETTER

      public static Function<String,String> BUILD_SETTER
      Function to build the setter method name for a given field.

      Example: for field "name", returns "setName".

    • BUILD_GETTERS

      public static Function<String,String[]> BUILD_GETTERS
      Function to build possible getter method names for a given field.

      For a field "active", returns an array with:

      • "getActive"
      • "isActive"
      • "active" (direct method)
  • Constructor Details

    • ObjectAccessUtils

      private ObjectAccessUtils()
      Private constructor to prevent instantiation. Always throws a NaftahBugError when called.
  • Method Details

    • get

      public static Object get(Object target, String name, Method getter, boolean safe, boolean failFast) throws InvocationTargetException, InstantiationException, IllegalAccessException
      Retrieves the value of a field or property from a target object.

      If a getter method is provided, it will be invoked. Otherwise, the method will attempt to access the field directly using reflection.

      Parameters:
      target - the object from which to retrieve the value; may be null
      name - the field name; may be null
      getter - optional getter Method to invoke; may be null
      safe - whether to swallow any exceptions and return null
      failFast - whether to throw immediately on resolution errors during getter execution
      Returns:
      the value of the field or property, or null if not found
      Throws:
      InvocationTargetException
      InstantiationException
      IllegalAccessException
    • set

      public static boolean set(Object target, String name, Method setter, Object value, boolean safe, boolean failFast) throws InvocationTargetException, InstantiationException, IllegalAccessException
      Sets the value of a field or property on a target object.

      If a setter method is provided, it will be invoked. Otherwise, the method will attempt to access the field directly using reflection.

      Parameters:
      target - the object on which to set the value; may be null
      name - the field name; may be null
      setter - optional setter Method to invoke; may be null
      safe - whether to swallow any exceptions and return null
      failFast - whether to throw immediately on resolution errors during setter execution
      value - the value to assign to the field
      Returns:
      true if the value was successfully set, false otherwise
      Throws:
      InvocationTargetException
      InstantiationException
      IllegalAccessException
    • findField

      public static Field findField(Class<?> cls, String name, boolean safe)
      Finds a declared field with the given name in the given class or its superclasses.
      Parameters:
      cls - the class to search
      name - the field name
      Returns:
      the Field if found, otherwise null
    • capitalize

      private static String capitalize(String str)
      Capitalizes the first character of the given string.
      Parameters:
      str - the input string
      Returns:
      the string with the first character capitalized, or the original string if empty/null
    • newNaftahNoSuchFieldError

      private static NaftahBugError newNaftahNoSuchFieldError(String name)
      Creates a NaftahBugError when a field with the given name cannot be found.

      The field name is expected to be in Arabic. This wraps the standard NoSuchFieldException inside a NaftahBugError.

      Parameters:
      name - the Arabic name of the field that was not found
      Returns:
      a NaftahBugError indicating that the field does not exist
      Throws:
      NaftahBugError - always thrown to indicate the missing field