这是indexloc提供的服务,不要输入任何密码
Skip to content

JSpecify Null Safety #18684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions static-analysis-modules/jspecify-nullsafety/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>jspecify-nullsafety</artifactId>
<packaging>jar</packaging>
<name>core-java-8-datetime-3</name>

<parent>
<groupId>com.baeldung</groupId>
<artifactId>static-analysis-modules</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
<version>0.3.0</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.baeldung.jspecify;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Objects;
import java.util.Optional;

import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;

public class JspecifyNullSafetyTest {

@Test
void givenKnownUserId_whenFindNickname_thenReturnsOptionalWithValue() {
Optional<String> nickname = findNickname("user123");

assertTrue(nickname.isPresent());
assertEquals("CoolUser", nickname.get());
}

@Test
void givenUnknownUserId_whenFindNickname_thenReturnsEmptyOptional() {
Optional<String> nickname = findNickname("unknownUser");

assertTrue(nickname.isEmpty());
}

@Test
void givenNonNullArgument_whenValidate_thenDoesNotThrowException() {
String result = processNickname("CoolUser");
assertEquals("Processed: CoolUser", result);
}

@Test
void givenNullArgument_whenValidate_thenThrowsNullPointerException() {
assertThrows(NullPointerException.class, () -> processNickname(null));
}

@Test
void givenUnknownUserId_whenFindNicknameOrNull_thenReturnsNull() {
String nickname = findNicknameOrNull("unknownUser");
assertTrue(nickname == null);
}

@Test
void givenNullableMethodResult_whenWrappedInOptional_thenHandledSafely() {
String nickname = findNicknameOrNull("unknownUser");
Optional<String> safeNickname = Optional.ofNullable(nickname);

assertTrue(safeNickname.isEmpty());
}

private Optional<String> findNickname(String userId) {
if ("user123".equals(userId)) {
return Optional.of("CoolUser");
} else {
return Optional.empty();
}
}

@Nullable
private String findNicknameOrNull(String userId) {
if ("user123".equals(userId)) {
return "CoolUser";
} else {
return null;
}
}

private String processNickname(String nickname) {
Objects.requireNonNull(nickname, "Nickname must not be null");
return "Processed: " + nickname;
}
}
1 change: 1 addition & 0 deletions static-analysis-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<modules>
<!-- <module>error-prone-library</module> --> <!-- requires additional configuration to be compiled due to the JVM strong encapsulation -->
<module>infer</module>
<module>jspecify-nullsafety</module>
</modules>

<properties>
Expand Down