HHH-14006 Support inherited fields in EnhancerTestUtils#getFieldByReflection

This commit is contained in:
Yoann Rodière 2020-05-11 13:40:57 +02:00 committed by Sanne Grinovero
parent 03d34ae84c
commit f6ebcc5f80
1 changed files with 17 additions and 1 deletions

View File

@ -32,7 +32,7 @@ public abstract class EnhancerTestUtils extends BaseUnitTestCase {
public static Object getFieldByReflection(Object entity, String fieldName) {
try {
Field field = entity.getClass().getDeclaredField( fieldName );
Field field = findField( entity.getClass(), fieldName );
ReflectHelper.ensureAccessibility( field );
return field.get( entity );
}
@ -75,4 +75,20 @@ public abstract class EnhancerTestUtils extends BaseUnitTestCase {
);
}
private static Field findField(Class<?> clazz, String fieldName) throws NoSuchFieldException {
NoSuchFieldException exception = null;
while ( clazz != null ) {
try {
return clazz.getDeclaredField( fieldName );
}
catch (NoSuchFieldException e) {
if ( exception == null ) {
exception = e;
}
clazz = clazz.getSuperclass();
}
}
throw exception;
}
}