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 ApplicationListenerMethodAdapter method getResolvableType.
private ResolvableType getResolvableType(ApplicationEvent event) {
ResolvableType payloadType = null;
if (event instanceof PayloadApplicationEvent) {
PayloadApplicationEvent<?> payloadEvent = (PayloadApplicationEvent<?>) event;
payloadType = payloadEvent.getResolvableType().as(PayloadApplicationEvent.class).getGeneric();
}
for (ResolvableType declaredEventType : this.declaredEventTypes) {
if (!ApplicationEvent.class.isAssignableFrom(declaredEventType.getRawClass()) && payloadType != null) {
if (declaredEventType.isAssignableFrom(payloadType)) {
return declaredEventType;
}
}
if (declaredEventType.getRawClass().isInstance(event)) {
return declaredEventType;
}
}
return null;
}
use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.
the class Jackson2JsonDecoderTests method canDecode.
@Test
public void canDecode() {
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
ResolvableType type = ResolvableType.forClass(Pojo.class);
assertTrue(decoder.canDecode(type, MediaType.APPLICATION_JSON));
assertTrue(decoder.canDecode(type, null));
assertFalse(decoder.canDecode(type, MediaType.APPLICATION_XML));
}
use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.
the class Jackson2JsonDecoderTests method jsonView.
@Test
public void jsonView() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer("{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"));
ResolvableType elementType = ResolvableType.forClass(JacksonViewBean.class);
Map<String, Object> hints = Collections.singletonMap(Jackson2JsonDecoder.JSON_VIEW_HINT, MyJacksonView1.class);
Flux<JacksonViewBean> flux = new Jackson2JsonDecoder().decode(source, elementType, null, hints).cast(JacksonViewBean.class);
StepVerifier.create(flux).consumeNextWith(b -> {
assertTrue(b.getWithView1().equals("with"));
assertNull(b.getWithView2());
assertNull(b.getWithoutView());
}).expectComplete().verify();
}
use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.
the class ResourceHttpMessageWriterTests method testWrite.
private void testWrite(MockServerHttpRequest request) {
Mono<Resource> input = Mono.just(this.resource);
ResolvableType type = ResolvableType.forClass(Resource.class);
MediaType contentType = MediaType.TEXT_PLAIN;
Map<String, Object> hints = Collections.emptyMap();
Mono<Void> mono = this.writer.write(input, null, type, contentType, request, this.response, hints);
StepVerifier.create(mono).expectNextCount(0).expectComplete().verify();
}
Aggregations