Class ObjectAccessUtils
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 Summary
FieldsModifier and TypeFieldDescriptionFunction to build possible getter method names for a given field.Function to build the setter method name for a given field. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor to prevent instantiation. -
Method Summary
Modifier and TypeMethodDescriptionprivate static Stringcapitalize(String str) Capitalizes the first character of the given string.static FieldFinds a declared field with the given name in the given class or its superclasses.static ObjectRetrieves the value of a field or property from a target object.private static NaftahBugErrorCreates aNaftahBugErrorwhen a field with the given name cannot be found.static booleanSets the value of a field or property on a target object.
-
Field Details
-
BUILD_SETTER
Function to build the setter method name for a given field.Example: for field "name", returns "setName".
-
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 aNaftahBugErrorwhen 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
gettermethod 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 nullname- the field name; may be nullgetter- optional getterMethodto invoke; may be nullsafe- whether to swallow any exceptions and returnnullfailFast- whether to throw immediately on resolution errors during getter execution- Returns:
- the value of the field or property, or
nullif not found - Throws:
InvocationTargetExceptionInstantiationExceptionIllegalAccessException
-
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
settermethod 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 nullname- the field name; may be nullsetter- optional setterMethodto invoke; may be nullsafe- whether to swallow any exceptions and returnnullfailFast- whether to throw immediately on resolution errors during setter executionvalue- the value to assign to the field- Returns:
trueif the value was successfully set,falseotherwise- Throws:
InvocationTargetExceptionInstantiationExceptionIllegalAccessException
-
findField
Finds a declared field with the given name in the given class or its superclasses.- Parameters:
cls- the class to searchname- the field name- Returns:
- the
Fieldif found, otherwisenull
-
capitalize
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
Creates aNaftahBugErrorwhen a field with the given name cannot be found.The field name is expected to be in Arabic. This wraps the standard
NoSuchFieldExceptioninside aNaftahBugError.- Parameters:
name- the Arabic name of the field that was not found- Returns:
- a
NaftahBugErrorindicating that the field does not exist - Throws:
NaftahBugError- always thrown to indicate the missing field
-