Skip to content

Commit 9c4b088

Browse files
committed
Annotate nullability in remaining org.junit.jupiter.engine.. packages
1 parent 3b9f4f6 commit 9c4b088

27 files changed

+133
-58
lines changed

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/discovery/AbstractOrderingVisitor.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
package org.junit.jupiter.engine.discovery;
1212

1313
import static java.util.Comparator.comparing;
14+
import static java.util.Objects.requireNonNull;
1415
import static java.util.stream.Collectors.toCollection;
1516
import static java.util.stream.Collectors.toList;
1617

@@ -23,6 +24,7 @@
2324
import java.util.function.Function;
2425
import java.util.stream.Stream;
2526

27+
import org.jspecify.annotations.Nullable;
2628
import org.junit.platform.commons.util.UnrecoverableExceptions;
2729
import org.junit.platform.engine.DiscoveryIssue;
2830
import org.junit.platform.engine.DiscoveryIssue.Severity;
@@ -137,12 +139,15 @@ protected static <ORDERER, WRAPPER> DescriptorWrapperOrderer<ORDERER, WRAPPER> n
137139
return (DescriptorWrapperOrderer<ORDERER, WRAPPER>) NOOP;
138140
}
139141

142+
@Nullable
140143
private final ORDERER orderer;
144+
@Nullable
141145
private final Consumer<List<WRAPPER>> orderingAction;
146+
142147
private final MessageGenerator descriptorsAddedMessageGenerator;
143148
private final MessageGenerator descriptorsRemovedMessageGenerator;
144149

145-
DescriptorWrapperOrderer(ORDERER orderer, Consumer<List<WRAPPER>> orderingAction,
150+
DescriptorWrapperOrderer(@Nullable ORDERER orderer, @Nullable Consumer<List<WRAPPER>> orderingAction,
146151
MessageGenerator descriptorsAddedMessageGenerator,
147152
MessageGenerator descriptorsRemovedMessageGenerator) {
148153

@@ -152,6 +157,7 @@ protected static <ORDERER, WRAPPER> DescriptorWrapperOrderer<ORDERER, WRAPPER> n
152157
this.descriptorsRemovedMessageGenerator = descriptorsRemovedMessageGenerator;
153158
}
154159

160+
@Nullable
155161
ORDERER getOrderer() {
156162
return orderer;
157163
}
@@ -162,7 +168,7 @@ private boolean canOrderWrappers() {
162168

163169
private void orderWrappers(List<WRAPPER> wrappers, Consumer<String> errorHandler) {
164170
List<WRAPPER> orderedWrappers = new ArrayList<>(wrappers);
165-
this.orderingAction.accept(orderedWrappers);
171+
requireNonNull(this.orderingAction).accept(orderedWrappers);
166172
Map<Object, Integer> distinctWrappersToIndex = distinctWrappersToIndex(orderedWrappers);
167173

168174
int difference = orderedWrappers.size() - wrappers.size();

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/discovery/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
* Contains resolvers for Java elements.
44
*/
55

6+
@NullMarked
67
package org.junit.jupiter.engine.discovery;
8+
9+
import org.jspecify.annotations.NullMarked;

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/discovery/predicates/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
* Internal predicate classes used by test discovery within the JUnit Jupiter test engine.
33
*/
44

5+
@NullMarked
56
package org.junit.jupiter.engine.discovery.predicates;
7+
8+
import org.jspecify.annotations.NullMarked;

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/ConstructorInvocation.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@
1717
import java.util.List;
1818
import java.util.Optional;
1919

20+
import org.jspecify.annotations.Nullable;
2021
import org.junit.jupiter.api.extension.InvocationInterceptor.Invocation;
2122
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
2223
import org.junit.platform.commons.util.ReflectionUtils;
2324

2425
class ConstructorInvocation<T> implements Invocation<T>, ReflectiveInvocationContext<Constructor<T>> {
2526

2627
private final Constructor<T> constructor;
27-
private final Object[] arguments;
28+
private final @Nullable Object[] arguments;
2829

29-
ConstructorInvocation(Constructor<T> constructor, Object[] arguments) {
30+
ConstructorInvocation(Constructor<T> constructor, @Nullable Object[] arguments) {
3031
this.constructor = constructor;
3132
this.arguments = arguments;
3233
}

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/DefaultExecutableInvoker.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Optional;
1919

2020
import org.apiguardian.api.API;
21+
import org.jspecify.annotations.Nullable;
2122
import org.junit.jupiter.api.extension.ExecutableInvoker;
2223
import org.junit.jupiter.api.extension.ExtensionContext;
2324
import org.junit.jupiter.engine.extension.ExtensionRegistry;
@@ -39,14 +40,17 @@ public DefaultExecutableInvoker(ExtensionContext extensionContext, ExtensionRegi
3940
}
4041

4142
@Override
42-
public <T> T invoke(Constructor<T> constructor, Object outerInstance) {
43+
public <T> T invoke(Constructor<T> constructor, @Nullable Object outerInstance) {
44+
@Nullable
4345
Object[] arguments = resolveParameters(constructor, Optional.empty(), Optional.ofNullable(outerInstance),
4446
extensionContext, extensionRegistry);
4547
return ReflectionUtils.newInstance(constructor, arguments);
4648
}
4749

4850
@Override
49-
public Object invoke(Method method, Object target) {
51+
@Nullable
52+
public Object invoke(Method method, @Nullable Object target) {
53+
@Nullable
5054
Object[] arguments = resolveParameters(method, Optional.ofNullable(target), extensionContext,
5155
extensionRegistry);
5256
return MethodReflectionUtils.invoke(method, target, arguments);

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/InterceptingExecutableInvoker.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Optional;
2020

2121
import org.apiguardian.api.API;
22+
import org.jspecify.annotations.Nullable;
2223
import org.junit.jupiter.api.extension.ExtensionContext;
2324
import org.junit.jupiter.api.extension.InvocationInterceptor;
2425
import org.junit.jupiter.api.extension.InvocationInterceptor.Invocation;
@@ -57,6 +58,7 @@ public <T> T invoke(Constructor<T> constructor, Optional<Object> outerInstance,
5758
ExtensionContextSupplier extensionContext, ExtensionRegistry extensionRegistry,
5859
ReflectiveInterceptorCall<Constructor<T>, T> interceptorCall) {
5960

61+
@Nullable
6062
Object[] arguments = resolveParameters(constructor, Optional.empty(), outerInstance, extensionContext,
6163
extensionRegistry);
6264
ConstructorInvocation<T> invocation = new ConstructorInvocation<>(constructor, arguments);
@@ -76,12 +78,13 @@ public <T> T invoke(Constructor<T> constructor, Optional<Object> outerInstance,
7678
* @param interceptorCall the call for intercepting this method invocation
7779
* via all registered {@linkplain InvocationInterceptor interceptors}
7880
*/
79-
public <T> T invoke(Method method, Object target, ExtensionContext extensionContext,
81+
public <T> T invoke(Method method, @Nullable Object target, ExtensionContext extensionContext,
8082
ExtensionRegistry extensionRegistry, ReflectiveInterceptorCall<Method, T> interceptorCall) {
8183

8284
@SuppressWarnings("unchecked")
8385
Optional<Object> optionalTarget = (target instanceof Optional ? (Optional<Object>) target
8486
: Optional.ofNullable(target));
87+
@Nullable
8588
Object[] arguments = resolveParameters(method, optionalTarget, extensionContext, extensionRegistry);
8689
MethodInvocation<T> invocation = new MethodInvocation<>(method, optionalTarget, arguments);
8790
return invoke(invocation, invocation, extensionContext, extensionRegistry, interceptorCall);
@@ -102,12 +105,13 @@ private <E extends Executable, T> T invoke(Invocation<T> originalInvocation,
102105
extensionContext.get(interceptor)));
103106
}
104107

105-
public interface ReflectiveInterceptorCall<E extends Executable, T> {
108+
public interface ReflectiveInterceptorCall<E extends Executable, T extends @Nullable Object> {
106109

107110
T apply(InvocationInterceptor interceptor, Invocation<T> invocation,
108111
ReflectiveInvocationContext<E> invocationContext, ExtensionContext extensionContext) throws Throwable;
109112

110-
static ReflectiveInterceptorCall<Method, Void> ofVoidMethod(VoidMethodInterceptorCall call) {
113+
@SuppressWarnings("NullAway")
114+
static ReflectiveInterceptorCall<Method, @Nullable Void> ofVoidMethod(VoidMethodInterceptorCall call) {
111115
return ((interceptorChain, invocation, invocationContext, extensionContext) -> {
112116
call.apply(interceptorChain, invocation, invocationContext, extensionContext);
113117
return null;

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/InvocationInterceptorChain.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.concurrent.atomic.AtomicBoolean;
1919

2020
import org.apiguardian.api.API;
21+
import org.jspecify.annotations.Nullable;
2122
import org.junit.jupiter.api.extension.InvocationInterceptor;
2223
import org.junit.jupiter.api.extension.InvocationInterceptor.Invocation;
2324
import org.junit.jupiter.engine.extension.ExtensionRegistry;
@@ -69,11 +70,12 @@ private <T> T proceed(Invocation<T> invocation) {
6970
}
7071

7172
@FunctionalInterface
72-
public interface InterceptorCall<T> {
73+
public interface InterceptorCall<T extends @Nullable Object> {
7374

7475
T apply(InvocationInterceptor interceptor, Invocation<T> invocation) throws Throwable;
7576

76-
static InterceptorCall<Void> ofVoid(VoidInterceptorCall call) {
77+
@SuppressWarnings("NullAway")
78+
static InterceptorCall<@Nullable Void> ofVoid(VoidInterceptorCall call) {
7779
return ((interceptorChain, invocation) -> {
7880
call.apply(interceptorChain, invocation);
7981
return null;
@@ -85,7 +87,7 @@ static InterceptorCall<Void> ofVoid(VoidInterceptorCall call) {
8587
@FunctionalInterface
8688
public interface VoidInterceptorCall {
8789

88-
void apply(InvocationInterceptor interceptor, Invocation<Void> invocation) throws Throwable;
90+
void apply(InvocationInterceptor interceptor, Invocation<@Nullable Void> invocation) throws Throwable;
8991

9092
}
9193

@@ -112,7 +114,7 @@ public void skip() {
112114
}
113115
}
114116

115-
private static class ValidatingInvocation<T> implements Invocation<T> {
117+
private static class ValidatingInvocation<T extends @Nullable Object> implements Invocation<T> {
116118

117119
private static final Logger logger = LoggerFactory.getLogger(ValidatingInvocation.class);
118120

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/JupiterEngineExecutionContext.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010

1111
package org.junit.jupiter.engine.execution;
1212

13+
import static java.util.Objects.requireNonNull;
1314
import static org.apiguardian.api.API.Status.INTERNAL;
1415

1516
import org.apiguardian.api.API;
17+
import org.jspecify.annotations.Nullable;
1618
import org.junit.jupiter.api.extension.ExtensionContext;
1719
import org.junit.jupiter.engine.config.JupiterConfiguration;
1820
import org.junit.jupiter.engine.descriptor.LauncherStoreFacade;
@@ -68,19 +70,19 @@ public LauncherStoreFacade getLauncherStoreFacade() {
6870
}
6971

7072
public TestInstancesProvider getTestInstancesProvider() {
71-
return this.state.testInstancesProvider;
73+
return requireNonNull(this.state.testInstancesProvider);
7274
}
7375

7476
public MutableExtensionRegistry getExtensionRegistry() {
75-
return this.state.extensionRegistry;
77+
return requireNonNull(this.state.extensionRegistry);
7678
}
7779

7880
public ExtensionContext getExtensionContext() {
79-
return this.state.extensionContext;
81+
return requireNonNull(this.state.extensionContext);
8082
}
8183

8284
public ThrowableCollector getThrowableCollector() {
83-
return this.state.throwableCollector;
85+
return requireNonNull(this.state.throwableCollector);
8486
}
8587

8688
/**
@@ -125,9 +127,17 @@ private static final class State implements Cloneable {
125127
final EngineExecutionListener executionListener;
126128
final JupiterConfiguration configuration;
127129
final LauncherStoreFacade launcherStoreFacade;
130+
131+
@Nullable
128132
TestInstancesProvider testInstancesProvider;
133+
134+
@Nullable
129135
MutableExtensionRegistry extensionRegistry;
136+
137+
@Nullable
130138
ExtensionContext extensionContext;
139+
140+
@Nullable
131141
ThrowableCollector throwableCollector;
132142

133143
State(EngineExecutionListener executionListener, JupiterConfiguration configuration,
@@ -152,6 +162,8 @@ public State clone() {
152162
public static class Builder {
153163

154164
private State originalState;
165+
166+
@Nullable
155167
private State newState = null;
156168

157169
private Builder(State originalState) {

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/MethodInvocation.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@
1717
import java.util.List;
1818
import java.util.Optional;
1919

20+
import org.jspecify.annotations.Nullable;
2021
import org.junit.jupiter.api.extension.InvocationInterceptor.Invocation;
2122
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
2223
import org.junit.jupiter.engine.support.MethodReflectionUtils;
2324

24-
class MethodInvocation<T> implements Invocation<T>, ReflectiveInvocationContext<Method> {
25+
class MethodInvocation<T extends @Nullable Object> implements Invocation<T>, ReflectiveInvocationContext<Method> {
2526

2627
private final Method method;
2728
private final Optional<Object> target;
28-
private final Object[] arguments;
29+
private final @Nullable Object[] arguments;
2930

30-
MethodInvocation(Method method, Optional<Object> target, Object[] arguments) {
31+
MethodInvocation(Method method, Optional<Object> target, @Nullable Object[] arguments) {
3132
this.method = method;
3233
this.target = target;
3334
this.arguments = arguments;
@@ -39,7 +40,6 @@ public Class<?> getTargetClass() {
3940
}
4041

4142
@Override
42-
@SuppressWarnings("unchecked")
4343
public Optional<Object> getTarget() {
4444
return this.target;
4545
}
@@ -55,7 +55,7 @@ public List<Object> getArguments() {
5555
}
5656

5757
@Override
58-
@SuppressWarnings("unchecked")
58+
@SuppressWarnings({ "unchecked", "NullAway" })
5959
public T proceed() {
6060
var actualTarget = this.target.orElse(null);
6161
return (T) MethodReflectionUtils.invoke(this.method, actualTarget, this.arguments);

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/NamespaceAwareStore.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.function.Supplier;
1717

1818
import org.apiguardian.api.API;
19+
import org.jspecify.annotations.Nullable;
1920
import org.junit.jupiter.api.extension.ExtensionContext.Store;
2021
import org.junit.jupiter.api.extension.ExtensionContextException;
2122
import org.junit.platform.commons.util.Preconditions;
@@ -38,26 +39,30 @@ public NamespaceAwareStore(NamespacedHierarchicalStore<Namespace> valuesStore, N
3839
}
3940

4041
@Override
42+
@Nullable
4143
public Object get(Object key) {
4244
Preconditions.notNull(key, "key must not be null");
4345
return accessStore(() -> this.valuesStore.get(this.namespace, key));
4446
}
4547

4648
@Override
49+
@Nullable
4750
public <T> T get(Object key, Class<T> requiredType) {
4851
Preconditions.notNull(key, "key must not be null");
4952
Preconditions.notNull(requiredType, "requiredType must not be null");
5053
return accessStore(() -> this.valuesStore.get(this.namespace, key, requiredType));
5154
}
5255

5356
@Override
57+
@Nullable
5458
public <K, V> Object getOrComputeIfAbsent(K key, Function<K, V> defaultCreator) {
5559
Preconditions.notNull(key, "key must not be null");
5660
Preconditions.notNull(defaultCreator, "defaultCreator function must not be null");
5761
return accessStore(() -> this.valuesStore.getOrComputeIfAbsent(this.namespace, key, defaultCreator));
5862
}
5963

6064
@Override
65+
@Nullable
6166
public <K, V> V getOrComputeIfAbsent(K key, Function<K, V> defaultCreator, Class<V> requiredType) {
6267
Preconditions.notNull(key, "key must not be null");
6368
Preconditions.notNull(defaultCreator, "defaultCreator function must not be null");
@@ -67,25 +72,28 @@ public <K, V> V getOrComputeIfAbsent(K key, Function<K, V> defaultCreator, Class
6772
}
6873

6974
@Override
70-
public void put(Object key, Object value) {
75+
public void put(Object key, @Nullable Object value) {
7176
Preconditions.notNull(key, "key must not be null");
7277
accessStore(() -> this.valuesStore.put(this.namespace, key, value));
7378
}
7479

7580
@Override
81+
@Nullable
7682
public Object remove(Object key) {
7783
Preconditions.notNull(key, "key must not be null");
7884
return accessStore(() -> this.valuesStore.remove(this.namespace, key));
7985
}
8086

8187
@Override
88+
@Nullable
8289
public <T> T remove(Object key, Class<T> requiredType) {
8390
Preconditions.notNull(key, "key must not be null");
8491
Preconditions.notNull(requiredType, "requiredType must not be null");
8592
return accessStore(() -> this.valuesStore.remove(this.namespace, key, requiredType));
8693
}
8794

88-
private <T> T accessStore(Supplier<T> action) {
95+
@Nullable
96+
private <T> T accessStore(Supplier<@Nullable T> action) {
8997
try {
9098
return action.get();
9199
}

0 commit comments

Comments
 (0)