diff --git a/easy-random-core/src/main/java/org/jeasy/random/FieldPopulator.java b/easy-random-core/src/main/java/org/jeasy/random/FieldPopulator.java index 482c75833..733b21fd4 100644 --- a/easy-random-core/src/main/java/org/jeasy/random/FieldPopulator.java +++ b/easy-random-core/src/main/java/org/jeasy/random/FieldPopulator.java @@ -76,7 +76,7 @@ class FieldPopulator { } void populateField(final Object target, final Field field, final RandomizationContext context) throws IllegalAccessException { - Randomizer randomizer = getRandomizer(field, context); + Randomizer randomizer = getRandomizer(field, context, target.getClass()); if (randomizer instanceof SkipRandomizer) { return; } @@ -113,14 +113,14 @@ void populateField(final Object target, final Field field, final RandomizationCo context.popStackItem(); } - private Randomizer getRandomizer(Field field, RandomizationContext context) { + private Randomizer getRandomizer(Field field, RandomizationContext context, Class fieldTargetType) { // issue 241: if there is no custom randomizer by field, then check by type Randomizer randomizer = randomizerProvider.getRandomizerByField(field, context); if (randomizer == null) { Type genericType = field.getGenericType(); if (isTypeVariable(genericType)) { // if generic type, retrieve actual type from declaring class - Class type = getParametrizedType(field, context); + Class type = getParametrizedType(field, fieldTargetType); randomizer = randomizerProvider.getRandomizerByType(type, context); } else { randomizer = randomizerProvider.getRandomizerByType(field.getType(), context); @@ -154,7 +154,7 @@ private Object generateRandomValue(final Field field, final RandomizationContext Type genericType = field.getGenericType(); if (isTypeVariable(genericType)) { // if generic type, try to retrieve actual type from hierarchy - Class type = getParametrizedType(field, context); + Class type = getParametrizedType(field, context.getTargetType()); return easyRandom.doPopulateBean(type, context); } return easyRandom.doPopulateBean(fieldType, context); @@ -162,10 +162,10 @@ private Object generateRandomValue(final Field field, final RandomizationContext } } - private Class getParametrizedType(Field field, RandomizationContext context) { + private Class getParametrizedType(Field field, Class fieldTargetType) { Class declaringClass = field.getDeclaringClass(); TypeVariable>[] typeParameters = declaringClass.getTypeParameters(); - Type genericSuperclass = getGenericSuperClass(context); + Type genericSuperclass = getGenericSuperClass(fieldTargetType); ParameterizedType parameterizedGenericSuperType = (ParameterizedType) genericSuperclass; Type[] actualTypeArguments = parameterizedGenericSuperType.getActualTypeArguments(); Type actualTypeArgument = null; @@ -192,8 +192,7 @@ private Class getParametrizedType(Field field, RandomizationContext context) } // find the generic base class in the hierarchy (which might not be the first super type) - private Type getGenericSuperClass(RandomizationContext context) { - Class targetType = context.getTargetType(); + private Type getGenericSuperClass(Class targetType) { Type genericSuperclass = targetType.getGenericSuperclass(); while (targetType != null && !(genericSuperclass instanceof ParameterizedType)) { targetType = targetType.getSuperclass(); diff --git a/easy-random-core/src/test/java/org/jeasy/random/Generic2Test.java b/easy-random-core/src/test/java/org/jeasy/random/Generic2Test.java new file mode 100644 index 000000000..c9260f26c --- /dev/null +++ b/easy-random-core/src/test/java/org/jeasy/random/Generic2Test.java @@ -0,0 +1,69 @@ +/* + * The MIT License + * + * Copyright (c) 2020, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.jeasy.random; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.Serializable; +import org.junit.jupiter.api.Test; + +/** Test suggested in https://github.com/j-easy/easy-random/issues/441 by @seregamorph */ +public class Generic2Test { + + @Test + void genericComposedShouldBeCorrectlyPopulated() { + // given + EasyRandom easyRandom = new EasyRandom(); + + // when + CompositeResource composite = easyRandom.nextObject(CompositeResource.class); + + // then + assertThat(composite.longResource.getId()) + .isInstanceOf(Long.class) + .isNotNull(); + } + + static abstract class IdResource> { + + private K id; + + @SuppressWarnings("unchecked") + public T setId(K id) { + this.id = id; + return (T) this; + } + + public K getId() { + return id; + } + } + + static class LongResource extends IdResource { + } + + static class CompositeResource { + private LongResource longResource; + } +}