Skip to content

Commit db48b54

Browse files
committed
Polishing
1 parent e48292f commit db48b54

File tree

12 files changed

+63
-70
lines changed

12 files changed

+63
-70
lines changed

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-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.
@@ -194,7 +194,7 @@ public MimeType(String type, String subtype, @Nullable Map<String, String> param
194194
* @see <a href="https://tools.ietf.org/html/rfc2616#section-2.2">HTTP 1.1, section 2.2</a>
195195
*/
196196
private void checkToken(String token) {
197-
for (int i = 0; i < token.length(); i++ ) {
197+
for (int i = 0; i < token.length(); i++) {
198198
char ch = token.charAt(i);
199199
if (!TOKEN.get(ch)) {
200200
throw new IllegalArgumentException("Invalid token character '" + ch + "' in token \"" + token + "\"");
@@ -207,8 +207,7 @@ protected void checkParameters(String attribute, String value) {
207207
Assert.hasLength(value, "'value' must not be empty");
208208
checkToken(attribute);
209209
if (PARAM_CHARSET.equals(attribute)) {
210-
value = unquote(value);
211-
Charset.forName(value);
210+
Charset.forName(unquote(value));
212211
}
213212
else if (!isQuotedString(value)) {
214213
checkToken(value);

spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void setTargetType(MessageType targetType) {
123123
/**
124124
* Specify the encoding to use when converting to and from text-based
125125
* message body content. The default encoding will be "UTF-8".
126-
* <p>When reading from a a text-based message, an encoding may have been
126+
* <p>When reading from a text-based message, an encoding may have been
127127
* suggested through a special JMS property which will then be preferred
128128
* over the encoding set on this MessageConverter instance.
129129
* @see #setEncodingPropertyName
@@ -457,11 +457,11 @@ protected JavaType getJavaTypeForMessage(Message message) throws JMSException {
457457
}
458458
Class<?> mappedClass = this.idClassMappings.get(typeId);
459459
if (mappedClass != null) {
460-
return this.objectMapper.getTypeFactory().constructType(mappedClass);
460+
return this.objectMapper.constructType(mappedClass);
461461
}
462462
try {
463463
Class<?> typeClass = ClassUtils.forName(typeId, this.beanClassLoader);
464-
return this.objectMapper.getTypeFactory().constructType(typeClass);
464+
return this.objectMapper.constructType(typeClass);
465465
}
466466
catch (Throwable ex) {
467467
throw new MessageConversionException("Failed to resolve type id [" + typeId + "]", ex);

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/converter/MappingJackson2MessageConverter.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.messaging.Message;
4444
import org.springframework.messaging.MessageHeaders;
4545
import org.springframework.util.Assert;
46+
import org.springframework.util.ClassUtils;
4647
import org.springframework.util.MimeType;
4748

4849
/**
@@ -141,6 +142,7 @@ private void configurePrettyPrint() {
141142
}
142143
}
143144

145+
144146
@Override
145147
protected boolean canConvertFrom(Message<?> message, @Nullable Class<?> targetClass) {
146148
if (targetClass == null || !supportsMimeType(message.getHeaders())) {
@@ -212,7 +214,7 @@ protected Object convertFromInternal(Message<?> message, Class<?> targetClass, @
212214
Object payload = message.getPayload();
213215
Class<?> view = getSerializationView(conversionHint);
214216
try {
215-
if (targetClass.isInstance(payload)) {
217+
if (ClassUtils.isAssignableValue(targetClass, payload)) {
216218
return payload;
217219
}
218220
else if (payload instanceof byte[]) {
@@ -248,7 +250,7 @@ private JavaType getJavaType(Class<?> targetClass, @Nullable Object conversionHi
248250
Type genericParameterType = param.getNestedGenericParameterType();
249251
Class<?> contextClass = param.getContainingClass();
250252
Type type = GenericTypeResolver.resolveType(genericParameterType, contextClass);
251-
return this.objectMapper.getTypeFactory().constructType(type);
253+
return this.objectMapper.constructType(type);
252254
}
253255
return this.objectMapper.constructType(targetClass);
254256
}
@@ -333,7 +335,7 @@ private Class<?> extractViewClass(JsonView annotation, Object conversionHint) {
333335
* @return the JSON encoding to use (never {@code null})
334336
*/
335337
protected JsonEncoding getJsonEncoding(@Nullable MimeType contentType) {
336-
if (contentType != null && (contentType.getCharset() != null)) {
338+
if (contentType != null && contentType.getCharset() != null) {
337339
Charset charset = contentType.getCharset();
338340
for (JsonEncoding encoding : JsonEncoding.values()) {
339341
if (charset.name().equals(encoding.getJavaName())) {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
*
4848
* @author Arjen Poutsma
4949
* @since 4.2
50+
* @see Marshaller
51+
* @see Unmarshaller
5052
*/
5153
public class MarshallingMessageConverter extends AbstractMessageConverter {
5254

@@ -62,7 +64,8 @@ public class MarshallingMessageConverter extends AbstractMessageConverter {
6264
* {@link #setUnmarshaller(Unmarshaller)} to be invoked separately.
6365
*/
6466
public MarshallingMessageConverter() {
65-
this(new MimeType("application", "xml"), new MimeType("text", "xml"), new MimeType("application", "*+xml"));
67+
this(new MimeType("application", "xml"), new MimeType("text", "xml"),
68+
new MimeType("application", "*+xml"));
6669
}
6770

6871
/**
@@ -161,7 +164,7 @@ private Source getSource(Object payload) {
161164
return new StreamSource(new ByteArrayInputStream((byte[]) payload));
162165
}
163166
else {
164-
return new StreamSource(new StringReader((String) payload));
167+
return new StreamSource(new StringReader(payload.toString()));
165168
}
166169
}
167170

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/codec/json/AbstractJackson2Decoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public int getMaxInMemorySize() {
105105

106106
@Override
107107
public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) {
108-
JavaType javaType = getObjectMapper().getTypeFactory().constructType(elementType.getType());
108+
JavaType javaType = getObjectMapper().constructType(elementType.getType());
109109
// Skip String: CharSequenceDecoder + "*/*" comes after
110110
return (!CharSequence.class.isAssignableFrom(elementType.toClass()) &&
111111
getObjectMapper().canDeserialize(javaType) && supportsMimeType(mimeType));

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.fasterxml.jackson.annotation.JsonView;
2828
import com.fasterxml.jackson.databind.JavaType;
2929
import com.fasterxml.jackson.databind.ObjectMapper;
30-
import com.fasterxml.jackson.databind.type.TypeFactory;
3130
import org.apache.commons.logging.Log;
3231

3332
import org.springframework.core.GenericTypeResolver;
@@ -100,8 +99,7 @@ protected boolean supportsMimeType(@Nullable MimeType mimeType) {
10099
}
101100

102101
protected JavaType getJavaType(Type type, @Nullable Class<?> contextClass) {
103-
TypeFactory typeFactory = this.objectMapper.getTypeFactory();
104-
return typeFactory.constructType(GenericTypeResolver.resolveType(type, contextClass));
102+
return this.objectMapper.constructType(GenericTypeResolver.resolveType(type, contextClass));
105103
}
106104

107105
protected Map<String, Object> getHints(ResolvableType resolvableType) {

spring-web/src/main/java/org/springframework/http/converter/HttpMessageConverter.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-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.
@@ -25,7 +25,7 @@
2525
import org.springframework.lang.Nullable;
2626

2727
/**
28-
* Strategy interface that specifies a converter that can convert from and to HTTP requests and responses.
28+
* Strategy interface for converting from and to HTTP requests and responses.
2929
*
3030
* @author Arjen Poutsma
3131
* @author Juergen Hoeller
@@ -54,7 +54,7 @@ public interface HttpMessageConverter<T> {
5454

5555
/**
5656
* Return the list of {@link MediaType} objects supported by this converter.
57-
* @return the list of supported media types
57+
* @return the list of supported media types, potentially an immutable copy
5858
*/
5959
List<MediaType> getSupportedMediaTypes();
6060

spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import com.fasterxml.jackson.databind.SerializationFeature;
4444
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
4545
import com.fasterxml.jackson.databind.ser.FilterProvider;
46-
import com.fasterxml.jackson.databind.type.TypeFactory;
4746

4847
import org.springframework.core.GenericTypeResolver;
4948
import org.springframework.http.HttpInputMessage;
@@ -376,8 +375,7 @@ protected void writeSuffix(JsonGenerator generator, Object object) throws IOExce
376375
* @return the Jackson JavaType
377376
*/
378377
protected JavaType getJavaType(Type type, @Nullable Class<?> contextClass) {
379-
TypeFactory typeFactory = this.objectMapper.getTypeFactory();
380-
return typeFactory.constructType(GenericTypeResolver.resolveType(type, contextClass));
378+
return this.objectMapper.constructType(GenericTypeResolver.resolveType(type, contextClass));
381379
}
382380

383381
/**

0 commit comments

Comments
 (0)