Skip to content

Commit 560ed7f

Browse files
authored
JSpecify Null Safety (#18684)
1 parent f355bbb commit 560ed7f

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>jspecify-nullsafety</artifactId>
7+
<packaging>jar</packaging>
8+
<name>core-java-8-datetime-3</name>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>static-analysis-modules</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.jspecify</groupId>
19+
<artifactId>jspecify</artifactId>
20+
<version>0.3.0</version>
21+
</dependency>
22+
</dependencies>
23+
24+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.baeldung.jspecify;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.util.Objects;
8+
import java.util.Optional;
9+
10+
import org.jspecify.annotations.Nullable;
11+
import org.junit.jupiter.api.Test;
12+
13+
public class JspecifyNullSafetyTest {
14+
15+
@Test
16+
void givenKnownUserId_whenFindNickname_thenReturnsOptionalWithValue() {
17+
Optional<String> nickname = findNickname("user123");
18+
19+
assertTrue(nickname.isPresent());
20+
assertEquals("CoolUser", nickname.get());
21+
}
22+
23+
@Test
24+
void givenUnknownUserId_whenFindNickname_thenReturnsEmptyOptional() {
25+
Optional<String> nickname = findNickname("unknownUser");
26+
27+
assertTrue(nickname.isEmpty());
28+
}
29+
30+
@Test
31+
void givenNonNullArgument_whenValidate_thenDoesNotThrowException() {
32+
String result = processNickname("CoolUser");
33+
assertEquals("Processed: CoolUser", result);
34+
}
35+
36+
@Test
37+
void givenNullArgument_whenValidate_thenThrowsNullPointerException() {
38+
assertThrows(NullPointerException.class, () -> processNickname(null));
39+
}
40+
41+
@Test
42+
void givenUnknownUserId_whenFindNicknameOrNull_thenReturnsNull() {
43+
String nickname = findNicknameOrNull("unknownUser");
44+
assertTrue(nickname == null);
45+
}
46+
47+
@Test
48+
void givenNullableMethodResult_whenWrappedInOptional_thenHandledSafely() {
49+
String nickname = findNicknameOrNull("unknownUser");
50+
Optional<String> safeNickname = Optional.ofNullable(nickname);
51+
52+
assertTrue(safeNickname.isEmpty());
53+
}
54+
55+
private Optional<String> findNickname(String userId) {
56+
if ("user123".equals(userId)) {
57+
return Optional.of("CoolUser");
58+
} else {
59+
return Optional.empty();
60+
}
61+
}
62+
63+
@Nullable
64+
private String findNicknameOrNull(String userId) {
65+
if ("user123".equals(userId)) {
66+
return "CoolUser";
67+
} else {
68+
return null;
69+
}
70+
}
71+
72+
private String processNickname(String nickname) {
73+
Objects.requireNonNull(nickname, "Nickname must not be null");
74+
return "Processed: " + nickname;
75+
}
76+
}

static-analysis-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<modules>
1818
<!-- <module>error-prone-library</module> --> <!-- requires additional configuration to be compiled due to the JVM strong encapsulation -->
1919
<module>infer</module>
20+
<module>jspecify-nullsafety</module>
2021
</modules>
2122

2223
<properties>

0 commit comments

Comments
 (0)