Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,9 @@ public static List<Field> findAnnotatedFields(Class<?> clazz, Class<? extends An
List<Field> fields = findAnnotatedFields(instance.getClass(), annotationType, ModifierSupport::isNotStatic,
HierarchyTraversalMode.TOP_DOWN);

return ReflectionUtils.readFieldValues(fields, instance);
@SuppressWarnings("unchecked")
List<@Nullable Object> result = (List<@Nullable Object>) ReflectionUtils.readFieldValues(fields, instance);
return result;
}

/**
Expand Down Expand Up @@ -482,7 +484,9 @@ public static List<Field> findAnnotatedFields(Class<?> clazz, Class<? extends An
List<Field> fields = findAnnotatedFields(clazz, annotationType, ModifierSupport::isStatic,
HierarchyTraversalMode.TOP_DOWN);

return ReflectionUtils.readFieldValues(fields, null);
@SuppressWarnings("unchecked")
List<@Nullable Object> result = (List<@Nullable Object>) ReflectionUtils.readFieldValues(fields, null);
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ public static <T> Try<Object> tryToReadFieldValue(Class<T> clazz, String fieldNa
* @return an immutable list of the values of the specified fields; never
* {@code null} but may be empty or contain {@code null} entries
*/
public static List<@Nullable Object> readFieldValues(List<Field> fields, @Nullable Object instance) {
public static List<?> readFieldValues(List<Field> fields, @Nullable Object instance) {
return readFieldValues(fields, instance, field -> true);
}

Expand All @@ -674,9 +674,7 @@ public static <T> Try<Object> tryToReadFieldValue(Class<T> clazz, String fieldNa
* @return an immutable list of the values of the specified fields; never
* {@code null} but may be empty or contain {@code null} entries
*/
@SuppressWarnings("NullAway")
public static List<@Nullable Object> readFieldValues(List<Field> fields, @Nullable Object instance,
Predicate<Field> predicate) {
public static List<?> readFieldValues(List<Field> fields, @Nullable Object instance, Predicate<Field> predicate) {
Preconditions.notNull(fields, "fields list must not be null");
Preconditions.notNull(predicate, "Predicate must not be null");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.assertj.core.api.Assertions;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -1952,7 +1953,8 @@ void readFieldValuesFromInstance() {

var values = readFieldValues(fields, new ClassWithFields());

assertThat(values).containsExactly("enigma", 3.14, "text", 2.5, null, 42, "constant", 99);
Assertions.<Object> assertThat(values).containsExactly("enigma", 3.14, "text", 2.5, null, 42, "constant",
99);
}

@Test
Expand All @@ -1961,7 +1963,7 @@ void readFieldValuesFromClass() {

var values = readFieldValues(fields, null);

assertThat(values).containsExactly(2.5, "constant", 99);
Assertions.<Object> assertThat(values).containsExactly(2.5, "constant", 99);
}

/**
Expand All @@ -1972,15 +1974,15 @@ void readFieldValuesFromClass() {
void readFieldValuesFromInterfacesAndClassesInTypeHierarchy() {
var fields = findFields(InterfaceWithField.class, ReflectionUtils::isStatic, TOP_DOWN);
var values = readFieldValues(fields, null);
assertThat(values).containsOnly("ifc");
Assertions.<Object> assertThat(values).containsOnly("ifc");

fields = findFields(SuperclassWithFieldAndFieldFromInterface.class, ReflectionUtils::isStatic, TOP_DOWN);
values = readFieldValues(fields, null);
assertThat(values).containsExactly("ifc", "super");
Assertions.<Object> assertThat(values).containsExactly("ifc", "super");

fields = findFields(SubclassWithFieldAndFieldFromInterface.class, ReflectionUtils::isStatic, TOP_DOWN);
values = readFieldValues(fields, null);
assertThat(values).containsExactly("ifc", "super", "sub");
Assertions.<Object> assertThat(values).containsExactly("ifc", "super", "sub");
}

@Test
Expand All @@ -1989,7 +1991,7 @@ void readFieldValuesFromInstanceWithTypeFilterForString() {

var values = readFieldValues(fields, new ClassWithFields(), isA(String.class));

assertThat(values).containsExactly("enigma", "text", null, "constant");
Assertions.<Object> assertThat(values).containsExactly("enigma", "text", null, "constant");
}

@Test
Expand All @@ -1998,7 +2000,7 @@ void readFieldValuesFromClassWithTypeFilterForString() {

var values = readFieldValues(fields, null, isA(String.class));

assertThat(values).containsExactly("constant");
Assertions.<Object> assertThat(values).containsExactly("constant");
}

@Test
Expand All @@ -2007,7 +2009,7 @@ void readFieldValuesFromInstanceWithTypeFilterForInteger() {

var values = readFieldValues(fields, new ClassWithFields(), isA(int.class));

assertThat(values).containsExactly(42);
Assertions.<Object> assertThat(values).containsExactly(42);
}

@Test
Expand All @@ -2016,7 +2018,7 @@ void readFieldValuesFromClassWithTypeFilterForInteger() {

var values = readFieldValues(fields, null, isA(Integer.class));

assertThat(values).containsExactly(99);
Assertions.<Object> assertThat(values).containsExactly(99);
}

@Test
Expand All @@ -2025,7 +2027,7 @@ void readFieldValuesFromInstanceWithTypeFilterForDouble() {

var values = readFieldValues(fields, new ClassWithFields(), isA(double.class));

assertThat(values).containsExactly(3.14);
Assertions.<Object> assertThat(values).containsExactly(3.14);
}

@Test
Expand All @@ -2034,7 +2036,7 @@ void readFieldValuesFromClassWithTypeFilterForDouble() {

var values = readFieldValues(fields, null, isA(Double.class));

assertThat(values).containsExactly(2.5);
Assertions.<Object> assertThat(values).containsExactly(2.5);
}

private static Predicate<Field> isA(Class<?> type) {
Expand Down