/** * Register the given Converter objects with the given target ConverterRegistry. * @param converters the converter objects: implementing {@link Converter}, * {@link ConverterFactory}, or {@link GenericConverter} * @param registry the target registry */ public static void registerConverters(Set<?> converters, ConverterRegistry registry) { if (converters != null) { for (Object converter : converters) { if (converter instanceof GenericConverter) { registry.addConverter((GenericConverter) converter); } else if (converter instanceof Converter<?, ?>) { registry.addConverter((Converter<?, ?>) converter); } else if (converter instanceof ConverterFactory<?, ?>) { registry.addConverterFactory((ConverterFactory<?, ?>) converter); } else { throw new IllegalArgumentException("Each converter object must implement one of the " + "Converter, ConverterFactory, or GenericConverter interfaces"); } } } }
/** * Register custom converters within given {@link org.springframework.core.convert.support.GenericConversionService} * * @param conversionService must not be null */ public void registerConvertersIn(GenericConversionService conversionService) { Assert.notNull(conversionService); for (Object converter : converters) { if (converter instanceof Converter) { conversionService.addConverter((Converter<?, ?>) converter); } else if (converter instanceof ConverterFactory) { conversionService.addConverterFactory((ConverterFactory<?, ?>) converter); } else if (converter instanceof GenericConverter) { conversionService.addConverter((GenericConverter) converter); } else { throw new IllegalArgumentException("Given object '" + converter + "' expected to be a Converter, ConverterFactory or GenericeConverter!"); } } }
/** * Populates the given {@link GenericConversionService} with the converters registered. * * @param conversionService the service to register. */ public void registerConvertersIn(final GenericConversionService conversionService) { for (Object converter : converters) { boolean added = false; if (converter instanceof Converter) { conversionService.addConverter((Converter<?, ?>) converter); added = true; } if (converter instanceof ConverterFactory) { conversionService.addConverterFactory((ConverterFactory<?, ?>) converter); added = true; } if (converter instanceof GenericConverter) { conversionService.addConverter((GenericConverter) converter); added = true; } if (!added) { throw new IllegalArgumentException("Given set contains element that is neither Converter nor ConverterFactory!"); } } }
/** * Register custom converters within given {@link GenericConversionService} * * @param conversionService must not be null */ public void registerConvertersIn(GenericConversionService conversionService) { Assert.notNull(conversionService); for (Object converter : converters) { if (converter instanceof Converter) { conversionService.addConverter((Converter<?, ?>) converter); } else if (converter instanceof ConverterFactory) { conversionService.addConverterFactory((ConverterFactory<?, ?>) converter); } else if (converter instanceof GenericConverter) { conversionService.addConverter((GenericConverter) converter); } else { throw new IllegalArgumentException("Given object '" + converter + "' expected to be a Converter, ConverterFactory or GenericeConverter!"); } } }
/** * Populates the given {@link GenericConversionService} with the convertes registered. * * @param conversionService */ public void registerConvertersIn(GenericConversionService conversionService) { for (Object converter : converters) { boolean added = false; if (converter instanceof Converter) { conversionService.addConverter((Converter<?, ?>) converter); added = true; } if (converter instanceof ConverterFactory) { conversionService.addConverterFactory((ConverterFactory<?, ?>) converter); added = true; } if (converter instanceof GenericConverter) { conversionService.addConverter((GenericConverter) converter); added = true; } if (!added) { throw new IllegalArgumentException( "Given set contains element that is neither Converter nor ConverterFactory!"); } } }
@Override public void addConverterFactory(ConverterFactory<?, ?> converterFactory) { ResolvableType[] typeInfo = getRequiredTypeInfo(converterFactory, ConverterFactory.class); Assert.notNull(typeInfo, "Unable to the determine source type <S> and target range type R which your " + "ConverterFactory<S, R> converts between; declare these generic types."); addConverter(new ConverterFactoryAdapter(converterFactory, new ConvertiblePair(typeInfo[0].resolve(), typeInfo[1].resolve()))); }
@Override public Object postProcessAfterInitialization(Object converter, String beanName) throws BeansException { if (converter instanceof GenericConverter) { getConversionService().addConverter((GenericConverter) converter); } else if (converter instanceof Converter<?, ?>) { getConversionService().addConverter((Converter<?, ?>) converter); } else if (converter instanceof ConverterFactory<?, ?>) { getConversionService().addConverterFactory((ConverterFactory<?, ?>) converter); } return converter; }
public void addConverterFactory(ConverterFactory<?, ?> converterFactory) { GenericConverter.ConvertiblePair typeInfo = getRequiredTypeInfo(converterFactory, ConverterFactory.class); if (typeInfo == null) { throw new IllegalArgumentException("Unable to the determine sourceType <S> and " + "targetRangeType R which your ConverterFactory<S, R> converts between; " + "declare these generic types."); } addConverter(new ConverterFactoryAdapter(converterFactory, typeInfo)); }
public ConverterFactoryAdapter(ConverterFactory<?, ?> converterFactory, ConvertiblePair typeInfo) { this.converterFactory = (ConverterFactory<Object, Object>) converterFactory; this.typeInfo = typeInfo; }