Class Generators
Registers generators declared in
/META-INF/services/dev.orne.test.rnd.Generator
SPI files in the class path.
Further registered generator adjustment can be performed with
getRegisteredGenerators(), getGenerator(Class),
register(Generator...), register(Collection),
remove(Generator...), remove(Collection)
and reset().
- Since:
- 0.1
- Version:
- 1.1, 2023-11
- Author:
- (w) Iker Hernaez
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classCache value for missing generators for a value type. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Comparator<Generator>The generator comparator by priority. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TdefaultValue(@NotNull Class<T> type) Returns the default value for the specified type.static <T> TdefaultValue(@NotNull Class<T> type, @NotNull Object... params) Returns the default value for the specified type.static <T> @NotNull PropertyTypeGenerator<T>Returns a targeted generator for the specified field.static <T> @NotNull PropertyTypeGenerator<T>Returns a targeted generator for the specified field.static <T> @NotNull ConstructorParameterTypeGenerator<T>forParameter(@NotNull Class<?> cls, @NotNull Class<?>[] parameterTypes, @javax.validation.constraints.NotNull int parameterIndex) Returns a targeted generator for the specified constructor parameter.static <T> @NotNull MethodParameterTypeGenerator<T>forParameter(@NotNull Class<?> cls, @NotNull String method, @NotNull Class<?>[] parameterTypes, @javax.validation.constraints.NotNull int parameterIndex) Returns a targeted generator for the specified method parameter.static <T> @NotNull ConstructorParameterTypeGenerator<T>forParameter(@NotNull Constructor<?> constructor, @javax.validation.constraints.NotNull int parameterIndex) Returns a targeted generator for the specified constructor parameter.static <T> @NotNull MethodParameterTypeGenerator<T>forParameter(@NotNull Method method, @javax.validation.constraints.NotNull int parameterIndex) Returns a targeted generator for the specified method parameter.static <T> @NotNull ParameterTypeGenerator<T>forParameter(@NotNull Parameter parameter) Returns a targeted generator for the specified parameter.static <T> @NotNull PropertyTypeGenerator<T>forProperty(@NotNull Class<?> beanType, @NotNull String property) Returns a targeted generator for the specified bean property.static <T> @NotNull MethodReturnTypeGenerator<T>forReturnType(@NotNull Class<?> cls, @NotNull String method, @NotNull Class<?>[] parameterTypes) Returns a targeted generator for the specified method return type.static <T> @NotNull MethodReturnTypeGenerator<T>forReturnType(@NotNull Method method) Returns a targeted generator for the specified method return type.static GeneratorgetGenerator(@NotNull Class<?> type) Returns the generator to use for the specified value type.static ParameterizableGeneratorgetParameterizableGenerator(@NotNull Class<?> type) Returns the parameterizable generator to use for the specified value type.Returns an unmodifiable list with the registered generators.static <T> TnullableDefaultValue(@NotNull Class<T> type) Returns the default value for the specified type allowingnullvalues.static <T> TnullableDefaultValue(@NotNull Class<T> type, @NotNull Object... params) Returns the default value for the specified type allowingnullvalues.static <T> TnullableRandomValue(@NotNull Class<T> type) Returns a random value of the specified type allowingnullvalues.static <T> TnullableRandomValue(@NotNull Class<T> type, @NotNull Object... params) Returns a random value of the specified type allowingnullvalues.static <T> TrandomValue(@NotNull Class<T> type) Returns a random value of the specified type.static <T> TrandomValue(@NotNull Class<T> type, @NotNull Object... params) Returns a random value of the specified type.static voidAdds the specified generators to the registered generators.static voidregister(@NotNull Collection<Generator> generators) Adds the specified generators to the registered generators.static voidRemoves the specified generators from the registered generators.static voidremove(@NotNull Collection<Generator> generators) Removes the specified generators from the registered generators.static voidreset()Resets the loaded and cached generators.static booleanChecks if values of the specified type can be generated by any registered generator.
-
Field Details
-
COMPARATOR
The generator comparator by priority.
-
-
Method Details
-
supports
Checks if values of the specified type can be generated by any registered generator.- Parameters:
type- The type to check.- Returns:
- If values of the specified type can be generated.
-
defaultValue
Returns the default value for the specified type.- Type Parameters:
T- The requested value type.- Parameters:
type- The requested value type.- Returns:
- The default value for the specified type.
- Throws:
GeneratorNotFoundException- If no generator supports the requested value type.
-
defaultValue
@API(status=EXPERIMENTAL, since="0.1") public static <T> T defaultValue(@NotNull @NotNull Class<T> type, @NotNull @NotNull Object... params) Returns the default value for the specified type.- Type Parameters:
T- The requested value type.- Parameters:
type- The requested value type.params- The generation parameter sources.- Returns:
- The default value for the specified type.
- Throws:
GeneratorNotFoundException- If no generator supports the requested value type.GeneratorNotParameterizableException- If the generator registered for the requested value type is not parameterizable.
-
nullableDefaultValue
Returns the default value for the specified type allowingnullvalues.This method should return
nullexcept for native types.- Type Parameters:
T- The requested value type.- Parameters:
type- The requested value type.- Returns:
- The nullable default value for the specified type.
- Throws:
GeneratorNotFoundException- If no generator supports the requested value type.
-
nullableDefaultValue
@API(status=EXPERIMENTAL, since="0.1") public static <T> T nullableDefaultValue(@NotNull @NotNull Class<T> type, @NotNull @NotNull Object... params) Returns the default value for the specified type allowingnullvalues.This method should return
nullexcept for native types.- Type Parameters:
T- The requested value type.- Parameters:
type- The requested value type.params- The generation parameter sources.- Returns:
- The nullable default value for the specified type.
- Throws:
GeneratorNotFoundException- If no generator supports the requested value type.GeneratorNotParameterizableException- If the generator registered for the requested value type is not parameterizable.
-
randomValue
Returns a random value of the specified type.- Type Parameters:
T- The requested value type.- Parameters:
type- The requested value type.- Returns:
- A random value for the specified type.
- Throws:
GeneratorNotFoundException- If no generator supports the requested value type.
-
randomValue
@API(status=EXPERIMENTAL, since="0.1") public static <T> T randomValue(@NotNull @NotNull Class<T> type, @NotNull @NotNull Object... params) Returns a random value of the specified type.- Type Parameters:
T- The requested value type.- Parameters:
type- The requested value type.params- The generation parameter sources.- Returns:
- A random value for the specified type.
- Throws:
GeneratorNotFoundException- If no generator supports the requested value type.GeneratorNotParameterizableException- If the generator registered for the requested value type is not parameterizable.
-
nullableRandomValue
Returns a random value of the specified type allowingnullvalues.The returned value has a probability of be
nullexcept for native types. If notnullbehaves asrandomValue().- Type Parameters:
T- The requested value type.- Parameters:
type- The requested value type.- Returns:
- A random nullable value for the specified type.
- Throws:
GeneratorNotFoundException- If no generator supports the requested value type.- See Also:
-
nullableRandomValue
@API(status=EXPERIMENTAL, since="0.1") public static <T> T nullableRandomValue(@NotNull @NotNull Class<T> type, @NotNull @NotNull Object... params) Returns a random value of the specified type allowingnullvalues.The returned value has a probability of be
nullexcept for native types. If notnullbehaves asrandomValue().- Type Parameters:
T- The requested value type.- Parameters:
type- The requested value type.params- The generation parameter sources.- Returns:
- A random nullable value for the specified type.
- Throws:
GeneratorNotFoundException- If no generator supports the requested value type.GeneratorNotParameterizableException- If the generator registered for the requested value type is not parameterizable.- See Also:
-
getGenerator
Returns the generator to use for the specified value type.- Parameters:
type- The value type to generate- Returns:
- The generator to use. Returns
nullis no one is suitable.
-
getParameterizableGenerator
Returns the parameterizable generator to use for the specified value type. If no generator supports the requested value type or the generator is not parameterizable returnsnull.- Parameters:
type- The value type to generate- Returns:
- The generator to use. Returns
nullis no one is suitable.
-
forField
@API(status=EXPERIMENTAL, since="0.2") @NotNull public static <T> @NotNull PropertyTypeGenerator<T> forField(@NotNull @NotNull Field field) Returns a targeted generator for the specified field.If a parameterizable generator has been registered for the specified type extracts the generation parameters from the field declaration.
- Type Parameters:
T- The type of the generated values- Parameters:
field- The bean field- Returns:
- A generator for the type of the specified field
- Since:
- 0.2
-
forField
@API(status=EXPERIMENTAL, since="0.2") @NotNull public static <T> @NotNull PropertyTypeGenerator<T> forField(@NotNull @NotNull Class<?> beanType, @NotNull @NotNull Field field) Returns a targeted generator for the specified field.If a parameterizable generator has been registered for the specified type extracts the generation parameters from the field declaration.
- Type Parameters:
T- The type of the generated values- Parameters:
beanType- The bean typefield- The bean field- Returns:
- A generator for the type of the specified field
- Since:
- 0.2
-
forProperty
@API(status=EXPERIMENTAL, since="0.1") @NotNull public static <T> @NotNull PropertyTypeGenerator<T> forProperty(@NotNull @NotNull Class<?> beanType, @NotNull @NotNull String property) Returns a targeted generator for the specified bean property.If a parameterizable generator has been registered for the specified type extracts the generation parameters from the property declaration.
- Type Parameters:
T- The type of the generated values- Parameters:
beanType- The bean typeproperty- The property of the bean- Returns:
- A generator for the type of the specified property
-
forParameter
@API(status=EXPERIMENTAL, since="0.2") @NotNull public static <T> @NotNull ParameterTypeGenerator<T> forParameter(@NotNull @NotNull Parameter parameter) Returns a targeted generator for the specified parameter.If a parameterizable generator has been registered for the specified type extracts the generation parameters from the parameter declaration.
- Type Parameters:
T- The type of the generated values- Parameters:
parameter- The parameter- Returns:
- A generator for the type of the specified parameter
- Since:
- 0.2
-
forParameter
@API(status=EXPERIMENTAL, since="0.1") @NotNull public static <T> @NotNull MethodParameterTypeGenerator<T> forParameter(@NotNull @NotNull Method method, @NotNull @javax.validation.constraints.NotNull int parameterIndex) Returns a targeted generator for the specified method parameter.If a parameterizable generator has been registered for the specified type extracts the generation parameters from the method declaration.
- Type Parameters:
T- The type of the generated values- Parameters:
method- The methodparameterIndex- The parameter index- Returns:
- A generator for the type of the specified method parameter
-
forParameter
@API(status=EXPERIMENTAL, since="0.1") @NotNull public static <T> @NotNull MethodParameterTypeGenerator<T> forParameter(@NotNull @NotNull Class<?> cls, @NotNull @NotNull String method, @NotNull @NotNull Class<?>[] parameterTypes, @NotNull @javax.validation.constraints.NotNull int parameterIndex) Returns a targeted generator for the specified method parameter.If a parameterizable generator has been registered for the specified type extracts the generation parameters from the method declaration.
- Type Parameters:
T- The type of the generated values- Parameters:
cls- The method's classmethod- The method nameparameterTypes- The method parameter typesparameterIndex- The parameter index- Returns:
- A generator for the type of the specified method parameter
-
forReturnType
@API(status=EXPERIMENTAL, since="0.1") @NotNull public static <T> @NotNull MethodReturnTypeGenerator<T> forReturnType(@NotNull @NotNull Method method) Returns a targeted generator for the specified method return type.If a parameterizable generator has been registered for the specified type extracts the generation parameters from the method declaration.
- Type Parameters:
T- The type of the generated values- Parameters:
method- The method- Returns:
- A generator for the type of the specified method return type
-
forReturnType
@API(status=EXPERIMENTAL, since="0.1") @NotNull public static <T> @NotNull MethodReturnTypeGenerator<T> forReturnType(@NotNull @NotNull Class<?> cls, @NotNull @NotNull String method, @NotNull @NotNull Class<?>[] parameterTypes) Returns a targeted generator for the specified method return type.If a parameterizable generator has been registered for the specified type extracts the generation parameters from the method declaration.
- Type Parameters:
T- The type of the generated values- Parameters:
cls- The method's classparameterTypes- The method parameter typesmethod- The method name- Returns:
- A generator for the type of the specified method return type
-
forParameter
@API(status=EXPERIMENTAL, since="0.1") @NotNull public static <T> @NotNull ConstructorParameterTypeGenerator<T> forParameter(@NotNull @NotNull Constructor<?> constructor, @NotNull @javax.validation.constraints.NotNull int parameterIndex) Returns a targeted generator for the specified constructor parameter.If a parameterizable generator has been registered for the specified type extracts the generation parameters from the constructor declaration.
- Type Parameters:
T- The type of the generated values- Parameters:
constructor- The constructorparameterIndex- The parameter index- Returns:
- A generator for the type of the specified constructor parameter
-
forParameter
@API(status=EXPERIMENTAL, since="0.1") @NotNull public static <T> @NotNull ConstructorParameterTypeGenerator<T> forParameter(@NotNull @NotNull Class<?> cls, @NotNull @NotNull Class<?>[] parameterTypes, @NotNull @javax.validation.constraints.NotNull int parameterIndex) Returns a targeted generator for the specified constructor parameter.If a parameterizable generator has been registered for the specified type extracts the generation parameters from the constructor declaration.
- Type Parameters:
T- The type of the generated values- Parameters:
cls- The class of the constructorparameterTypes- The constructor parameter typesparameterIndex- The parameter index- Returns:
- A generator for the type of the specified constructor parameter
-
getRegisteredGenerators
Returns an unmodifiable list with the registered generators.- Returns:
- The registered generators
-
register
Adds the specified generators to the registered generators.- Parameters:
generators- The generators to register
-
register
Adds the specified generators to the registered generators.- Parameters:
generators- The generators to register
-
remove
Removes the specified generators from the registered generators.- Parameters:
generators- The generators to remove
-
remove
Removes the specified generators from the registered generators.- Parameters:
generators- The generators to remove
-
reset
public static void reset()Resets the loaded and cached generators. Next call will regenerate the generator list (including SPI generators) and restart generator caching.
-