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;
}
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);
}
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);
}
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);
}
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);
}
Aggregations