Skip to content

Commit bacf6ca

Browse files
committed
Polishing
1 parent 95c0f11 commit bacf6ca

File tree

8 files changed

+92
-52
lines changed

8 files changed

+92
-52
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-2013 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.
@@ -35,10 +35,11 @@
3535
*
3636
* @author Juergen Hoeller
3737
* @since 4.0
38+
* @see javax.enterprise.concurrent.ManagedScheduledExecutorService
3839
*/
3940
public class DefaultManagedTaskScheduler extends ConcurrentTaskScheduler implements InitializingBean {
4041

41-
private JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate();
42+
private final JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate();
4243

4344
private String jndiName = "java:comp/DefaultManagedScheduledExecutorService";
4445

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

Lines changed: 3 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.
@@ -193,7 +193,7 @@ public MimeType(String type, String subtype, Map<String, String> parameters) {
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);

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

Lines changed: 21 additions & 19 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,20 +149,6 @@ public Class<?> getSerializedPayloadClass() {
149149
}
150150

151151

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

179-
protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) {
180-
return (supports(targetClass) && supportsMimeType(message.getHeaders()));
181-
}
182-
183165
@Override
184166
public final Message<?> toMessage(Object payload, MessageHeaders headers) {
185167
return toMessage(payload, headers, null);
@@ -213,6 +195,11 @@ public final Message<?> toMessage(Object payload, MessageHeaders headers, Object
213195
return builder.build();
214196
}
215197

198+
199+
protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) {
200+
return (supports(targetClass) && supportsMimeType(message.getHeaders()));
201+
}
202+
216203
protected boolean canConvertTo(Object payload, MessageHeaders headers) {
217204
Class<?> clazz = (payload != null ? payload.getClass() : null);
218205
return (supports(clazz) && supportsMimeType(headers));
@@ -238,6 +225,21 @@ protected MimeType getMimeType(MessageHeaders headers) {
238225
return (this.contentTypeResolver != null ? this.contentTypeResolver.resolve(headers) : null);
239226
}
240227

228+
/**
229+
* Return the default content type for the payload. Called when
230+
* {@link #toMessage(Object, MessageHeaders)} is invoked without
231+
* message headers or without a content type header.
232+
* <p>By default, this returns the first element of the
233+
* {@link #getSupportedMimeTypes() supportedMimeTypes}, if any.
234+
* Can be overridden in subclasses.
235+
* @param payload the payload being converted to a message
236+
* @return the content type, or {@code null} if not known
237+
*/
238+
protected MimeType getDefaultContentType(Object payload) {
239+
List<MimeType> mimeTypes = getSupportedMimeTypes();
240+
return (!mimeTypes.isEmpty() ? mimeTypes.get(0) : null);
241+
}
242+
241243

242244
/**
243245
* 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-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.
@@ -654,8 +654,10 @@ public void run() {
654654
if (conn != null) {
655655
conn.send(HEARTBEAT).addCallback(
656656
new ListenableFutureCallback<Void>() {
657+
@Override
657658
public void onSuccess(Void result) {
658659
}
660+
@Override
659661
public void onFailure(Throwable ex) {
660662
handleFailure(ex);
661663
}

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

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

5353

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

6161
/**
62-
* A protected constructor to create new headers.
62+
* Protected constructor to create an instance with the given native headers.
6363
* @param nativeHeaders native headers to create the message with (may be {@code null})
6464
*/
6565
protected NativeMessageHeaderAccessor(Map<String, List<String>> nativeHeaders) {
@@ -69,7 +69,7 @@ protected NativeMessageHeaderAccessor(Map<String, List<String>> nativeHeaders) {
6969
}
7070

7171
/**
72-
* A protected constructor accepting the headers of an existing message to copy.
72+
* Protected constructor that copies headers from another message.
7373
*/
7474
protected NativeMessageHeaderAccessor(Message<?> message) {
7575
super(message);
@@ -84,13 +84,17 @@ protected NativeMessageHeaderAccessor(Message<?> message) {
8484
}
8585
}
8686

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

9296
/**
93-
* Return a copy of the native header values or an empty map.
97+
* Return a copy of the native headers sub-map, or an empty map.
9498
*/
9599
public Map<String, List<String>> toNativeHeaderMap() {
96100
Map<String, List<String>> map = getNativeHeaders();
@@ -112,28 +116,33 @@ public void setImmutable() {
112116

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

121126
/**
122-
* @return all values for the specified native header or {@code null}.
127+
* Return all values for the specified native header, if present.
128+
* @param headerName the name of the header
129+
* @return the associated values, or {@code null} if none
123130
*/
124131
public List<String> getNativeHeader(String headerName) {
125132
Map<String, List<String>> map = getNativeHeaders();
126133
return (map != null ? map.get(headerName) : null);
127134
}
128135

129136
/**
130-
* @return the first value for the specified native header of {@code null}.
137+
* Return the first value for the specified native header, if present.
138+
* @param headerName the name of the header
139+
* @return the associated value, or {@code null} if none
131140
*/
132141
public String getFirstNativeHeader(String headerName) {
133142
Map<String, List<String>> map = getNativeHeaders();
134143
if (map != null) {
135144
List<String> values = map.get(headerName);
136-
if (values != null) {
145+
if (!CollectionUtils.isEmpty(values)) {
137146
return values.get(0);
138147
}
139148
}
@@ -142,6 +151,8 @@ public String getFirstNativeHeader(String headerName) {
142151

143152
/**
144153
* Set the specified native header value replacing existing values.
154+
* <p>In order for this to work, the accessor must be {@link #isMutable()
155+
* mutable}. See {@link MessageHeaderAccessor} for details.
145156
*/
146157
public void setNativeHeader(String name, String value) {
147158
Assert.state(isMutable(), "Already immutable");
@@ -167,6 +178,10 @@ public void setNativeHeader(String name, String value) {
167178

168179
/**
169180
* Add the specified native header value to existing values.
181+
* <p>In order for this to work, the accessor must be {@link #isMutable()
182+
* mutable}. See {@link MessageHeaderAccessor} for details.
183+
* @param name the name of the header
184+
* @param value the header value to set
170185
*/
171186
public void addNativeHeader(String name, String value) {
172187
Assert.state(isMutable(), "Already immutable");
@@ -187,6 +202,10 @@ public void addNativeHeader(String name, String value) {
187202
setModified(true);
188203
}
189204

205+
/**
206+
* Add the specified native headers to existing values.
207+
* @param headers the headers to set
208+
*/
190209
public void addNativeHeaders(MultiValueMap<String, String> headers) {
191210
if (headers == null) {
192211
return;
@@ -198,21 +217,36 @@ public void addNativeHeaders(MultiValueMap<String, String> headers) {
198217
}
199218
}
200219

201-
public List<String> removeNativeHeader(String name) {
220+
/**
221+
* Remove the specified native header value replacing existing values.
222+
* <p>In order for this to work, the accessor must be {@link #isMutable()
223+
* mutable}. See {@link MessageHeaderAccessor} for details.
224+
* @param headerName the name of the header
225+
* @return the associated values, or {@code null} if the header was not present
226+
*/
227+
public List<String> removeNativeHeader(String headerName) {
202228
Assert.state(isMutable(), "Already immutable");
203229
Map<String, List<String>> nativeHeaders = getNativeHeaders();
204-
if (nativeHeaders == null) {
230+
if (CollectionUtils.isEmpty(nativeHeaders)) {
205231
return null;
206232
}
207-
return nativeHeaders.remove(name);
233+
return nativeHeaders.remove(headerName);
208234
}
209235

236+
237+
/**
238+
* Return the first value for the specified native header,
239+
* or {@code null} if none.
240+
* @param headerName the name of the header
241+
* @param headers the headers map to introspect
242+
* @return the associated value, or {@code null} if none
243+
*/
210244
@SuppressWarnings("unchecked")
211245
public static String getFirstNativeHeader(String headerName, Map<String, Object> headers) {
212246
Map<String, List<String>> map = (Map<String, List<String>>) headers.get(NATIVE_HEADERS);
213247
if (map != null) {
214248
List<String> values = map.get(headerName);
215-
if (values != null) {
249+
if (!CollectionUtils.isEmpty(values)) {
216250
return values.get(0);
217251
}
218252
}

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

Lines changed: 12 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.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.messaging.converter;
1818

19-
import java.io.IOException;
2019
import java.lang.reflect.Method;
2120
import java.nio.charset.Charset;
2221
import java.nio.charset.StandardCharsets;
@@ -74,7 +73,7 @@ public void mimetypesParametrizedConstructor() {
7473
}
7574

7675
@Test
77-
public void fromMessage() throws Exception {
76+
public void fromMessage() {
7877
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
7978
String payload = "{\"bytes\":\"AQI=\",\"array\":[\"Foo\",\"Bar\"],\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}";
8079
Message<?> message = MessageBuilder.withPayload(payload.getBytes(UTF_8)).build();
@@ -89,7 +88,7 @@ public void fromMessage() throws Exception {
8988
}
9089

9190
@Test
92-
public void fromMessageUntyped() throws Exception {
91+
public void fromMessageUntyped() {
9392
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
9493
String payload = "{\"bytes\":\"AQI=\",\"array\":[\"Foo\",\"Bar\"],"
9594
+ "\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}";
@@ -106,15 +105,15 @@ public void fromMessageUntyped() throws Exception {
106105
}
107106

108107
@Test(expected = MessageConversionException.class)
109-
public void fromMessageInvalidJson() throws Exception {
108+
public void fromMessageInvalidJson() {
110109
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
111110
String payload = "FooBar";
112111
Message<?> message = MessageBuilder.withPayload(payload.getBytes(UTF_8)).build();
113112
converter.fromMessage(message, MyBean.class);
114113
}
115114

116115
@Test
117-
public void fromMessageValidJsonWithUnknownProperty() throws IOException {
116+
public void fromMessageValidJsonWithUnknownProperty() {
118117
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
119118
String payload = "{\"string\":\"string\",\"unknownProperty\":\"value\"}";
120119
Message<?> message = MessageBuilder.withPayload(payload.getBytes(UTF_8)).build();
@@ -151,7 +150,7 @@ public void fromMessageToMessageWithPojo() throws Exception {
151150
}
152151

153152
@Test
154-
public void toMessage() throws Exception {
153+
public void toMessage() {
155154
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
156155
MyBean payload = new MyBean();
157156
payload.setString("Foo");
@@ -239,11 +238,14 @@ public JacksonViewBean jsonViewResponse() {
239238
return bean;
240239
}
241240

242-
public void jsonViewPayload(@JsonView(MyJacksonView2.class) JacksonViewBean payload) {}
241+
public void jsonViewPayload(@JsonView(MyJacksonView2.class) JacksonViewBean payload) {
242+
}
243243

244-
void handleList(List<Long> payload) {}
244+
void handleList(List<Long> payload) {
245+
}
245246

246-
void handleMessage(Message<MyBean> message) {}
247+
void handleMessage(Message<MyBean> message) {
248+
}
247249

248250

249251
public static class MyBean {

0 commit comments

Comments
 (0)