Skip to content

Commit f24714f

Browse files
committed
Polishing
1 parent 6d137b5 commit f24714f

File tree

12 files changed

+107
-77
lines changed

12 files changed

+107
-77
lines changed

spring-context/src/main/java/org/springframework/scheduling/concurrent/DefaultManagedTaskScheduler.java

Lines changed: 3 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.
@@ -36,10 +36,11 @@
3636
*
3737
* @author Juergen Hoeller
3838
* @since 4.0
39+
* @see javax.enterprise.concurrent.ManagedScheduledExecutorService
3940
*/
4041
public class DefaultManagedTaskScheduler extends ConcurrentTaskScheduler implements InitializingBean {
4142

42-
private JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate();
43+
private final JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate();
4344

4445
@Nullable
4546
private String jndiName = "java:comp/DefaultManagedScheduledExecutorService";

spring-core/src/main/java/org/springframework/util/MimeType.java

Lines changed: 9 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-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.
@@ -193,7 +193,7 @@ public MimeType(String type, String subtype, @Nullable Map<String, String> param
193193
* @see <a href="https://tools.ietf.org/html/rfc2616#section-2.2">HTTP 1.1, section 2.2</a>
194194
*/
195195
private void checkToken(String token) {
196-
for (int i = 0; i < token.length(); i++ ) {
196+
for (int i = 0; i < token.length(); i++) {
197197
char ch = token.charAt(i);
198198
if (!TOKEN.get(ch)) {
199199
throw new IllegalArgumentException("Invalid token character '" + ch + "' in token \"" + token + "\"");
@@ -206,8 +206,7 @@ protected void checkParameters(String attribute, String value) {
206206
Assert.hasLength(value, "'value' must not be empty");
207207
checkToken(attribute);
208208
if (PARAM_CHARSET.equals(attribute)) {
209-
value = unquote(value);
210-
Charset.forName(value);
209+
Charset.forName(unquote(value));
211210
}
212211
else if (!isQuotedString(value)) {
213212
checkToken(value);
@@ -401,7 +400,7 @@ public boolean equals(Object other) {
401400
/**
402401
* Determine if the parameters in this {@code MimeType} and the supplied
403402
* {@code MimeType} are equal, performing case-insensitive comparisons
404-
* for {@link Charset}s.
403+
* for {@link Charset Charsets}.
405404
* @since 4.2
406405
*/
407406
private boolean parametersAreEqual(MimeType other) {
@@ -542,6 +541,11 @@ private static Map<String, String> addCharsetParameter(Charset charset, Map<Stri
542541
}
543542

544543

544+
/**
545+
* Comparator to sort {@link MimeType MimeTypes} in order of specificity.
546+
*
547+
* @param <T> the type of mime types that may be compared by this comparator
548+
*/
545549
public static class SpecificityComparator<T extends MimeType> implements Comparator<T> {
546550

547551
@Override

spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractMessageConverter.java

Lines changed: 22 additions & 20 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.
@@ -151,21 +151,6 @@ public Class<?> getSerializedPayloadClass() {
151151
}
152152

153153

154-
/**
155-
* Returns the default content type for the payload. Called when
156-
* {@link #toMessage(Object, MessageHeaders)} is invoked without message headers or
157-
* without a content type header.
158-
* <p>By default, this returns the first element of the {@link #getSupportedMimeTypes()
159-
* supportedMimeTypes}, if any. Can be overridden in sub-classes.
160-
* @param payload the payload being converted to message
161-
* @return the content type, or {@code null} if not known
162-
*/
163-
@Nullable
164-
protected MimeType getDefaultContentType(Object payload) {
165-
List<MimeType> mimeTypes = getSupportedMimeTypes();
166-
return (!mimeTypes.isEmpty() ? mimeTypes.get(0) : null);
167-
}
168-
169154
@Override
170155
@Nullable
171156
public final Object fromMessage(Message<?> message, Class<?> targetClass) {
@@ -181,10 +166,6 @@ public final Object fromMessage(Message<?> message, Class<?> targetClass, @Nulla
181166
return convertFromInternal(message, targetClass, conversionHint);
182167
}
183168

184-
protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) {
185-
return (supports(targetClass) && supportsMimeType(message.getHeaders()));
186-
}
187-
188169
@Override
189170
@Nullable
190171
public final Message<?> toMessage(Object payload, @Nullable MessageHeaders headers) {
@@ -224,6 +205,11 @@ public final Message<?> toMessage(Object payload, @Nullable MessageHeaders heade
224205
return builder.build();
225206
}
226207

208+
209+
protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) {
210+
return (supports(targetClass) && supportsMimeType(message.getHeaders()));
211+
}
212+
227213
protected boolean canConvertTo(Object payload, @Nullable MessageHeaders headers) {
228214
return (supports(payload.getClass()) && supportsMimeType(headers));
229215
}
@@ -249,6 +235,22 @@ protected MimeType getMimeType(@Nullable MessageHeaders headers) {
249235
return (headers != null && this.contentTypeResolver != null ? this.contentTypeResolver.resolve(headers) : null);
250236
}
251237

238+
/**
239+
* Return the default content type for the payload. Called when
240+
* {@link #toMessage(Object, MessageHeaders)} is invoked without
241+
* message headers or without a content type header.
242+
* <p>By default, this returns the first element of the
243+
* {@link #getSupportedMimeTypes() supportedMimeTypes}, if any.
244+
* Can be overridden in subclasses.
245+
* @param payload the payload being converted to a message
246+
* @return the content type, or {@code null} if not known
247+
*/
248+
@Nullable
249+
protected MimeType getDefaultContentType(Object payload) {
250+
List<MimeType> mimeTypes = getSupportedMimeTypes();
251+
return (!mimeTypes.isEmpty() ? mimeTypes.get(0) : null);
252+
}
253+
252254

253255
/**
254256
* Whether the given class is supported by this converter.

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/DefaultStompSession.java

Lines changed: 3 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-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.
@@ -684,8 +684,10 @@ public void run() {
684684
if (conn != null) {
685685
conn.send(HEARTBEAT).addCallback(
686686
new ListenableFutureCallback<Void>() {
687+
@Override
687688
public void onSuccess(@Nullable Void result) {
688689
}
690+
@Override
689691
public void onFailure(Throwable ex) {
690692
handleFailure(ex);
691693
}

spring-messaging/src/main/java/org/springframework/messaging/support/NativeMessageHeaderAccessor.java

Lines changed: 46 additions & 14 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.
@@ -53,14 +53,14 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor {
5353

5454

5555
/**
56-
* A protected constructor to create new headers.
56+
* Protected constructor to create a new instance.
5757
*/
5858
protected NativeMessageHeaderAccessor() {
5959
this((Map<String, List<String>>) null);
6060
}
6161

6262
/**
63-
* A protected constructor to create new headers.
63+
* Protected constructor to create an instance with the given native headers.
6464
* @param nativeHeaders native headers to create the message with (may be {@code null})
6565
*/
6666
protected NativeMessageHeaderAccessor(@Nullable Map<String, List<String>> nativeHeaders) {
@@ -70,7 +70,7 @@ protected NativeMessageHeaderAccessor(@Nullable Map<String, List<String>> native
7070
}
7171

7272
/**
73-
* A protected constructor accepting the headers of an existing message to copy.
73+
* Protected constructor that copies headers from another message.
7474
*/
7575
protected NativeMessageHeaderAccessor(@Nullable Message<?> message) {
7676
super(message);
@@ -85,14 +85,18 @@ protected NativeMessageHeaderAccessor(@Nullable Message<?> message) {
8585
}
8686
}
8787

88+
89+
/**
90+
* Subclasses can use this method to access the "native" headers sub-map.
91+
*/
8892
@SuppressWarnings("unchecked")
8993
@Nullable
9094
protected Map<String, List<String>> getNativeHeaders() {
9195
return (Map<String, List<String>>) getHeader(NATIVE_HEADERS);
9296
}
9397

9498
/**
95-
* Return a copy of the native header values or an empty map.
99+
* Return a copy of the native headers sub-map, or an empty map.
96100
*/
97101
public Map<String, List<String>> toNativeHeaderMap() {
98102
Map<String, List<String>> map = getNativeHeaders();
@@ -114,15 +118,17 @@ public void setImmutable() {
114118

115119
/**
116120
* Whether the native header map contains the give header name.
121+
* @param headerName the name of the header
117122
*/
118123
public boolean containsNativeHeader(String headerName) {
119124
Map<String, List<String>> map = getNativeHeaders();
120125
return (map != null && map.containsKey(headerName));
121126
}
122127

123128
/**
124-
* Return all values for the specified native header.
125-
* or {@code null} if none.
129+
* Return all values for the specified native header, if present.
130+
* @param headerName the name of the header
131+
* @return the associated values, or {@code null} if none
126132
*/
127133
@Nullable
128134
public List<String> getNativeHeader(String headerName) {
@@ -131,15 +137,16 @@ public List<String> getNativeHeader(String headerName) {
131137
}
132138

133139
/**
134-
* Return the first value for the specified native header,
135-
* or {@code null} if none.
140+
* Return the first value for the specified native header, if present.
141+
* @param headerName the name of the header
142+
* @return the associated value, or {@code null} if none
136143
*/
137144
@Nullable
138145
public String getFirstNativeHeader(String headerName) {
139146
Map<String, List<String>> map = getNativeHeaders();
140147
if (map != null) {
141148
List<String> values = map.get(headerName);
142-
if (values != null) {
149+
if (!CollectionUtils.isEmpty(values)) {
143150
return values.get(0);
144151
}
145152
}
@@ -148,6 +155,8 @@ public String getFirstNativeHeader(String headerName) {
148155

149156
/**
150157
* Set the specified native header value replacing existing values.
158+
* <p>In order for this to work, the accessor must be {@link #isMutable()
159+
* mutable}. See {@link MessageHeaderAccessor} for details.
151160
*/
152161
public void setNativeHeader(String name, @Nullable String value) {
153162
Assert.state(isMutable(), "Already immutable");
@@ -173,6 +182,10 @@ public void setNativeHeader(String name, @Nullable String value) {
173182

174183
/**
175184
* Add the specified native header value to existing values.
185+
* <p>In order for this to work, the accessor must be {@link #isMutable()
186+
* mutable}. See {@link MessageHeaderAccessor} for details.
187+
* @param name the name of the header
188+
* @param value the header value to set
176189
*/
177190
public void addNativeHeader(String name, @Nullable String value) {
178191
Assert.state(isMutable(), "Already immutable");
@@ -189,30 +202,49 @@ public void addNativeHeader(String name, @Nullable String value) {
189202
setModified(true);
190203
}
191204

205+
/**
206+
* Add the specified native headers to existing values.
207+
* @param headers the headers to set
208+
*/
192209
public void addNativeHeaders(@Nullable MultiValueMap<String, String> headers) {
193210
if (headers == null) {
194211
return;
195212
}
196213
headers.forEach((key, values) -> values.forEach(value -> addNativeHeader(key, value)));
197214
}
198215

216+
/**
217+
* Remove the specified native header value replacing existing values.
218+
* <p>In order for this to work, the accessor must be {@link #isMutable()
219+
* mutable}. See {@link MessageHeaderAccessor} for details.
220+
* @param headerName the name of the header
221+
* @return the associated values, or {@code null} if the header was not present
222+
*/
199223
@Nullable
200-
public List<String> removeNativeHeader(String name) {
224+
public List<String> removeNativeHeader(String headerName) {
201225
Assert.state(isMutable(), "Already immutable");
202226
Map<String, List<String>> nativeHeaders = getNativeHeaders();
203-
if (nativeHeaders == null) {
227+
if (CollectionUtils.isEmpty(nativeHeaders)) {
204228
return null;
205229
}
206-
return nativeHeaders.remove(name);
230+
return nativeHeaders.remove(headerName);
207231
}
208232

233+
234+
/**
235+
* Return the first value for the specified native header,
236+
* or {@code null} if none.
237+
* @param headerName the name of the header
238+
* @param headers the headers map to introspect
239+
* @return the associated value, or {@code null} if none
240+
*/
209241
@SuppressWarnings("unchecked")
210242
@Nullable
211243
public static String getFirstNativeHeader(String headerName, Map<String, Object> headers) {
212244
Map<String, List<String>> map = (Map<String, List<String>>) headers.get(NATIVE_HEADERS);
213245
if (map != null) {
214246
List<String> values = map.get(headerName);
215-
if (values != null) {
247+
if (!CollectionUtils.isEmpty(values)) {
216248
return values.get(0);
217249
}
218250
}

spring-messaging/src/test/java/org/springframework/messaging/converter/MappingJackson2MessageConverterTests.java

Lines changed: 8 additions & 10 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.
@@ -75,13 +75,8 @@ public void mimetypesParametrizedConstructor() {
7575
@Test
7676
public void fromMessage() {
7777
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
78-
String payload = "{" +
79-
"\"bytes\":\"AQI=\"," +
80-
"\"array\":[\"Foo\",\"Bar\"]," +
81-
"\"number\":42," +
82-
"\"string\":\"Foo\"," +
83-
"\"bool\":true," +
84-
"\"fraction\":42.0}";
78+
String payload = "{\"bytes\":\"AQI=\",\"array\":[\"Foo\",\"Bar\"]," +
79+
"\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}";
8580
Message<?> message = MessageBuilder.withPayload(payload.getBytes(StandardCharsets.UTF_8)).build();
8681
MyBean actual = (MyBean) converter.fromMessage(message, MyBean.class);
8782

@@ -245,9 +240,12 @@ public JacksonViewBean jsonViewResponse() {
245240
public void jsonViewPayload(@JsonView(MyJacksonView2.class) JacksonViewBean payload) {
246241
}
247242

248-
void handleList(List<Long> payload) {}
243+
void handleList(List<Long> payload) {
244+
}
245+
246+
void handleMessage(Message<MyBean> message) {
247+
}
249248

250-
void handleMessage(Message<MyBean> message) {}
251249

252250
public static class MyBean {
253251

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

Lines changed: 2 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.
@@ -144,7 +144,6 @@ public String toString() {
144144
long millis = this.maxAge.getSeconds() > 0 ? System.currentTimeMillis() + this.maxAge.toMillis() : 0;
145145
sb.append(HttpHeaders.formatDate(millis));
146146
}
147-
148147
if (this.secure) {
149148
sb.append("; Secure");
150149
}
@@ -160,7 +159,7 @@ public String toString() {
160159
* with a name-value pair and may also include attributes.
161160
* @param name the cookie name
162161
* @param value the cookie value
163-
* @return the created cookie instance
162+
* @return a builder to create the cookie with
164163
*/
165164
public static ResponseCookieBuilder from(final String name, final String value) {
166165

spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Decoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected AbstractJackson2Decoder(ObjectMapper mapper, MimeType... mimeTypes) {
7474

7575
@Override
7676
public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) {
77-
JavaType javaType = getObjectMapper().getTypeFactory().constructType(elementType.getType());
77+
JavaType javaType = getObjectMapper().constructType(elementType.getType());
7878
// Skip String: CharSequenceDecoder + "*/*" comes after
7979
return (!CharSequence.class.isAssignableFrom(elementType.resolve(Object.class)) &&
8080
getObjectMapper().canDeserialize(javaType) && supportsMimeType(mimeType));

0 commit comments

Comments
 (0)