Search in sources :

Example 6 with ResolvableType

use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.

the class MapFactoryBean method createInstance.

@Override
@SuppressWarnings("unchecked")
protected Map<Object, Object> createInstance() {
    if (this.sourceMap == null) {
        throw new IllegalArgumentException("'sourceMap' is required");
    }
    Map<Object, Object> result = null;
    if (this.targetMapClass != null) {
        result = BeanUtils.instantiateClass(this.targetMapClass);
    } else {
        result = new LinkedHashMap<>(this.sourceMap.size());
    }
    Class<?> keyType = null;
    Class<?> valueType = null;
    if (this.targetMapClass != null) {
        ResolvableType mapType = ResolvableType.forClass(this.targetMapClass).asMap();
        keyType = mapType.resolveGeneric(0);
        valueType = mapType.resolveGeneric(1);
    }
    if (keyType != null || valueType != null) {
        TypeConverter converter = getBeanTypeConverter();
        for (Map.Entry<?, ?> entry : this.sourceMap.entrySet()) {
            Object convertedKey = converter.convertIfNecessary(entry.getKey(), keyType);
            Object convertedValue = converter.convertIfNecessary(entry.getValue(), valueType);
            result.put(convertedKey, convertedValue);
        }
    } else {
        result.putAll(this.sourceMap);
    }
    return result;
}
Also used : TypeConverter(org.springframework.beans.TypeConverter) ResolvableType(org.springframework.core.ResolvableType) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 7 with ResolvableType

use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.

the class GenericApplicationListenerAdapterTests method supportsEventTypeWithSmartApplicationListener.

@Test
public void supportsEventTypeWithSmartApplicationListener() {
    SmartApplicationListener smartListener = mock(SmartApplicationListener.class);
    GenericApplicationListenerAdapter listener = new GenericApplicationListenerAdapter(smartListener);
    ResolvableType type = ResolvableType.forClass(ApplicationEvent.class);
    listener.supportsEventType(type);
    verify(smartListener, times(1)).supportsEventType(ApplicationEvent.class);
}
Also used : ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.Test)

Example 8 with ResolvableType

use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.

the class GenericApplicationListenerAdapterTests method genericListenerWildcardTypeTypeErasure.

// Demonstrates we cant inject that event because the listener has a wildcard
@Test
public void genericListenerWildcardTypeTypeErasure() {
    GenericTestEvent<String> stringEvent = createGenericTestEvent("test");
    ResolvableType eventType = ResolvableType.forType(stringEvent.getClass());
    supportsEventType(true, GenericEventListener.class, eventType);
}
Also used : ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.Test)

Example 9 with ResolvableType

use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.

the class GenericApplicationListenerAdapterTests method genericListenerStrictTypeAndResolvableType.

// But it works if we specify the type properly
@Test
public void genericListenerStrictTypeAndResolvableType() {
    ResolvableType eventType = ResolvableType.forClassWithGenerics(GenericTestEvent.class, String.class);
    supportsEventType(true, StringEventListener.class, eventType);
}
Also used : ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.Test)

Example 10 with ResolvableType

use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.

the class GenericApplicationListenerAdapterTests method genericListenerStrictTypeNotMatchTypeErasure.

@Test
public void genericListenerStrictTypeNotMatchTypeErasure() {
    GenericTestEvent<Long> longEvent = createGenericTestEvent(123L);
    ResolvableType eventType = ResolvableType.forType(longEvent.getClass());
    supportsEventType(false, 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