use of org.springframework.core.ResolvableType in project spring-boot by spring-projects.
the class AbstractJsonMarshalTesterTests method writeMapShouldReturnJsonContent.
@Test
public void writeMapShouldReturnJsonContent() throws Exception {
ResolvableType type = ResolvableTypes.get("mapOfExampleObject");
Map<String, Object> value = new LinkedHashMap<>();
value.put("a", OBJECT);
JsonContent<Object> content = createTester(type).write(value);
assertThat(content).isEqualToJson(MAP_JSON);
}
use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.
the class TypeDescriptor method collection.
/**
* Create a new type descriptor from a {@link java.util.Collection} type.
* <p>Useful for converting to typed Collections.
* <p>For example, a {@code List<String>} could be converted to a
* {@code List<EmailAddress>} by converting to a targetType built with this method.
* The method call to construct such a {@code TypeDescriptor} would look something
* like: {@code collection(List.class, TypeDescriptor.valueOf(EmailAddress.class));}
* @param collectionType the collection type, which must implement {@link Collection}.
* @param elementTypeDescriptor a descriptor for the collection's element type,
* used to convert collection elements
* @return the collection type descriptor
*/
public static TypeDescriptor collection(Class<?> collectionType, TypeDescriptor elementTypeDescriptor) {
Assert.notNull(collectionType, "Collection type must not be null");
if (!Collection.class.isAssignableFrom(collectionType)) {
throw new IllegalArgumentException("Collection type must be a [java.util.Collection]");
}
ResolvableType element = (elementTypeDescriptor != null ? elementTypeDescriptor.resolvableType : null);
return new TypeDescriptor(ResolvableType.forClassWithGenerics(collectionType, element), null, null);
}
use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.
the class GenericConversionService method getRequiredTypeInfo.
// Internal helpers
private ResolvableType[] getRequiredTypeInfo(Class<?> converterClass, Class<?> genericIfc) {
ResolvableType resolvableType = ResolvableType.forClass(converterClass).as(genericIfc);
ResolvableType[] generics = resolvableType.getGenerics();
if (generics.length < 2) {
return null;
}
Class<?> sourceType = generics[0].resolve();
Class<?> targetType = generics[1].resolve();
if (sourceType == null || targetType == null) {
return null;
}
return generics;
}
use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.
the class GenericApplicationListenerAdapterTests method genericListenerStrictTypeTypeErasure.
// Demonstrates we can't inject that event because the generic type is lost
@Test
public void genericListenerStrictTypeTypeErasure() {
GenericTestEvent<String> stringEvent = createGenericTestEvent("test");
ResolvableType eventType = ResolvableType.forType(stringEvent.getClass());
supportsEventType(false, StringEventListener.class, eventType);
}
use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.
the class GenericApplicationListenerAdapterTests method genericListenerStrictTypeAndResolvableTypeProvider.
// or if the event provides its precise type
@Test
public void genericListenerStrictTypeAndResolvableTypeProvider() {
ResolvableType eventType = new SmartGenericTestEvent<>(this, "foo").getResolvableType();
supportsEventType(true, StringEventListener.class, eventType);
}
Aggregations