use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.
the class TypeDescriptor method map.
/**
* Create a new type descriptor from a {@link java.util.Map} type.
* <p>Useful for converting to typed Maps.
* <p>For example, a Map<String, String> could be converted to a Map<Id, EmailAddress>
* by converting to a targetType built with this method:
* The method call to construct such a TypeDescriptor would look something like:
* <pre class="code">
* map(Map.class, TypeDescriptor.valueOf(Id.class), TypeDescriptor.valueOf(EmailAddress.class));
* </pre>
* @param mapType the map type, which must implement {@link Map}
* @param keyTypeDescriptor a descriptor for the map's key type, used to convert map keys
* @param valueTypeDescriptor the map's value type, used to convert map values
* @return the map type descriptor
*/
public static TypeDescriptor map(Class<?> mapType, TypeDescriptor keyTypeDescriptor, TypeDescriptor valueTypeDescriptor) {
Assert.notNull(mapType, "Map type must not be null");
if (!Map.class.isAssignableFrom(mapType)) {
throw new IllegalArgumentException("Map type must be a [java.util.Map]");
}
ResolvableType key = (keyTypeDescriptor != null ? keyTypeDescriptor.resolvableType : null);
ResolvableType value = (valueTypeDescriptor != null ? valueTypeDescriptor.resolvableType : null);
return new TypeDescriptor(ResolvableType.forClassWithGenerics(mapType, key, value), null, null);
}
use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.
the class MessageMethodArgumentResolver method getPayloadType.
private Class<?> getPayloadType(MethodParameter parameter) {
Type genericParamType = parameter.getGenericParameterType();
ResolvableType resolvableType = ResolvableType.forType(genericParamType).as(Message.class);
return resolvableType.getGeneric(0).resolve(Object.class);
}
use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.
the class ServerSentEventHttpMessageReader method read.
@Override
public Flux<Object> read(ResolvableType elementType, ReactiveHttpInputMessage inputMessage, Map<String, Object> hints) {
boolean hasSseWrapper = ServerSentEvent.class.isAssignableFrom(elementType.getRawClass());
ResolvableType dataType = (hasSseWrapper ? elementType.getGeneric(0) : elementType);
return Flux.from(inputMessage.getBody()).concatMap(ServerSentEventHttpMessageReader::splitOnNewline).map(buffer -> {
CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer.asByteBuffer());
DataBufferUtils.release(buffer);
return charBuffer.toString();
}).bufferUntil(line -> line.equals("\n")).concatMap(rawLines -> {
String[] lines = rawLines.stream().collect(joining()).split("\\r?\\n");
ServerSentEvent<Object> event = buildEvent(lines, dataType, hints);
return (hasSseWrapper ? Mono.just(event) : Mono.justOrEmpty(event.data()));
}).cast(Object.class);
}
use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.
the class ServerSentEventHttpMessageWriter method applyEncoder.
@SuppressWarnings("unchecked")
private <T> Flux<DataBuffer> applyEncoder(Object data, DataBufferFactory bufferFactory, Map<String, Object> hints) {
ResolvableType elementType = ResolvableType.forClass(data.getClass());
Optional<Encoder<?>> encoder = dataEncoders.stream().filter(e -> e.canEncode(elementType, MimeTypeUtils.APPLICATION_JSON)).findFirst();
return ((Encoder<T>) encoder.orElseThrow(() -> new CodecException("No suitable encoder found!"))).encode(Mono.just((T) data), bufferFactory, elementType, MimeTypeUtils.APPLICATION_JSON, hints).concatWith(encodeString("\n", bufferFactory));
}
use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.
the class Jackson2JsonEncoder method encode.
@Override
public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
Assert.notNull(inputStream, "'inputStream' must not be null");
Assert.notNull(bufferFactory, "'bufferFactory' must not be null");
Assert.notNull(elementType, "'elementType' must not be null");
if (inputStream instanceof Mono) {
return Flux.from(inputStream).map(value -> encodeValue(value, bufferFactory, elementType, hints));
} else if (APPLICATION_STREAM_JSON.isCompatibleWith(mimeType)) {
return Flux.from(inputStream).map(value -> {
DataBuffer buffer = encodeValue(value, bufferFactory, elementType, hints);
buffer.write(new byte[] { '\n' });
return buffer;
});
}
ResolvableType listType = ResolvableType.forClassWithGenerics(List.class, elementType);
return Flux.from(inputStream).collectList().map(list -> encodeValue(list, bufferFactory, listType, hints)).flux();
}
Aggregations