Skip to content

Commit fe3316c

Browse files
committed
Annotate nullability in org.junit.platform.engine.discovery
1 parent 69df514 commit fe3316c

File tree

10 files changed

+80
-47
lines changed

10 files changed

+80
-47
lines changed

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

Lines changed: 7 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.platform.commons.PreconditionViolationException;
2223
import org.junit.platform.commons.function.Try;
2324
import org.junit.platform.commons.support.ReflectionSupport;
@@ -47,12 +48,15 @@
4748
@API(status = STABLE, since = "1.0")
4849
public class ClassSelector implements DiscoverySelector {
4950

51+
@Nullable
5052
private final ClassLoader classLoader;
53+
5154
private final String className;
5255

56+
@Nullable
5357
private Class<?> javaClass;
5458

55-
ClassSelector(ClassLoader classLoader, String className) {
59+
ClassSelector(@Nullable ClassLoader classLoader, String className) {
5660
this.className = className;
5761
this.classLoader = classLoader;
5862
}
@@ -70,6 +74,7 @@ public class ClassSelector implements DiscoverySelector {
7074
* @since 1.10
7175
*/
7276
@API(status = EXPERIMENTAL, since = "1.10")
77+
@Nullable
7378
public ClassLoader getClassLoader() {
7479
return this.classLoader;
7580
}
@@ -94,7 +99,7 @@ public Class<?> getJavaClass() {
9499
Try<Class<?>> tryToLoadClass = this.classLoader == null
95100
? ReflectionSupport.tryToLoadClass(this.className)
96101
: ReflectionSupport.tryToLoadClass(this.className, this.classLoader);
97-
this.javaClass = tryToLoadClass.getOrThrow(cause ->
102+
this.javaClass = tryToLoadClass.getNonNullOrThrow(cause ->
98103
new PreconditionViolationException("Could not load class with name: " + this.className, cause));
99104
// @formatter:on
100105
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Set;
2222

2323
import org.apiguardian.api.API;
24+
import org.jspecify.annotations.Nullable;
2425
import org.junit.platform.commons.PreconditionViolationException;
2526
import org.junit.platform.commons.function.Try;
2627
import org.junit.platform.commons.support.Resource;
@@ -55,10 +56,14 @@
5556
public class ClasspathResourceSelector implements DiscoverySelector {
5657

5758
private final String classpathResourceName;
59+
60+
@Nullable
5861
private final FilePosition position;
62+
63+
@Nullable
5964
private Set<Resource> classpathResources;
6065

61-
ClasspathResourceSelector(String classpathResourceName, FilePosition position) {
66+
ClasspathResourceSelector(String classpathResourceName, @Nullable FilePosition position) {
6267
boolean startsWithSlash = classpathResourceName.startsWith("/");
6368
this.classpathResourceName = (startsWithSlash ? classpathResourceName.substring(1) : classpathResourceName);
6469
this.position = position;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ static Optional<? extends DiscoverySelector> parse(String identifier) {
5454

5555
static Optional<? extends DiscoverySelector> parse(DiscoverySelectorIdentifier identifier) {
5656
Preconditions.notNull(identifier, "identifier must not be null");
57-
DiscoverySelectorIdentifierParser parser = Singleton.INSTANCE.parsersByPrefix.get(identifier.getPrefix());
58-
Preconditions.notNull(parser, "No parser for prefix: " + identifier.getPrefix());
57+
DiscoverySelectorIdentifierParser parser = Preconditions.notNull(
58+
Singleton.INSTANCE.parsersByPrefix.get(identifier.getPrefix()),
59+
"No parser for prefix: " + identifier.getPrefix());
5960

6061
return parser.parse(identifier, DiscoverySelectorIdentifierParsers::parse);
6162
}

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.stream.Stream;
3030

3131
import org.apiguardian.api.API;
32+
import org.jspecify.annotations.Nullable;
3233
import org.junit.platform.commons.PreconditionViolationException;
3334
import org.junit.platform.commons.support.ReflectionSupport;
3435
import org.junit.platform.commons.support.Resource;
@@ -153,7 +154,7 @@ public static FileSelector selectFile(File file) {
153154
* @see #selectDirectory(String)
154155
* @see #selectDirectory(File)
155156
*/
156-
public static FileSelector selectFile(String path, FilePosition position) {
157+
public static FileSelector selectFile(String path, @Nullable FilePosition position) {
157158
Preconditions.notBlank(path, "File path must not be null or blank");
158159
return new FileSelector(path, position);
159160
}
@@ -174,7 +175,7 @@ public static FileSelector selectFile(String path, FilePosition position) {
174175
* @see #selectDirectory(String)
175176
* @see #selectDirectory(File)
176177
*/
177-
public static FileSelector selectFile(File file, FilePosition position) {
178+
public static FileSelector selectFile(File file, @Nullable FilePosition position) {
178179
Preconditions.notNull(file, "File must not be null");
179180
Preconditions.condition(file.isFile(),
180181
() -> "The supplied java.io.File [%s] must represent an existing file".formatted(file));
@@ -321,7 +322,7 @@ public static ClasspathResourceSelector selectClasspathResource(String classpath
321322
* @see ClassLoader#getResources(String)
322323
*/
323324
public static ClasspathResourceSelector selectClasspathResource(String classpathResourceName,
324-
FilePosition position) {
325+
@Nullable FilePosition position) {
325326
Preconditions.notBlank(classpathResourceName, "classpath resource name must not be null or blank");
326327
return new ClasspathResourceSelector(classpathResourceName, position);
327328
}
@@ -446,7 +447,7 @@ public static ClassSelector selectClass(String className) {
446447
* @see ClassSelector
447448
*/
448449
@API(status = EXPERIMENTAL, since = "1.10")
449-
public static ClassSelector selectClass(ClassLoader classLoader, String className) {
450+
public static ClassSelector selectClass(@Nullable ClassLoader classLoader, String className) {
450451
Preconditions.notBlank(className, "Class name must not be null or blank");
451452
return new ClassSelector(classLoader, className);
452453
}
@@ -514,7 +515,7 @@ public static MethodSelector selectMethod(String fullyQualifiedMethodName) throw
514515
* @see MethodSelector
515516
*/
516517
@API(status = EXPERIMENTAL, since = "1.10")
517-
public static MethodSelector selectMethod(ClassLoader classLoader, String fullyQualifiedMethodName)
518+
public static MethodSelector selectMethod(@Nullable ClassLoader classLoader, String fullyQualifiedMethodName)
518519
throws PreconditionViolationException {
519520
String[] methodParts = ReflectionUtils.parseFullyQualifiedMethodName(fullyQualifiedMethodName);
520521
return selectMethod(classLoader, methodParts[0], methodParts[1], methodParts[2]);
@@ -546,7 +547,7 @@ public static MethodSelector selectMethod(String className, String methodName) {
546547
* @see MethodSelector
547548
*/
548549
@API(status = EXPERIMENTAL, since = "1.10")
549-
public static MethodSelector selectMethod(ClassLoader classLoader, String className, String methodName) {
550+
public static MethodSelector selectMethod(@Nullable ClassLoader classLoader, String className, String methodName) {
550551
return selectMethod(classLoader, className, methodName, "");
551552
}
552553

@@ -590,7 +591,7 @@ public static MethodSelector selectMethod(String className, String methodName, S
590591
* @see MethodSelector
591592
*/
592593
@API(status = EXPERIMENTAL, since = "1.10")
593-
public static MethodSelector selectMethod(ClassLoader classLoader, String className, String methodName,
594+
public static MethodSelector selectMethod(@Nullable ClassLoader classLoader, String className, String methodName,
594595
String parameterTypeNames) {
595596
Preconditions.notBlank(className, "Class name must not be null or blank");
596597
Preconditions.notBlank(methodName, "Method name must not be null or blank");
@@ -731,8 +732,8 @@ public static NestedClassSelector selectNestedClass(List<String> enclosingClassN
731732
* @see NestedClassSelector
732733
*/
733734
@API(status = EXPERIMENTAL, since = "1.10")
734-
public static NestedClassSelector selectNestedClass(ClassLoader classLoader, List<String> enclosingClassNames,
735-
String nestedClassName) {
735+
public static NestedClassSelector selectNestedClass(@Nullable ClassLoader classLoader,
736+
List<String> enclosingClassNames, String nestedClassName) {
736737
Preconditions.notEmpty(enclosingClassNames, "Enclosing class names must not be null or empty");
737738
Preconditions.notBlank(nestedClassName, "Nested class name must not be null or blank");
738739
return new NestedClassSelector(classLoader, enclosingClassNames, nestedClassName);
@@ -767,8 +768,9 @@ public static NestedMethodSelector selectNestedMethod(List<String> enclosingClas
767768
* @see NestedMethodSelector
768769
*/
769770
@API(status = EXPERIMENTAL, since = "1.10")
770-
public static NestedMethodSelector selectNestedMethod(ClassLoader classLoader, List<String> enclosingClassNames,
771-
String nestedClassName, String methodName) throws PreconditionViolationException {
771+
public static NestedMethodSelector selectNestedMethod(@Nullable ClassLoader classLoader,
772+
List<String> enclosingClassNames, String nestedClassName, String methodName)
773+
throws PreconditionViolationException {
772774
Preconditions.notEmpty(enclosingClassNames, "Enclosing class names must not be null or empty");
773775
Preconditions.notBlank(nestedClassName, "Nested class name must not be null or blank");
774776
Preconditions.notBlank(methodName, "Method name must not be null or blank");
@@ -815,8 +817,8 @@ public static NestedMethodSelector selectNestedMethod(List<String> enclosingClas
815817
* @see #selectNestedMethod(List, String, String, String)
816818
*/
817819
@API(status = EXPERIMENTAL, since = "1.10")
818-
public static NestedMethodSelector selectNestedMethod(ClassLoader classLoader, List<String> enclosingClassNames,
819-
String nestedClassName, String methodName, String parameterTypeNames) {
820+
public static NestedMethodSelector selectNestedMethod(@Nullable ClassLoader classLoader,
821+
List<String> enclosingClassNames, String nestedClassName, String methodName, String parameterTypeNames) {
820822

821823
Preconditions.notEmpty(enclosingClassNames, "Enclosing class names must not be null or empty");
822824
Preconditions.notBlank(nestedClassName, "Nested class name must not be null or blank");

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

Lines changed: 3 additions & 0 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.platform.commons.logging.Logger;
2223
import org.junit.platform.commons.logging.LoggerFactory;
2324
import org.junit.platform.commons.util.Preconditions;
@@ -121,6 +122,8 @@ else if (column == null && "column".equals(key)) {
121122
}
122123

123124
private final int line;
125+
126+
@Nullable
124127
private final Integer column;
125128

126129
private FilePosition(int line) {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Optional;
2222

2323
import org.apiguardian.api.API;
24+
import org.jspecify.annotations.Nullable;
2425
import org.junit.platform.commons.util.StringUtils;
2526
import org.junit.platform.commons.util.ToStringBuilder;
2627
import org.junit.platform.engine.DiscoverySelector;
@@ -44,9 +45,11 @@
4445
public class FileSelector implements DiscoverySelector {
4546

4647
private final String path;
48+
49+
@Nullable
4750
private final FilePosition position;
4851

49-
FileSelector(String path, FilePosition position) {
52+
FileSelector(String path, @Nullable FilePosition position) {
5053
this.path = path;
5154
this.position = position;
5255
}

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

Lines changed: 30 additions & 25 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.platform.commons.JUnitException;
2324
import org.junit.platform.commons.PreconditionViolationException;
2425
import org.junit.platform.commons.function.Try;
@@ -59,19 +60,24 @@
5960
@API(status = STABLE, since = "1.0")
6061
public class MethodSelector implements DiscoverySelector {
6162

63+
@Nullable
6264
private final ClassLoader classLoader;
6365
private final String className;
6466
private final String methodName;
6567
private final String parameterTypeNames;
6668

69+
@Nullable
6770
private volatile Class<?> javaClass;
71+
72+
@Nullable
6873
private volatile Method javaMethod;
69-
private volatile Class<?>[] parameterTypes;
74+
75+
private volatile Class<?> @Nullable [] parameterTypes;
7076

7177
/**
7278
* @since 1.10
7379
*/
74-
MethodSelector(ClassLoader classLoader, String className, String methodName, String parameterTypeNames) {
80+
MethodSelector(@Nullable ClassLoader classLoader, String className, String methodName, String parameterTypeNames) {
7581
this.classLoader = classLoader;
7682
this.className = className;
7783
this.methodName = methodName;
@@ -89,7 +95,7 @@ public class MethodSelector implements DiscoverySelector {
8995
/**
9096
* @since 1.10
9197
*/
92-
MethodSelector(ClassLoader classLoader, String className, String methodName, Class<?>... parameterTypes) {
98+
MethodSelector(@Nullable ClassLoader classLoader, String className, String methodName, Class<?>... parameterTypes) {
9399
this.classLoader = classLoader;
94100
this.className = className;
95101
this.methodName = methodName;
@@ -125,6 +131,7 @@ public class MethodSelector implements DiscoverySelector {
125131
* @return the {@code ClassLoader}; potentially {@code null}
126132
* @since 1.10
127133
*/
134+
@Nullable
128135
@API(status = EXPERIMENTAL, since = "1.10")
129136
public ClassLoader getClassLoader() {
130137
return this.classLoader;
@@ -176,8 +183,7 @@ public String getParameterTypeNames() {
176183
* @see #getJavaMethod()
177184
*/
178185
public Class<?> getJavaClass() {
179-
lazyLoadJavaClass();
180-
return this.javaClass;
186+
return lazyLoadJavaClass();
181187
}
182188

183189
/**
@@ -190,8 +196,7 @@ public Class<?> getJavaClass() {
190196
* @see #getJavaClass()
191197
*/
192198
public Method getJavaMethod() {
193-
lazyLoadJavaMethod();
194-
return this.javaMethod;
199+
return lazyLoadJavaMethod();
195200
}
196201

197202
/**
@@ -211,48 +216,48 @@ public Method getJavaMethod() {
211216
*/
212217
@API(status = EXPERIMENTAL, since = "1.10")
213218
public Class<?>[] getParameterTypes() {
214-
lazyLoadParameterTypes();
215-
return this.parameterTypes.clone();
219+
return lazyLoadParameterTypes().clone();
216220
}
217221

218-
private void lazyLoadJavaClass() {
222+
private Class<?> lazyLoadJavaClass() {
219223
// @formatter:off
220224
if (this.javaClass == null) {
221225
Try<Class<?>> tryToLoadClass = this.classLoader == null
222226
? ReflectionSupport.tryToLoadClass(this.className)
223227
: ReflectionSupport.tryToLoadClass(this.className, this.classLoader);
224-
this.javaClass = tryToLoadClass.getOrThrow(cause ->
228+
this.javaClass = tryToLoadClass.getNonNullOrThrow(cause ->
225229
new PreconditionViolationException("Could not load class with name: " + this.className, cause));
226230
}
227231
// @formatter:on
232+
return this.javaClass;
228233
}
229234

230-
private void lazyLoadJavaMethod() {
235+
private Method lazyLoadJavaMethod() {
231236
if (this.javaMethod == null) {
232-
lazyLoadJavaClass();
233-
lazyLoadParameterTypes();
234-
if (this.parameterTypes.length > 0) {
235-
this.javaMethod = ReflectionSupport.findMethod(this.javaClass, this.methodName,
236-
this.parameterTypes).orElseThrow(
237-
() -> new PreconditionViolationException(
238-
"Could not find method with name [%s] and parameter types [%s] in class [%s].".formatted(
239-
this.methodName, this.parameterTypeNames, this.javaClass.getName())));
237+
Class<?> javaClass = lazyLoadJavaClass();
238+
var parameterTypes = lazyLoadParameterTypes();
239+
if (parameterTypes.length > 0) {
240+
this.javaMethod = ReflectionSupport.findMethod(javaClass, this.methodName, parameterTypes).orElseThrow(
241+
() -> new PreconditionViolationException(
242+
"Could not find method with name [%s] and parameter types [%s] in class [%s].".formatted(
243+
this.methodName, this.parameterTypeNames, javaClass.getName())));
240244
}
241245
else {
242-
this.javaMethod = ReflectionSupport.findMethod(this.javaClass, this.methodName).orElseThrow(
246+
this.javaMethod = ReflectionSupport.findMethod(javaClass, this.methodName).orElseThrow(
243247
() -> new PreconditionViolationException(
244248
"Could not find method with name [%s] in class [%s].".formatted(this.methodName,
245-
this.javaClass.getName())));
249+
javaClass.getName())));
246250
}
247251
}
252+
return this.javaMethod;
248253
}
249254

250-
private void lazyLoadParameterTypes() {
255+
private Class<?>[] lazyLoadParameterTypes() {
251256
if (this.parameterTypes == null) {
252-
lazyLoadJavaClass();
253-
this.parameterTypes = ReflectionUtils.resolveParameterTypes(this.javaClass, this.methodName,
257+
this.parameterTypes = ReflectionUtils.resolveParameterTypes(lazyLoadJavaClass(), this.methodName,
254258
this.parameterTypeNames);
255259
}
260+
return this.parameterTypes;
256261
}
257262

258263
/**

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.stream.Stream;
2525

2626
import org.apiguardian.api.API;
27+
import org.jspecify.annotations.Nullable;
2728
import org.junit.platform.commons.PreconditionViolationException;
2829
import org.junit.platform.commons.util.ToStringBuilder;
2930
import org.junit.platform.engine.DiscoverySelector;
@@ -54,11 +55,13 @@
5455
@API(status = STABLE, since = "1.6")
5556
public class NestedClassSelector implements DiscoverySelector {
5657

58+
@Nullable
5759
private final ClassLoader classLoader;
60+
5861
private final List<ClassSelector> enclosingClassSelectors;
5962
private final ClassSelector nestedClassSelector;
6063

61-
NestedClassSelector(ClassLoader classLoader, List<String> enclosingClassNames, String nestedClassName) {
64+
NestedClassSelector(@Nullable ClassLoader classLoader, List<String> enclosingClassNames, String nestedClassName) {
6265
this.classLoader = classLoader;
6366
this.enclosingClassSelectors = enclosingClassNames.stream() //
6467
.map(className -> new ClassSelector(classLoader, className)) //
@@ -78,6 +81,7 @@ public class NestedClassSelector implements DiscoverySelector {
7881
* @return the {@code ClassLoader}; potentially {@code null}
7982
* @since 1.10
8083
*/
84+
@Nullable
8185
@API(status = EXPERIMENTAL, since = "1.10")
8286
public ClassLoader getClassLoader() {
8387
return this.classLoader;

0 commit comments

Comments
 (0)