Skip to content

Commit 40d0560

Browse files
committed
Add nullability annotations to module/spring-boot-health
See gh-46587
1 parent 99f5b4d commit 40d0560

24 files changed

+101
-41
lines changed

module/spring-boot-health/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ description = "Spring Boot Health"
2626
dependencies {
2727
api(project(":core:spring-boot"))
2828

29+
compileOnly("com.google.code.findbugs:jsr305")
30+
2931
optional(project(":core:spring-boot-autoconfigure"))
3032
optional("io.projectreactor:reactor-core")
3133
optional("com.fasterxml.jackson.core:jackson-annotations")

module/spring-boot-health/src/main/java/org/springframework/boot/health/autoconfigure/contributor/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Auto-configuration for health contributors.
1919
*/
20+
@NullMarked
2021
package org.springframework.boot.health.autoconfigure.contributor;
22+
23+
import org.jspecify.annotations.NullMarked;

module/spring-boot-health/src/main/java/org/springframework/boot/health/autoconfigure/registry/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Auto-configuration for health registries.
1919
*/
20+
@NullMarked
2021
package org.springframework.boot.health.autoconfigure.registry;
22+
23+
import org.jspecify.annotations.NullMarked;

module/spring-boot-health/src/main/java/org/springframework/boot/health/contributor/AbstractHealthIndicator.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.apache.commons.logging.Log;
2222
import org.apache.commons.logging.LogFactory;
23+
import org.jspecify.annotations.Nullable;
2324

2425
import org.springframework.util.Assert;
2526
import org.springframework.util.StringUtils;
@@ -37,13 +38,13 @@
3738
*/
3839
public abstract class AbstractHealthIndicator implements HealthIndicator {
3940

40-
private static final String NO_MESSAGE = null;
41+
private static final @Nullable String NO_MESSAGE = null;
4142

4243
private static final String DEFAULT_MESSAGE = "Health check failed";
4344

4445
private final Log logger = LogFactory.getLog(getClass());
4546

46-
private final Function<Exception, String> healthCheckFailedMessage;
47+
private final Function<Exception, @Nullable String> healthCheckFailedMessage;
4748

4849
/**
4950
* Create a new {@link AbstractHealthIndicator} instance with a default
@@ -58,7 +59,7 @@ protected AbstractHealthIndicator() {
5859
* log when the health check fails.
5960
* @param healthCheckFailedMessage the message to log on health check failure
6061
*/
61-
protected AbstractHealthIndicator(String healthCheckFailedMessage) {
62+
protected AbstractHealthIndicator(@Nullable String healthCheckFailedMessage) {
6263
this.healthCheckFailedMessage = (ex) -> healthCheckFailedMessage;
6364
}
6465

@@ -67,7 +68,7 @@ protected AbstractHealthIndicator(String healthCheckFailedMessage) {
6768
* log when the health check fails.
6869
* @param healthCheckFailedMessage the message to log on health check failure
6970
*/
70-
protected AbstractHealthIndicator(Function<Exception, String> healthCheckFailedMessage) {
71+
protected AbstractHealthIndicator(Function<Exception, @Nullable String> healthCheckFailedMessage) {
7172
Assert.notNull(healthCheckFailedMessage, "'healthCheckFailedMessage' must not be null");
7273
this.healthCheckFailedMessage = healthCheckFailedMessage;
7374
}
@@ -85,7 +86,7 @@ public final Health health() {
8586
return builder.build();
8687
}
8788

88-
private void logExceptionIfPresent(Throwable throwable) {
89+
private void logExceptionIfPresent(@Nullable Throwable throwable) {
8990
if (throwable != null && this.logger.isWarnEnabled()) {
9091
String message = (throwable instanceof Exception ex) ? this.healthCheckFailedMessage.apply(ex) : null;
9192
this.logger.warn(StringUtils.hasText(message) ? message : DEFAULT_MESSAGE, throwable);

module/spring-boot-health/src/main/java/org/springframework/boot/health/contributor/AbstractReactiveHealthIndicator.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.apache.commons.logging.Log;
2222
import org.apache.commons.logging.LogFactory;
23+
import org.jspecify.annotations.Nullable;
2324
import reactor.core.publisher.Mono;
2425

2526
import org.springframework.util.Assert;
@@ -36,13 +37,13 @@
3637
*/
3738
public abstract class AbstractReactiveHealthIndicator implements ReactiveHealthIndicator {
3839

39-
private static final String NO_MESSAGE = null;
40+
private static final @Nullable String NO_MESSAGE = null;
4041

4142
private static final String DEFAULT_MESSAGE = "Health check failed";
4243

4344
private final Log logger = LogFactory.getLog(getClass());
4445

45-
private final Function<Throwable, String> healthCheckFailedMessage;
46+
private final Function<Throwable, @Nullable String> healthCheckFailedMessage;
4647

4748
/**
4849
* Create a new {@link AbstractReactiveHealthIndicator} instance with a default
@@ -57,7 +58,7 @@ protected AbstractReactiveHealthIndicator() {
5758
* message to log when the health check fails.
5859
* @param healthCheckFailedMessage the message to log on health check failure
5960
*/
60-
protected AbstractReactiveHealthIndicator(String healthCheckFailedMessage) {
61+
protected AbstractReactiveHealthIndicator(@Nullable String healthCheckFailedMessage) {
6162
this.healthCheckFailedMessage = (ex) -> healthCheckFailedMessage;
6263
}
6364

@@ -66,7 +67,7 @@ protected AbstractReactiveHealthIndicator(String healthCheckFailedMessage) {
6667
* message to log when the health check fails.
6768
* @param healthCheckFailedMessage the message to log on health check failure
6869
*/
69-
protected AbstractReactiveHealthIndicator(Function<Throwable, String> healthCheckFailedMessage) {
70+
protected AbstractReactiveHealthIndicator(Function<Throwable, @Nullable String> healthCheckFailedMessage) {
7071
Assert.notNull(healthCheckFailedMessage, "'healthCheckFailedMessage' must not be null");
7172
this.healthCheckFailedMessage = healthCheckFailedMessage;
7273
}
@@ -83,7 +84,7 @@ public final Mono<Health> health() {
8384
}
8485
}
8586

86-
private void logExceptionIfPresent(Throwable ex) {
87+
private void logExceptionIfPresent(@Nullable Throwable ex) {
8788
if (ex != null && this.logger.isWarnEnabled()) {
8889
String message = (ex instanceof Exception) ? this.healthCheckFailedMessage.apply(ex) : null;
8990
this.logger.warn(StringUtils.hasText(message) ? message : DEFAULT_MESSAGE, ex);

module/spring-boot-health/src/main/java/org/springframework/boot/health/contributor/CompositeHealthContributors.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.Set;
2323
import java.util.stream.Stream;
2424

25+
import org.jspecify.annotations.Nullable;
26+
2527
/**
2628
* {@link HealthContributors} composed of other {@link HealthContributors}.
2729
*
@@ -37,7 +39,7 @@ class CompositeHealthContributors implements HealthContributors {
3739
}
3840

3941
@Override
40-
public HealthContributor getContributor(String name) {
42+
public @Nullable HealthContributor getContributor(String name) {
4143
return this.contributors.stream()
4244
.map((contributors) -> contributors.getContributor(name))
4345
.filter(Objects::nonNull)

module/spring-boot-health/src/main/java/org/springframework/boot/health/contributor/CompositeReactiveHealthContributors.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.Set;
2323
import java.util.stream.Stream;
2424

25+
import org.jspecify.annotations.Nullable;
26+
2527
/**
2628
* {@link ReactiveHealthContributors} composed of other
2729
* {@link ReactiveHealthContributors}.
@@ -38,7 +40,7 @@ class CompositeReactiveHealthContributors implements ReactiveHealthContributors
3840
}
3941

4042
@Override
41-
public ReactiveHealthContributor getContributor(String name) {
43+
public @Nullable ReactiveHealthContributor getContributor(String name) {
4244
return this.contributors.stream()
4345
.map((reactiveContributors) -> reactiveContributors.getContributor(name))
4446
.filter(Objects::nonNull)

module/spring-boot-health/src/main/java/org/springframework/boot/health/contributor/Health.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.fasterxml.jackson.annotation.JsonInclude;
2424
import com.fasterxml.jackson.annotation.JsonInclude.Include;
2525
import com.fasterxml.jackson.annotation.JsonUnwrapped;
26+
import org.jspecify.annotations.Nullable;
2627

2728
import org.springframework.util.Assert;
2829

@@ -183,7 +184,7 @@ public static class Builder {
183184

184185
private final Map<String, Object> details;
185186

186-
private Throwable exception;
187+
private @Nullable Throwable exception;
187188

188189
/**
189190
* Create new Builder instance.
@@ -325,7 +326,7 @@ public Health build() {
325326
* Return the {@link Exception}.
326327
* @return the exception or {@code null} if the builder has no exception
327328
*/
328-
Throwable getException() {
329+
@Nullable Throwable getException() {
329330
return this.exception;
330331
}
331332

module/spring-boot-health/src/main/java/org/springframework/boot/health/contributor/HealthContributors.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.Iterator;
2020
import java.util.stream.Stream;
2121

22+
import org.jspecify.annotations.Nullable;
23+
2224
import org.springframework.util.Assert;
2325

2426
/**
@@ -34,7 +36,7 @@ public interface HealthContributors extends Iterable<HealthContributors.Entry> {
3436
* @param name the name of the contributor
3537
* @return a contributor instance or {@code null}
3638
*/
37-
HealthContributor getContributor(String name);
39+
@Nullable HealthContributor getContributor(String name);
3840

3941
@Override
4042
default Iterator<Entry> iterator() {

module/spring-boot-health/src/main/java/org/springframework/boot/health/contributor/HealthContributorsAdapter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.stream.Stream;
2020

21+
import org.jspecify.annotations.Nullable;
22+
2123
/**
2224
* Adapts {@link HealthContributors} to {@link ReactiveHealthContributors}.
2325
*
@@ -33,7 +35,7 @@ class HealthContributorsAdapter implements ReactiveHealthContributors {
3335
}
3436

3537
@Override
36-
public ReactiveHealthContributor getContributor(String name) {
38+
public @Nullable ReactiveHealthContributor getContributor(String name) {
3739
return ReactiveHealthContributor.adapt(this.delegate.getContributor(name));
3840
}
3941

0 commit comments

Comments
 (0)