From 6fb60bc3f570416ce11f2c7d6dedea0511f68ce8 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Mon, 4 Aug 2025 13:24:07 +0200 Subject: [PATCH] Add nullability annotations to module/spring-boot-neo4j See gh-46587 --- .../autoconfigure/Neo4jAutoConfiguration.java | 7 +-- .../autoconfigure/Neo4jConnectionDetails.java | 3 +- .../neo4j/autoconfigure/Neo4jProperties.java | 44 ++++++++++--------- .../autoconfigure/health/package-info.java | 3 ++ .../neo4j/autoconfigure/package-info.java | 3 ++ ...DockerComposeConnectionDetailsFactory.java | 6 ++- .../docker/compose/Neo4jEnvironment.java | 7 +-- .../neo4j/docker/compose/package-info.java | 3 ++ .../health/Neo4jReactiveHealthIndicator.java | 5 ++- .../boot/neo4j/health/package-info.java | 3 ++ .../neo4j/testcontainers/package-info.java | 3 ++ 11 files changed, 56 insertions(+), 31 deletions(-) diff --git a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/Neo4jAutoConfiguration.java b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/Neo4jAutoConfiguration.java index 1e781a49e17..4dde54f423f 100644 --- a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/Neo4jAutoConfiguration.java +++ b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/Neo4jAutoConfiguration.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Locale; import java.util.concurrent.TimeUnit; +import org.jspecify.annotations.Nullable; import org.neo4j.driver.AuthToken; import org.neo4j.driver.AuthTokenManager; import org.neo4j.driver.AuthTokens; @@ -185,9 +186,9 @@ public final class Neo4jAutoConfiguration { private final Neo4jProperties properties; - private final AuthTokenManager authTokenManager; + private final @Nullable AuthTokenManager authTokenManager; - PropertiesNeo4jConnectionDetails(Neo4jProperties properties, AuthTokenManager authTokenManager) { + PropertiesNeo4jConnectionDetails(Neo4jProperties properties, @Nullable AuthTokenManager authTokenManager) { this.properties = properties; this.authTokenManager = authTokenManager; } @@ -219,7 +220,7 @@ public final class Neo4jAutoConfiguration { } @Override - public AuthTokenManager getAuthTokenManager() { + public @Nullable AuthTokenManager getAuthTokenManager() { return this.authTokenManager; } diff --git a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/Neo4jConnectionDetails.java b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/Neo4jConnectionDetails.java index da328aa0bd8..94a87803bde 100644 --- a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/Neo4jConnectionDetails.java +++ b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/Neo4jConnectionDetails.java @@ -18,6 +18,7 @@ package org.springframework.boot.neo4j.autoconfigure; import java.net.URI; +import org.jspecify.annotations.Nullable; import org.neo4j.driver.AuthToken; import org.neo4j.driver.AuthTokenManager; import org.neo4j.driver.AuthTokens; @@ -55,7 +56,7 @@ public interface Neo4jConnectionDetails extends ConnectionDetails { * {@code null} in which case the {@link #getAuthToken() auth token} should be used. * @return the auth token manager */ - default AuthTokenManager getAuthTokenManager() { + default @Nullable AuthTokenManager getAuthTokenManager() { return null; } diff --git a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/Neo4jProperties.java b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/Neo4jProperties.java index 25ed6e42a0f..93b0a10e920 100644 --- a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/Neo4jProperties.java +++ b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/Neo4jProperties.java @@ -20,6 +20,8 @@ import java.io.File; import java.net.URI; import java.time.Duration; +import org.jspecify.annotations.Nullable; + import org.springframework.boot.context.properties.ConfigurationProperties; /** @@ -35,7 +37,7 @@ public class Neo4jProperties { /** * URI used by the driver. */ - private URI uri; + private @Nullable URI uri; /** * Timeout for borrowing connections from the pool. @@ -53,11 +55,11 @@ public class Neo4jProperties { private final Security security = new Security(); - public URI getUri() { + public @Nullable URI getUri() { return this.uri; } - public void setUri(URI uri) { + public void setUri(@Nullable URI uri) { this.uri = uri; } @@ -94,53 +96,53 @@ public class Neo4jProperties { /** * Login user of the server. */ - private String username; + private @Nullable String username; /** * Login password of the server. */ - private String password; + private @Nullable String password; /** * Realm to connect to. */ - private String realm; + private @Nullable String realm; /** * Kerberos ticket for connecting to the database. Mutual exclusive with a given * username. */ - private String kerberosTicket; + private @Nullable String kerberosTicket; - public String getUsername() { + public @Nullable String getUsername() { return this.username; } - public void setUsername(String username) { + public void setUsername(@Nullable String username) { this.username = username; } - public String getPassword() { + public @Nullable String getPassword() { return this.password; } - public void setPassword(String password) { + public void setPassword(@Nullable String password) { this.password = password; } - public String getRealm() { + public @Nullable String getRealm() { return this.realm; } - public void setRealm(String realm) { + public void setRealm(@Nullable String realm) { this.realm = realm; } - public String getKerberosTicket() { + public @Nullable String getKerberosTicket() { return this.kerberosTicket; } - public void setKerberosTicket(String kerberosTicket) { + public void setKerberosTicket(@Nullable String kerberosTicket) { this.kerberosTicket = kerberosTicket; } @@ -167,7 +169,7 @@ public class Neo4jProperties { * Pooled connections that have been idle in the pool for longer than this * threshold will be tested before they are used again. */ - private Duration idleTimeBeforeConnectionTest; + private @Nullable Duration idleTimeBeforeConnectionTest; /** * Pooled connections older than this threshold will be closed and removed from @@ -197,11 +199,11 @@ public class Neo4jProperties { this.maxConnectionPoolSize = maxConnectionPoolSize; } - public Duration getIdleTimeBeforeConnectionTest() { + public @Nullable Duration getIdleTimeBeforeConnectionTest() { return this.idleTimeBeforeConnectionTest; } - public void setIdleTimeBeforeConnectionTest(Duration idleTimeBeforeConnectionTest) { + public void setIdleTimeBeforeConnectionTest(@Nullable Duration idleTimeBeforeConnectionTest) { this.idleTimeBeforeConnectionTest = idleTimeBeforeConnectionTest; } @@ -246,7 +248,7 @@ public class Neo4jProperties { /** * Path to the file that holds the trusted certificates. */ - private File certFile; + private @Nullable File certFile; /** * Whether hostname verification is required. @@ -269,11 +271,11 @@ public class Neo4jProperties { this.trustStrategy = trustStrategy; } - public File getCertFile() { + public @Nullable File getCertFile() { return this.certFile; } - public void setCertFile(File certFile) { + public void setCertFile(@Nullable File certFile) { this.certFile = certFile; } diff --git a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/health/package-info.java b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/health/package-info.java index 2ea10f016c5..8ee8442d7d4 100644 --- a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/health/package-info.java +++ b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/health/package-info.java @@ -17,4 +17,7 @@ /** * Auto-configuration for Neo4j health. */ +@NullMarked package org.springframework.boot.neo4j.autoconfigure.health; + +import org.jspecify.annotations.NullMarked; diff --git a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/package-info.java b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/package-info.java index 0ad30d64d4d..659137b3027 100644 --- a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/package-info.java +++ b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/autoconfigure/package-info.java @@ -17,4 +17,7 @@ /** * Auto-configuration for Neo4j. */ +@NullMarked package org.springframework.boot.neo4j.autoconfigure; + +import org.jspecify.annotations.NullMarked; diff --git a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/docker/compose/Neo4jDockerComposeConnectionDetailsFactory.java b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/docker/compose/Neo4jDockerComposeConnectionDetailsFactory.java index 4886a28ed4a..f46fe8be8ed 100644 --- a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/docker/compose/Neo4jDockerComposeConnectionDetailsFactory.java +++ b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/docker/compose/Neo4jDockerComposeConnectionDetailsFactory.java @@ -18,7 +18,9 @@ package org.springframework.boot.neo4j.docker.compose; import java.net.URI; +import org.jspecify.annotations.Nullable; import org.neo4j.driver.AuthToken; +import org.neo4j.driver.AuthTokens; import org.springframework.boot.docker.compose.core.RunningService; import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory; @@ -53,7 +55,7 @@ class Neo4jDockerComposeConnectionDetailsFactory extends DockerComposeConnection private static final int BOLT_PORT = 7687; - private final AuthToken authToken; + private final @Nullable AuthToken authToken; private final URI uri; @@ -71,7 +73,7 @@ class Neo4jDockerComposeConnectionDetailsFactory extends DockerComposeConnection @Override public AuthToken getAuthToken() { - return this.authToken; + return (this.authToken != null) ? this.authToken : AuthTokens.none(); } } diff --git a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/docker/compose/Neo4jEnvironment.java b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/docker/compose/Neo4jEnvironment.java index 52ef83c0bf2..9017829b709 100644 --- a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/docker/compose/Neo4jEnvironment.java +++ b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/docker/compose/Neo4jEnvironment.java @@ -18,6 +18,7 @@ package org.springframework.boot.neo4j.docker.compose; import java.util.Map; +import org.jspecify.annotations.Nullable; import org.neo4j.driver.AuthToken; import org.neo4j.driver.AuthTokens; @@ -29,7 +30,7 @@ import org.neo4j.driver.AuthTokens; */ class Neo4jEnvironment { - private final AuthToken authToken; + private final @Nullable AuthToken authToken; Neo4jEnvironment(Map env) { AuthToken authToken = parse(env.get("NEO4J_AUTH")); @@ -39,7 +40,7 @@ class Neo4jEnvironment { this.authToken = authToken; } - private AuthToken parse(String neo4jAuth) { + private @Nullable AuthToken parse(@Nullable String neo4jAuth) { if (neo4jAuth == null) { return null; } @@ -55,7 +56,7 @@ class Neo4jEnvironment { + " the neo4j user's password"); } - AuthToken getAuthToken() { + @Nullable AuthToken getAuthToken() { return this.authToken; } diff --git a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/docker/compose/package-info.java b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/docker/compose/package-info.java index ee809d69b35..9814da657b0 100644 --- a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/docker/compose/package-info.java +++ b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/docker/compose/package-info.java @@ -17,4 +17,7 @@ /** * Support for Docker Compose Neo4J service connections. */ +@NullMarked package org.springframework.boot.neo4j.docker.compose; + +import org.jspecify.annotations.NullMarked; diff --git a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/health/Neo4jReactiveHealthIndicator.java b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/health/Neo4jReactiveHealthIndicator.java index ca23f586137..1174b2326b4 100644 --- a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/health/Neo4jReactiveHealthIndicator.java +++ b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/health/Neo4jReactiveHealthIndicator.java @@ -18,6 +18,7 @@ package org.springframework.boot.neo4j.health; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.jspecify.annotations.Nullable; import org.neo4j.driver.Driver; import org.neo4j.driver.Record; import org.neo4j.driver.exceptions.SessionExpiredException; @@ -31,6 +32,7 @@ import reactor.util.retry.Retry; import org.springframework.boot.health.contributor.AbstractReactiveHealthIndicator; import org.springframework.boot.health.contributor.Health; import org.springframework.boot.health.contributor.ReactiveHealthIndicator; +import org.springframework.util.Assert; /** * {@link ReactiveHealthIndicator} that tests the status of a Neo4j by executing a Cypher @@ -90,13 +92,14 @@ public final class Neo4jReactiveHealthIndicator extends AbstractReactiveHealthIn */ private static final class Neo4jHealthDetailsBuilder { - private Record record; + private @Nullable Record record; void record(Record record) { this.record = record; } private Neo4jHealthDetails build(ResultSummary summary) { + Assert.state(this.record != null, "'record' must not be null"); return new Neo4jHealthDetails(this.record, summary); } diff --git a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/health/package-info.java b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/health/package-info.java index de96196cf62..9ae329d24ab 100644 --- a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/health/package-info.java +++ b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/health/package-info.java @@ -17,4 +17,7 @@ /** * Health integration for Neo4j. */ +@NullMarked package org.springframework.boot.neo4j.health; + +import org.jspecify.annotations.NullMarked; diff --git a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/testcontainers/package-info.java b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/testcontainers/package-info.java index b363d0069fa..7be26c65782 100644 --- a/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/testcontainers/package-info.java +++ b/module/spring-boot-neo4j/src/main/java/org/springframework/boot/neo4j/testcontainers/package-info.java @@ -17,4 +17,7 @@ /** * Support for testcontainers Neo4J service connections. */ +@NullMarked package org.springframework.boot.neo4j.testcontainers; + +import org.jspecify.annotations.NullMarked;