The package o.s.messaging.handler.annotation.support was missing
@NonnullApi and @NonNullFields. This commit corrects that and also
adds @Nullable to methods and arguments as needed to address
warnings.
This commit is contained in:
Rossen Stoyanchev 2019-01-28 16:39:49 -05:00
parent 567c559da8
commit 5b3b0b1a7b
7 changed files with 45 additions and 22 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -26,7 +26,6 @@ import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.ValueConstants; import org.springframework.messaging.handler.annotation.ValueConstants;
@ -61,8 +60,10 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
private final ConversionService conversionService; private final ConversionService conversionService;
@Nullable
private final ConfigurableBeanFactory configurableBeanFactory; private final ConfigurableBeanFactory configurableBeanFactory;
@Nullable
private final BeanExpressionContext expressionContext; private final BeanExpressionContext expressionContext;
private final Map<MethodParameter, NamedValueInfo> namedValueInfoCache = new ConcurrentHashMap<>(256); private final Map<MethodParameter, NamedValueInfo> namedValueInfoCache = new ConcurrentHashMap<>(256);
@ -70,16 +71,16 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
/** /**
* Constructor with a {@link ConversionService} and a {@link BeanFactory}. * Constructor with a {@link ConversionService} and a {@link BeanFactory}.
* @param cs conversion service for converting values to match the * @param conversionService conversion service for converting values to match the
* target method parameter type * target method parameter type
* @param beanFactory a bean factory to use for resolving {@code ${...}} placeholder * @param beanFactory a bean factory to use for resolving {@code ${...}} placeholder
* and {@code #{...}} SpEL expressions in default values, or {@code null} if default * and {@code #{...}} SpEL expressions in default values, or {@code null} if default
* values are not expected to contain expressions * values are not expected to contain expressions
*/ */
protected AbstractNamedValueMethodArgumentResolver(ConversionService cs, protected AbstractNamedValueMethodArgumentResolver(ConversionService conversionService,
@Nullable ConfigurableBeanFactory beanFactory) { @Nullable ConfigurableBeanFactory beanFactory) {
this.conversionService = (cs != null ? cs : DefaultConversionService.getSharedInstance()); this.conversionService = conversionService;
this.configurableBeanFactory = beanFactory; this.configurableBeanFactory = beanFactory;
this.expressionContext = (beanFactory != null ? new BeanExpressionContext(beanFactory, null) : null); this.expressionContext = (beanFactory != null ? new BeanExpressionContext(beanFactory, null) : null);
} }
@ -161,8 +162,9 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
* Resolve the given annotation-specified value, * Resolve the given annotation-specified value,
* potentially containing placeholders and expressions. * potentially containing placeholders and expressions.
*/ */
@Nullable
private Object resolveStringValue(String value) { private Object resolveStringValue(String value) {
if (this.configurableBeanFactory == null) { if (this.configurableBeanFactory == null || this.expressionContext == null) {
return value; return value;
} }
String placeholdersResolved = this.configurableBeanFactory.resolveEmbeddedValue(value); String placeholdersResolved = this.configurableBeanFactory.resolveEmbeddedValue(value);
@ -199,6 +201,7 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
* A {@code null} results in a {@code false} value for {@code boolean}s or an * A {@code null} results in a {@code false} value for {@code boolean}s or an
* exception for other primitives. * exception for other primitives.
*/ */
@Nullable
private Object handleNullValue(String name, @Nullable Object value, Class<?> paramType) { private Object handleNullValue(String name, @Nullable Object value, Class<?> paramType) {
if (value == null) { if (value == null) {
if (Boolean.TYPE.equals(paramType)) { if (Boolean.TYPE.equals(paramType)) {
@ -221,7 +224,8 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
* @param parameter the argument parameter type * @param parameter the argument parameter type
* @param message the message * @param message the message
*/ */
protected void handleResolvedValue(Object arg, String name, MethodParameter parameter, Message<?> message) { protected void handleResolvedValue(
@Nullable Object arg, String name, MethodParameter parameter, Message<?> message) {
} }
@ -235,9 +239,10 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
private final boolean required; private final boolean required;
@Nullable
private final String defaultValue; private final String defaultValue;
protected NamedValueInfo(String name, boolean required, String defaultValue) { protected NamedValueInfo(String name, boolean required, @Nullable String defaultValue) {
this.name = name; this.name = name;
this.required = required; this.required = required;
this.defaultValue = defaultValue; this.defaultValue = defaultValue;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -26,11 +26,13 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.format.support.DefaultFormattingConversionService; import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.lang.Nullable;
import org.springframework.messaging.converter.GenericMessageConverter; import org.springframework.messaging.converter.GenericMessageConverter;
import org.springframework.messaging.converter.MessageConverter; import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver; import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite; import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite;
import org.springframework.messaging.handler.invocation.InvocableHandlerMethod; import org.springframework.messaging.handler.invocation.InvocableHandlerMethod;
import org.springframework.util.Assert;
import org.springframework.validation.Validator; import org.springframework.validation.Validator;
/** /**
@ -60,15 +62,19 @@ public class DefaultMessageHandlerMethodFactory
private ConversionService conversionService = new DefaultFormattingConversionService(); private ConversionService conversionService = new DefaultFormattingConversionService();
@Nullable
private MessageConverter messageConverter; private MessageConverter messageConverter;
@Nullable
private Validator validator; private Validator validator;
@Nullable
private List<HandlerMethodArgumentResolver> customArgumentResolvers; private List<HandlerMethodArgumentResolver> customArgumentResolvers;
private final HandlerMethodArgumentResolverComposite argumentResolvers = private final HandlerMethodArgumentResolverComposite argumentResolvers =
new HandlerMethodArgumentResolverComposite(); new HandlerMethodArgumentResolverComposite();
@Nullable
private BeanFactory beanFactory; private BeanFactory beanFactory;
@ -114,6 +120,7 @@ public class DefaultMessageHandlerMethodFactory
* the ones configured by default. This is an advanced option. For most use cases * the ones configured by default. This is an advanced option. For most use cases
* it should be sufficient to use {@link #setCustomArgumentResolvers(java.util.List)}. * it should be sufficient to use {@link #setCustomArgumentResolvers(java.util.List)}.
*/ */
@SuppressWarnings("ConstantConditions")
public void setArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { public void setArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
if (argumentResolvers == null) { if (argumentResolvers == null) {
this.argumentResolvers.clear(); this.argumentResolvers.clear();
@ -151,11 +158,11 @@ public class DefaultMessageHandlerMethodFactory
protected List<HandlerMethodArgumentResolver> initArgumentResolvers() { protected List<HandlerMethodArgumentResolver> initArgumentResolvers() {
List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>(); List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
ConfigurableBeanFactory cbf = (this.beanFactory instanceof ConfigurableBeanFactory ? ConfigurableBeanFactory beanFactory = (this.beanFactory instanceof ConfigurableBeanFactory ?
(ConfigurableBeanFactory) this.beanFactory : null); (ConfigurableBeanFactory) this.beanFactory : null);
// Annotation-based argument resolution // Annotation-based argument resolution
resolvers.add(new HeaderMethodArgumentResolver(this.conversionService, cbf)); resolvers.add(new HeaderMethodArgumentResolver(this.conversionService, beanFactory));
resolvers.add(new HeadersMethodArgumentResolver()); resolvers.add(new HeadersMethodArgumentResolver());
// Type-based argument resolution // Type-based argument resolution
@ -164,6 +171,8 @@ public class DefaultMessageHandlerMethodFactory
if (this.customArgumentResolvers != null) { if (this.customArgumentResolvers != null) {
resolvers.addAll(this.customArgumentResolvers); resolvers.addAll(this.customArgumentResolvers);
} }
Assert.notNull(this.messageConverter, "MessageConverter not configured");
resolvers.add(new PayloadArgumentResolver(this.messageConverter, this.validator)); resolvers.add(new PayloadArgumentResolver(this.messageConverter, this.validator));
return resolvers; return resolvers;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -43,8 +43,8 @@ public class DestinationVariableMethodArgumentResolver extends AbstractNamedValu
DestinationVariableMethodArgumentResolver.class.getSimpleName() + ".templateVariables"; DestinationVariableMethodArgumentResolver.class.getSimpleName() + ".templateVariables";
public DestinationVariableMethodArgumentResolver(ConversionService cs) { public DestinationVariableMethodArgumentResolver(ConversionService conversionService) {
super(cs, null); super(conversionService, null);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -43,8 +43,10 @@ public class HeaderMethodArgumentResolver extends AbstractNamedValueMethodArgume
private static final Log logger = LogFactory.getLog(HeaderMethodArgumentResolver.class); private static final Log logger = LogFactory.getLog(HeaderMethodArgumentResolver.class);
public HeaderMethodArgumentResolver(ConversionService cs, ConfigurableBeanFactory beanFactory) { public HeaderMethodArgumentResolver(
super(cs, beanFactory); ConversionService conversionService, @Nullable ConfigurableBeanFactory beanFactory) {
super(conversionService, beanFactory);
} }
@ -94,9 +96,9 @@ public class HeaderMethodArgumentResolver extends AbstractNamedValueMethodArgume
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Nullable
private Map<String, List<String>> getNativeHeaders(Message<?> message) { private Map<String, List<String>> getNativeHeaders(Message<?> message) {
return (Map<String, List<String>>) message.getHeaders().get( return (Map<String, List<String>>) message.getHeaders().get(NativeMessageHeaderAccessor.NATIVE_HEADERS);
NativeMessageHeaderAccessor.NATIVE_HEADERS);
} }
@Override @Override

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -43,6 +43,7 @@ import org.springframework.util.StringUtils;
*/ */
public class MessageMethodArgumentResolver implements HandlerMethodArgumentResolver { public class MessageMethodArgumentResolver implements HandlerMethodArgumentResolver {
@Nullable
private final MessageConverter converter; private final MessageConverter converter;

View File

@ -55,6 +55,7 @@ public class PayloadArgumentResolver implements HandlerMethodArgumentResolver {
private final MessageConverter converter; private final MessageConverter converter;
@Nullable
private final Validator validator; private final Validator validator;
private final boolean useDefaultResolution; private final boolean useDefaultResolution;
@ -76,7 +77,7 @@ public class PayloadArgumentResolver implements HandlerMethodArgumentResolver {
* @param messageConverter the MessageConverter to use (required) * @param messageConverter the MessageConverter to use (required)
* @param validator the Validator to use (optional) * @param validator the Validator to use (optional)
*/ */
public PayloadArgumentResolver(MessageConverter messageConverter, Validator validator) { public PayloadArgumentResolver(MessageConverter messageConverter, @Nullable Validator validator) {
this(messageConverter, validator, true); this(messageConverter, validator, true);
} }
@ -89,7 +90,7 @@ public class PayloadArgumentResolver implements HandlerMethodArgumentResolver {
* all parameters; if "false" then only arguments with the {@code @Payload} * all parameters; if "false" then only arguments with the {@code @Payload}
* annotation are supported. * annotation are supported.
*/ */
public PayloadArgumentResolver(MessageConverter messageConverter, Validator validator, public PayloadArgumentResolver(MessageConverter messageConverter, @Nullable Validator validator,
boolean useDefaultResolution) { boolean useDefaultResolution) {
Assert.notNull(messageConverter, "MessageConverter must not be null"); Assert.notNull(messageConverter, "MessageConverter must not be null");

View File

@ -1,4 +1,9 @@
/** /**
* Support classes for working with annotated message-handling methods. * Support classes for working with annotated message-handling methods.
*/ */
@NonNullApi
@NonNullFields
package org.springframework.messaging.handler.annotation.support; package org.springframework.messaging.handler.annotation.support;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;