From e7974f785d30ab54f7646c3af76e2b0d74d1df54 Mon Sep 17 00:00:00 2001 From: Jan Ouwens Date: Wed, 6 Nov 2024 22:20:14 +0100 Subject: [PATCH] Finds fields in supertypes as well --- .../internal/util/PrefabValuesApi.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/PrefabValuesApi.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/PrefabValuesApi.java index ff99f6333..9a47b11c8 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/PrefabValuesApi.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/PrefabValuesApi.java @@ -3,6 +3,7 @@ import java.lang.reflect.Field; import nl.jqno.equalsverifier.Func.Func1; import nl.jqno.equalsverifier.Func.Func2; +import nl.jqno.equalsverifier.internal.reflection.SuperclassIterable; import nl.jqno.equalsverifier.internal.reflection.instantiation.GenericPrefabValueProvider.GenericFactories; import nl.jqno.equalsverifier.internal.reflection.instantiation.PrefabValueProvider; import nl.jqno.equalsverifier.internal.reflection.vintage.ObjectAccessor; @@ -43,7 +44,7 @@ public static void addPrefabValuesForField( T red, T blue ) { - Field field = getField(enclosingType, fieldName); + Field field = findField(enclosingType, fieldName); Class type = (Class) field.getType(); Validations.validateRedAndBluePrefabValues(type, red, blue); @@ -62,18 +63,21 @@ public static void addPrefabValuesForField( } } - private static Field getField(Class type, String fieldName) { - try { - return type.getDeclaredField(fieldName); - } catch (NoSuchFieldException e) { - throw new IllegalStateException( - "Precondition: class " + - type.getSimpleName() + - " does not contain field " + - fieldName + - "." - ); + private static Field findField(Class type, String fieldName) { + for (Class c : SuperclassIterable.ofIncludeSelf(type)) { + try { + return c.getDeclaredField(fieldName); + } catch (NoSuchFieldException ignored) { + // Do nothing + } } + throw new IllegalStateException( + "Precondition: class " + + type.getSimpleName() + + " does not contain field " + + fieldName + + "." + ); } public static void addGenericPrefabValues(