Class FieldUtils

java.lang.Object
ru.objectsfill.utils.FieldUtils

public class FieldUtils extends Object
Utility class for reflective field traversal and population. Provides caching of declared fields and resolution of per-field mutation functions.
  • Method Details

    • doWithFields

      public static void doWithFields(Fill fill)
      Iterates over all declared fields of the target object (including superclass fields) and populates each one using RandomValueFieldSetterCallback. If the object is null but 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 null
      IllegalStateException - if a field cannot be accessed
    • addObjectWithParamConstruct

      public static <T> T addObjectWithParamConstruct(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.
      Type Parameters:
      T - the type of the created instance
      Parameters:
      constructor - the constructor to invoke
      fill - the generation parameters
      Returns:
      the created instance, or null if 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 invoke
      fill - the generation parameters
      tClass - the class whose fields are matched to constructor parameters
      Returns:
      the created instance, or null if instantiation fails
    • getObjectUnaryOperator

      public static UnaryOperator<Object> getObjectUnaryOperator(Fill fill)
      Resolves the mutation function for the Fill configuration. Delegates to getObjectUnaryOperator(Fill, Field) with a null field.
      Parameters:
      fill - the Fill configuration
      Returns:
      the mutation function, or identity if none is configured
    • getObjectUnaryOperator

      public static UnaryOperator<Object> getObjectUnaryOperator(Fill fill, Field field)
      Resolves the mutation function for a specific field. If the field is null, returns the first global Extend function (one with no field name and no class). Otherwise, returns the function from the matching Extend parameter for the given field.
      Parameters:
      fill - the Fill configuration
      field - the target field, or null for global resolution
      Returns:
      the mutation function, or identity (t -> t) if none is configured
    • getExtFillParam

      public static Optional<Extend> getExtFillParam(Field field, Fill fill)
      Finds the Extend parameter that matches the given field by name or class type using RandomValueFieldSetterCallback.getExtendPredicate(java.lang.reflect.Field).
      Parameters:
      field - the field to match against
      fill - the Fill configuration containing extended parameters
      Returns:
      the matching Extend, or empty if no match