Package ru.objectsfill.utils
Class FieldUtils
java.lang.Object
ru.objectsfill.utils.FieldUtils
Utility class for reflective field traversal and population.
Provides caching of declared fields and resolution of per-field mutation functions.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TaddObjectWithParamConstruct(Constructor<?> constructor, Fill fill) Creates an instance of the Fill's target class using the given constructor, populating constructor parameters with generated values matched by parameter name.static <T> TaddObjectWithParamConstruct(Constructor<?> constructor, Fill fill, Class<?> tClass) Creates an instance of the specified class using the given constructor, populating constructor parameters with generated values matched by parameter name against the class's declared fields.static voiddoWithFields(Fill fill) Iterates over all declared fields of the target object (including superclass fields) and populates each one usingRandomValueFieldSetterCallback.getExtFillParam(Field field, Fill fill) Finds theExtendparameter that matches the given field by name or class type usingRandomValueFieldSetterCallback.getExtendPredicate(java.lang.reflect.Field).static UnaryOperator<Object>getObjectUnaryOperator(Fill fill) Resolves the mutation function for the Fill configuration.static UnaryOperator<Object>getObjectUnaryOperator(Fill fill, Field field) Resolves the mutation function for a specific field.
-
Method Details
-
doWithFields
Iterates over all declared fields of the target object (including superclass fields) and populates each one usingRandomValueFieldSetterCallback. If the object isnullbut the class is set, attempts to create an instance via the constructor with the fewest parameters.- Parameters:
fill- the Fill configuration containing the target object and generation parameters- Throws:
IllegalArgumentException- if the class is nullIllegalStateException- if a field cannot be accessed
-
addObjectWithParamConstruct
Creates an instance of the Fill's target class using the given constructor, populating constructor parameters with generated values matched by parameter name.- Type Parameters:
T- the type of the created instance- Parameters:
constructor- the constructor to invokefill- the generation parameters- Returns:
- the created instance, or
nullif instantiation fails
-
addObjectWithParamConstruct
public static <T> T addObjectWithParamConstruct(Constructor<?> constructor, Fill fill, Class<?> tClass) Creates an instance of the specified class using the given constructor, populating constructor parameters with generated values matched by parameter name against the class's declared fields.- Type Parameters:
T- the type of the created instance- Parameters:
constructor- the constructor to invokefill- the generation parameterstClass- the class whose fields are matched to constructor parameters- Returns:
- the created instance, or
nullif instantiation fails
-
getObjectUnaryOperator
Resolves the mutation function for the Fill configuration. Delegates togetObjectUnaryOperator(Fill, Field)with anullfield.- Parameters:
fill- the Fill configuration- Returns:
- the mutation function, or identity if none is configured
-
getObjectUnaryOperator
Resolves the mutation function for a specific field. If the field isnull, returns the first globalExtendfunction (one with no field name and no class). Otherwise, returns the function from the matchingExtendparameter for the given field.- Parameters:
fill- the Fill configurationfield- the target field, ornullfor global resolution- Returns:
- the mutation function, or identity (
t -> t) if none is configured
-
getExtFillParam
Finds theExtendparameter that matches the given field by name or class type usingRandomValueFieldSetterCallback.getExtendPredicate(java.lang.reflect.Field).- Parameters:
field- the field to match againstfill- the Fill configuration containing extended parameters- Returns:
- the matching Extend, or empty if no match
-