Consistent use of ResolvableType.toClass() for assignability checks

Issue: SPR-17086
This commit is contained in:
Juergen Hoeller
2018-11-12 20:29:37 +01:00
parent 3a66927bd2
commit 40148c0560
22 changed files with 64 additions and 94 deletions

View File

@@ -105,8 +105,7 @@ public class FormHttpMessageWriter extends LoggingCodecSupport
@Override
public boolean canWrite(ResolvableType elementType, @Nullable MediaType mediaType) {
Class<?> rawClass = elementType.getRawClass();
if (rawClass == null || !MultiValueMap.class.isAssignableFrom(rawClass)) {
if (!MultiValueMap.class.isAssignableFrom(elementType.toClass())) {
return false;
}
if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType)) {

View File

@@ -93,14 +93,13 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader<Objec
}
private boolean isServerSentEvent(ResolvableType elementType) {
Class<?> rawClass = elementType.getRawClass();
return (rawClass != null && ServerSentEvent.class.isAssignableFrom(rawClass));
return ServerSentEvent.class.isAssignableFrom(elementType.toClass());
}
@Override
public Flux<Object> read(ResolvableType elementType, ReactiveHttpInputMessage message,
Map<String, Object> hints) {
public Flux<Object> read(
ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
boolean shouldWrap = isServerSentEvent(elementType);
ResolvableType valueType = (shouldWrap ? elementType.getGeneric() : elementType);
@@ -170,13 +169,13 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader<Objec
}
@Override
public Mono<Object> readMono(ResolvableType elementType, ReactiveHttpInputMessage message,
Map<String, Object> hints) {
public Mono<Object> readMono(
ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
// We're ahead of String + "*/*"
// Let's see if we can aggregate the output (lest we time out)...
if (String.class.equals(elementType.getRawClass())) {
if (elementType.resolve() == String.class) {
Flux<DataBuffer> body = message.getBody();
return stringDecoder.decodeToMono(body, elementType, null, null).cast(Object.class);
}

View File

@@ -113,8 +113,7 @@ public class ServerSentEventHttpMessageWriter implements HttpMessageWriter<Objec
private Flux<Publisher<DataBuffer>> encode(Publisher<?> input, ResolvableType elementType,
MediaType mediaType, DataBufferFactory factory, Map<String, Object> hints) {
Class<?> elementClass = elementType.getRawClass();
ResolvableType valueType = (elementClass != null && ServerSentEvent.class.isAssignableFrom(elementClass) ?
ResolvableType valueType = (ServerSentEvent.class.isAssignableFrom(elementType.toClass()) ?
elementType.getGeneric() : elementType);
return Flux.from(input).map(element -> {

View File

@@ -177,10 +177,9 @@ public class MultipartHttpMessageWriter extends LoggingCodecSupport
@Override
public boolean canWrite(ResolvableType elementType, @Nullable MediaType mediaType) {
Class<?> rawClass = elementType.getRawClass();
return rawClass != null && MultiValueMap.class.isAssignableFrom(rawClass) &&
return (MultiValueMap.class.isAssignableFrom(elementType.toClass()) &&
(mediaType == null ||
this.supportedMediaTypes.stream().anyMatch(m -> m.isCompatibleWith(mediaType)));
this.supportedMediaTypes.stream().anyMatch(element -> element.isCompatibleWith(mediaType))));
}
@Override
@@ -281,7 +280,7 @@ public class MultipartHttpMessageWriter extends LoggingCodecSupport
if (body instanceof Resource) {
outputHeaders.setContentDispositionFormData(name, ((Resource) body).getFilename());
}
else if (Resource.class.equals(resolvableType.getRawClass())) {
else if (resolvableType.resolve() == Resource.class) {
body = (T) Mono.from((Publisher<?>) body).doOnNext(o -> outputHeaders
.setContentDispositionFormData(name, ((Resource) o).getFilename()));
}

View File

@@ -168,13 +168,11 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes
private int messageBytesToRead;
public MessageDecoderFunction(ResolvableType elementType, int maxMessageSize) {
this.elementType = elementType;
this.maxMessageSize = maxMessageSize;
}
@Override
public Iterable<? extends Message> apply(DataBuffer input) {
try {
@@ -186,13 +184,13 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder<Mes
if (this.output == null) {
int firstByte = input.read();
if (firstByte == -1) {
throw new DecodingException("Can't parse message size");
throw new DecodingException("Cannot parse message size");
}
this.messageBytesToRead = CodedInputStream.readRawVarint32(firstByte, input.asInputStream());
if (this.messageBytesToRead > this.maxMessageSize) {
throw new DecodingException(
"The number of bytes to read parsed in the incoming stream (" +
this.messageBytesToRead + ") exceeds the configured limit (" + this.maxMessageSize + ")");
this.messageBytesToRead + ") exceeds the configured limit (" + this.maxMessageSize + ")");
}
this.output = input.factory().allocateBuffer(this.messageBytesToRead);
}

View File

@@ -84,9 +84,9 @@ public class Jaxb2XmlDecoder extends AbstractDecoder<Object> {
@Override
public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) {
if (super.canDecode(elementType, mimeType)) {
Class<?> outputClass = elementType.getRawClass();
return (outputClass != null && (outputClass.isAnnotationPresent(XmlRootElement.class) ||
outputClass.isAnnotationPresent(XmlType.class)));
Class<?> outputClass = elementType.toClass();
return (outputClass.isAnnotationPresent(XmlRootElement.class) ||
outputClass.isAnnotationPresent(XmlType.class));
}
else {
return false;
@@ -97,12 +97,10 @@ public class Jaxb2XmlDecoder extends AbstractDecoder<Object> {
public Flux<Object> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType,
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
Class<?> outputClass = elementType.getRawClass();
Assert.state(outputClass != null, "Unresolvable output class");
Flux<XMLEvent> xmlEventFlux = this.xmlEventDecoder.decode(
inputStream, ResolvableType.forClass(XMLEvent.class), mimeType, hints);
Class<?> outputClass = elementType.toClass();
QName typeName = toQName(outputClass);
Flux<List<XMLEvent>> splitEvents = split(xmlEventFlux, typeName);
@@ -119,6 +117,7 @@ public class Jaxb2XmlDecoder extends AbstractDecoder<Object> {
@Override
public Mono<Object> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType,
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
return decode(inputStream, elementType, mimeType, hints).singleOrEmpty();
}

View File

@@ -45,8 +45,8 @@ import org.springframework.util.MimeTypeUtils;
* Encode from single value to a byte stream containing XML elements.
*
* <p>{@link javax.xml.bind.annotation.XmlElements @XmlElements} and
* {@link javax.xml.bind.annotation.XmlElement @XmlElement} can be used to specify how
* collections should be marshalled.
* {@link javax.xml.bind.annotation.XmlElement @XmlElement} can be used
* to specify how collections should be marshalled.
*
* @author Sebastien Deleuze
* @author Arjen Poutsma
@@ -73,7 +73,6 @@ public class Jaxb2XmlEncoder extends AbstractSingleValueEncoder<Object> {
else {
return false;
}
}
@Override