Skip to content

Commit 788eff6

Browse files
committed
Fix IDEA-detected nullability problems in main source sets
1 parent a8229d8 commit 788eff6

File tree

10 files changed

+46
-27
lines changed

10 files changed

+46
-27
lines changed

documentation/src/main/java/example/util/StringUtils.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010

1111
package example.util;
1212

13+
import static java.util.Objects.requireNonNull;
14+
15+
import org.jspecify.annotations.Nullable;
16+
1317
public class StringUtils {
1418

15-
public static boolean isPalindrome(String candidate) {
16-
int length = candidate.length();
19+
public static boolean isPalindrome(@Nullable String candidate) {
20+
int length = requireNonNull(candidate).length();
1721
for (int i = 0; i < length / 2; i++) {
1822
if (candidate.charAt(i) != candidate.charAt(length - (i + 1))) {
1923
return false;

junit-jupiter-api/src/main/java/org/junit/jupiter/api/condition/MethodBasedCondition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Method getConditionMethod(String fullyQualifiedMethodName, ExtensionContext cont
6868
String className = methodParts[0];
6969
String methodName = methodParts[1];
7070
ClassLoader classLoader = ClassLoaderUtils.getClassLoader(testClass);
71-
Class<?> clazz = ReflectionSupport.tryToLoadClass(className, classLoader).getOrThrow(
71+
Class<?> clazz = ReflectionSupport.tryToLoadClass(className, classLoader).getNonNullOrThrow(
7272
cause -> new JUnitException("Could not load class [%s]".formatted(className), cause));
7373
return findMethod(clazz, methodName);
7474
}

junit-jupiter-params/src/jmh/java/org/junit/jupiter/params/ParameterizedInvocationNameFormatterBenchmarks.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.junit.jupiter.params;
1212

13+
import static java.util.Objects.requireNonNull;
1314
import static org.junit.jupiter.params.ParameterizedInvocationConstants.DEFAULT_DISPLAY_NAME;
1415
import static org.junit.jupiter.params.ParameterizedInvocationConstants.DISPLAY_NAME_PLACEHOLDER;
1516

@@ -50,7 +51,9 @@ public void formatTestNames(Blackhole blackhole) throws Exception {
5051
var method = TestCase.class.getDeclaredMethod("parameterizedTest", int.class);
5152
var formatter = new ParameterizedInvocationNameFormatter(
5253
DISPLAY_NAME_PLACEHOLDER + " " + DEFAULT_DISPLAY_NAME + " ({0})", "displayName",
53-
new ParameterizedTestContext(TestCase.class, method, method.getAnnotation(ParameterizedTest.class)), 512);
54+
new ParameterizedTestContext(TestCase.class, method,
55+
requireNonNull(method.getAnnotation(ParameterizedTest.class))),
56+
512);
5457
for (int i = 0; i < argumentsList.size(); i++) {
5558
Arguments arguments = argumentsList.get(i);
5659
blackhole.consume(formatter.format(i, EvaluatedArgumentSet.allOf(arguments)));

junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/NullArgumentsProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.util.stream.Stream;
1616

17+
import org.jspecify.annotations.Nullable;
1718
import org.junit.jupiter.api.extension.ExtensionContext;
1819
import org.junit.jupiter.params.support.ParameterDeclarations;
1920
import org.junit.platform.commons.util.Preconditions;
@@ -24,7 +25,7 @@
2425
*/
2526
class NullArgumentsProvider implements ArgumentsProvider {
2627

27-
private static final Arguments nullArguments = arguments(new Object[] { null });
28+
private static final Arguments nullArguments = arguments(new @Nullable Object[] { null });
2829

2930
@Override
3031
public Stream<? extends Arguments> provideArguments(ParameterDeclarations parameters, ExtensionContext context) {

junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/FallbackStringToObjectConverter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class FallbackStringToObjectConverter implements StringToObjectConverter {
6767
* This prevents the framework from repeatedly searching for things which
6868
* are already known not to exist.
6969
*/
70-
private static final ConcurrentHashMap<Class<?>, Function<String, Object>> factoryExecutableCache //
70+
private static final ConcurrentHashMap<Class<?>, Function<String, @Nullable Object>> factoryExecutableCache //
7171
= new ConcurrentHashMap<>(64);
7272

7373
@Override
@@ -76,15 +76,16 @@ public boolean canConvertTo(Class<?> targetType) {
7676
}
7777

7878
@Override
79+
@Nullable
7980
public Object convert(String source, Class<?> targetType) throws Exception {
80-
Function<String, Object> executable = findFactoryExecutable(targetType);
81+
Function<String, @Nullable Object> executable = findFactoryExecutable(targetType);
8182
Preconditions.condition(executable != NULL_EXECUTABLE,
8283
"Illegal state: convert() must not be called if canConvert() returned false");
8384

8485
return executable.apply(source);
8586
}
8687

87-
private static Function<String, Object> findFactoryExecutable(Class<?> targetType) {
88+
private static Function<String, @Nullable Object> findFactoryExecutable(Class<?> targetType) {
8889
return factoryExecutableCache.computeIfAbsent(targetType, type -> {
8990
Method factoryMethod = findFactoryMethod(type);
9091
if (factoryMethod != null) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.junit.platform.commons.util;
1212

13+
import static java.util.Objects.requireNonNull;
1314
import static java.util.function.Predicate.isEqual;
1415
import static java.util.stream.Collectors.toCollection;
1516
import static java.util.stream.Collectors.toSet;
@@ -298,10 +299,10 @@ List<Resource> scan(ModuleReference reference) {
298299

299300
private Resource loadResourceUnchecked(String binaryName) {
300301
try {
301-
URI uri = classLoader.getResource(binaryName).toURI();
302+
URI uri = requireNonNull(classLoader.getResource(binaryName)).toURI();
302303
return new DefaultResource(binaryName, uri);
303304
}
304-
catch (URISyntaxException e) {
305+
catch (NullPointerException | URISyntaxException e) {
305306
throw new JUnitException("Failed to load resource with name '" + binaryName + "'.", e);
306307
}
307308
}

junit-platform-engine/src/main/java/org/junit/platform/engine/discovery/MethodSelector.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,44 +219,50 @@ public Class<?>[] getParameterTypes() {
219219
}
220220

221221
private Class<?> lazyLoadJavaClass() {
222-
// @formatter:off
223-
if (this.javaClass == null) {
222+
Class<?> value = this.javaClass;
223+
if (value == null) {
224+
// @formatter:off
224225
Try<Class<?>> tryToLoadClass = this.classLoader == null
225226
? ReflectionSupport.tryToLoadClass(this.className)
226227
: ReflectionSupport.tryToLoadClass(this.className, this.classLoader);
227-
this.javaClass = tryToLoadClass.getNonNullOrThrow(cause ->
228+
value = tryToLoadClass.getNonNullOrThrow(cause ->
228229
new PreconditionViolationException("Could not load class with name: " + this.className, cause));
230+
// @formatter:on
231+
this.javaClass = value;
229232
}
230-
// @formatter:on
231-
return this.javaClass;
233+
return value;
232234
}
233235

234236
private Method lazyLoadJavaMethod() {
235-
if (this.javaMethod == null) {
237+
var value = this.javaMethod;
238+
if (value == null) {
236239
Class<?> javaClass = lazyLoadJavaClass();
237240
var parameterTypes = lazyLoadParameterTypes();
238241
if (parameterTypes.length > 0) {
239-
this.javaMethod = ReflectionSupport.findMethod(javaClass, this.methodName, parameterTypes).orElseThrow(
242+
value = ReflectionSupport.findMethod(javaClass, this.methodName, parameterTypes).orElseThrow(
240243
() -> new PreconditionViolationException(
241244
"Could not find method with name [%s] and parameter types [%s] in class [%s].".formatted(
242245
this.methodName, this.parameterTypeNames, javaClass.getName())));
243246
}
244247
else {
245-
this.javaMethod = ReflectionSupport.findMethod(javaClass, this.methodName).orElseThrow(
248+
value = ReflectionSupport.findMethod(javaClass, this.methodName).orElseThrow(
246249
() -> new PreconditionViolationException(
247250
"Could not find method with name [%s] in class [%s].".formatted(this.methodName,
248251
javaClass.getName())));
249252
}
253+
this.javaMethod = value;
250254
}
251-
return this.javaMethod;
255+
return value;
252256
}
253257

254258
private Class<?>[] lazyLoadParameterTypes() {
255-
if (this.parameterTypes == null) {
256-
this.parameterTypes = ReflectionUtils.resolveParameterTypes(lazyLoadJavaClass(), this.methodName,
259+
var value = this.parameterTypes;
260+
if (value == null) {
261+
value = ReflectionUtils.resolveParameterTypes(lazyLoadJavaClass(), this.methodName,
257262
this.parameterTypeNames);
263+
this.parameterTypes = value;
258264
}
259-
return this.parameterTypes;
265+
return value;
260266
}
261267

262268
/**

junit-platform-launcher/src/main/java/org/junit/platform/launcher/core/HierarchicalOutputDirectoryProvider.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ public Path createOutputDirectory(TestDescriptor testDescriptor) throws IOExcept
5757

5858
@Override
5959
public synchronized Path getRootDirectory() {
60-
if (rootDir == null) {
61-
rootDir = rootDirSupplier.get();
60+
var currentRootDir = rootDir;
61+
if (currentRootDir == null) {
62+
currentRootDir = rootDirSupplier.get();
63+
rootDir = currentRootDir;
6264
}
63-
return rootDir;
65+
return currentRootDir;
6466
}
6567

6668
private static Path toSanitizedPath(Segment segment) {

junit-vintage-engine/src/main/java/org/junit/vintage/engine/discovery/ClassSelectorResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public Resolution resolve(UniqueIdSelector selector, Context context) {
5656
String testClassName = lastSegment.getValue();
5757
if (classFilter.match(testClassName)) {
5858
Class<?> testClass = ReflectionSupport.tryToLoadClass(testClassName)//
59-
.getOrThrow(cause -> new JUnitException("Unknown class: " + testClassName, cause));
59+
.getNonNullOrThrow(cause -> new JUnitException("Unknown class: " + testClassName, cause));
6060
return resolveTestClassThatPassedNameFilter(testClass, context);
6161
}
6262
}

junit-vintage-engine/src/main/java/org/junit/vintage/engine/execution/RunListenerAdapter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.Optional;
1616
import java.util.function.Function;
1717

18+
import org.jspecify.annotations.Nullable;
1819
import org.junit.Ignore;
1920
import org.junit.platform.engine.EngineExecutionListener;
2021
import org.junit.platform.engine.TestDescriptor;
@@ -192,7 +193,7 @@ private Optional<String> determineReasonForIgnoredTest(TestDescriptor testDescri
192193
.flatMap(testClass -> getReason(testClass.getAnnotation(Ignore.class)));
193194
}
194195

195-
private static Optional<String> getReason(Ignore annotation) {
196+
private static Optional<String> getReason(@Nullable Ignore annotation) {
196197
return Optional.ofNullable(annotation).map(Ignore::value);
197198
}
198199

0 commit comments

Comments
 (0)