Skip to content

Commit

Permalink
issue #242 : add support for generic enums
Browse files Browse the repository at this point in the history
  • Loading branch information
fmbenhassine committed Feb 24, 2017
1 parent 5622495 commit 035c378
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class EnhancedRandomImpl extends EnhancedRandom {

@Override
public <T> T nextObject(final Class<T> type, final String... excludedFields) {
return doPopulateBean(type, new RandomizationContext(parameters, excludedFields));
return doPopulateBean(type, new RandomizationContext(type, parameters, excludedFields));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@

import io.github.benas.randombeans.api.ObjectGenerationException;
import io.github.benas.randombeans.api.Randomizer;
import io.github.benas.randombeans.randomizers.misc.EnumRandomizer;
import io.github.benas.randombeans.randomizers.misc.SkipRandomizer;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;

import static io.github.benas.randombeans.util.CollectionUtils.randomElementOf;
import static io.github.benas.randombeans.util.ReflectionUtils.*;
Expand Down Expand Up @@ -104,7 +107,9 @@ private Object generateRandomValue(final Field field, final RandomizationContext
Type fieldGenericType = field.getGenericType();

Object value;
if (isArrayType(fieldType)) {
if (isEnumType(fieldType)){
value = new EnumRandomizer(getGenericEnumTypeIfAny(field, context)).getRandomValue();
} else if (isArrayType(fieldType)) {
value = arrayPopulator.getRandomArray(fieldType, context);
} else if (isCollectionType(fieldType)) {
value = collectionPopulator.getRandomCollection(field, context);
Expand All @@ -125,6 +130,24 @@ private Object generateRandomValue(final Field field, final RandomizationContext
return value;
}

private Class<?> getGenericEnumTypeIfAny(Field field, RandomizationContext context) {
Class<?> declaringClass = field.getDeclaringClass();
TypeVariable<? extends Class<?>>[] typeParameters = declaringClass.getTypeParameters();
// if generic type, retrieve actual type from declaring class
if (typeParameters.length > 0) {
ParameterizedType genericSuperclass = (ParameterizedType)context.getRootType().getGenericSuperclass();
Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
Type actualTypeArgument = actualTypeArguments[0];
try {
return Class.forName(actualTypeArgument.getTypeName());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e); // the class should have been already loaded without any problem at this point
}
}
return field.getType();

}

void setScanClasspathForConcreteTypes(boolean scanClasspathForConcreteTypes) {
this.scanClasspathForConcreteTypes = scanClasspathForConcreteTypes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public static boolean isArrayType(final Class<?> type) {
* @return true if the type is an enum type, false otherwise.
*/
public static boolean isEnumType(final Class<?> type) {
return type.isEnum();
return type.isEnum() || Enum.class.isAssignableFrom(type);
}

/**
Expand Down

0 comments on commit 035c378

Please sign in to comment.