Refine null-safety in spring-messaging

See gh-32475
This commit is contained in:
Sébastien Deleuze
2024-03-25 12:24:53 +01:00
parent 06b91c4ea5
commit dea31dd55e
13 changed files with 22 additions and 6 deletions

View File

@@ -16,6 +16,8 @@
package org.springframework.messaging; package org.springframework.messaging;
import org.springframework.lang.Nullable;
/** /**
* Exception that indicates an error occurred during message delivery. * Exception that indicates an error occurred during message delivery.
* *
@@ -37,11 +39,11 @@ public class MessageDeliveryException extends MessagingException {
super(undeliveredMessage, description); super(undeliveredMessage, description);
} }
public MessageDeliveryException(Message<?> message, Throwable cause) { public MessageDeliveryException(Message<?> message, @Nullable Throwable cause) {
super(message, cause); super(message, cause);
} }
public MessageDeliveryException(Message<?> undeliveredMessage, String description, Throwable cause) { public MessageDeliveryException(Message<?> undeliveredMessage, String description, @Nullable Throwable cause) {
super(undeliveredMessage, description, cause); super(undeliveredMessage, description, cause);
} }

View File

@@ -53,7 +53,7 @@ public class MessagingException extends NestedRuntimeException {
this.failedMessage = message; this.failedMessage = message;
} }
public MessagingException(Message<?> message, Throwable cause) { public MessagingException(Message<?> message, @Nullable Throwable cause) {
super(null, cause); super(null, cause);
this.failedMessage = message; this.failedMessage = message;
} }

View File

@@ -200,7 +200,8 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter {
} }
// Do not log warning for serializer not found (note: different message wording on Jackson 2.9) // Do not log warning for serializer not found (note: different message wording on Jackson 2.9)
boolean debugLevel = (cause instanceof JsonMappingException && cause.getMessage().startsWith("Cannot find")); boolean debugLevel = (cause instanceof JsonMappingException && cause.getMessage() != null
&& cause.getMessage().startsWith("Cannot find"));
if (debugLevel ? logger.isDebugEnabled() : logger.isWarnEnabled()) { if (debugLevel ? logger.isDebugEnabled() : logger.isWarnEnabled()) {
String msg = "Failed to evaluate Jackson " + (type instanceof JavaType ? "de" : "") + String msg = "Failed to evaluate Jackson " + (type instanceof JavaType ? "de" : "") +

View File

@@ -80,6 +80,7 @@ public class CompositeMessageCondition implements MessageCondition<CompositeMess
} }
@Override @Override
@Nullable
public CompositeMessageCondition getMatchingCondition(Message<?> message) { public CompositeMessageCondition getMatchingCondition(Message<?> message) {
List<MessageCondition<?>> result = new ArrayList<>(this.messageConditions.size()); List<MessageCondition<?>> result = new ArrayList<>(this.messageConditions.size());
for (MessageCondition<?> condition : this.messageConditions) { for (MessageCondition<?> condition : this.messageConditions) {

View File

@@ -81,6 +81,7 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements SyncHa
@Override @Override
@Nullable
public Object resolveArgumentValue(MethodParameter parameter, Message<?> message) { public Object resolveArgumentValue(MethodParameter parameter, Message<?> message) {
NamedValueInfo namedValueInfo = getNamedValueInfo(parameter); NamedValueInfo namedValueInfo = getNamedValueInfo(parameter);
MethodParameter nestedParameter = parameter.nestedIfOptional(); MethodParameter nestedParameter = parameter.nestedIfOptional();

View File

@@ -272,6 +272,7 @@ public class MessageMappingMessageHandler extends AbstractMethodMessageHandler<C
@Override @Override
@Nullable
protected CompositeMessageCondition getMappingForMethod(Method method, Class<?> handlerType) { protected CompositeMessageCondition getMappingForMethod(Method method, Class<?> handlerType) {
CompositeMessageCondition methodCondition = getCondition(method); CompositeMessageCondition methodCondition = getCondition(method);
if (methodCondition != null) { if (methodCondition != null) {
@@ -325,12 +326,14 @@ public class MessageMappingMessageHandler extends AbstractMethodMessageHandler<C
} }
@Override @Override
@Nullable
protected RouteMatcher.Route getDestination(Message<?> message) { protected RouteMatcher.Route getDestination(Message<?> message) {
return (RouteMatcher.Route) message.getHeaders() return (RouteMatcher.Route) message.getHeaders()
.get(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER); .get(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER);
} }
@Override @Override
@Nullable
protected CompositeMessageCondition getMatchingMapping(CompositeMessageCondition mapping, Message<?> message) { protected CompositeMessageCondition getMatchingMapping(CompositeMessageCondition mapping, Message<?> message) {
return mapping.getMatchingCondition(message); return mapping.getMatchingCondition(message);
} }

View File

@@ -89,6 +89,7 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
@Override @Override
@Nullable
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception { public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
NamedValueInfo namedValueInfo = getNamedValueInfo(parameter); NamedValueInfo namedValueInfo = getNamedValueInfo(parameter);
MethodParameter nestedParameter = parameter.nestedIfOptional(); MethodParameter nestedParameter = parameter.nestedIfOptional();

View File

@@ -111,7 +111,7 @@ public class InvocableHandlerMethod extends HandlerMethod {
* @see #doInvoke * @see #doInvoke
*/ */
@Nullable @Nullable
public Object invoke(Message<?> message, Object... providedArgs) throws Exception { public Object invoke(Message<?> message, @Nullable Object... providedArgs) throws Exception {
Object[] args = getMethodArgumentValues(message, providedArgs); Object[] args = getMethodArgumentValues(message, providedArgs);
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Arguments: " + Arrays.toString(args)); logger.trace("Arguments: " + Arrays.toString(args));
@@ -125,7 +125,7 @@ public class InvocableHandlerMethod extends HandlerMethod {
* <p>The resulting array will be passed into {@link #doInvoke}. * <p>The resulting array will be passed into {@link #doInvoke}.
* @since 5.1.2 * @since 5.1.2
*/ */
protected Object[] getMethodArgumentValues(Message<?> message, Object... providedArgs) throws Exception { protected Object[] getMethodArgumentValues(Message<?> message, @Nullable Object... providedArgs) throws Exception {
MethodParameter[] parameters = getMethodParameters(); MethodParameter[] parameters = getMethodParameters();
if (ObjectUtils.isEmpty(parameters)) { if (ObjectUtils.isEmpty(parameters)) {
return EMPTY_ARGS; return EMPTY_ARGS;

View File

@@ -23,6 +23,7 @@ import reactor.core.publisher.Mono;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.core.ReactiveAdapter; import org.springframework.core.ReactiveAdapter;
import org.springframework.core.ReactiveAdapterRegistry; import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.lang.Nullable;
/** /**
* Support for single-value reactive types (like {@code Mono} or {@code Single}) * Support for single-value reactive types (like {@code Mono} or {@code Single})
@@ -57,6 +58,7 @@ public class ReactiveReturnValueHandler extends AbstractAsyncReturnValueHandler
} }
@Override @Override
@Nullable
public CompletableFuture<?> toCompletableFuture(Object returnValue, MethodParameter returnType) { public CompletableFuture<?> toCompletableFuture(Object returnValue, MethodParameter returnType) {
ReactiveAdapter adapter = this.adapterRegistry.getAdapter(returnType.getParameterType(), returnValue); ReactiveAdapter adapter = this.adapterRegistry.getAdapter(returnType.getParameterType(), returnValue);
if (adapter != null) { if (adapter != null) {

View File

@@ -134,6 +134,7 @@ public class RSocketFrameTypeMessageCondition extends AbstractMessageCondition<R
} }
@Override @Override
@Nullable
public RSocketFrameTypeMessageCondition getMatchingCondition(Message<?> message) { public RSocketFrameTypeMessageCondition getMatchingCondition(Message<?> message) {
FrameType actual = message.getHeaders().get(FRAME_TYPE_HEADER, FrameType.class); FrameType actual = message.getHeaders().get(FRAME_TYPE_HEADER, FrameType.class);
if (actual != null) { if (actual != null) {

View File

@@ -247,6 +247,7 @@ public final class RSocketServiceProxyFactory {
} }
@Override @Override
@Nullable
public Object invoke(MethodInvocation invocation) throws Throwable { public Object invoke(MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod(); Method method = invocation.getMethod();
RSocketServiceMethod serviceMethod = this.serviceMethods.get(method); RSocketServiceMethod serviceMethod = this.serviceMethods.get(method);

View File

@@ -20,6 +20,7 @@ import java.security.Principal;
import java.util.Optional; import java.util.Optional;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver; import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor; import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
@@ -40,6 +41,7 @@ public class PrincipalMethodArgumentResolver implements HandlerMethodArgumentRes
} }
@Override @Override
@Nullable
public Object resolveArgument(MethodParameter parameter, Message<?> message){ public Object resolveArgument(MethodParameter parameter, Message<?> message){
Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders()); Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders());
return parameter.isOptional() ? Optional.ofNullable(user) : user; return parameter.isOptional() ? Optional.ofNullable(user) : user;

View File

@@ -476,6 +476,7 @@ public class SimpAnnotationMethodMessageHandler extends AbstractMethodMessageHan
} }
@Override @Override
@Nullable
protected String getLookupDestination(@Nullable String destination) { protected String getLookupDestination(@Nullable String destination) {
if (destination == null) { if (destination == null) {
return null; return null;