Skip to content

Commit 496a8b2

Browse files
committed
chore: replace List<@nullable Object> with List<?>
The latter is easier to read, and it forbids list.add(...) at the compile-time. See https://jspecify.dev/docs/user-guide/#wildcard-bounds
1 parent 7d02a6a commit 496a8b2

File tree

7 files changed

+24
-23
lines changed

7 files changed

+24
-23
lines changed

junit-jupiter-params/src/main/java/org/junit/jupiter/params/aggregator/ArgumentsAccessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public interface ArgumentsAccessor {
198198
/**
199199
* Get all arguments in this accessor as an immutable list.
200200
*/
201-
List<@Nullable Object> toList();
201+
List<?> toList();
202202

203203
/**
204204
* Get the index of the current test invocation.

junit-jupiter-params/src/main/java/org/junit/jupiter/params/aggregator/DefaultArgumentsAccessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public int size() {
139139
}
140140

141141
@Override
142-
public List<@Nullable Object> toList() {
142+
public List<?> toList() {
143143
return Collections.unmodifiableList(Arrays.asList(this.arguments));
144144
}
145145

junit-platform-commons/src/main/java/org/junit/platform/commons/support/AnnotationSupport.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,7 @@ public static List<Field> findAnnotatedFields(Class<?> clazz, Class<? extends An
443443
* @see ReflectionSupport#tryToReadFieldValue(Field, Object)
444444
*/
445445
@API(status = MAINTAINED, since = "1.4")
446-
public static List<@Nullable Object> findAnnotatedFieldValues(Object instance,
447-
Class<? extends Annotation> annotationType) {
446+
public static List<?> findAnnotatedFieldValues(Object instance, Class<? extends Annotation> annotationType) {
448447
Preconditions.notNull(instance, "instance must not be null");
449448

450449
List<Field> fields = findAnnotatedFields(instance.getClass(), annotationType, ModifierSupport::isNotStatic,
@@ -476,8 +475,7 @@ public static List<Field> findAnnotatedFields(Class<?> clazz, Class<? extends An
476475
* @see ReflectionSupport#tryToReadFieldValue(Field, Object)
477476
*/
478477
@API(status = MAINTAINED, since = "1.4")
479-
public static List<@Nullable Object> findAnnotatedFieldValues(Class<?> clazz,
480-
Class<? extends Annotation> annotationType) {
478+
public static List<?> findAnnotatedFieldValues(Class<?> clazz, Class<? extends Annotation> annotationType) {
481479

482480
List<Field> fields = findAnnotatedFields(clazz, annotationType, ModifierSupport::isStatic,
483481
HierarchyTraversalMode.TOP_DOWN);

junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ public static <T> Try<Object> tryToReadFieldValue(Class<T> clazz, String fieldNa
656656
* @return an immutable list of the values of the specified fields; never
657657
* {@code null} but may be empty or contain {@code null} entries
658658
*/
659-
public static List<@Nullable Object> readFieldValues(List<Field> fields, @Nullable Object instance) {
659+
public static List<?> readFieldValues(List<Field> fields, @Nullable Object instance) {
660660
return readFieldValues(fields, instance, field -> true);
661661
}
662662

@@ -673,8 +673,7 @@ public static <T> Try<Object> tryToReadFieldValue(Class<T> clazz, String fieldNa
673673
* @return an immutable list of the values of the specified fields; never
674674
* {@code null} but may be empty or contain {@code null} entries
675675
*/
676-
public static List<@Nullable Object> readFieldValues(List<Field> fields, @Nullable Object instance,
677-
Predicate<Field> predicate) {
676+
public static List<?> readFieldValues(List<Field> fields, @Nullable Object instance, Predicate<Field> predicate) {
678677
Preconditions.notNull(fields, "fields list must not be null");
679678
Preconditions.notNull(predicate, "Predicate must not be null");
680679

jupiter-tests/src/test/java/org/junit/jupiter/params/aggregator/DefaultArgumentsAccessorTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ void toList() {
157157
assertIterableEquals(Arrays.asList("foo", "bar"), copy);
158158

159159
// Modify local copy:
160-
assertThrows(UnsupportedOperationException.class, () -> copy.set(0, "Boom!"));
160+
assertThrows(UnsupportedOperationException.class, () -> copy.set(0, null));
161+
// Note that copy.set(0, "foo") does not compile
161162
}
162163

163164
@Test

platform-tests/src/test/java/org/junit/platform/commons/support/AnnotationSupportTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.List;
2424
import java.util.Optional;
2525

26+
import org.assertj.core.api.Assertions;
2627
import org.jspecify.annotations.NullUnmarked;
2728
import org.junit.jupiter.api.Tag;
2829
import org.junit.jupiter.api.Test;
@@ -247,15 +248,15 @@ void findAnnotatedFieldValuesForNonStaticFields() {
247248

248249
assertThat(AnnotationSupport.findAnnotatedFieldValues(instance, Tag.class)).isEmpty();
249250

250-
assertThat(AnnotationSupport.findAnnotatedFieldValues(instance, FieldMarker.class))//
251+
Assertions.<Object> assertThat(AnnotationSupport.findAnnotatedFieldValues(instance, FieldMarker.class))//
251252
.containsExactlyInAnyOrder("i1", "i2");
252253
}
253254

254255
@Test
255256
void findAnnotatedFieldValuesForStaticFields() {
256257
assertThat(AnnotationSupport.findAnnotatedFieldValues(Fields.class, Tag.class)).isEmpty();
257258

258-
assertThat(AnnotationSupport.findAnnotatedFieldValues(Fields.class, FieldMarker.class))//
259+
Assertions.<Object> assertThat(AnnotationSupport.findAnnotatedFieldValues(Fields.class, FieldMarker.class))//
259260
.containsExactlyInAnyOrder("s1", "s2");
260261
}
261262

platform-tests/src/test/java/org/junit/platform/commons/util/ReflectionUtilsTests.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import java.util.stream.IntStream;
6161
import java.util.stream.Stream;
6262

63+
import org.assertj.core.api.Assertions;
6364
import org.jspecify.annotations.Nullable;
6465
import org.junit.jupiter.api.Nested;
6566
import org.junit.jupiter.api.Test;
@@ -1953,7 +1954,8 @@ void readFieldValuesFromInstance() {
19531954

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

1956-
assertThat(values).containsExactly("enigma", 3.14, "text", 2.5, null, 42, "constant", 99);
1957+
Assertions.<Object> assertThat(values).containsExactly("enigma", 3.14, "text", 2.5, null, 42, "constant",
1958+
99);
19571959
}
19581960

19591961
@Test
@@ -1962,7 +1964,7 @@ void readFieldValuesFromClass() {
19621964

19631965
var values = readFieldValues(fields, null);
19641966

1965-
assertThat(values).containsExactly(2.5, "constant", 99);
1967+
Assertions.<Object> assertThat(values).containsExactly(2.5, "constant", 99);
19661968
}
19671969

19681970
/**
@@ -1973,15 +1975,15 @@ void readFieldValuesFromClass() {
19731975
void readFieldValuesFromInterfacesAndClassesInTypeHierarchy() {
19741976
var fields = findFields(InterfaceWithField.class, ReflectionUtils::isStatic, TOP_DOWN);
19751977
var values = readFieldValues(fields, null);
1976-
assertThat(values).containsOnly("ifc");
1978+
Assertions.<Object> assertThat(values).containsOnly("ifc");
19771979

19781980
fields = findFields(SuperclassWithFieldAndFieldFromInterface.class, ReflectionUtils::isStatic, TOP_DOWN);
19791981
values = readFieldValues(fields, null);
1980-
assertThat(values).containsExactly("ifc", "super");
1982+
Assertions.<Object> assertThat(values).containsExactly("ifc", "super");
19811983

19821984
fields = findFields(SubclassWithFieldAndFieldFromInterface.class, ReflectionUtils::isStatic, TOP_DOWN);
19831985
values = readFieldValues(fields, null);
1984-
assertThat(values).containsExactly("ifc", "super", "sub");
1986+
Assertions.<Object> assertThat(values).containsExactly("ifc", "super", "sub");
19851987
}
19861988

19871989
@Test
@@ -1990,7 +1992,7 @@ void readFieldValuesFromInstanceWithTypeFilterForString() {
19901992

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

1993-
assertThat(values).containsExactly("enigma", "text", null, "constant");
1995+
Assertions.<Object> assertThat(values).containsExactly("enigma", "text", null, "constant");
19941996
}
19951997

19961998
@Test
@@ -1999,7 +2001,7 @@ void readFieldValuesFromClassWithTypeFilterForString() {
19992001

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

2002-
assertThat(values).containsExactly("constant");
2004+
Assertions.<Object> assertThat(values).containsExactly("constant");
20032005
}
20042006

20052007
@Test
@@ -2008,7 +2010,7 @@ void readFieldValuesFromInstanceWithTypeFilterForInteger() {
20082010

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

2011-
assertThat(values).containsExactly(42);
2013+
Assertions.<Object> assertThat(values).containsExactly(42);
20122014
}
20132015

20142016
@Test
@@ -2017,7 +2019,7 @@ void readFieldValuesFromClassWithTypeFilterForInteger() {
20172019

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

2020-
assertThat(values).containsExactly(99);
2022+
Assertions.<Object> assertThat(values).containsExactly(99);
20212023
}
20222024

20232025
@Test
@@ -2026,7 +2028,7 @@ void readFieldValuesFromInstanceWithTypeFilterForDouble() {
20262028

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

2029-
assertThat(values).containsExactly(3.14);
2031+
Assertions.<Object> assertThat(values).containsExactly(3.14);
20302032
}
20312033

20322034
@Test
@@ -2035,7 +2037,7 @@ void readFieldValuesFromClassWithTypeFilterForDouble() {
20352037

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

2038-
assertThat(values).containsExactly(2.5);
2040+
Assertions.<Object> assertThat(values).containsExactly(2.5);
20392041
}
20402042

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

0 commit comments

Comments
 (0)