Skip to content

Commit

Permalink
Refine null-safety in spring-messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Mar 25, 2024
1 parent 06b91c4 commit dea31dd
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.messaging;

import org.springframework.lang.Nullable;

/**
* Exception that indicates an error occurred during message delivery.
*
Expand All @@ -37,11 +39,11 @@ public MessageDeliveryException(Message<?> undeliveredMessage, String descriptio
super(undeliveredMessage, description);
}

public MessageDeliveryException(Message<?> message, Throwable cause) {
public MessageDeliveryException(Message<?> message, @Nullable Throwable 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public MessagingException(Message<?> message, String description) {
this.failedMessage = message;
}

public MessagingException(Message<?> message, Throwable cause) {
public MessagingException(Message<?> message, @Nullable Throwable cause) {
super(null, cause);
this.failedMessage = message;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ protected void logWarningIfNecessary(Type type, @Nullable Throwable cause) {
}

// 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()) {
String msg = "Failed to evaluate Jackson " + (type instanceof JavaType ? "de" : "") +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private <T extends MessageCondition<T>> T combine(MessageCondition<?> first, Mes
}

@Override
@Nullable
public CompositeMessageCondition getMatchingCondition(Message<?> message) {
List<MessageCondition<?>> result = new ArrayList<>(this.messageConditions.size());
for (MessageCondition<?> condition : this.messageConditions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ protected AbstractNamedValueMethodArgumentResolver(ConversionService conversionS


@Override
@Nullable
public Object resolveArgumentValue(MethodParameter parameter, Message<?> message) {
NamedValueInfo namedValueInfo = getNamedValueInfo(parameter);
MethodParameter nestedParameter = parameter.nestedIfOptional();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ protected List<? extends HandlerMethodReturnValueHandler> initReturnValueHandler


@Override
@Nullable
protected CompositeMessageCondition getMappingForMethod(Method method, Class<?> handlerType) {
CompositeMessageCondition methodCondition = getCondition(method);
if (methodCondition != null) {
Expand Down Expand Up @@ -325,12 +326,14 @@ protected Set<String> getDirectLookupMappings(CompositeMessageCondition mapping)
}

@Override
@Nullable
protected RouteMatcher.Route getDestination(Message<?> message) {
return (RouteMatcher.Route) message.getHeaders()
.get(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER);
}

@Override
@Nullable
protected CompositeMessageCondition getMatchingMapping(CompositeMessageCondition mapping, Message<?> message) {
return mapping.getMatchingCondition(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ protected AbstractNamedValueMethodArgumentResolver(ConversionService conversionS


@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
NamedValueInfo namedValueInfo = getNamedValueInfo(parameter);
MethodParameter nestedParameter = parameter.nestedIfOptional();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDisc
* @see #doInvoke
*/
@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);
if (logger.isTraceEnabled()) {
logger.trace("Arguments: " + Arrays.toString(args));
Expand All @@ -125,7 +125,7 @@ public Object invoke(Message<?> message, Object... providedArgs) throws Exceptio
* <p>The resulting array will be passed into {@link #doInvoke}.
* @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();
if (ObjectUtils.isEmpty(parameters)) {
return EMPTY_ARGS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.core.MethodParameter;
import org.springframework.core.ReactiveAdapter;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.lang.Nullable;

/**
* Support for single-value reactive types (like {@code Mono} or {@code Single})
Expand Down Expand Up @@ -57,6 +58,7 @@ public boolean isAsyncReturnValue(Object returnValue, MethodParameter returnType
}

@Override
@Nullable
public CompletableFuture<?> toCompletableFuture(Object returnValue, MethodParameter returnType) {
ReactiveAdapter adapter = this.adapterRegistry.getAdapter(returnType.getParameterType(), returnValue);
if (adapter != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public RSocketFrameTypeMessageCondition combine(RSocketFrameTypeMessageCondition
}

@Override
@Nullable
public RSocketFrameTypeMessageCondition getMatchingCondition(Message<?> message) {
FrameType actual = message.getHeaders().get(FRAME_TYPE_HEADER, FrameType.class);
if (actual != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ private ServiceMethodInterceptor(List<RSocketServiceMethod> methods) {
}

@Override
@Nullable
public Object invoke(MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod();
RSocketServiceMethod serviceMethod = this.serviceMethods.get(method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Optional;

import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
Expand All @@ -40,6 +41,7 @@ public boolean supportsParameter(MethodParameter parameter) {
}

@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, Message<?> message){
Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders());
return parameter.isOptional() ? Optional.ofNullable(user) : user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ protected String getDestination(Message<?> message) {
}

@Override
@Nullable
protected String getLookupDestination(@Nullable String destination) {
if (destination == null) {
return null;
Expand Down

0 comments on commit dea31dd

Please sign in to comment.