Switch to JSpecify annotations

This commit updates the whole Spring Framework codebase to use JSpecify
annotations instead of Spring null-safety annotations with JSR 305
semantics.

JSpecify provides signficant enhancements such as properly defined
specifications, a canonical dependency with no split-package issue,
better tooling, better Kotlin integration and the capability to specify
generic type, array and varargs element null-safety. Generic type
null-safety is not defined by this commit yet and will be specified
later.

A key difference is that Spring null-safety annotations, following
JSR 305 semantics, apply to fields, parameters and return values,
while JSpecify annotations apply to type usages. That's why this
commit moves nullability annotations closer to the type for fields
and return values.

See gh-28797
This commit is contained in:
Sébastien Deleuze
2024-12-03 15:22:37 +01:00
parent fcb8aed03f
commit bc5d771a06
3459 changed files with 14118 additions and 22059 deletions

View File

@@ -19,9 +19,9 @@ package org.springframework.messaging.converter;
import java.util.HashMap;
import java.util.Map;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;

View File

@@ -18,8 +18,9 @@ package org.springframework.messaging.handler.annotation;
import java.util.function.Predicate;
import org.jspecify.annotations.Nullable;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
/**
* Predicates for messaging annotations.
@@ -55,8 +56,7 @@ public class MessagingPredicates {
public static class DestinationVariablePredicate implements Predicate<MethodParameter> {
@Nullable
private String value;
private @Nullable String value;
public DestinationVariablePredicate value(@Nullable String name) {
@@ -79,14 +79,11 @@ public class MessagingPredicates {
public static class HeaderPredicate implements Predicate<MethodParameter> {
@Nullable
private String name;
private @Nullable String name;
@Nullable
private Boolean required;
private @Nullable Boolean required;
@Nullable
private String defaultValue;
private @Nullable String defaultValue;
public HeaderPredicate name(@Nullable String name) {

View File

@@ -23,6 +23,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
@@ -35,7 +36,6 @@ import org.springframework.core.codec.Decoder;
import org.springframework.core.codec.StringDecoder;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.handler.annotation.Payload;
@@ -160,8 +160,7 @@ class PayloadMethodArgumentResolverTests {
@SuppressWarnings("unchecked")
@Nullable
private <T> T resolveValue(MethodParameter param, Publisher<DataBuffer> content, Validator validator) {
private <T> @Nullable T resolveValue(MethodParameter param, Publisher<DataBuffer> content, Validator validator) {
Message<?> message = new GenericMessage<>(content,
Collections.singletonMap(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN));

View File

@@ -18,10 +18,10 @@ package org.springframework.messaging.handler.invocation;
import java.lang.reflect.Method;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import static org.assertj.core.api.Assertions.assertThat;
@@ -154,8 +154,7 @@ class InvocableHandlerMethodTests {
.withMessageContaining("Illegal argument");
}
@Nullable
private Object invoke(Object handler, Method method, Object... providedArgs) throws Exception {
private @Nullable Object invoke(Object handler, Method method, Object... providedArgs) throws Exception {
InvocableHandlerMethod handlerMethod = new InvocableHandlerMethod(handler, method);
handlerMethod.setMessageMethodArgumentResolvers(this.resolvers);
return handlerMethod.invoke(this.message, providedArgs);

View File

@@ -32,6 +32,7 @@ import java.util.function.Supplier;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jspecify.annotations.Nullable;
import org.springframework.cglib.core.SpringNamingPolicy;
import org.springframework.cglib.proxy.Callback;
@@ -47,7 +48,6 @@ import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.SynthesizingMethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.objenesis.ObjenesisException;
import org.springframework.objenesis.SpringObjenesis;
import org.springframework.util.Assert;
@@ -613,12 +613,10 @@ public class ResolvableMethod {
private static class MethodInvocationInterceptor implements MethodInterceptor, InvocationHandler {
@Nullable
private Method invokedMethod;
private @Nullable Method invokedMethod;
@Override
@Nullable
public Object intercept(Object object, Method method, @Nullable Object[] args, @Nullable MethodProxy proxy) {
public @Nullable Object intercept(Object object, Method method, Object @Nullable [] args, @Nullable MethodProxy proxy) {
if (ReflectionUtils.isObjectMethod(method)) {
return ReflectionUtils.invokeMethod(method, object, args);
}
@@ -629,13 +627,11 @@ public class ResolvableMethod {
}
@Override
@Nullable
public Object invoke(Object proxy, Method method, @Nullable Object[] args) {
public @Nullable Object invoke(Object proxy, Method method, Object @Nullable [] args) {
return intercept(proxy, method, args, null);
}
@Nullable
Method getInvokedMethod() {
@Nullable Method getInvokedMethod() {
return this.invokedMethod;
}
}

View File

@@ -19,8 +19,9 @@ package org.springframework.messaging.handler.invocation;
import java.util.ArrayList;
import java.util.List;
import org.jspecify.annotations.Nullable;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
/**
@@ -32,8 +33,7 @@ public class StubArgumentResolver implements HandlerMethodArgumentResolver {
private final Class<?> valueType;
@Nullable
private final Object value;
private final @Nullable Object value;
private List<MethodParameter> resolvedParameters = new ArrayList<>();

View File

@@ -19,6 +19,7 @@ package org.springframework.messaging.handler.invocation.reactive;
import java.util.Collections;
import io.reactivex.rxjava3.core.Completable;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -27,7 +28,6 @@ import reactor.test.StepVerifier;
import org.springframework.core.MethodParameter;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.codec.CharSequenceEncoder;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;

View File

@@ -22,12 +22,12 @@ import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.invocation.MethodArgumentResolutionException;
import org.springframework.messaging.handler.invocation.ResolvableMethod;
@@ -161,8 +161,7 @@ class InvocableHandlerMethodTests {
}
@Nullable
private Object invokeAndBlock(Object handler, Method method, Object... providedArgs) {
private @Nullable Object invokeAndBlock(Object handler, Method method, Object... providedArgs) {
return invoke(handler, method, providedArgs).block(Duration.ofSeconds(5));
}

View File

@@ -25,13 +25,13 @@ import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.DestinationPatternsMessageCondition;
import org.springframework.messaging.handler.HandlerMethod;
@@ -218,8 +218,7 @@ class MethodMessageHandlerTests {
return Collections.singletonList(this.returnValueHandler);
}
@Nullable
public Object getLastReturnValue() {
public @Nullable Object getLastReturnValue() {
return this.returnValueHandler.getLastReturnValue();
}
@@ -238,8 +237,7 @@ class MethodMessageHandlerTests {
}
@Override
@Nullable
protected RouteMatcher.Route getDestination(Message<?> message) {
protected RouteMatcher.@Nullable Route getDestination(Message<?> message) {
return (RouteMatcher.Route) message.getHeaders().get(
DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER);
}

View File

@@ -19,10 +19,10 @@ package org.springframework.messaging.handler.invocation.reactive;
import java.util.ArrayList;
import java.util.List;
import org.jspecify.annotations.Nullable;
import reactor.core.publisher.Mono;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
/**
@@ -34,8 +34,7 @@ public class StubArgumentResolver implements HandlerMethodArgumentResolver {
private final Class<?> valueType;
@Nullable
private final Object value;
private final @Nullable Object value;
private List<MethodParameter> resolvedParameters = new ArrayList<>();

View File

@@ -16,11 +16,11 @@
package org.springframework.messaging.handler.invocation.reactive;
import org.jspecify.annotations.Nullable;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
/**
@@ -29,12 +29,10 @@ import org.springframework.messaging.Message;
*/
public class TestReturnValueHandler implements HandlerMethodReturnValueHandler {
@Nullable
private Object lastReturnValue;
private @Nullable Object lastReturnValue;
@Nullable
public Object getLastReturnValue() {
public @Nullable Object getLastReturnValue() {
return this.lastReturnValue;
}

View File

@@ -24,6 +24,7 @@ import java.util.Map;
import io.netty.buffer.PooledByteBufAllocator;
import io.rsocket.Payload;
import io.rsocket.metadata.WellKnownMimeType;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -35,7 +36,6 @@ import org.springframework.core.codec.StringDecoder;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;

View File

@@ -18,12 +18,11 @@ package org.springframework.messaging.rsocket;
import io.rsocket.Payload;
import io.rsocket.RSocket;
import org.jspecify.annotations.Nullable;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.lang.Nullable;
/**
* {@link RSocket} that saves the name of the invoked method and the input payload(s).
*/
@@ -33,11 +32,11 @@ public class TestRSocket implements RSocket {
private Flux<Payload> payloadFluxToReturn = Flux.empty();
@Nullable private volatile String savedMethodName;
private volatile @Nullable String savedMethodName;
@Nullable private volatile Payload savedPayload;
private volatile @Nullable Payload savedPayload;
@Nullable private volatile Flux<Payload> savedPayloadFlux;
private volatile @Nullable Flux<Payload> savedPayloadFlux;
public void setPayloadMonoToReturn(Mono<Payload> payloadMonoToReturn) {
@@ -48,18 +47,15 @@ public class TestRSocket implements RSocket {
this.payloadFluxToReturn = payloadFluxToReturn;
}
@Nullable
public String getSavedMethodName() {
public @Nullable String getSavedMethodName() {
return this.savedMethodName;
}
@Nullable
public Payload getSavedPayload() {
public @Nullable Payload getSavedPayload() {
return this.savedPayload;
}
@Nullable
public Flux<Payload> getSavedPayloadFlux() {
public @Nullable Flux<Payload> getSavedPayloadFlux() {
return this.savedPayloadFlux;
}

View File

@@ -19,9 +19,9 @@ package org.springframework.messaging.rsocket.annotation.support;
import java.util.Arrays;
import io.rsocket.frame.FrameType;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;

View File

@@ -18,6 +18,7 @@ package org.springframework.messaging.rsocket.service;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Single;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
@@ -25,7 +26,6 @@ import reactor.core.publisher.Mono;
import org.springframework.core.MethodParameter;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.lang.Nullable;
import org.springframework.messaging.handler.annotation.Payload;
import static org.assertj.core.api.Assertions.assertThat;

View File

@@ -16,6 +16,7 @@
package org.springframework.messaging.rsocket.service;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.springframework.aop.SpringProxy;
@@ -28,7 +29,6 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.DecoratingProxy;
import org.springframework.lang.Nullable;
import org.springframework.messaging.handler.annotation.Payload;
import static org.assertj.core.api.Assertions.assertThat;
@@ -70,8 +70,7 @@ class RSocketExchangeBeanRegistrationAotProcessorTests {
}
}
@Nullable
private static BeanRegistrationAotContribution createContribution(Class<?> beanClass) {
private static @Nullable BeanRegistrationAotContribution createContribution(Class<?> beanClass) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
beanFactory.registerBeanDefinition(beanClass.getName(), new RootBeanDefinition(beanClass));
return new RSocketExchangeBeanRegistrationAotProcessor()

View File

@@ -18,8 +18,9 @@ package org.springframework.messaging.rsocket.service;
import java.lang.reflect.Method;
import org.jspecify.annotations.Nullable;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
/**
@@ -28,13 +29,11 @@ import org.springframework.util.ClassUtils;
*/
public abstract class RSocketServiceArgumentResolverTestSupport {
@Nullable
private RSocketServiceArgumentResolver resolver;
private @Nullable RSocketServiceArgumentResolver resolver;
private final RSocketRequestValues.Builder requestValuesBuilder = RSocketRequestValues.builder(null);
@Nullable
private RSocketRequestValues requestValues;
private @Nullable RSocketRequestValues requestValues;
protected RSocketServiceArgumentResolverTestSupport() {

View File

@@ -25,6 +25,7 @@ import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -37,7 +38,6 @@ import reactor.core.publisher.Mono;
import reactor.core.publisher.Sinks;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;

View File

@@ -24,6 +24,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
@@ -32,7 +33,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;

View File

@@ -29,12 +29,12 @@ import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.TransportConnector;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.springframework.lang.Nullable;
import org.springframework.messaging.converter.StringMessageConverter;
import org.springframework.messaging.simp.stomp.StompSession.Subscription;
import org.springframework.messaging.tcp.reactor.ReactorNetty2TcpClient;

View File

@@ -20,10 +20,9 @@ import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.springframework.lang.Nullable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

View File

@@ -19,7 +19,7 @@ package org.springframework.messaging.simp.user;
import java.util.HashSet;
import java.util.Set;
import org.springframework.lang.Nullable;
import org.jspecify.annotations.Nullable;
/**
* @author Rossen Stoyanchev

View File

@@ -18,7 +18,8 @@ package org.springframework.messaging.simp.user;
import java.util.Objects;
import org.springframework.lang.Nullable;
import org.jspecify.annotations.Nullable;
import org.springframework.util.ObjectUtils;
/**

View File

@@ -22,7 +22,7 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.springframework.lang.Nullable;
import org.jspecify.annotations.Nullable;
/**
* @author Rossen Stoyanchev
@@ -44,9 +44,8 @@ public class TestSimpUser implements SimpUser {
return name;
}
@Nullable
@Override
public Principal getPrincipal() {
public @Nullable Principal getPrincipal() {
return null;
}

View File

@@ -18,12 +18,12 @@ package org.springframework.messaging.simp.user;
import java.nio.charset.StandardCharsets;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.springframework.core.testfixture.security.TestPrincipal;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.StubMessageChannel;
import org.springframework.messaging.SubscribableChannel;