Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add advanced generic and wild-card parse mechanism #513

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/main/java/org/jeasy/random/CollectionPopulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ class CollectionPopulator {

private final EasyRandom easyRandom;

CollectionPopulator(final EasyRandom easyRandom) {
private final GenericResolver genericResolver;

CollectionPopulator(final EasyRandom easyRandom, final GenericResolver genericResolver) {
this.easyRandom = easyRandom;
this.genericResolver = genericResolver;
}

@SuppressWarnings({"unchecked", "rawtypes"})
Collection<?> getRandomCollection(final Field field, final RandomizationContext context) {
int randomSize = getRandomCollectionSize(context.getParameters());
Class<?> fieldType = field.getType();
Type fieldGenericType = field.getGenericType();
Class<?> fieldType = genericResolver.resolveRawFieldType(field, context);
Type fieldGenericType = genericResolver.resolveFieldType(field, context);
Collection collection;

if (isInterface(fieldType)) {
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/org/jeasy/random/EasyRandom.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class EasyRandom extends Random {

private final ExclusionPolicy exclusionPolicy;

private final GenericResolver genericResolver;

/**
* Create a new {@link EasyRandom} instance with default parameters.
*/
Expand All @@ -77,13 +79,14 @@ public EasyRandom(final EasyRandomParameters easyRandomParameters) {
randomizerProvider.setRandomizerRegistries(registries);
objectFactory = easyRandomParameters.getObjectFactory();
arrayPopulator = new ArrayPopulator(this);
CollectionPopulator collectionPopulator = new CollectionPopulator(this);
MapPopulator mapPopulator = new MapPopulator(this, objectFactory);
genericResolver = new GenericResolver();
CollectionPopulator collectionPopulator = new CollectionPopulator(this, genericResolver);
MapPopulator mapPopulator = new MapPopulator(this, objectFactory, genericResolver);
OptionalPopulator optionalPopulator = new OptionalPopulator(this);
enumRandomizersByType = new ConcurrentHashMap<>();
fieldPopulator = new FieldPopulator(this,
this.randomizerProvider, arrayPopulator,
collectionPopulator, mapPopulator, optionalPopulator);
collectionPopulator, mapPopulator, optionalPopulator, this.genericResolver);
exclusionPolicy = easyRandomParameters.getExclusionPolicy();
parameters = easyRandomParameters;
}
Expand Down Expand Up @@ -143,6 +146,7 @@ <T> T doPopulateBean(final Class<T> type, final RandomizationContext context) {

T result;
try {
genericResolver.resolveInheritanceGenerics(type, context);

Randomizer<?> randomizer = randomizerProvider.getRandomizerByType(type, context);
if (randomizer != null) {
Expand Down Expand Up @@ -190,6 +194,8 @@ <T> T doPopulateBean(final Class<T> type, final RandomizationContext context) {
} else {
throw new ObjectCreationException("Unable to create a random instance of type " + type, e);
}
} finally {
context.popGenericsContext();
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/jeasy/random/EasyRandomParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public class EasyRandomParameters {
private boolean overrideDefaultInitialization;
private boolean ignoreRandomizationErrors;
private boolean bypassSetters;
private boolean advancedGenericParseMechanism;
private Range<Integer> collectionSizeRange;
private Range<Integer> stringLengthRange;
private Range<LocalDate> dateRange;
Expand All @@ -123,6 +124,7 @@ public EasyRandomParameters() {
overrideDefaultInitialization = false;
ignoreRandomizationErrors = false;
bypassSetters = false;
advancedGenericParseMechanism = false;
objectPoolSize = DEFAULT_OBJECT_POOL_SIZE;
randomizationDepth = DEFAULT_RANDOMIZATION_DEPTH;
dateRange = new Range<>(DEFAULT_DATES_RANGE.getMin().toLocalDate(), DEFAULT_DATES_RANGE.getMax().toLocalDate());
Expand Down Expand Up @@ -231,6 +233,14 @@ public void setBypassSetters(boolean bypassSetters) {
this.bypassSetters = bypassSetters;
}

public boolean isAdvancedGenericParseMechanism() {
return advancedGenericParseMechanism;
}

public void setAdvancedGenericParseMechanism(boolean advancedGenericParseMechanism) {
this.advancedGenericParseMechanism = advancedGenericParseMechanism;
}

public ExclusionPolicy getExclusionPolicy() {
return exclusionPolicy;
}
Expand Down Expand Up @@ -561,6 +571,17 @@ public EasyRandomParameters bypassSetters(boolean bypassSetters) {
return this;
}

/**
* Flag to use advanced generic parse mechanism allowed resolve any TypeVariable, WildCards, etc.
*
* @param advancedGenericParseMechanism true if parse mechanism enabled
* @return the current {@link EasyRandomParameters} instance for method chaining
*/
public EasyRandomParameters advancedGenericParseMechanism(boolean advancedGenericParseMechanism) {
setAdvancedGenericParseMechanism(advancedGenericParseMechanism);
return this;
}

/**
* Utility class to hold a range of values.
*
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/org/jeasy/random/FieldPopulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;

Expand Down Expand Up @@ -68,15 +69,19 @@ class FieldPopulator {

private final RandomizerProvider randomizerProvider;

private final GenericResolver genericResolver;

FieldPopulator(final EasyRandom easyRandom, final RandomizerProvider randomizerProvider,
final ArrayPopulator arrayPopulator, final CollectionPopulator collectionPopulator,
final MapPopulator mapPopulator, OptionalPopulator optionalPopulator) {
final MapPopulator mapPopulator, OptionalPopulator optionalPopulator,
final GenericResolver genericResolver) {
this.easyRandom = easyRandom;
this.randomizerProvider = randomizerProvider;
this.arrayPopulator = arrayPopulator;
this.collectionPopulator = collectionPopulator;
this.mapPopulator = mapPopulator;
this.optionalPopulator = optionalPopulator;
this.genericResolver = genericResolver;
}

void populateField(final Object target, final Field field, final RandomizationContext context) throws IllegalAccessException {
Expand Down Expand Up @@ -127,7 +132,7 @@ private Randomizer<?> getRandomizer(Field field, RandomizationContext context) {
Type genericType = field.getGenericType();
if (isTypeVariable(genericType)) {
// if generic type, retrieve actual type from declaring class
Class<?> type = getParametrizedType(field, context);
Class<?> type = getFieldType(field, context);
randomizer = randomizerProvider.getRandomizerByType(type, context);
} else {
randomizer = randomizerProvider.getRandomizerByType(field.getType(), context);
Expand All @@ -137,8 +142,8 @@ private Randomizer<?> getRandomizer(Field field, RandomizationContext context) {
}

private Object generateRandomValue(final Field field, final RandomizationContext context) {
Class<?> fieldType = field.getType();
Type fieldGenericType = field.getGenericType();
Class<?> fieldType = genericResolver.resolveRawFieldType(field, context);
Type fieldGenericType = genericResolver.resolveFieldType(field, context);

if (isArrayType(fieldType)) {
return arrayPopulator.getRandomArray(fieldType, context);
Expand All @@ -158,10 +163,10 @@ private Object generateRandomValue(final Field field, final RandomizationContext
return easyRandom.doPopulateBean(randomConcreteSubType, context);
}
} else {
Type genericType = field.getGenericType();
Type genericType = genericResolver.resolveFieldType(field, context);
if (isTypeVariable(genericType)) {
// if generic type, try to retrieve actual type from hierarchy
Class<?> type = getParametrizedType(field, context);
Class<?> type = getFieldType(field, context);
return easyRandom.doPopulateBean(type, context);
}
return easyRandom.doPopulateBean(fieldType, context);
Expand Down Expand Up @@ -210,4 +215,12 @@ private Type getGenericSuperClass(RandomizationContext context) {
}
return genericSuperclass;
}

private Class<?> getFieldType(Field field, RandomizationContext context) {
if (context.getParameters().isAdvancedGenericParseMechanism()) {
return genericResolver.resolveRawFieldType(field, context);
} else {
return getParametrizedType(field, context);
}
}
}
Loading