Skip to content

Commit bfd20da

Browse files
committed
Polishing (backported from 5.2.x)
1 parent ce4001d commit bfd20da

File tree

11 files changed

+77
-70
lines changed

11 files changed

+77
-70
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@
9494
* operating on pre-resolved bean definition metadata objects.
9595
*
9696
* <p>Note that readers for specific bean definition formats are typically
97-
* implemented separately rather than as bean factory subclasses:
98-
* see for example {@link PropertiesBeanDefinitionReader} and
97+
* implemented separately rather than as bean factory subclasses: see for example
9998
* {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}.
10099
*
101100
* <p>For an alternative implementation of the
@@ -182,7 +181,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
182181
private volatile String[] frozenBeanDefinitionNames;
183182

184183
/** Whether bean definition metadata may be cached for all beans. */
185-
private volatile boolean configurationFrozen = false;
184+
private volatile boolean configurationFrozen;
186185

187186

188187
/**
@@ -354,12 +353,11 @@ public <T> T getBean(Class<T> requiredType, @Nullable Object... args) throws Bea
354353
}
355354

356355
@Override
357-
public <T> ObjectProvider<T> getBeanProvider(Class<T> requiredType) throws BeansException {
356+
public <T> ObjectProvider<T> getBeanProvider(Class<T> requiredType) {
358357
Assert.notNull(requiredType, "Required type must not be null");
359358
return getBeanProvider(ResolvableType.forRawClass(requiredType));
360359
}
361360

362-
@SuppressWarnings("unchecked")
363361
@Override
364362
public <T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType) {
365363
return new BeanObjectProvider<T>() {
@@ -389,15 +387,20 @@ public T getIfAvailable() throws BeansException {
389387
public T getIfUnique() throws BeansException {
390388
return resolveBean(requiredType, null, true);
391389
}
390+
@SuppressWarnings("unchecked")
392391
@Override
393392
public Stream<T> stream() {
394393
return Arrays.stream(getBeanNamesForTypedStream(requiredType))
395394
.map(name -> (T) getBean(name))
396395
.filter(bean -> !(bean instanceof NullBean));
397396
}
397+
@SuppressWarnings("unchecked")
398398
@Override
399399
public Stream<T> orderedStream() {
400400
String[] beanNames = getBeanNamesForTypedStream(requiredType);
401+
if (beanNames.length == 0) {
402+
return Stream.empty();
403+
}
401404
Map<String, T> matchingBeans = new LinkedHashMap<>(beanNames.length);
402405
for (String beanName : beanNames) {
403406
Object beanInstance = getBean(beanName);
@@ -1352,9 +1355,11 @@ else if (Collection.class.isAssignableFrom(type) && type.isInterface()) {
13521355
TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
13531356
Object result = converter.convertIfNecessary(matchingBeans.values(), type);
13541357
if (result instanceof List) {
1355-
Comparator<Object> comparator = adaptDependencyComparator(matchingBeans);
1356-
if (comparator != null) {
1357-
((List<?>) result).sort(comparator);
1358+
if (((List<?>) result).size() > 1) {
1359+
Comparator<Object> comparator = adaptDependencyComparator(matchingBeans);
1360+
if (comparator != null) {
1361+
((List<?>) result).sort(comparator);
1362+
}
13581363
}
13591364
}
13601365
return result;

spring-context/src/main/java/org/springframework/cache/support/AbstractValueAdaptingCache.java

Lines changed: 2 additions & 4 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.
@@ -55,8 +55,7 @@ public final boolean isAllowNullValues() {
5555
@Override
5656
@Nullable
5757
public ValueWrapper get(Object key) {
58-
Object value = lookup(key);
59-
return toValueWrapper(value);
58+
return toValueWrapper(lookup(key));
6059
}
6160

6261
@Override
@@ -123,5 +122,4 @@ protected Cache.ValueWrapper toValueWrapper(@Nullable Object storeValue) {
123122
return (storeValue != null ? new SimpleValueWrapper(fromStoreValue(storeValue)) : null);
124123
}
125124

126-
127125
}

spring-core/src/main/java/org/springframework/core/io/FileUrlResource.java

Lines changed: 2 additions & 2 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.
@@ -52,7 +52,7 @@ public class FileUrlResource extends UrlResource implements WritableResource {
5252
/**
5353
* Create a new {@code FileUrlResource} based on the given URL object.
5454
* <p>Note that this does not enforce "file" as URL protocol. If a protocol
55-
* is known to be resolvable to a file,
55+
* is known to be resolvable to a file, it is acceptable for this purpose.
5656
* @param url a URL
5757
* @see ResourceUtils#isFileURL(URL)
5858
* @see #getFile()

spring-tx/src/main/java/org/springframework/dao/support/PersistenceExceptionTranslationInterceptor.java

Lines changed: 7 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.
@@ -146,7 +146,8 @@ public Object invoke(MethodInvocation mi) throws Throwable {
146146
else {
147147
PersistenceExceptionTranslator translator = this.persistenceExceptionTranslator;
148148
if (translator == null) {
149-
Assert.state(this.beanFactory != null, "No PersistenceExceptionTranslator set");
149+
Assert.state(this.beanFactory != null,
150+
"Cannot use PersistenceExceptionTranslator autodetection without ListableBeanFactory");
150151
translator = detectPersistenceExceptionTranslators(this.beanFactory);
151152
this.persistenceExceptionTranslator = translator;
152153
}
@@ -157,16 +158,15 @@ public Object invoke(MethodInvocation mi) throws Throwable {
157158

158159
/**
159160
* Detect all PersistenceExceptionTranslators in the given BeanFactory.
160-
* @param beanFactory the ListableBeanFactory to obtaining all
161-
* PersistenceExceptionTranslators from
161+
* @param bf the ListableBeanFactory to obtain PersistenceExceptionTranslators from
162162
* @return a chained PersistenceExceptionTranslator, combining all
163-
* PersistenceExceptionTranslators found in the factory
163+
* PersistenceExceptionTranslators found in the given bean factory
164164
* @see ChainedPersistenceExceptionTranslator
165165
*/
166-
protected PersistenceExceptionTranslator detectPersistenceExceptionTranslators(ListableBeanFactory beanFactory) {
166+
protected PersistenceExceptionTranslator detectPersistenceExceptionTranslators(ListableBeanFactory bf) {
167167
// Find all translators, being careful not to activate FactoryBeans.
168168
Map<String, PersistenceExceptionTranslator> pets = BeanFactoryUtils.beansOfTypeIncludingAncestors(
169-
beanFactory, PersistenceExceptionTranslator.class, false, false);
169+
bf, PersistenceExceptionTranslator.class, false, false);
170170
ChainedPersistenceExceptionTranslator cpet = new ChainedPersistenceExceptionTranslator();
171171
for (PersistenceExceptionTranslator pet : pets.values()) {
172172
cpet.addDelegate(pet);

spring-web/src/main/java/org/springframework/http/MediaType.java

Lines changed: 4 additions & 7 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.
@@ -85,7 +85,6 @@ public class MediaType extends MimeType implements Serializable {
8585

8686
/**
8787
* Public constant media type for {@code application/json}.
88-
* @see #APPLICATION_JSON_UTF8
8988
*/
9089
public static final MediaType APPLICATION_JSON;
9190

@@ -97,7 +96,6 @@ public class MediaType extends MimeType implements Serializable {
9796

9897
/**
9998
* Public constant media type for {@code application/json;charset=UTF-8}.
100-
*
10199
* <p>This {@link MediaType#APPLICATION_JSON} variant should be used to set JSON
102100
* content type because while
103101
* <a href="https://tools.ietf.org/html/rfc7159#section-11">RFC7159</a>
@@ -108,7 +106,6 @@ public class MediaType extends MimeType implements Serializable {
108106

109107
/**
110108
* A String equivalent of {@link MediaType#APPLICATION_JSON_UTF8}.
111-
*
112109
* <p>This {@link MediaType#APPLICATION_JSON_VALUE} variant should be used to set JSON
113110
* content type because while
114111
* <a href="https://tools.ietf.org/html/rfc7159#section-11">RFC7159</a>
@@ -407,7 +404,7 @@ public MediaType(MediaType other, Charset charset) {
407404

408405
/**
409406
* Copy-constructor that copies the type and subtype of the given {@code MediaType},
410-
* and allows for different parameter.
407+
* and allows for different parameters.
411408
* @param other the other media type
412409
* @param parameters the parameters, may be {@code null}
413410
* @throws IllegalArgumentException if any of the parameters contain illegal characters
@@ -454,7 +451,7 @@ public double getQualityValue() {
454451
* <p>For instance, {@code text/*} includes {@code text/plain} and {@code text/html},
455452
* and {@code application/*+xml} includes {@code application/soap+xml}, etc.
456453
* This method is <b>not</b> symmetric.
457-
* <p>Simply calls {@link #includes(MimeType)} but declared with a
454+
* <p>Simply calls {@link MimeType#includes(MimeType)} but declared with a
458455
* {@code MediaType} parameter for binary backwards compatibility.
459456
* @param other the reference media type with which to compare
460457
* @return {@code true} if this media type includes the given media type;
@@ -469,7 +466,7 @@ public boolean includes(@Nullable MediaType other) {
469466
* <p>For instance, {@code text/*} is compatible with {@code text/plain},
470467
* {@code text/html}, and vice versa. In effect, this method is similar to
471468
* {@link #includes}, except that it <b>is</b> symmetric.
472-
* <p>Simply calls {@link #isCompatibleWith(MimeType)} but declared with a
469+
* <p>Simply calls {@link MimeType#isCompatibleWith(MimeType)} but declared with a
473470
* {@code MediaType} parameter for binary backwards compatibility.
474471
* @param other the reference media type with which to compare
475472
* @return {@code true} if this media type is compatible with the given media type;

spring-web/src/main/java/org/springframework/web/bind/support/WebExchangeDataBinder.java

Lines changed: 2 additions & 2 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.
@@ -64,7 +64,7 @@ public WebExchangeDataBinder(@Nullable Object target, String objectName) {
6464

6565
/**
6666
* Bind query params, form data, and or multipart form data to the binder target.
67-
* @param exchange the current exchange.
67+
* @param exchange the current exchange
6868
* @return a {@code Mono<Void>} when binding is complete
6969
*/
7070
public Mono<Void> bind(ServerWebExchange exchange) {

spring-web/src/main/java/org/springframework/web/bind/support/WebRequestDataBinder.java

Lines changed: 6 additions & 6 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.
@@ -101,20 +101,20 @@ public WebRequestDataBinder(@Nullable Object target, String objectName) {
101101
* <p>The type of the target property for a multipart file can be Part, MultipartFile,
102102
* byte[], or String. The latter two receive the contents of the uploaded file;
103103
* all metadata like original file name, content type, etc are lost in those cases.
104-
* @param request request with parameters to bind (can be multipart)
104+
* @param request the request with parameters to bind (can be multipart)
105105
* @see org.springframework.web.multipart.MultipartRequest
106106
* @see org.springframework.web.multipart.MultipartFile
107107
* @see javax.servlet.http.Part
108108
* @see #bind(org.springframework.beans.PropertyValues)
109109
*/
110110
public void bind(WebRequest request) {
111111
MutablePropertyValues mpvs = new MutablePropertyValues(request.getParameterMap());
112-
if (isMultipartRequest(request) && request instanceof NativeWebRequest) {
112+
if (request instanceof NativeWebRequest) {
113113
MultipartRequest multipartRequest = ((NativeWebRequest) request).getNativeRequest(MultipartRequest.class);
114114
if (multipartRequest != null) {
115115
bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
116116
}
117-
else {
117+
else if (isMultipartRequest(request)) {
118118
HttpServletRequest servletRequest = ((NativeWebRequest) request).getNativeRequest(HttpServletRequest.class);
119119
if (servletRequest != null) {
120120
bindParts(servletRequest, mpvs);
@@ -126,11 +126,11 @@ public void bind(WebRequest request) {
126126

127127
/**
128128
* Check if the request is a multipart request (by checking its Content-Type header).
129-
* @param request request with parameters to bind
129+
* @param request the request with parameters to bind
130130
*/
131131
private boolean isMultipartRequest(WebRequest request) {
132132
String contentType = request.getHeader("Content-Type");
133-
return (contentType != null && StringUtils.startsWithIgnoreCase(contentType, "multipart"));
133+
return StringUtils.startsWithIgnoreCase(contentType, "multipart/");
134134
}
135135

136136
private void bindParts(HttpServletRequest request, MutablePropertyValues mpvs) {

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

Lines changed: 1 addition & 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.
@@ -85,7 +85,6 @@ protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
8585
}
8686

8787
@Override
88-
@SuppressWarnings("unchecked")
8988
protected Object resolveNamedValue(String name, MethodParameter parameter, ServerWebExchange exchange) {
9089
String attributeName = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
9190
return exchange.getAttributeOrDefault(attributeName, Collections.emptyMap()).get(name);
@@ -97,7 +96,6 @@ protected void handleMissingValue(String name, MethodParameter parameter) {
9796
}
9897

9998
@Override
100-
@SuppressWarnings("unchecked")
10199
protected void handleResolvedValue(
102100
@Nullable Object arg, String name, MethodParameter parameter, Model model, ServerWebExchange exchange) {
103101

spring-webmvc/src/main/java/org/springframework/web/servlet/HandlerExecutionChain.java

Lines changed: 13 additions & 4 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.
@@ -88,10 +88,16 @@ public Object getHandler() {
8888
return this.handler;
8989
}
9090

91+
/**
92+
* Add the given interceptor to the end of this chain.
93+
*/
9194
public void addInterceptor(HandlerInterceptor interceptor) {
9295
initInterceptorList().add(interceptor);
9396
}
9497

98+
/**
99+
* Add the given interceptors to the end of this chain.
100+
*/
95101
public void addInterceptors(HandlerInterceptor... interceptors) {
96102
if (!ObjectUtils.isEmpty(interceptors)) {
97103
CollectionUtils.mergeArrayIntoCollection(interceptors, initInterceptorList());
@@ -188,13 +194,16 @@ void applyAfterConcurrentHandlingStarted(HttpServletRequest request, HttpServlet
188194
HandlerInterceptor[] interceptors = getInterceptors();
189195
if (!ObjectUtils.isEmpty(interceptors)) {
190196
for (int i = interceptors.length - 1; i >= 0; i--) {
191-
if (interceptors[i] instanceof AsyncHandlerInterceptor) {
197+
HandlerInterceptor interceptor = interceptors[i];
198+
if (interceptor instanceof AsyncHandlerInterceptor) {
192199
try {
193-
AsyncHandlerInterceptor asyncInterceptor = (AsyncHandlerInterceptor) interceptors[i];
200+
AsyncHandlerInterceptor asyncInterceptor = (AsyncHandlerInterceptor) interceptor;
194201
asyncInterceptor.afterConcurrentHandlingStarted(request, response, this.handler);
195202
}
196203
catch (Throwable ex) {
197-
logger.error("Interceptor [" + interceptors[i] + "] failed in afterConcurrentHandlingStarted", ex);
204+
if (logger.isErrorEnabled()) {
205+
logger.error("Interceptor [" + interceptor + "] failed in afterConcurrentHandlingStarted", ex);
206+
}
198207
}
199208
}
200209
}

0 commit comments

Comments
 (0)