Skip to content

Commit a29929d

Browse files
committed
Polishing
1 parent f355b17 commit a29929d

File tree

8 files changed

+92
-67
lines changed

8 files changed

+92
-67
lines changed

spring-core/src/main/java/org/springframework/core/OrderComparator.java

Lines changed: 7 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-2020 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.
@@ -27,6 +27,10 @@
2727
* {@link Comparator} implementation for {@link Ordered} objects, sorting
2828
* by order value ascending, respectively by priority descending.
2929
*
30+
* <h3>{@code PriorityOrdered} Objects</h3>
31+
* <p>{@link PriorityOrdered} objects will be sorted with higher priority than
32+
* <em>plain</em> {@code Ordered} objects.
33+
*
3034
* <h3>Same Order Objects</h3>
3135
* <p>Objects that have the same order value will be sorted with arbitrary
3236
* ordering with respect to other objects with the same order value.
@@ -41,6 +45,7 @@
4145
* @author Sam Brannen
4246
* @since 07.04.2003
4347
* @see Ordered
48+
* @see PriorityOrdered
4449
* @see org.springframework.core.annotation.AnnotationAwareOrderComparator
4550
* @see java.util.List#sort(java.util.Comparator)
4651
* @see java.util.Arrays#sort(Object[], java.util.Comparator)
@@ -96,8 +101,7 @@ private int getOrder(@Nullable Object obj, @Nullable OrderSourceProvider sourceP
96101
Object orderSource = sourceProvider.getOrderSource(obj);
97102
if (orderSource != null) {
98103
if (orderSource.getClass().isArray()) {
99-
Object[] sources = ObjectUtils.toObjectArray(orderSource);
100-
for (Object source : sources) {
104+
for (Object source : ObjectUtils.toObjectArray(orderSource)) {
101105
order = findOrder(source);
102106
if (order != null) {
103107
break;

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

Lines changed: 4 additions & 3 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-2020 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.
@@ -149,8 +149,9 @@ private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValu
149149
if (info.name.isEmpty()) {
150150
name = parameter.getParameterName();
151151
if (name == null) {
152-
throw new IllegalArgumentException("Name for argument type [" + parameter.getParameterType().getName() +
153-
"] not available, and parameter name information not found in class file either.");
152+
throw new IllegalArgumentException(
153+
"Name for argument of type [" + parameter.getNestedParameterType().getName() +
154+
"] not specified, and parameter name information not found in class file either.");
154155
}
155156
}
156157
String defaultValue = (ValueConstants.DEFAULT_NONE.equals(info.defaultValue) ? null : info.defaultValue);

spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java

Lines changed: 6 additions & 6 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-2020 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.
@@ -78,6 +78,7 @@ public AbstractNamedValueMethodArgumentResolver() {
7878
}
7979

8080
/**
81+
* Create a new {@link AbstractNamedValueMethodArgumentResolver} instance.
8182
* @param beanFactory a bean factory to use for resolving ${...} placeholder
8283
* and #{...} SpEL expressions in default values, or {@code null} if default
8384
* values are not expected to contain expressions
@@ -129,7 +130,6 @@ else if ("".equals(arg) && namedValueInfo.defaultValue != null) {
129130
catch (TypeMismatchException ex) {
130131
throw new MethodArgumentTypeMismatchException(arg, ex.getRequiredType(),
131132
namedValueInfo.name, parameter, ex.getCause());
132-
133133
}
134134
}
135135

@@ -168,8 +168,8 @@ private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValu
168168
name = parameter.getParameterName();
169169
if (name == null) {
170170
throw new IllegalArgumentException(
171-
"Name for argument type [" + parameter.getNestedParameterType().getName() +
172-
"] not available, and parameter name information not found in class file either.");
171+
"Name for argument of type [" + parameter.getNestedParameterType().getName() +
172+
"] not specified, and parameter name information not found in class file either.");
173173
}
174174
}
175175
String defaultValue = (ValueConstants.DEFAULT_NONE.equals(info.defaultValue) ? null : info.defaultValue);
@@ -182,12 +182,12 @@ private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValu
182182
*/
183183
@Nullable
184184
private Object resolveStringValue(String value) {
185-
if (this.configurableBeanFactory == null) {
185+
if (this.configurableBeanFactory == null || this.expressionContext == null) {
186186
return value;
187187
}
188188
String placeholdersResolved = this.configurableBeanFactory.resolveEmbeddedValue(value);
189189
BeanExpressionResolver exprResolver = this.configurableBeanFactory.getBeanExpressionResolver();
190-
if (exprResolver == null || this.expressionContext == null) {
190+
if (exprResolver == null) {
191191
return value;
192192
}
193193
return exprResolver.evaluate(placeholdersResolved, this.expressionContext);

spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java

Lines changed: 16 additions & 17 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-2020 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.
@@ -48,13 +48,13 @@
4848
*/
4949
public class InvocableHandlerMethod extends HandlerMethod {
5050

51-
@Nullable
52-
private WebDataBinderFactory dataBinderFactory;
53-
54-
private HandlerMethodArgumentResolverComposite argumentResolvers = new HandlerMethodArgumentResolverComposite();
51+
private HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite();
5552

5653
private ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
5754

55+
@Nullable
56+
private WebDataBinderFactory dataBinderFactory;
57+
5858

5959
/**
6060
* Create an instance from a {@code HandlerMethod}.
@@ -84,20 +84,11 @@ public InvocableHandlerMethod(Object bean, String methodName, Class<?>... parame
8484
}
8585

8686

87-
/**
88-
* Set the {@link WebDataBinderFactory} to be passed to argument resolvers allowing them to create
89-
* a {@link WebDataBinder} for data binding and type conversion purposes.
90-
* @param dataBinderFactory the data binder factory.
91-
*/
92-
public void setDataBinderFactory(WebDataBinderFactory dataBinderFactory) {
93-
this.dataBinderFactory = dataBinderFactory;
94-
}
95-
9687
/**
9788
* Set {@link HandlerMethodArgumentResolver}s to use to use for resolving method argument values.
9889
*/
9990
public void setHandlerMethodArgumentResolvers(HandlerMethodArgumentResolverComposite argumentResolvers) {
100-
this.argumentResolvers = argumentResolvers;
91+
this.resolvers = argumentResolvers;
10192
}
10293

10394
/**
@@ -109,6 +100,14 @@ public void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDisc
109100
this.parameterNameDiscoverer = parameterNameDiscoverer;
110101
}
111102

103+
/**
104+
* Set the {@link WebDataBinderFactory} to be passed to argument resolvers allowing them
105+
* to create a {@link WebDataBinder} for data binding and type conversion purposes.
106+
*/
107+
public void setDataBinderFactory(WebDataBinderFactory dataBinderFactory) {
108+
this.dataBinderFactory = dataBinderFactory;
109+
}
110+
112111

113112
/**
114113
* Invoke the method after resolving its argument values in the context of the given request.
@@ -156,9 +155,9 @@ private Object[] getMethodArgumentValues(NativeWebRequest request, @Nullable Mod
156155
if (args[i] != null) {
157156
continue;
158157
}
159-
if (this.argumentResolvers.supportsParameter(parameter)) {
158+
if (this.resolvers.supportsParameter(parameter)) {
160159
try {
161-
args[i] = this.argumentResolvers.resolveArgument(
160+
args[i] = this.resolvers.resolveArgument(
162161
parameter, mavContainer, request, this.dataBinderFactory);
163162
continue;
164163
}

spring-web/src/main/java/org/springframework/web/multipart/support/MissingServletRequestPartException.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2020 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,7 @@
2626
*
2727
* <p>This may be because the request is not a multipart/form-data request,
2828
* because the part is not present in the request, or because the web
29-
* application is not configured correctly for processing multipart requests,
29+
* application is not configured correctly for processing multipart requests,
3030
* e.g. no {@link MultipartResolver}.
3131
*
3232
* @author Rossen Stoyanchev
@@ -35,17 +35,24 @@
3535
@SuppressWarnings("serial")
3636
public class MissingServletRequestPartException extends ServletException {
3737

38-
private final String partName;
38+
private final String requestPartName;
3939

4040

41-
public MissingServletRequestPartException(String partName) {
42-
super("Required request part '" + partName + "' is not present");
43-
this.partName = partName;
41+
/**
42+
* Constructor for MissingServletRequestPartException.
43+
* @param requestPartName the name of the missing part of the multipart request
44+
*/
45+
public MissingServletRequestPartException(String requestPartName) {
46+
super("Required request part '" + requestPartName + "' is not present");
47+
this.requestPartName = requestPartName;
4448
}
4549

4650

51+
/**
52+
* Return the name of the offending part of the multipart request.
53+
*/
4754
public String getRequestPartName() {
48-
return this.partName;
55+
return this.requestPartName;
4956
}
5057

5158
}

spring-web/src/main/java/org/springframework/web/multipart/support/RequestPartServletServerHttpRequest.java

Lines changed: 29 additions & 22 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-2020 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.
@@ -21,6 +21,7 @@
2121
import java.io.InputStream;
2222
import java.nio.charset.Charset;
2323
import javax.servlet.http.HttpServletRequest;
24+
import javax.servlet.http.Part;
2425

2526
import org.springframework.http.HttpHeaders;
2627
import org.springframework.http.MediaType;
@@ -45,59 +46,65 @@ public class RequestPartServletServerHttpRequest extends ServletServerHttpReques
4546

4647
private final MultipartHttpServletRequest multipartRequest;
4748

48-
private final String partName;
49+
private final String requestPartName;
4950

50-
private final HttpHeaders headers;
51+
private final HttpHeaders multipartHeaders;
5152

5253

5354
/**
5455
* Create a new {@code RequestPartServletServerHttpRequest} instance.
5556
* @param request the current servlet request
56-
* @param partName the name of the part to adapt to the {@link ServerHttpRequest} contract
57+
* @param requestPartName the name of the part to adapt to the {@link ServerHttpRequest} contract
5758
* @throws MissingServletRequestPartException if the request part cannot be found
5859
* @throws MultipartException if MultipartHttpServletRequest cannot be initialized
5960
*/
60-
public RequestPartServletServerHttpRequest(HttpServletRequest request, String partName)
61+
public RequestPartServletServerHttpRequest(HttpServletRequest request, String requestPartName)
6162
throws MissingServletRequestPartException {
6263

6364
super(request);
6465

6566
this.multipartRequest = MultipartResolutionDelegate.asMultipartHttpServletRequest(request);
66-
this.partName = partName;
67+
this.requestPartName = requestPartName;
6768

68-
HttpHeaders headers = this.multipartRequest.getMultipartHeaders(this.partName);
69-
if (headers == null) {
70-
throw new MissingServletRequestPartException(partName);
69+
HttpHeaders multipartHeaders = this.multipartRequest.getMultipartHeaders(requestPartName);
70+
if (multipartHeaders == null) {
71+
throw new MissingServletRequestPartException(requestPartName);
7172
}
72-
this.headers = headers;
73+
this.multipartHeaders = multipartHeaders;
7374
}
7475

7576

7677
@Override
7778
public HttpHeaders getHeaders() {
78-
return this.headers;
79+
return this.multipartHeaders;
7980
}
8081

8182
@Override
8283
public InputStream getBody() throws IOException {
84+
// Prefer Servlet Part resolution to cover file as well as parameter streams
8385
if (this.multipartRequest instanceof StandardMultipartHttpServletRequest) {
8486
try {
85-
return this.multipartRequest.getPart(this.partName).getInputStream();
87+
Part part = this.multipartRequest.getPart(this.requestPartName);
88+
if (part != null) {
89+
return part.getInputStream();
90+
}
8691
}
8792
catch (Exception ex) {
88-
throw new MultipartException("Could not parse multipart servlet request", ex);
93+
throw new MultipartException("Failed to retrieve request part '" + this.requestPartName + "'", ex);
8994
}
9095
}
91-
else {
92-
MultipartFile file = this.multipartRequest.getFile(this.partName);
93-
if (file != null) {
94-
return file.getInputStream();
95-
}
96-
else {
97-
String paramValue = this.multipartRequest.getParameter(this.partName);
98-
return new ByteArrayInputStream(paramValue.getBytes(determineCharset()));
99-
}
96+
97+
// Spring-style distinction between MultipartFile and String parameters
98+
MultipartFile file = this.multipartRequest.getFile(this.requestPartName);
99+
if (file != null) {
100+
return file.getInputStream();
100101
}
102+
String paramValue = this.multipartRequest.getParameter(this.requestPartName);
103+
if (paramValue != null) {
104+
return new ByteArrayInputStream(paramValue.getBytes(determineCharset()));
105+
}
106+
107+
throw new IllegalStateException("No body available for request part '" + this.requestPartName + "'");
101108
}
102109

103110
private Charset determineCharset() {

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -62,17 +62,23 @@ public class InvocableHandlerMethod extends HandlerMethod {
6262
private static final Object NO_ARG_VALUE = new Object();
6363

6464

65-
private List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
65+
private final List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
6666

6767
private ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
6868

6969
private ReactiveAdapterRegistry reactiveAdapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
7070

7171

72+
/**
73+
* Create an instance from a {@code HandlerMethod}.
74+
*/
7275
public InvocableHandlerMethod(HandlerMethod handlerMethod) {
7376
super(handlerMethod);
7477
}
7578

79+
/**
80+
* Create an instance from a bean instance and a method.
81+
*/
7682
public InvocableHandlerMethod(Object bean, Method method) {
7783
super(bean, method);
7884
}

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractNamedValueArgumentResolver.java

Lines changed: 8 additions & 7 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-2020 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.
@@ -70,14 +70,15 @@ public abstract class AbstractNamedValueArgumentResolver extends HandlerMethodAr
7070

7171

7272
/**
73-
* @param factory a bean factory to use for resolving ${...} placeholder
74-
* and #{...} SpEL expressions in default values, or {@code null} if default
73+
* Create a new {@link AbstractNamedValueArgumentResolver} instance.
74+
* @param factory a bean factory to use for resolving {@code ${...}} placeholder
75+
* and {@code #{...}} SpEL expressions in default values, or {@code null} if default
7576
* values are not expected to contain expressions
7677
* @param registry for checking reactive type wrappers
7778
*/
7879
public AbstractNamedValueArgumentResolver(@Nullable ConfigurableBeanFactory factory,
7980
ReactiveAdapterRegistry registry) {
80-
81+
8182
super(registry);
8283
this.configurableBeanFactory = factory;
8384
this.expressionContext = (factory != null ? new BeanExpressionContext(factory, null) : null);
@@ -143,9 +144,9 @@ private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValu
143144
if (info.name.isEmpty()) {
144145
name = parameter.getParameterName();
145146
if (name == null) {
146-
String type = parameter.getNestedParameterType().getName();
147-
throw new IllegalArgumentException("Name for argument type [" + type + "] not " +
148-
"available, and parameter name information not found in class file either.");
147+
throw new IllegalArgumentException(
148+
"Name for argument of type [" + parameter.getNestedParameterType().getName() +
149+
"] not specified, and parameter name information not found in class file either.");
149150
}
150151
}
151152
String defaultValue = (ValueConstants.DEFAULT_NONE.equals(info.defaultValue) ? null : info.defaultValue);

0 commit comments

Comments
 (0)