diff --git a/src/main/java/ru/objectsfill/core/RandomValue.java b/src/main/java/ru/objectsfill/core/RandomValue.java index a410984..344eae5 100644 --- a/src/main/java/ru/objectsfill/core/RandomValue.java +++ b/src/main/java/ru/objectsfill/core/RandomValue.java @@ -14,7 +14,22 @@ import static ru.objectsfill.utils.FieldUtils.getObjectUnaryOperator; /** - The RandomValue class is designed to populate POJO classes with random data. + * Main entry point for populating POJO classes with random data. + * Provides static methods for filling single objects, collections, arrays, and streams. + * + *

Basic usage: + *

{@code
+ * MyClass obj = RandomValue.fill(MyClass.class);
+ * }
+ * + *

Advanced usage with {@link Fill} builder: + *

{@code
+ * MyClass obj = RandomValue.fill(
+ *     Fill.object(MyClass.class)
+ *         .collectionSize(10)
+ *         .valueLength(15)
+ *         .gen());
+ * }
*/ public final class RandomValue { @@ -23,11 +38,11 @@ private RandomValue() { } /** - - Fills the given object with random values. - @param fill The object containing all the information about the target object. - @param The type of the target object. - @return The filled object. + * Fills the given object with random values using the provided configuration. + * + * @param fill the configuration object describing the target object and generation parameters + * @param the type of the target object + * @return the filled object */ @SuppressWarnings("unchecked") public static T fill(Fill fill) { @@ -36,10 +51,11 @@ public static T fill(Fill fill) { } /** - Fills the given object with random values shorter. - @param object The object to create fill. - @param The type of the target object. - @return The filled object. + * Fills the given object instance with random values using default parameters. + * + * @param object the object instance to fill + * @param the type of the target object + * @return the filled object */ public static T fill(Object object) { Fill gen = Fill.object(object).gen(); @@ -47,10 +63,11 @@ public static T fill(Object object) { } /** - Fills the given object with random values shorter. - @param clazz The class to create fill. - @param The type of the target object. - @return The filled object. + * Creates a new instance of the given class and fills it with random values using default parameters. + * + * @param clazz the class to instantiate and fill + * @param the type of the target object + * @return the filled object */ public static T fill(Class clazz) { Fill gen = Fill.object(clazz).gen(); @@ -58,12 +75,15 @@ public static T fill(Class clazz) { } /** - - Fills the given collection with random values. - @param collection The collection type for populating. - @param fill The object containing all the information about the target collection. - @param The type of the collection. - @param The type of the elements in the collection. + * Populates the given collection with randomly generated elements. + * The number of elements is determined by {@link Fill#getCollectionSize()}. + * If a mutation function is configured via {@link ru.objectsfill.object_param.Extend#wrapByFunction}, + * each generated element is transformed through it before being added. + * + * @param collection the collection to populate + * @param fill the configuration describing the element type and generation parameters + * @param the collection type + * @param the type of elements in the collection */ @SuppressWarnings({"unchecked", "unused"}) public static , K> void fillCollection(T collection, Fill fill) { @@ -73,18 +93,20 @@ public static , K> void fillCollection(T collection, Fil for (int i = 0; i < fill.getCollectionSize(); i++) { Object o = mutationFunction.apply(new ElementCreationService().generateSingleValue(fill.getClazz(), fill)); - if (o.getClass().isAssignableFrom(fill.getClazz())) { + if (fill.getClazz().isAssignableFrom(o.getClass())) { collection.add((K) o); } } } /** - - Fills the given collection with random values. - @param fill The object containing all the information about the target collection. - @param The type of stream - @return not closed stream + * Creates a stream of randomly generated elements. + * The stream size is determined by {@link Fill#getCollectionSize()}. + * If a mutation function is configured, each element is transformed through it. + * + * @param fill the configuration describing the element type and generation parameters + * @param the type of stream elements + * @return a finite stream of randomly generated elements */ @SuppressWarnings({"unchecked", "unused"}) public static Stream fillStream(Fill fill) { @@ -95,7 +117,7 @@ public static Stream fillStream(Fill fill) { for (int i = 0; i < fill.getCollectionSize(); i++) { Object o = mutationFunction.apply(new ElementCreationService().generateSingleValue(fill.getClazz(), fill)); - if (o.getClass().isAssignableFrom(fill.getClazz())) { + if (fill.getClazz().isAssignableFrom(o.getClass())) { listForStream.add((K) o); } } @@ -103,10 +125,12 @@ public static Stream fillStream(Fill fill) { } /** - Creates and fills an array with random values. - @param fill The object containing all the information about the target array. - @param The type of the elements in the array. - @return The filled array. + * Creates and fills an array with randomly generated elements. + * The array size is determined by {@link Fill#getCollectionSize()}. + * + * @param fill the configuration describing the element type and generation parameters + * @param the component type of the array + * @return the filled array */ @SuppressWarnings({"unchecked", "unused"}) public static T[] fillArray(Fill fill) { @@ -114,11 +138,12 @@ public static T[] fillArray(Fill fill) { } /** - - Generates a single random value. - @param fill The object containing all the information about the target value. - @param The type of the value. - @return The generated value. + * Generates a single random value of the type specified in the {@link Fill} configuration. + * If a mutation function is configured, the generated value is transformed through it. + * + * @param fill the configuration describing the value type and generation parameters + * @param the type of the generated value + * @return the generated value */ @SuppressWarnings({"unchecked", "unused"}) public static K fillSingleVal(Fill fill) { diff --git a/src/main/java/ru/objectsfill/core/RandomValueFieldSetterCallback.java b/src/main/java/ru/objectsfill/core/RandomValueFieldSetterCallback.java index e4e3119..a6c9e5e 100644 --- a/src/main/java/ru/objectsfill/core/RandomValueFieldSetterCallback.java +++ b/src/main/java/ru/objectsfill/core/RandomValueFieldSetterCallback.java @@ -16,12 +16,13 @@ import static ru.objectsfill.types.primitive_type.PrimitiveTypeName.*; /** - * A necessary class for reflection to traverse fields. + * Callback that populates each field of an object with a randomly generated value during reflection traversal. + * Handles field exclusion, extended per-field parameters, and primitive array special-casing. */ public record RandomValueFieldSetterCallback(Fill fill) implements FieldCallback { /** - * Function to check excluded fields. + * Curried function that checks whether a field is not in the excluded field list. */ private static final Function> checkExcludedName = checkField -> @@ -29,10 +30,11 @@ public record RandomValueFieldSetterCallback(Fill fill) implements FieldCallback !fillObjectParam.getExcludedField().contains(checkField.getName()); /** - * Overrides the method for field traversal. + * Processes a single field: skips final and excluded fields, applies extended parameters if present, + * and delegates to the appropriate generation method. * - * @param field The field to process. - * @throws IllegalAccessException If the field is inaccessible. + * @param field the field to populate + * @throws IllegalAccessException if the field cannot be accessed via reflection */ @Override public void doWith(Field field) throws IllegalAccessException { @@ -54,10 +56,11 @@ public void doWith(Field field) throws IllegalAccessException { /** - * split field fill + * Generates a value for the field using the default (unmodified) {@link Fill} parameters. + * If the field is a primitive array, delegates to {@link PrimitiveTypeName} for specialized handling. * - * @param field The field to process. - * @throws IllegalAccessException If the field is inaccessible. + * @param field the field to populate + * @throws IllegalAccessException if the field cannot be accessed via reflection */ private void generateWithNoFillChange(Field field) throws IllegalAccessException { Class type = getTypeClass(field, fill); @@ -77,10 +80,12 @@ private void generateWithNoFillChange(Field field) throws IllegalAccessException } /** - * split field fill + * Generates a value for the field using the given {@link Fill} configuration + * and sets it on the target object via reflection. * - * @param field The field to process. - * @throws IllegalAccessException If the field is inaccessible. + * @param field the field to populate + * @param extendedParameterForField the {@link Fill} configuration (possibly with per-field overrides) + * @throws IllegalAccessException if the field cannot be accessed via reflection */ private void generateExtendedValue(Field field, Fill extendedParameterForField) throws IllegalAccessException { @@ -93,9 +98,11 @@ private void generateExtendedValue(Field field, Fill extendedParameterForField) } /** - * split field fill + * Builds a new {@link Fill} with per-field overrides (collection size, value length) + * from the matching {@link Extend} parameter, if one exists for the given field. * - * @param field The field to process. + * @param field the field to look up in the extended parameters list + * @return an {@link Optional} containing the customized {@link Fill}, or empty if no match */ private Optional getExtendedParameterForField(Field field) { @@ -106,7 +113,7 @@ private Optional getExtendedParameterForField(Field field) { .excludeField(fill.getExcludedField()) .fieldParams(fill.getExtendedFieldParams()) .collectionSize(Optional.ofNullable(extendedFieldParameter.getCollectionSize()).orElse(fill.getCollectionSize())) - .valueLength(Optional.ofNullable(extendedFieldParameter.getValueLength()).orElse(fill.getCollectionSize())) + .valueLength(Optional.ofNullable(extendedFieldParameter.getValueLength()).orElse(fill.getValueLength())) .setDeep(fill.getDeep()) .withGeneric(fill.getGenericType()) .gen() @@ -115,10 +122,11 @@ private Optional getExtendedParameterForField(Field field) { } /** - * get fields by class or name + * Returns a predicate that matches an {@link Extend} parameter to a field + * either by field name or by assignable class type. * - * @param field The field to process. - * @return get fields by class or name + * @param field the field to match against + * @return a predicate for filtering {@link Extend} parameters */ public static Predicate getExtendPredicate(Field field) { return fillFieldParameter -> { diff --git a/src/main/java/ru/objectsfill/functions/BinaryFunction.java b/src/main/java/ru/objectsfill/functions/BinaryFunction.java index e7a9eed..d9cb9c7 100644 --- a/src/main/java/ru/objectsfill/functions/BinaryFunction.java +++ b/src/main/java/ru/objectsfill/functions/BinaryFunction.java @@ -1,16 +1,21 @@ package ru.objectsfill.functions; /** - * binary operator function + * A two-argument functional interface that performs an operation and returns no result. + * Used for field-level operations that may require reflective access. + * + * @param the type of the first argument + * @param the type of the second argument */ @FunctionalInterface public interface BinaryFunction { /** - * binary operator function - * @param t first argument - * @param r second argument - * @throws IllegalAccessException add ex for function + * Applies this function to the given arguments. + * + * @param t the first argument + * @param r the second argument + * @throws IllegalAccessException if reflective field access is denied */ void apply(T t, R r) throws IllegalAccessException; } diff --git a/src/main/java/ru/objectsfill/object_param/Extend.java b/src/main/java/ru/objectsfill/object_param/Extend.java index 4bc5be0..2c58ed2 100644 --- a/src/main/java/ru/objectsfill/object_param/Extend.java +++ b/src/main/java/ru/objectsfill/object_param/Extend.java @@ -3,110 +3,118 @@ import java.util.function.UnaryOperator; /** - * class with parameters for field + * Per-field parameter override for customizing how individual fields are populated. + * Allows setting custom collection sizes, value lengths, and mutation functions + * that target specific fields by name, by class type, or apply globally. + * + *

Example: + *

{@code
+ * Extend.field("name").addMutationFunction(n -> "Custom").gen()
+ * Extend.clazz(String.class).valueLength(20).gen()
+ * Extend.wrapByFunction(obj -> transform(obj)).gen()
+ * }
*/ public class Extend { - /** - * class type for extend - */ + /** Class type to match fields by their declared type. */ private Class clazz; - /** - * name of the field - */ + /** Field name to match a specific field. */ private String fieldName; - /** - * The number of objects to be created in a collection. - */ + + /** Number of elements to generate for this field's collection. */ private Integer collectionSize; - /** - * The length of randomly generated values. - */ + + /** Length of randomly generated string values for this field. */ private Integer valueLength; - /** - * add to single field generation function - */ + /** Mutation function applied to the generated value before assignment. */ private UnaryOperator singleChangeFunction; /** - Gets class type for extend. - @return class type for extend. + * Returns the class type used for field matching. + * + * @return the class type, or {@code null} if matching by field name */ public Class getClazz() { return clazz; } /** - Gets mutation function. - @return mutation function. + * Returns the mutation function applied to generated values. + * + * @return the mutation function, or {@code null} if none is set */ public UnaryOperator getFieldChangeFunction() { return singleChangeFunction; } /** - - Gets the field name. - @return field name. + * Returns the field name used for matching. + * + * @return the field name, or {@code null} if matching by class type */ public String getFieldName() { return fieldName; } /** - - Gets the collection size. - @return The collection size. + * Returns the custom collection size for this field. + * + * @return the collection size override, or {@code null} to use the default */ public Integer getCollectionSize() { return collectionSize; } /** - - Gets the value length. - @return The value length. + * Returns the custom value length for this field. + * + * @return the value length override, or {@code null} to use the default */ public Integer getValueLength() { return valueLength; } /** - start builder with name - @param fieldName set field name - @return fill field parameter builder. + * Starts building an {@link Extend} that targets a specific field by name. + * + * @param fieldName the name of the field to customize + * @return a new builder */ public static FillFieldParametersBuilder field(String fieldName) { return new FillFieldParametersBuilder(fieldName); } /** - start builder with name - @param clazz set class type - @return fill field parameter builder. + * Starts building an {@link Extend} that targets all fields of a specific class type. + * + * @param clazz the class type to match fields against + * @return a new builder */ public static FillFieldParametersBuilder clazz(Class clazz) { return new FillFieldParametersBuilder(clazz); } /** - start function builder - @param singleChangeFunction mutation function - @return fill field parameter builder. + * Starts building an {@link Extend} with a global mutation function + * applied to every generated value (when no field name or class is specified). + * + * @param singleChangeFunction the mutation function + * @return a new builder */ public static FillFieldParametersBuilder wrapByFunction(UnaryOperator singleChangeFunction) { return new FillFieldParametersBuilder(singleChangeFunction); } /** - * constructor for builder - * @param fieldName field name - * @param collectionSize size - * @param valueLength length - * @param clazz class type for extend - * @param singleChangeFunction mutation function + * Constructs a new Extend with all parameters. + * + * @param fieldName the target field name (or {@code null}) + * @param collectionSize the collection size override (or {@code null}) + * @param valueLength the value length override (or {@code null}) + * @param singleChangeFunction the mutation function (or {@code null}) + * @param clazz the target class type (or {@code null}) */ public Extend(String fieldName, Integer collectionSize, Integer valueLength, UnaryOperator singleChangeFunction, Class clazz) { @@ -118,70 +126,54 @@ public Extend(String fieldName, Integer collectionSize, Integer valueLength, } /** - * builder for extend class + * Fluent builder for constructing {@link Extend} instances. */ public static final class FillFieldParametersBuilder { - /** - * class type for extend - */ private Class clazz; - - /** - * name of the field - */ private String fieldName; - - /** - * The number of objects to be created in a collection. - */ private Integer collectionSize; - - /** - * The length of randomly generated values. - */ private Integer valueLength; - - /** - * add to single field generation function - */ UnaryOperator singleChangeFunction; /** - constructor with field name - @param fieldName set field name + * Creates a builder targeting a field by name. + * + * @param fieldName the field name to match */ public FillFieldParametersBuilder(String fieldName) { this.fieldName = fieldName; } /** - constructor with field class type - @param clazz set field class type + * Creates a builder targeting fields by class type. + * + * @param clazz the class type to match */ public FillFieldParametersBuilder(Class clazz) { this.clazz = clazz; } /** - constructor with function - @param singleChangeFunction with function + * Creates a builder with a global mutation function. + * + * @param singleChangeFunction the mutation function to apply */ public FillFieldParametersBuilder(UnaryOperator singleChangeFunction) { this.singleChangeFunction = singleChangeFunction; } /** - constructor + * Creates an empty builder. */ public FillFieldParametersBuilder() { } - /** - Sets the collection size. - @param collectionSize The collection size. - @return The FillBuilder instance. + * Sets the collection size override for this field. + * + * @param collectionSize the number of collection elements + * @return this builder */ public FillFieldParametersBuilder collectionSize(Integer collectionSize) { this.collectionSize = collectionSize; @@ -189,9 +181,10 @@ public FillFieldParametersBuilder collectionSize(Integer collectionSize) { } /** - Sets the value length. - @param valueLength The value length. - @return The FillBuilder instance. + * Sets the value length override for this field. + * + * @param valueLength the string value length + * @return this builder */ public FillFieldParametersBuilder valueLength(Integer valueLength) { this.valueLength = valueLength; @@ -199,9 +192,10 @@ public FillFieldParametersBuilder valueLength(Integer valueLength) { } /** - Sets mutation function - @param singleChangeFunction mutation function - @return The FillBuilder instance. + * Sets the mutation function applied to the generated value before assignment. + * + * @param singleChangeFunction the mutation function + * @return this builder */ public FillFieldParametersBuilder addMutationFunction(UnaryOperator singleChangeFunction) { this.singleChangeFunction = singleChangeFunction; @@ -209,8 +203,9 @@ public FillFieldParametersBuilder addMutationFunction(UnaryOperator sing } /** - Builds and returns the FillFieldParameters object. - @return The created FillFieldParameters object. + * Builds and returns the {@link Extend} instance. + * + * @return the constructed Extend */ public Extend gen() { return new Extend(fieldName, collectionSize, valueLength, singleChangeFunction, clazz); diff --git a/src/main/java/ru/objectsfill/object_param/Fill.java b/src/main/java/ru/objectsfill/object_param/Fill.java index daf098b..d07919b 100644 --- a/src/main/java/ru/objectsfill/object_param/Fill.java +++ b/src/main/java/ru/objectsfill/object_param/Fill.java @@ -7,60 +7,56 @@ import java.util.*; /** - - The Fill class is used to provide information for populating POJO (Plain Old Java Object) classes with random data. + * Configuration object that holds all parameters for populating a POJO with random data. + * Use the {@link FillBuilder} (via {@link #object(Class)} or {@link #object(Object)}) to construct instances. + * + *

Example: + *

{@code
+ * Fill fill = Fill.object(MyClass.class)
+ *     .collectionSize(10)
+ *     .valueLength(15)
+ *     .setDeep(5)
+ *     .excludeField("id", "createdAt")
+ *     .gen();
+ * }
*/ public final class Fill { - /** - The object to be filled with random data. - */ + /** The object instance to be filled with random data. */ private Object objectz; - /** - The class of the object to be created and filled. - */ + /** The class of the object to be created and filled. */ private Class clazz; - /** - The names of fields excluded from filling. - */ + /** Field names excluded from random value generation. */ private List excludedFieldName; - /** - The depth limit for traversing dependency trees or preventing cyclic dependencies. - */ + /** Maximum recursion depth for nested object generation. */ private Integer deep; - /** - The number of objects to be created in a collection. - */ + /** Number of elements to generate for collections and arrays. */ private Integer collectionSize; - /** - The length of randomly generated values. - */ + /** Length of randomly generated string values. */ private Integer valueLength; - /** - The generic types used in the object. - */ + /** Mapping of generic type parameter names to their resolved types. */ private Map genericType; - /** - The extended field parameters. - */ + /** Per-field generation overrides (custom sizes, mutation functions, etc.). */ private List extendedFieldParams; /** - Constructs a new Fill object. - @param objectz The object to be filled. - @param clazz The class of the object. - @param excludedFieldName The names of excluded fields. - @param deep The depth limit. - @param genericType The generic types. - @param collectionSize The collection size. - @param valueLength The value length. + * Constructs a new Fill configuration. + * + * @param objectz the object instance to fill + * @param clazz the class of the object + * @param excludedFieldName field names to exclude from generation + * @param deep maximum recursion depth + * @param genericType generic type parameter mappings + * @param collectionSize number of elements for collections + * @param valueLength length of generated string values + * @param extendedFieldParams per-field parameter overrides */ private Fill(Object objectz, Class clazz, List excludedFieldName, Integer deep, Map genericType, Integer collectionSize, @@ -76,183 +72,169 @@ private Fill(Object objectz, Class clazz, List excludedFieldName, } /** - set new object if old object was without simple construct - @param objectz new created object. + * Replaces the target object instance. Used when the original class could not be instantiated + * via a no-arg constructor and a parameterized constructor was used instead. + * + * @param objectz the new object instance */ public void setObjectz(Object objectz) { this.objectz = objectz; } - /** - Gets the generic types. - @return The generic types. + /** + * Returns the generic type parameter mappings. + * + * @return map of generic type names to their resolved {@link Type} instances */ public Map getGenericType() { return genericType; } - /** - Sets the depth limit. - @param deep The depth limit. + /** + * Sets the maximum recursion depth for nested object generation. + * + * @param deep the depth limit */ public void setDeep(Integer deep) { this.deep = deep; } - /** - Sets the generic types. - @param genericType The generic types. + /** + * Replaces all generic type parameter mappings. + * + * @param genericType the new generic type map */ public void setGenericType(Map genericType) { this.genericType = genericType; } - /** - Sets a specific generic type. - @param genericName The name of the generic type. - @param genericType The specific generic type. + /** + * Adds a single generic type parameter mapping. Creates the map if it does not yet exist. + * + * @param genericName the type parameter name (e.g. "T") + * @param genericType the resolved type */ public void setGenericType(String genericName, Type genericType) { if (this.genericType == null) this.genericType = new HashMap<>(); this.genericType.putIfAbsent(genericName, genericType); } - /** - Gets the class of the object. - @return The class of the object. + /** + * Returns the class of the object being filled. + * + * @return the target class */ public Class getClazz() { return clazz; } - /** - Gets the object to be filled. - @return The object to be filled. + /** + * Returns the object instance being filled. + * + * @return the target object */ public Object getObjectz() { return objectz; } - /** - Gets the names of excluded fields. - @return The names of excluded fields. + /** + * Returns the list of field names excluded from generation. + * + * @return excluded field names */ public List getExcludedField() { return excludedFieldName; } - /** - Gets the depth limit. - @return The depth limit. + /** + * Returns the maximum recursion depth. + * + * @return the depth limit */ public Integer getDeep() { return deep; } - /** - Gets the collection size. - @return The collection size. + /** + * Returns the number of elements to generate for collections. + * + * @return the collection size */ public Integer getCollectionSize() { return collectionSize; } - /** - Gets the value length. - @return The value length. + /** + * Returns the length of randomly generated string values. + * + * @return the value length */ public Integer getValueLength() { return valueLength; } /** - - Gets the extended field parameters. - @return The extended field parameters. + * Returns the list of per-field parameter overrides. + * + * @return the extended field parameters */ public List getExtendedFieldParams() { return extendedFieldParams; } - /** - Starts building a Fill object with the specified object. - @param object The object to be filled. - @return A FillBuilder instance. + /** + * Starts building a {@link Fill} configuration for an existing object instance. + * + * @param object the object to fill + * @return a new {@link FillBuilder} */ public static FillBuilder object(Object object) { return new FillBuilder(object); } - /** - Starts building a Fill object with the specified class. - @param clazz The class to be created and filled. - @return A FillBuilder instance. + /** + * Starts building a {@link Fill} configuration for a class. + * The class will be instantiated automatically via its no-arg constructor. + * + * @param clazz the class to instantiate and fill + * @return a new {@link FillBuilder} */ public static FillBuilder object(Class clazz) { return new FillBuilder(clazz); } - /** - The FillBuilder class provides a fluent interface for building a Fill object. + /** + * Fluent builder for constructing {@link Fill} configurations. */ public static final class FillBuilder { - /** - - The object to be filled. - */ private Object objectz; - /** - - The class to be created and filled. - */ private Class clazz; - /** - - The names of excluded fields. - */ private List excludedFieldName = new ArrayList<>(); - /** - - The depth limit for traversing dependency trees. - */ private Integer deep = 3; - /** - - The number of objects to be created in a collection. - */ private Integer collectionSize = 5; - /** - - The length of randomly generated values. - */ private Integer valueLength = 5; - /** - - The generic types used in the object. - */ private Map genericType = new HashMap<>(); - - /** - - The extended field parameters/ - */ private List extendedFieldParams = new ArrayList<>(); - /** - Constructs a new FillBuilder object with the specified object. - @param objectz The object to be filled. - @param The class to be created and filled. + /** + * Creates a builder from an existing object instance. + * + * @param objectz the object to fill + * @param the object type */ public FillBuilder(T objectz) { this.objectz = objectz; this.clazz = objectz.getClass(); } - /** - Constructs a new FillBuilder object with the specified class. - @param clazz The class to be created and filled. + /** + * Creates a builder from a class, attempting to instantiate it via the no-arg constructor. + * If instantiation fails (no public no-arg constructor, abstract class, etc.), + * the object will be created later via a parameterized constructor. + * + * @param clazz the class to instantiate and fill */ public FillBuilder(Class clazz) { try { @@ -261,21 +243,26 @@ public FillBuilder(Class clazz) { } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException ignore) { } } - /** - Sets the collection size. - @param collectionSize The collection size. - @return The FillBuilder instance. + /** + * Sets the number of elements to generate for collections and arrays. + * + * @param collectionSize the collection size (must be >= 1) + * @return this builder + * @throws FillException if the value is less than 1 */ public FillBuilder collectionSize(Integer collectionSize) { checkPositive(collectionSize); this.collectionSize = collectionSize; return this; } + /** - Sets the value length. - @param valueLength The value length. - @return The FillBuilder instance. + * Sets the length of randomly generated string values. + * + * @param valueLength the value length (must be >= 1) + * @return this builder + * @throws FillException if the value is less than 1 */ public FillBuilder valueLength(Integer valueLength) { checkPositive(valueLength); @@ -284,10 +271,10 @@ public FillBuilder valueLength(Integer valueLength) { } /** - - Sets the extended field params. - @param parameter The field params. - @return The FillBuilder instance. + * Adds a single per-field parameter override. + * + * @param parameter the field parameter override + * @return this builder */ public FillBuilder fieldParams(Extend parameter) { this.extendedFieldParams.add(parameter); @@ -295,10 +282,10 @@ public FillBuilder fieldParams(Extend parameter) { } /** - - Sets the extended field params. - @param parameter The field params. - @return The FillBuilder instance. + * Adds multiple per-field parameter overrides. + * + * @param parameter the field parameter overrides + * @return this builder */ public FillBuilder fieldParams(Extend... parameter) { this.extendedFieldParams.addAll(Arrays.stream(parameter).toList()); @@ -306,42 +293,45 @@ public FillBuilder fieldParams(Extend... parameter) { } /** - - Sets the extended field params list. - @param parameter The field params. - @return The FillBuilder instance. + * Adds a list of per-field parameter overrides. + * + * @param parameter the field parameter overrides + * @return this builder */ public FillBuilder fieldParams(List parameter) { this.extendedFieldParams.addAll(parameter); return this; } - /** - Adds a generic type to the FillBuilder. - @param genericName The name of the generic type. - @param genericType The generic type. - @return The FillBuilder instance. + /** + * Registers a generic type parameter mapping by name. + * + * @param genericName the type parameter name (e.g. "T") + * @param genericType the resolved type + * @return this builder */ public FillBuilder withGeneric(String genericName, Type genericType) { this.genericType.putIfAbsent(genericName, genericType); return this; } - /** - Sets multiple generic types in the FillBuilder. - @param genericType The map of generic types. - @return The FillBuilder instance. + /** + * Registers multiple generic type parameter mappings. + * + * @param genericType map of type parameter names to resolved types + * @return this builder */ public FillBuilder withGeneric(Map genericType) { this.genericType.putAll(genericType); return this; } - /** - Adds a generic type to the FillBuilder using a Class object. - @param genericName The name of the generic type. - @param genericClass The generic class. - @return The FillBuilder instance. + /** + * Registers a generic type parameter mapping by name using a {@link Class}. + * + * @param genericName the type parameter name (e.g. "T") + * @param genericClass the resolved class (ignored if null) + * @return this builder */ public FillBuilder withGeneric(String genericName, Class genericClass) { if (genericClass != null) { @@ -349,11 +339,12 @@ public FillBuilder withGeneric(String genericName, Class genericClass) { } return this; } - /** - Sets the excluded field names. - @param excludedFieldName The excluded field names. - @return The FillBuilder instance. + /** + * Sets the list of field names to exclude from generation. + * + * @param excludedFieldName the field names to exclude + * @return this builder */ public FillBuilder excludeField(List excludedFieldName) { this.excludedFieldName.addAll(excludedFieldName); @@ -361,38 +352,44 @@ public FillBuilder excludeField(List excludedFieldName) { } /** - Sets the excluded field names. - @param excludedFieldName The excluded field names. - @return The FillBuilder instance. + * Sets the field names to exclude from generation. + * + * @param excludedFieldName the field names to exclude + * @return this builder */ public FillBuilder excludeField(String... excludedFieldName) { List excludedFieldNameList = Arrays.stream(excludedFieldName).toList(); this.excludedFieldName.addAll(excludedFieldNameList); return this; } - /** - Sets the depth limit. - @param deep The depth limit. - @return The FillBuilder instance. + /** + * Sets the maximum recursion depth for nested object generation. + * + * @param deep the depth limit (must be >= 1) + * @return this builder + * @throws FillException if the value is less than 1 */ public FillBuilder setDeep(Integer deep) { checkPositive(deep); this.deep = deep; return this; } + /** - Builds and returns the Fill object. - @return The created Fill object. + * Builds and returns the {@link Fill} configuration. + * + * @return the constructed Fill object */ public Fill gen() { return new Fill(objectz, clazz, excludedFieldName, deep, genericType, collectionSize, valueLength, extendedFieldParams); } - /** - Checks if a number is positive. - @param num The number to check. - @throws FillException if the number is not positive. + /** + * Validates that the given number is positive. + * + * @param num the number to check + * @throws FillException if the number is less than 1 */ private void checkPositive(Integer num) { if (num < 1) { @@ -400,4 +397,4 @@ private void checkPositive(Integer num) { } } } -} \ No newline at end of file +} diff --git a/src/main/java/ru/objectsfill/service/CollectionElementCreationService.java b/src/main/java/ru/objectsfill/service/CollectionElementCreationService.java index 8e04f3c..5e6bbd5 100644 --- a/src/main/java/ru/objectsfill/service/CollectionElementCreationService.java +++ b/src/main/java/ru/objectsfill/service/CollectionElementCreationService.java @@ -19,37 +19,30 @@ import static ru.objectsfill.utils.FieldUtils.getObjectUnaryOperator; /** - - The CollectionElementCreationService class is responsible for generating collection elements - - based on the field type and the Fill object provided. - - It utilizes a container that holds mappings between collection types and their respective CollectionTypeFill implementations. + * Service responsible for generating collection, array, and single-value field contents. + * Resolves the field's element type, looks up the appropriate {@link CollectionTypeFill} handler, + * and delegates generation. Falls back to array handling or single-value generation + * if no collection handler matches. */ public class CollectionElementCreationService { private final Map, CollectionTypeFill> containerCollectionType = new HashMap<>(); - /** - Constructs a new CollectionElementCreationService and initializes the container - by searching for local container implementations and adding default mappings. + /** + * Constructs a new service instance and initializes the collection type registry + * by scanning for user-defined implementations and adding default mappings. */ public CollectionElementCreationService() { findLocalContainerForCollectionType(); new DefaultCollectionTypeContainer().getContainer().forEach(containerCollectionType::putIfAbsent); } - /** - - Generates a collection based on the provided field and Fill object. - - Determines the element type of the collection, retrieves the appropriate CollectionTypeFill implementation, - - and delegates the generation to that implementation. - @param field The field for which the collection is being generated. - - @param fill The Fill object containing the necessary information for generation. - - @return The generated collection. + /** + * Generates a value for the given field based on its type. + * Handles parameterized collections (List, Set, Map, Stream), arrays, and plain single values. + * + * @param field the field to generate a value for + * @param fill the generation parameters + * @return the generated collection, array, or single value */ public Object generateCollection(Field field, Fill fill) { @@ -78,14 +71,13 @@ public Object generateCollection(Field field, Fill fill) { /** - - Retrieves the class of the field's type, taking into account generic types defined in Fill object. - - @param field The field for which the type class is being retrieved. - - @param fill The Fill object containing the generic type information. - - @return The class representing the field's type. + * Resolves the runtime class for a field's type, substituting generic type parameters + * from the {@link Fill} configuration when the declared type is {@code Object}. + * + * @param field the field whose type is being resolved + * @param fill the Fill object containing generic type mappings + * @return the resolved class + * @throws FillException if a required generic type mapping is not found */ public static Class getTypeClass(Field field, Fill fill) { Class type = field.getType(); @@ -100,9 +92,8 @@ public static Class getTypeClass(Field field, Fill fill) { } /** - - Scans the local container implementations for CollectionTypeContainerService, - retrieves their containers, and adds them to the containerCollectionType map. + * Scans for user-defined {@link CollectionTypeContainerService} implementations + * and registers their handlers in the collection type registry. */ private void findLocalContainerForCollectionType() { ScanningForClassUtils.scanClassImplInterface(CollectionTypeContainerService.class, ElementCreationService.DEFAULT_LOCAL_CLASS_CREATION_PATH) diff --git a/src/main/java/ru/objectsfill/service/ElementCreationService.java b/src/main/java/ru/objectsfill/service/ElementCreationService.java index 977de3d..b6eb11c 100644 --- a/src/main/java/ru/objectsfill/service/ElementCreationService.java +++ b/src/main/java/ru/objectsfill/service/ElementCreationService.java @@ -23,10 +23,13 @@ /** - The ElementCreationService class is responsible for generating single values or instances of classes - based on the provided field type and Fill object. - It maintains separate containers for BoxTypeFill and ObjectTypeFill implementations, - which are used to generate values for primitive types and object types, respectively. + * Service responsible for generating individual values or object instances based on field type. + * Maintains two type registries: + *
    + *
  • {@code containerBoxType} — handlers for primitive/wrapper types (String, Integer, etc.)
  • + *
  • {@code containerObjectType} — handlers for complex object types (Enum, custom types)
  • + *
+ * Resolution order: BoxTypeFill → ObjectTypeFill → reflective instantiation via {@link #createInstance}. */ public class ElementCreationService { @@ -34,14 +37,12 @@ public class ElementCreationService { private final Map, ObjectTypeFill> containerObjectType = new HashMap<>(); - /** - * DEFAULT_LOCAL_CLASS_CREATION_PATH - */ + /** Package path scanned at runtime for user-defined container implementations generated by the annotation processor. */ public static final String DEFAULT_LOCAL_CLASS_CREATION_PATH = "generated.fill"; /** - Constructs a new ElementCreationService and initializes the containers by searching for local container implementations - and adding default mappings. + * Constructs a new service instance and initializes the type registries by scanning for + * user-defined container implementations and adding default mappings. */ public ElementCreationService() { findLocalContainerForBoxType(); @@ -102,12 +103,14 @@ public Stream fillCollectionStream(Field field, Fill fill) { } /** - * Get stream by Type - * @param field The field for which the collection stream is being filled. - * @param fill The Fill object containing the necessary information for generation. - * @param genericCollectionType generic collection Type - * @return fill stream - * @param The type of elements in the collection. + * Resolves the element class from the generic type and delegates to + * {@link #generateCollectionByClassType} for stream creation. + * + * @param field the field whose collection is being filled + * @param fill the generation parameters + * @param genericCollectionType the generic type argument of the collection + * @param the element type + * @return a stream of generated elements */ @SuppressWarnings("unchecked") private Stream getStreamByType(Field field, Fill fill, Type genericCollectionType) { diff --git a/src/main/java/ru/objectsfill/service/containers/DefaultBoxTypeContainer.java b/src/main/java/ru/objectsfill/service/containers/DefaultBoxTypeContainer.java index 27c208b..b767fdb 100644 --- a/src/main/java/ru/objectsfill/service/containers/DefaultBoxTypeContainer.java +++ b/src/main/java/ru/objectsfill/service/containers/DefaultBoxTypeContainer.java @@ -11,19 +11,15 @@ import java.util.UUID; /** - - The DefaultBoxTypeContainer class implements the BoxTypeContainerService interface - - and provides a default implementation of the container for BoxTypeFill instances. - - It contains mappings between Class objects and their respective BoxTypeFill implementations. + * Default registry of {@link BoxTypeFill} implementations for all built-in types: + * primitives, wrappers, String, UUID, BigDecimal, and Date. */ public class DefaultBoxTypeContainer implements BoxTypeContainerService { private final Map, BoxTypeFill> container; - /** - Constructs a new DefaultBoxTypeContainer and initializes the container with default mappings. + /** + * Constructs the container and registers all built-in type handlers. */ public DefaultBoxTypeContainer() { container = new HashMap<>(); @@ -41,12 +37,10 @@ public DefaultBoxTypeContainer() { container.putIfAbsent(double.class, new PrimitiveDouble()); container.putIfAbsent(boolean.class, new PrimitiveBoolean()); container.putIfAbsent(char.class, new PrimitiveChar()); - } - /** - Returns the container that holds the mappings between Class objects and BoxTypeFill implementations. - @return The container. + /** + * {@inheritDoc} */ public Map, BoxTypeFill> getContainer() { return container; diff --git a/src/main/java/ru/objectsfill/service/containers/DefaultCollectionTypeContainer.java b/src/main/java/ru/objectsfill/service/containers/DefaultCollectionTypeContainer.java index a9f3f9b..518556d 100644 --- a/src/main/java/ru/objectsfill/service/containers/DefaultCollectionTypeContainer.java +++ b/src/main/java/ru/objectsfill/service/containers/DefaultCollectionTypeContainer.java @@ -10,19 +10,15 @@ import java.util.stream.Stream; /** - - The DefaultCollectionTypeContainer class implements the CollectionTypeContainerService interface - - and provides a default implementation of the container for CollectionTypeFill instances. - - It contains mappings between Class objects and their respective CollectionTypeFill implementations. + * Default registry of {@link CollectionTypeFill} implementations for built-in collection types: + * List, Set, Map, and Stream. */ public class DefaultCollectionTypeContainer implements CollectionTypeContainerService { private final Map, CollectionTypeFill> container; - /** - Constructs a new DefaultCollectionTypeContainer and initializes the container with default mappings. + /** + * Constructs the container and registers all built-in collection type handlers. */ public DefaultCollectionTypeContainer() { container = new HashMap<>(); @@ -31,10 +27,9 @@ public DefaultCollectionTypeContainer() { container.putIfAbsent(Set.class, new FillSetCollection()); container.putIfAbsent(Stream.class, new FillRawStream()); } - /** - Returns the container that holds the mappings between Class objects and CollectionTypeFill implementations. - @return The container. + /** + * {@inheritDoc} */ public Map, CollectionTypeFill> getContainer() { return container; diff --git a/src/main/java/ru/objectsfill/service/containers/DefaultObjectTypeContainer.java b/src/main/java/ru/objectsfill/service/containers/DefaultObjectTypeContainer.java index 1fc7048..9b05be9 100644 --- a/src/main/java/ru/objectsfill/service/containers/DefaultObjectTypeContainer.java +++ b/src/main/java/ru/objectsfill/service/containers/DefaultObjectTypeContainer.java @@ -6,30 +6,26 @@ import java.util.HashMap; import java.util.Map; -/** - - The DefaultObjectTypeContainer class implements the ObjectTypeContainerService interface - and provides a default implementation of the container for ObjectTypeFill instances. - - It contains mappings between Class objects and their respective ObjectTypeFill implementations. +/** + * Default registry of {@link ObjectTypeFill} implementations for built-in complex types. + * Currently registers only Enum handling. */ public class DefaultObjectTypeContainer implements ObjectTypeContainerService { private final Map, ObjectTypeFill> container; - /** - Constructs a new DefaultObjectTypeContainer and initializes the container with default mappings. + /** + * Constructs the container and registers the default Enum type handler. */ public DefaultObjectTypeContainer() { container = new HashMap<>(); container.putIfAbsent(Enum.class, new EnumFill()); } - /** - Returns the container that holds the mappings between Class objects and ObjectTypeFill implementations. - @return The container. + /** + * {@inheritDoc} */ public Map,ObjectTypeFill> getContainer() { return container; diff --git a/src/main/java/ru/objectsfill/types/array/FillArray.java b/src/main/java/ru/objectsfill/types/array/FillArray.java index 27322fa..d3f9e87 100644 --- a/src/main/java/ru/objectsfill/types/array/FillArray.java +++ b/src/main/java/ru/objectsfill/types/array/FillArray.java @@ -10,17 +10,17 @@ import static ru.objectsfill.utils.FieldUtils.getObjectUnaryOperator; /** - * The `FillArray` class provides a method to create an array and fill it with generated values. + * Creates and fills arrays (both reference and primitive types) with randomly generated values. */ public class FillArray { /** - * Creates an array of the specified component type and fills it with generated values. + * Creates an array of the specified type and fills it with generated values. * - * @param fieldType the class representing the component type of the array - * @param fill the `Fill` object containing the generation parameters - * @param the component type of the array - * @return the created array filled with generated values + * @param fieldType the array or component type + * @param fill the generation parameters + * @param the component type + * @return the filled array */ public Object[] createArray(Class fieldType, Fill fill) { @@ -28,13 +28,14 @@ public Object[] createArray(Class fieldType, Fill fill) { } /** - * Creates an array of the specified component type and fills it with generated values. + * Creates an array of the specified type and fills it with generated values. + * Dispatches to specialized handling for primitive arrays or reference arrays. * - * @param fieldType the class representing the component type of the array - * @param fill the `Fill` object containing the generation parameters - * @param field the field for which the array is generated - * @param the component type of the array - * @return the created array filled with generated values + * @param fieldType the array or component type + * @param fill the generation parameters + * @param field the field for which the array is generated (may be {@code null}) + * @param the component type + * @return the filled array */ public Object[] createArray(Class fieldType, Fill fill, Field field) { @@ -49,13 +50,14 @@ public Object[] createArray(Class fieldType, Fill fill, Field field) { } /** - * Creates an array of the specified component type and fills it with generated values. + * Creates a typed array for reference (non-primitive) component types and fills each element + * with a value generated by {@link ElementCreationService}. * - * @param componentType the class representing the component type of the array - * @param fill the `Fill` object containing the generation parameters - * @param field the field for which the array is generated - * @param the component type of the array - * @return the created array filled with generated values + * @param fill the generation parameters + * @param componentType the component type of the array + * @param field the source field (may be {@code null}) + * @param the component type + * @return the filled array */ @SuppressWarnings("unchecked") private T[] forReferenceArrays(Fill fill, Class componentType, Field field) { @@ -68,12 +70,13 @@ private T[] forReferenceArrays(Fill fill, Class componentType, Field fiel } /** - * Creates an array of the specified component type and fills it with generated values. + * Creates an {@code Object[]} for primitive-compatible component types and fills each element + * with a value generated by {@link ElementCreationService}. * - * @param componentType the class representing the component type of the array - * @param fill the `Fill` object containing the generation parameters - * @param field the field for which the array is generated - * @return the created array filled with generated values + * @param fill the generation parameters + * @param componentType the primitive component type + * @param field the source field (may be {@code null}) + * @return the filled array */ private static Object[] forPrimitiveArrays(Fill fill, Class componentType, Field field) { Object[] genericArray = new Object[fill.getCollectionSize()]; diff --git a/src/main/java/ru/objectsfill/types/collection_type/FillListCollection.java b/src/main/java/ru/objectsfill/types/collection_type/FillListCollection.java index 831a4bf..66f727b 100644 --- a/src/main/java/ru/objectsfill/types/collection_type/FillListCollection.java +++ b/src/main/java/ru/objectsfill/types/collection_type/FillListCollection.java @@ -6,7 +6,8 @@ /** - * class FillListCollection + * Generates a {@link java.util.List} by collecting elements from a stream produced by + * {@link CollectionTypeFill#fillCollectionStream}. */ public class FillListCollection implements CollectionTypeFill { diff --git a/src/main/java/ru/objectsfill/types/collection_type/FillRawStream.java b/src/main/java/ru/objectsfill/types/collection_type/FillRawStream.java index 0018ba9..a81ae02 100644 --- a/src/main/java/ru/objectsfill/types/collection_type/FillRawStream.java +++ b/src/main/java/ru/objectsfill/types/collection_type/FillRawStream.java @@ -6,17 +6,16 @@ /** - * class FillRawStream + * Generates a raw {@link java.util.stream.Stream} of elements without collecting into a concrete collection. */ public class FillRawStream implements CollectionTypeFill { /** - * Generates a set collection based on the provided field and fill parameters. - * The generated collection will be in the form of a raw stream object. + * Returns a stream of randomly generated elements for the given field. * - * @param field the field for which the set collection is generated - * @param fill the `Fill` object containing the generation parameters - * @return the generated set collection + * @param field the field for which the stream is generated + * @param fill the generation parameters + * @return a stream of generated elements */ @Override public Object generate(Field field, Fill fill) { diff --git a/src/main/java/ru/objectsfill/types/collection_type/FillSetCollection.java b/src/main/java/ru/objectsfill/types/collection_type/FillSetCollection.java index d336737..6f756e9 100644 --- a/src/main/java/ru/objectsfill/types/collection_type/FillSetCollection.java +++ b/src/main/java/ru/objectsfill/types/collection_type/FillSetCollection.java @@ -6,7 +6,8 @@ import java.util.stream.Collectors; /** - * class FillSetCollection + * Generates a {@link java.util.Set} by collecting elements from a stream produced by + * {@link CollectionTypeFill#fillCollectionStream}. */ public class FillSetCollection implements CollectionTypeFill { diff --git a/src/main/java/ru/objectsfill/types/collection_type/MapFill.java b/src/main/java/ru/objectsfill/types/collection_type/MapFill.java index 04326cb..8644643 100644 --- a/src/main/java/ru/objectsfill/types/collection_type/MapFill.java +++ b/src/main/java/ru/objectsfill/types/collection_type/MapFill.java @@ -10,7 +10,8 @@ /** - * class MapFill + * Generates a {@link java.util.HashMap} with randomly generated key-value pairs. + * Extracts key and value types from the field's generic type parameters. */ public class MapFill implements CollectionTypeFill { diff --git a/src/main/java/ru/objectsfill/types/interfaces/BoxCollectionFillExtension.java b/src/main/java/ru/objectsfill/types/interfaces/BoxCollectionFillExtension.java index 27918ee..ae5239a 100644 --- a/src/main/java/ru/objectsfill/types/interfaces/BoxCollectionFillExtension.java +++ b/src/main/java/ru/objectsfill/types/interfaces/BoxCollectionFillExtension.java @@ -5,7 +5,8 @@ import java.util.stream.Stream; /** - * Collection interface for filling objects + * Extension interface providing stream-based generation for boxed/wrapper types. + * Implemented by {@link ru.objectsfill.types.box_type.BoxTypeFill}. */ public interface BoxCollectionFillExtension { diff --git a/src/main/java/ru/objectsfill/types/interfaces/ObjectCollectionFillExtension.java b/src/main/java/ru/objectsfill/types/interfaces/ObjectCollectionFillExtension.java index 212ae96..85ed679 100644 --- a/src/main/java/ru/objectsfill/types/interfaces/ObjectCollectionFillExtension.java +++ b/src/main/java/ru/objectsfill/types/interfaces/ObjectCollectionFillExtension.java @@ -5,7 +5,8 @@ import java.util.stream.Stream; /** - * interface ObjectCollectionFillExtension + * Extension interface providing stream-based generation for complex object types. + * Implemented by {@link ru.objectsfill.types.object_type.ObjectTypeFill}. */ public interface ObjectCollectionFillExtension { diff --git a/src/main/java/ru/objectsfill/utils/FieldUtils.java b/src/main/java/ru/objectsfill/utils/FieldUtils.java index 9ee4231..9b2cd9d 100644 --- a/src/main/java/ru/objectsfill/utils/FieldUtils.java +++ b/src/main/java/ru/objectsfill/utils/FieldUtils.java @@ -16,7 +16,8 @@ import static ru.objectsfill.core.RandomValueFieldSetterCallback.getExtendPredicate; /** - * Utility class for working with fields in Java classes. + * Utility class for reflective field traversal and population. + * Provides caching of declared fields and resolution of per-field mutation functions. */ public class FieldUtils { @@ -25,12 +26,16 @@ private FieldUtils() { } private static final Map, Field[]> declaredFieldsCache = new ConcurrentHashMap<>(256); + /** - * Invokes the given FieldCallback for each field in the specified class and its superclasses. + * Iterates over all declared fields of the target object (including superclass fields) + * and populates each one using {@link RandomValueFieldSetterCallback}. + * If the object is {@code null} but the class is set, attempts to create an instance + * via the constructor with the fewest parameters. * - * @param fill special generated object - * @throws IllegalArgumentException if the class is null - * @throws IllegalStateException if the FieldCallback encounters an error while processing a field + * @param fill the Fill configuration containing the target object and generation parameters + * @throws IllegalArgumentException if the class is null + * @throws IllegalStateException if a field cannot be accessed */ public static void doWithFields(Fill fill) { RandomValueFieldSetterCallback fc = new RandomValueFieldSetterCallback(fill); @@ -62,29 +67,28 @@ public static void doWithFields(Fill fill) { } /** - * create instance of class with construct parameters + * Creates an instance of the Fill's target class using the given constructor, + * populating constructor parameters with generated values matched by parameter name. * - * @param fill special generated object - * @param constructor get construct for object creation - * @param type of object - * @return T constructed object - * @throws IllegalArgumentException if the class is null - * @throws IllegalStateException if the FieldCallback encounters an error while processing a field + * @param constructor the constructor to invoke + * @param fill the generation parameters + * @param the type of the created instance + * @return the created instance, or {@code null} if instantiation fails */ public static T addObjectWithParamConstruct(Constructor constructor, Fill fill) { return addObjectWithParamConstruct(constructor, fill, fill.getClazz()); } /** - * create instance of class with construct parameters + * 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. * - * @param fill special generated object - * @param constructor get construct for object creation - * @param type of object - * @param tClass class of outer object for creation - * @return T constructed object - * @throws IllegalArgumentException if the class is null - * @throws IllegalStateException if the FieldCallback encounters an error while processing a field + * @param constructor the constructor to invoke + * @param fill the generation parameters + * @param tClass the class whose fields are matched to constructor parameters + * @param the type of the created instance + * @return the created instance, or {@code null} if instantiation fails */ @SuppressWarnings("unchecked") public static T addObjectWithParamConstruct(Constructor constructor, Fill fill, Class tClass) { @@ -104,14 +108,14 @@ public static T addObjectWithParamConstruct(Constructor constructor, Fill } catch (Exception ignored) {} return null; } + /** - * Retrieves the declared fields of the specified class. - * The fields are cached in the declaredFieldsCache map for performance optimization. + * Returns the declared fields for the given class, using a cache for performance. * - * @param clazz the class to retrieve declared fields from - * @return an array of Field objects representing the declared fields of the class + * @param clazz the class to introspect + * @return an array of declared fields * @throws IllegalArgumentException if the class is null - * @throws IllegalStateException if an error occurs while introspecting the class + * @throws IllegalStateException if class introspection fails */ private static Field[] getDeclaredFields(Class clazz) { if (clazz == null) @@ -131,20 +135,25 @@ private static Field[] getDeclaredFields(Class clazz) { } /** - * get wrap function or default t -> t. If we don't have reference to field + * Resolves the mutation function for the Fill configuration. + * Delegates to {@link #getObjectUnaryOperator(Fill, Field)} with a {@code null} field. * - * @param fill the `Fill` object containing the generation parameters - * @return function + * @param fill the Fill configuration + * @return the mutation function, or identity if none is configured */ public static UnaryOperator getObjectUnaryOperator(Fill fill) { return getObjectUnaryOperator(fill, null); } + /** - * get wrap function or default t -> t. If we don't have reference to field + * Resolves the mutation function for a specific field. + * If the field is {@code null}, returns the first global {@link Extend} function + * (one with no field name and no class). Otherwise, returns the function + * from the matching {@link Extend} parameter for the given field. * - * @param fill the `Fill` object containing the generation parameters - * @param field The field for which the collection stream is being filled. - * @return function + * @param fill the Fill configuration + * @param field the target field, or {@code null} for global resolution + * @return the mutation function, or identity ({@code t -> t}) if none is configured */ public static UnaryOperator getObjectUnaryOperator(Fill fill, Field field) { UnaryOperator mutationFunction = t -> t; @@ -166,10 +175,10 @@ public static UnaryOperator getObjectUnaryOperator(Fill fill, Field fiel } /** - * Get first of ext parameters + * Returns the first {@link Extend} parameter from the Fill's extended parameter list. * - * @param fill the `Fill` object containing the generation parameters - * @return get any of param + * @param fill the Fill configuration + * @return the first Extend parameter, or empty if none exist */ private static Optional getFirstSingleParamFunction(Fill fill) { return fill.getExtendedFieldParams() @@ -178,10 +187,12 @@ private static Optional getFirstSingleParamFunction(Fill fill) { } /** + * Finds the {@link Extend} parameter that matches the given field + * by name or class type using {@link RandomValueFieldSetterCallback#getExtendPredicate}. * - * @param field The field for which the collection stream is being filled. - * @param fill The Fill object containing the necessary information for generation. - * @return extended parameter for some field + * @param field the field to match against + * @param fill the Fill configuration containing extended parameters + * @return the matching Extend, or empty if no match */ public static Optional getExtFillParam(Field field, Fill fill) { return fill.getExtendedFieldParams()