Search in sources :

Example 1 with TypeConverterRegistrar

use of io.micronaut.core.convert.TypeConverterRegistrar in project micronaut-core by micronaut-projects.

the class DefaultApplicationContext method initializeTypeConverters.

/**
 * @param beanContext The bean context
 */
protected void initializeTypeConverters(BeanContext beanContext) {
    Collection<BeanRegistration<TypeConverter>> typeConverters = beanContext.getBeanRegistrations(TypeConverter.class);
    for (BeanRegistration<TypeConverter> typeConverterRegistration : typeConverters) {
        TypeConverter typeConverter = typeConverterRegistration.getBean();
        List<Argument<?>> typeArguments = typeConverterRegistration.getBeanDefinition().getTypeArguments(TypeConverter.class);
        if (typeArguments.size() == 2) {
            Class source = typeArguments.get(0).getType();
            Class target = typeArguments.get(1).getType();
            if (source != null && target != null && !(source == Object.class && target == Object.class)) {
                getConversionService().addConverter(source, target, typeConverter);
            }
        }
    }
    Collection<TypeConverterRegistrar> registrars = beanContext.getBeansOfType(TypeConverterRegistrar.class);
    for (TypeConverterRegistrar registrar : registrars) {
        registrar.register(conversionService);
    }
}
Also used : TypeConverter(io.micronaut.core.convert.TypeConverter) Argument(io.micronaut.core.type.Argument) TypeConverterRegistrar(io.micronaut.core.convert.TypeConverterRegistrar)

Aggregations

TypeConverter (io.micronaut.core.convert.TypeConverter)1 TypeConverterRegistrar (io.micronaut.core.convert.TypeConverterRegistrar)1 Argument (io.micronaut.core.type.Argument)1