Search in sources :

Example 76 with ResolvableType

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);
}
Also used : ResolvableType(org.springframework.core.ResolvableType) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 77 with ResolvableType

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);
}
Also used : Collection(java.util.Collection) ResolvableType(org.springframework.core.ResolvableType)

Example 78 with ResolvableType

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;
}
Also used : ResolvableType(org.springframework.core.ResolvableType)

Example 79 with ResolvableType

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);
}
Also used : ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.Test)

Example 80 with ResolvableType

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);
}
Also used : ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.Test)

Aggregations

ResolvableType (org.springframework.core.ResolvableType)96 Test (org.junit.Test)66 MethodParameter (org.springframework.core.MethodParameter)20 DataBuffer (org.springframework.core.io.buffer.DataBuffer)15 ServerWebExchange (org.springframework.web.server.ServerWebExchange)14 List (java.util.List)11 MediaType (org.springframework.http.MediaType)10 Flux (reactor.core.publisher.Flux)10 Mono (reactor.core.publisher.Mono)10 Map (java.util.Map)9 Assert (org.springframework.util.Assert)7 Collections (java.util.Collections)6 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)6 ArrayList (java.util.ArrayList)5 ReactiveAdapter (org.springframework.core.ReactiveAdapter)5 Pojo (org.springframework.http.codec.Pojo)5 ServerWebInputException (org.springframework.web.server.ServerWebInputException)5 Publisher (org.reactivestreams.Publisher)4 HttpMessageReader (org.springframework.http.codec.HttpMessageReader)4 Single (rx.Single)4