org.springframework.core.convert.converter.ConverterFactory Java Examples

The following examples show how to use org.springframework.core.convert.converter.ConverterFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConversionServiceFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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");
			}
		}
	}
}
 
Example #2
Source File: CustomConversions.java    From spring-data-crate with Apache License 2.0 6 votes vote down vote up
/**
 * 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!");
    }
  }
}
 
Example #3
Source File: ConversionServiceFactory.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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");
			}
		}
	}
}
 
Example #4
Source File: CustomConversions.java    From dubbox with Apache License 2.0 6 votes vote down vote up
/**
 * 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!");
		}
	}
}
 
Example #5
Source File: ConversionServiceFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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(@Nullable 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");
			}
		}
	}
}
 
Example #6
Source File: ConversionServiceFactory.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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(@Nullable 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");
			}
		}
	}
}
 
Example #7
Source File: ExtendedDefaultConversionService.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Autowired(required = false)
public void setConverterFactories(Set<ConverterFactory<?, ?>> converterFactories) {
    if (converterFactories != null) {
        for (ConverterFactory<?, ?> converterFactory : converterFactories) {
            addConverterFactory(converterFactory);
        }
    }
}
 
Example #8
Source File: GenericConversionService.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void addConverterFactory(ConverterFactory<?, ?> factory) {
	ResolvableType[] typeInfo = getRequiredTypeInfo(factory.getClass(), ConverterFactory.class);
	if (typeInfo == null && factory instanceof DecoratingProxy) {
		typeInfo = getRequiredTypeInfo(((DecoratingProxy) factory).getDecoratedClass(), ConverterFactory.class);
	}
	if (typeInfo == null) {
		throw new IllegalArgumentException("Unable to determine source type <S> and target type <T> for your " +
				"ConverterFactory [" + factory.getClass().getName() + "]; does the class parameterize those types?");
	}
	addConverter(new ConverterFactoryAdapter(factory,
			new ConvertiblePair(typeInfo[0].toClass(), typeInfo[1].toClass())));
}
 
Example #9
Source File: GenericConversionService.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void addConverterFactory(ConverterFactory<?, ?> factory) {
	ResolvableType[] typeInfo = getRequiredTypeInfo(factory.getClass(), ConverterFactory.class);
	if (typeInfo == null && factory instanceof DecoratingProxy) {
		typeInfo = getRequiredTypeInfo(((DecoratingProxy) factory).getDecoratedClass(), ConverterFactory.class);
	}
	if (typeInfo == null) {
		throw new IllegalArgumentException("Unable to determine source type <S> and target type <T> for your " +
				"ConverterFactory [" + factory.getClass().getName() + "]; does the class parameterize those types?");
	}
	addConverter(new ConverterFactoryAdapter(factory,
			new ConvertiblePair(typeInfo[0].resolve(), typeInfo[1].resolve())));
}
 
Example #10
Source File: GenericConversionService.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void addConverterFactory(ConverterFactory<?, ?> factory) {
	ResolvableType[] typeInfo = getRequiredTypeInfo(factory.getClass(), ConverterFactory.class);
	if (typeInfo == null && factory instanceof DecoratingProxy) {
		typeInfo = getRequiredTypeInfo(((DecoratingProxy) factory).getDecoratedClass(), ConverterFactory.class);
	}
	if (typeInfo == null) {
		throw new IllegalArgumentException("Unable to determine source type <S> and target type <T> for your " +
				"ConverterFactory [" + factory.getClass().getName() + "]; does the class parameterize those types?");
	}
	addConverter(new ConverterFactoryAdapter(factory,
			new ConvertiblePair(typeInfo[0].toClass(), typeInfo[1].toClass())));
}
 
Example #11
Source File: GenericConversionService.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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())));
}
 
Example #12
Source File: FormattingConversionServiceTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void proxiedConverterFactory() {
	ConverterFactory<?, ?> converterFactory = new IntegerConverterFactory();
	formattingService.addConverterFactory((ConverterFactory<?, ?>) new ProxyFactory(converterFactory).getProxy());
	assertEquals(Integer.valueOf(1), formattingService.convert("1", Integer.class));
}
 
Example #13
Source File: GenericConversionService.java    From java-technology-stack with MIT License 4 votes vote down vote up
public ConverterFactoryAdapter(ConverterFactory<?, ?> converterFactory, ConvertiblePair typeInfo) {
	this.converterFactory = (ConverterFactory<Object, Object>) converterFactory;
	this.typeInfo = typeInfo;
}
 
Example #14
Source File: FormattingConversionServiceTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void proxiedConverterFactory() {
	ConverterFactory<?, ?> converterFactory = new IntegerConverterFactory();
	formattingService.addConverterFactory((ConverterFactory<?, ?>) new ProxyFactory(converterFactory).getProxy());
	assertEquals(Integer.valueOf(1), formattingService.convert("1", Integer.class));
}
 
Example #15
Source File: GenericConversionService.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public ConverterFactoryAdapter(ConverterFactory<?, ?> converterFactory, ConvertiblePair typeInfo) {
	this.converterFactory = (ConverterFactory<Object, Object>) converterFactory;
	this.typeInfo = typeInfo;
}
 
Example #16
Source File: GenericConversionService.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public ConverterFactoryAdapter(ConverterFactory<?, ?> converterFactory, ConvertiblePair typeInfo) {
	this.converterFactory = (ConverterFactory<Object, Object>) converterFactory;
	this.typeInfo = typeInfo;
}
 
Example #17
Source File: GenericConversionService.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public ConverterFactoryAdapter(ConverterFactory<?, ?> converterFactory, ConvertiblePair typeInfo) {
	this.converterFactory = (ConverterFactory<Object, Object>) converterFactory;
	this.typeInfo = typeInfo;
}
 
Example #18
Source File: DefaultRetrofitClientConfiguration.java    From spring-cloud-square with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean(ConverterFactory.class)
public SpringConverterFactory springConverterFactory(ConversionService conversionService) {
	return new SpringConverterFactory(messageConverters, conversionService);
}