@@ -76,7 +76,7 @@ class FieldPopulator {
76
76
}
77
77
78
78
void populateField (final Object target , final Field field , final RandomizationContext context ) throws IllegalAccessException {
79
- Randomizer <?> randomizer = getRandomizer (field , context );
79
+ Randomizer <?> randomizer = getRandomizer (field , context , target . getClass () );
80
80
if (randomizer instanceof SkipRandomizer ) {
81
81
return ;
82
82
}
@@ -113,14 +113,14 @@ void populateField(final Object target, final Field field, final RandomizationCo
113
113
context .popStackItem ();
114
114
}
115
115
116
- private Randomizer <?> getRandomizer (Field field , RandomizationContext context ) {
116
+ private Randomizer <?> getRandomizer (Field field , RandomizationContext context , Class <?> fieldTargetType ) {
117
117
// issue 241: if there is no custom randomizer by field, then check by type
118
118
Randomizer <?> randomizer = randomizerProvider .getRandomizerByField (field , context );
119
119
if (randomizer == null ) {
120
120
Type genericType = field .getGenericType ();
121
121
if (isTypeVariable (genericType )) {
122
122
// if generic type, retrieve actual type from declaring class
123
- Class <?> type = getParametrizedType (field , context );
123
+ Class <?> type = getParametrizedType (field , fieldTargetType );
124
124
randomizer = randomizerProvider .getRandomizerByType (type , context );
125
125
} else {
126
126
randomizer = randomizerProvider .getRandomizerByType (field .getType (), context );
@@ -154,18 +154,18 @@ private Object generateRandomValue(final Field field, final RandomizationContext
154
154
Type genericType = field .getGenericType ();
155
155
if (isTypeVariable (genericType )) {
156
156
// if generic type, try to retrieve actual type from hierarchy
157
- Class <?> type = getParametrizedType (field , context );
157
+ Class <?> type = getParametrizedType (field , context . getTargetType () );
158
158
return easyRandom .doPopulateBean (type , context );
159
159
}
160
160
return easyRandom .doPopulateBean (fieldType , context );
161
161
}
162
162
}
163
163
}
164
164
165
- private Class <?> getParametrizedType (Field field , RandomizationContext context ) {
165
+ private Class <?> getParametrizedType (Field field , Class <?> fieldTargetType ) {
166
166
Class <?> declaringClass = field .getDeclaringClass ();
167
167
TypeVariable <? extends Class <?>>[] typeParameters = declaringClass .getTypeParameters ();
168
- Type genericSuperclass = getGenericSuperClass (context );
168
+ Type genericSuperclass = getGenericSuperClass (fieldTargetType );
169
169
ParameterizedType parameterizedGenericSuperType = (ParameterizedType ) genericSuperclass ;
170
170
Type [] actualTypeArguments = parameterizedGenericSuperType .getActualTypeArguments ();
171
171
Type actualTypeArgument = null ;
@@ -192,8 +192,7 @@ private Class<?> getParametrizedType(Field field, RandomizationContext context)
192
192
}
193
193
194
194
// find the generic base class in the hierarchy (which might not be the first super type)
195
- private Type getGenericSuperClass (RandomizationContext context ) {
196
- Class <?> targetType = context .getTargetType ();
195
+ private Type getGenericSuperClass (Class <?> targetType ) {
197
196
Type genericSuperclass = targetType .getGenericSuperclass ();
198
197
while (targetType != null && !(genericSuperclass instanceof ParameterizedType )) {
199
198
targetType = targetType .getSuperclass ();
0 commit comments