Skip to content

Commit

Permalink
Finds fields in supertypes as well
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Nov 6, 2024
1 parent 8b2c31b commit e7974f7
Showing 1 changed file with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,7 +44,7 @@ public static <T> void addPrefabValuesForField(
T red,
T blue
) {
Field field = getField(enclosingType, fieldName);
Field field = findField(enclosingType, fieldName);
Class<T> type = (Class<T>) field.getType();

Validations.validateRedAndBluePrefabValues(type, red, blue);
Expand All @@ -62,18 +63,21 @@ public static <T> 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 <T> void addGenericPrefabValues(
Expand Down

0 comments on commit e7974f7

Please sign in to comment.