Skip to content

Commit 5b3b0b1

Browse files
committed
Polish
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.
1 parent 567c559 commit 5b3b0b1

File tree

7 files changed

+45
-22
lines changed

7 files changed

+45
-22
lines changed

spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/AbstractNamedValueMethodArgumentResolver.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,6 @@
2626
import org.springframework.core.MethodParameter;
2727
import org.springframework.core.convert.ConversionService;
2828
import org.springframework.core.convert.TypeDescriptor;
29-
import org.springframework.core.convert.support.DefaultConversionService;
3029
import org.springframework.lang.Nullable;
3130
import org.springframework.messaging.Message;
3231
import org.springframework.messaging.handler.annotation.ValueConstants;
@@ -61,25 +60,27 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
6160

6261
private final ConversionService conversionService;
6362

63+
@Nullable
6464
private final ConfigurableBeanFactory configurableBeanFactory;
6565

66+
@Nullable
6667
private final BeanExpressionContext expressionContext;
6768

6869
private final Map<MethodParameter, NamedValueInfo> namedValueInfoCache = new ConcurrentHashMap<>(256);
6970

7071

7172
/**
7273
* Constructor with a {@link ConversionService} and a {@link BeanFactory}.
73-
* @param cs conversion service for converting values to match the
74+
* @param conversionService conversion service for converting values to match the
7475
* target method parameter type
7576
* @param beanFactory a bean factory to use for resolving {@code ${...}} placeholder
7677
* and {@code #{...}} SpEL expressions in default values, or {@code null} if default
7778
* values are not expected to contain expressions
7879
*/
79-
protected AbstractNamedValueMethodArgumentResolver(ConversionService cs,
80+
protected AbstractNamedValueMethodArgumentResolver(ConversionService conversionService,
8081
@Nullable ConfigurableBeanFactory beanFactory) {
8182

82-
this.conversionService = (cs != null ? cs : DefaultConversionService.getSharedInstance());
83+
this.conversionService = conversionService;
8384
this.configurableBeanFactory = beanFactory;
8485
this.expressionContext = (beanFactory != null ? new BeanExpressionContext(beanFactory, null) : null);
8586
}
@@ -161,8 +162,9 @@ private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValu
161162
* Resolve the given annotation-specified value,
162163
* potentially containing placeholders and expressions.
163164
*/
165+
@Nullable
164166
private Object resolveStringValue(String value) {
165-
if (this.configurableBeanFactory == null) {
167+
if (this.configurableBeanFactory == null || this.expressionContext == null) {
166168
return value;
167169
}
168170
String placeholdersResolved = this.configurableBeanFactory.resolveEmbeddedValue(value);
@@ -199,6 +201,7 @@ protected abstract Object resolveArgumentInternal(MethodParameter parameter, Mes
199201
* A {@code null} results in a {@code false} value for {@code boolean}s or an
200202
* exception for other primitives.
201203
*/
204+
@Nullable
202205
private Object handleNullValue(String name, @Nullable Object value, Class<?> paramType) {
203206
if (value == null) {
204207
if (Boolean.TYPE.equals(paramType)) {
@@ -221,7 +224,8 @@ else if (paramType.isPrimitive()) {
221224
* @param parameter the argument parameter type
222225
* @param message the message
223226
*/
224-
protected void handleResolvedValue(Object arg, String name, MethodParameter parameter, Message<?> message) {
227+
protected void handleResolvedValue(
228+
@Nullable Object arg, String name, MethodParameter parameter, Message<?> message) {
225229
}
226230

227231

@@ -235,9 +239,10 @@ protected static class NamedValueInfo {
235239

236240
private final boolean required;
237241

242+
@Nullable
238243
private final String defaultValue;
239244

240-
protected NamedValueInfo(String name, boolean required, String defaultValue) {
245+
protected NamedValueInfo(String name, boolean required, @Nullable String defaultValue) {
241246
this.name = name;
242247
this.required = required;
243248
this.defaultValue = defaultValue;

spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/DefaultMessageHandlerMethodFactory.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,11 +26,13 @@
2626
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
2727
import org.springframework.core.convert.ConversionService;
2828
import org.springframework.format.support.DefaultFormattingConversionService;
29+
import org.springframework.lang.Nullable;
2930
import org.springframework.messaging.converter.GenericMessageConverter;
3031
import org.springframework.messaging.converter.MessageConverter;
3132
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
3233
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite;
3334
import org.springframework.messaging.handler.invocation.InvocableHandlerMethod;
35+
import org.springframework.util.Assert;
3436
import org.springframework.validation.Validator;
3537

3638
/**
@@ -60,15 +62,19 @@ public class DefaultMessageHandlerMethodFactory
6062

6163
private ConversionService conversionService = new DefaultFormattingConversionService();
6264

65+
@Nullable
6366
private MessageConverter messageConverter;
6467

68+
@Nullable
6569
private Validator validator;
6670

71+
@Nullable
6772
private List<HandlerMethodArgumentResolver> customArgumentResolvers;
6873

6974
private final HandlerMethodArgumentResolverComposite argumentResolvers =
7075
new HandlerMethodArgumentResolverComposite();
7176

77+
@Nullable
7278
private BeanFactory beanFactory;
7379

7480

@@ -114,6 +120,7 @@ public void setCustomArgumentResolvers(List<HandlerMethodArgumentResolver> custo
114120
* the ones configured by default. This is an advanced option. For most use cases
115121
* it should be sufficient to use {@link #setCustomArgumentResolvers(java.util.List)}.
116122
*/
123+
@SuppressWarnings("ConstantConditions")
117124
public void setArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
118125
if (argumentResolvers == null) {
119126
this.argumentResolvers.clear();
@@ -151,11 +158,11 @@ public InvocableHandlerMethod createInvocableHandlerMethod(Object bean, Method m
151158

152159
protected List<HandlerMethodArgumentResolver> initArgumentResolvers() {
153160
List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
154-
ConfigurableBeanFactory cbf = (this.beanFactory instanceof ConfigurableBeanFactory ?
161+
ConfigurableBeanFactory beanFactory = (this.beanFactory instanceof ConfigurableBeanFactory ?
155162
(ConfigurableBeanFactory) this.beanFactory : null);
156163

157164
// Annotation-based argument resolution
158-
resolvers.add(new HeaderMethodArgumentResolver(this.conversionService, cbf));
165+
resolvers.add(new HeaderMethodArgumentResolver(this.conversionService, beanFactory));
159166
resolvers.add(new HeadersMethodArgumentResolver());
160167

161168
// Type-based argument resolution
@@ -164,6 +171,8 @@ protected List<HandlerMethodArgumentResolver> initArgumentResolvers() {
164171
if (this.customArgumentResolvers != null) {
165172
resolvers.addAll(this.customArgumentResolvers);
166173
}
174+
175+
Assert.notNull(this.messageConverter, "MessageConverter not configured");
167176
resolvers.add(new PayloadArgumentResolver(this.messageConverter, this.validator));
168177

169178
return resolvers;

spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/DestinationVariableMethodArgumentResolver.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,8 +43,8 @@ public class DestinationVariableMethodArgumentResolver extends AbstractNamedValu
4343
DestinationVariableMethodArgumentResolver.class.getSimpleName() + ".templateVariables";
4444

4545

46-
public DestinationVariableMethodArgumentResolver(ConversionService cs) {
47-
super(cs, null);
46+
public DestinationVariableMethodArgumentResolver(ConversionService conversionService) {
47+
super(conversionService, null);
4848
}
4949

5050

spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolver.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,8 +43,10 @@ public class HeaderMethodArgumentResolver extends AbstractNamedValueMethodArgume
4343
private static final Log logger = LogFactory.getLog(HeaderMethodArgumentResolver.class);
4444

4545

46-
public HeaderMethodArgumentResolver(ConversionService cs, ConfigurableBeanFactory beanFactory) {
47-
super(cs, beanFactory);
46+
public HeaderMethodArgumentResolver(
47+
ConversionService conversionService, @Nullable ConfigurableBeanFactory beanFactory) {
48+
49+
super(conversionService, beanFactory);
4850
}
4951

5052

@@ -94,9 +96,9 @@ private Object getNativeHeaderValue(Message<?> message, String name) {
9496
}
9597

9698
@SuppressWarnings("unchecked")
99+
@Nullable
97100
private Map<String, List<String>> getNativeHeaders(Message<?> message) {
98-
return (Map<String, List<String>>) message.getHeaders().get(
99-
NativeMessageHeaderAccessor.NATIVE_HEADERS);
101+
return (Map<String, List<String>>) message.getHeaders().get(NativeMessageHeaderAccessor.NATIVE_HEADERS);
100102
}
101103

102104
@Override

spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/MessageMethodArgumentResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,6 +43,7 @@
4343
*/
4444
public class MessageMethodArgumentResolver implements HandlerMethodArgumentResolver {
4545

46+
@Nullable
4647
private final MessageConverter converter;
4748

4849

spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/PayloadArgumentResolver.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class PayloadArgumentResolver implements HandlerMethodArgumentResolver {
5555

5656
private final MessageConverter converter;
5757

58+
@Nullable
5859
private final Validator validator;
5960

6061
private final boolean useDefaultResolution;
@@ -76,7 +77,7 @@ public PayloadArgumentResolver(MessageConverter messageConverter) {
7677
* @param messageConverter the MessageConverter to use (required)
7778
* @param validator the Validator to use (optional)
7879
*/
79-
public PayloadArgumentResolver(MessageConverter messageConverter, Validator validator) {
80+
public PayloadArgumentResolver(MessageConverter messageConverter, @Nullable Validator validator) {
8081
this(messageConverter, validator, true);
8182
}
8283

@@ -89,7 +90,7 @@ public PayloadArgumentResolver(MessageConverter messageConverter, Validator vali
8990
* all parameters; if "false" then only arguments with the {@code @Payload}
9091
* annotation are supported.
9192
*/
92-
public PayloadArgumentResolver(MessageConverter messageConverter, Validator validator,
93+
public PayloadArgumentResolver(MessageConverter messageConverter, @Nullable Validator validator,
9394
boolean useDefaultResolution) {
9495

9596
Assert.notNull(messageConverter, "MessageConverter must not be null");
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
/**
22
* Support classes for working with annotated message-handling methods.
33
*/
4+
@NonNullApi
5+
@NonNullFields
46
package org.springframework.messaging.handler.annotation.support;
7+
8+
import org.springframework.lang.NonNullApi;
9+
import org.springframework.lang.NonNullFields;

0 commit comments

Comments
 (0)