Skip to content

Commit 08257cb

Browse files
committed
Enable NullAway in junit-jupiter-migrationsupport
1 parent 9c4b088 commit 08257cb

File tree

12 files changed

+33
-8
lines changed

12 files changed

+33
-8
lines changed

junit-jupiter-migrationsupport/junit-jupiter-migrationsupport.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
plugins {
22
id("junitbuild.java-library-conventions")
3+
id("junitbuild.java-nullability-conventions")
34
id("junitbuild.junit4-compatibility")
45
}
56

@@ -11,6 +12,7 @@ dependencies {
1112
api(projects.junitJupiterApi)
1213

1314
compileOnlyApi(libs.apiguardian)
15+
compileOnly(libs.jspecify)
1416

1517
osgiVerification(projects.junitJupiterEngine)
1618
osgiVerification(projects.junitPlatformLauncher)
@@ -23,10 +25,12 @@ tasks {
2325
jar {
2426
bundle {
2527
val importAPIGuardian: String by extra
28+
val importJSpecify: String by extra
2629
bnd("""
2730
# Import JUnit4 packages with a version
2831
Import-Package: \
2932
$importAPIGuardian,\
33+
$importJSpecify,\
3034
org.junit;version="[${libs.versions.junit4Min.get()},5)",\
3135
org.junit.platform.commons.logging;status=INTERNAL,\
3236
org.junit.rules;version="[${libs.versions.junit4Min.get()},5)",\

junit-jupiter-migrationsupport/src/main/java/org/junit/jupiter/migrationsupport/conditions/IgnoreCondition.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.lang.reflect.AnnotatedElement;
1717

1818
import org.apiguardian.api.API;
19+
import org.jspecify.annotations.Nullable;
1920
import org.junit.Ignore;
2021
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
2122
import org.junit.jupiter.api.extension.ExecutionCondition;
@@ -53,7 +54,7 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
5354
.orElse(ENABLED);
5455
}
5556

56-
private ConditionEvaluationResult toResult(AnnotatedElement element, Ignore annotation) {
57+
private ConditionEvaluationResult toResult(@Nullable AnnotatedElement element, Ignore annotation) {
5758
String value = annotation.value();
5859
String reason = StringUtils.isNotBlank(value) ? value : element + " is disabled via @org.junit.Ignore";
5960
return ConditionEvaluationResult.disabled(reason);

junit-jupiter-migrationsupport/src/main/java/org/junit/jupiter/migrationsupport/conditions/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
* Jupiter.
55
*/
66

7+
@NullMarked
78
package org.junit.jupiter.migrationsupport.conditions;
9+
10+
import org.jspecify.annotations.NullMarked;

junit-jupiter-migrationsupport/src/main/java/org/junit/jupiter/migrationsupport/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
* Support for migrating from JUnit 4 to JUnit Jupiter.
33
*/
44

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

junit-jupiter-migrationsupport/src/main/java/org/junit/jupiter/migrationsupport/rules/ExpectedExceptionSupport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public void handleTestExecutionException(ExtensionContext context, Throwable thr
5757

5858
@Override
5959
public void afterEach(ExtensionContext context) throws Exception {
60-
boolean handled = getStore(context).getOrComputeIfAbsent(EXCEPTION_WAS_HANDLED, key -> FALSE, Boolean.class);
61-
if (!handled) {
60+
Boolean handled = getStore(context).getOrComputeIfAbsent(EXCEPTION_WAS_HANDLED, key -> FALSE, Boolean.class);
61+
if (handled != null && !handled) {
6262
this.support.afterEach(context);
6363
}
6464
}

junit-jupiter-migrationsupport/src/main/java/org/junit/jupiter/migrationsupport/rules/TestRuleSupport.java

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

1313
import static java.util.Collections.unmodifiableList;
14+
import static java.util.Objects.requireNonNull;
1415
import static org.junit.platform.commons.support.AnnotationSupport.findPublicAnnotatedFields;
1516
import static org.junit.platform.commons.support.AnnotationSupport.isAnnotated;
1617
import static org.junit.platform.commons.support.HierarchyTraversalMode.TOP_DOWN;
@@ -146,8 +147,8 @@ private List<TestRuleAnnotatedMember> getRuleAnnotatedMembers(ExtensionContext c
146147
Object testInstance = context.getRequiredTestInstance();
147148
Namespace namespace = Namespace.create(TestRuleSupport.class, context.getRequiredTestClass());
148149
// @formatter:off
149-
return new ArrayList<>(context.getStore(namespace)
150-
.getOrComputeIfAbsent("rule-annotated-members", key -> findRuleAnnotatedMembers(testInstance), List.class));
150+
return new ArrayList<TestRuleAnnotatedMember>(requireNonNull(context.getStore(namespace)
151+
.getOrComputeIfAbsent("rule-annotated-members", key -> findRuleAnnotatedMembers(testInstance), List.class)));
151152
// @formatter:on
152153
}
153154

junit-jupiter-migrationsupport/src/main/java/org/junit/jupiter/migrationsupport/rules/adapter/AbstractTestRuleAdapter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.lang.reflect.Method;
1818

1919
import org.apiguardian.api.API;
20+
import org.jspecify.annotations.Nullable;
2021
import org.junit.jupiter.migrationsupport.rules.member.TestRuleAnnotatedMember;
2122
import org.junit.platform.commons.JUnitException;
2223
import org.junit.platform.commons.util.ClassUtils;
@@ -37,10 +38,12 @@ public AbstractTestRuleAdapter(TestRuleAnnotatedMember annotatedMember, Class<?
3738
() -> adapteeClass + " is not assignable from " + this.target.getClass());
3839
}
3940

41+
@Nullable
4042
protected Object executeMethod(String name) {
4143
return executeMethod(name, new Class<?>[0]);
4244
}
4345

46+
@Nullable
4447
protected Object executeMethod(String methodName, Class<?>[] parameterTypes, Object... arguments) {
4548
Method method = findMethod(this.target.getClass(), methodName, parameterTypes).orElseThrow(
4649
() -> new JUnitException("Failed to find method %s(%s) in class %s".formatted(methodName,

junit-jupiter-migrationsupport/src/main/java/org/junit/jupiter/migrationsupport/rules/adapter/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
* Simple wrappers for JUnit 4 rules to overcome visibility limitations of the JUnit 4 implementations.
33
*/
44

5+
@NullMarked
56
package org.junit.jupiter.migrationsupport.rules.adapter;
7+
8+
import org.jspecify.annotations.NullMarked;

junit-jupiter-migrationsupport/src/main/java/org/junit/jupiter/migrationsupport/rules/member/AbstractTestRuleAnnotatedMember.java

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

1111
package org.junit.jupiter.migrationsupport.rules.member;
1212

13+
import org.jspecify.annotations.Nullable;
14+
import org.junit.platform.commons.util.Preconditions;
1315
import org.junit.rules.TestRule;
1416

1517
/**
@@ -19,8 +21,8 @@ abstract class AbstractTestRuleAnnotatedMember implements TestRuleAnnotatedMembe
1921

2022
private final TestRule testRule;
2123

22-
AbstractTestRuleAnnotatedMember(TestRule testRule) {
23-
this.testRule = testRule;
24+
AbstractTestRuleAnnotatedMember(@Nullable TestRule testRule) {
25+
this.testRule = Preconditions.notNull(testRule, "TestRule must not be null");
2426
}
2527

2628
@Override

junit-jupiter-migrationsupport/src/main/java/org/junit/jupiter/migrationsupport/rules/member/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
* Abstractions for members which can be targets of JUnit 4 rule annotations.
33
*/
44

5+
@NullMarked
56
package org.junit.jupiter.migrationsupport.rules.member;
7+
8+
import org.jspecify.annotations.NullMarked;

0 commit comments

Comments
 (0)