org.springframework.core.convert.converter.GenericConverter.ConvertiblePair Java Examples

The following examples show how to use org.springframework.core.convert.converter.GenericConverter.ConvertiblePair. 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: GenericConversionService.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Find a {@link GenericConverter} given a source and target type.
 * <p>This method will attempt to match all possible converters by working
 * through the class and interface hierarchy of the types.
 * @param sourceType the source type
 * @param targetType the target type
 * @return a matching {@link GenericConverter}, or {@code null} if none found
 */
@Nullable
public GenericConverter find(TypeDescriptor sourceType, TypeDescriptor targetType) {
	// Search the full type hierarchy
	List<Class<?>> sourceCandidates = getClassHierarchy(sourceType.getType());
	List<Class<?>> targetCandidates = getClassHierarchy(targetType.getType());
	for (Class<?> sourceCandidate : sourceCandidates) {
		for (Class<?> targetCandidate : targetCandidates) {
			ConvertiblePair convertiblePair = new ConvertiblePair(sourceCandidate, targetCandidate);
			GenericConverter converter = getRegisteredConverter(sourceType, targetType, convertiblePair);
			if (converter != null) {
				return converter;
			}
		}
	}
	return null;
}
 
Example #2
Source File: GenericConversionService.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Find a {@link GenericConverter} given a source and target type.
 * <p>This method will attempt to match all possible converters by working
 * through the class and interface hierarchy of the types.
 * @param sourceType the source type
 * @param targetType the target type
 * @return a matching {@link GenericConverter}, or {@code null} if none found
 */
@Nullable
public GenericConverter find(TypeDescriptor sourceType, TypeDescriptor targetType) {
	// Search the full type hierarchy
	List<Class<?>> sourceCandidates = getClassHierarchy(sourceType.getType());
	List<Class<?>> targetCandidates = getClassHierarchy(targetType.getType());
	for (Class<?> sourceCandidate : sourceCandidates) {
		for (Class<?> targetCandidate : targetCandidates) {
			ConvertiblePair convertiblePair = new ConvertiblePair(sourceCandidate, targetCandidate);
			GenericConverter converter = getRegisteredConverter(sourceType, targetType, convertiblePair);
			if (converter != null) {
				return converter;
			}
		}
	}
	return null;
}
 
Example #3
Source File: GenericConversionService.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Nullable
private GenericConverter getRegisteredConverter(TypeDescriptor sourceType,
		TypeDescriptor targetType, ConvertiblePair convertiblePair) {

	// Check specifically registered converters
	ConvertersForPair convertersForPair = this.converters.get(convertiblePair);
	if (convertersForPair != null) {
		GenericConverter converter = convertersForPair.getConverter(sourceType, targetType);
		if (converter != null) {
			return converter;
		}
	}
	// Check ConditionalConverters for a dynamic match
	for (GenericConverter globalConverter : this.globalConverters) {
		if (((ConditionalConverter) globalConverter).matches(sourceType, targetType)) {
			return globalConverter;
		}
	}
	return null;
}
 
Example #4
Source File: GenericConversionService.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Find a {@link GenericConverter} given a source and target type.
 * <p>This method will attempt to match all possible converters by working
 * through the class and interface hierarchy of the types.
 * @param sourceType the source type
 * @param targetType the target type
 * @return a matching {@link GenericConverter}, or {@code null} if none found
 */
public GenericConverter find(TypeDescriptor sourceType, TypeDescriptor targetType) {
	// Search the full type hierarchy
	List<Class<?>> sourceCandidates = getClassHierarchy(sourceType.getType());
	List<Class<?>> targetCandidates = getClassHierarchy(targetType.getType());
	for (Class<?> sourceCandidate : sourceCandidates) {
		for (Class<?> targetCandidate : targetCandidates) {
			ConvertiblePair convertiblePair = new ConvertiblePair(sourceCandidate, targetCandidate);
			GenericConverter converter = getRegisteredConverter(sourceType, targetType, convertiblePair);
			if (converter != null) {
				return converter;
			}
		}
	}
	return null;
}
 
Example #5
Source File: GenericConversionService.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private GenericConverter getRegisteredConverter(TypeDescriptor sourceType,
		TypeDescriptor targetType, ConvertiblePair convertiblePair) {

	// Check specifically registered converters
	ConvertersForPair convertersForPair = this.converters.get(convertiblePair);
	if (convertersForPair != null) {
		GenericConverter converter = convertersForPair.getConverter(sourceType, targetType);
		if (converter != null) {
			return converter;
		}
	}
	// Check ConditionalConverters for a dynamic match
	for (GenericConverter globalConverter : this.globalConverters) {
		if (((ConditionalConverter) globalConverter).matches(sourceType, targetType)) {
			return globalConverter;
		}
	}
	return null;
}
 
Example #6
Source File: GenericConversionService.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Nullable
private GenericConverter getRegisteredConverter(TypeDescriptor sourceType,
		TypeDescriptor targetType, ConvertiblePair convertiblePair) {

	// Check specifically registered converters
	ConvertersForPair convertersForPair = this.converters.get(convertiblePair);
	if (convertersForPair != null) {
		GenericConverter converter = convertersForPair.getConverter(sourceType, targetType);
		if (converter != null) {
			return converter;
		}
	}
	// Check ConditionalConverters for a dynamic match
	for (GenericConverter globalConverter : this.globalConverters) {
		if (((ConditionalConverter) globalConverter).matches(sourceType, targetType)) {
			return globalConverter;
		}
	}
	return null;
}
 
Example #7
Source File: GenericConversionService.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Find a {@link GenericConverter} given a source and target type.
 * <p>This method will attempt to match all possible converters by working
 * through the class and interface hierarchy of the types.
 * @param sourceType the source type
 * @param targetType the target type
 * @return a matching {@link GenericConverter}, or {@code null} if none found
 */
public GenericConverter find(TypeDescriptor sourceType, TypeDescriptor targetType) {
	// Search the full type hierarchy
	List<Class<?>> sourceCandidates = getClassHierarchy(sourceType.getType());
	List<Class<?>> targetCandidates = getClassHierarchy(targetType.getType());
	for (Class<?> sourceCandidate : sourceCandidates) {
		for (Class<?> targetCandidate : targetCandidates) {
			ConvertiblePair convertiblePair = new ConvertiblePair(sourceCandidate, targetCandidate);
			GenericConverter converter = getRegisteredConverter(sourceType, targetType, convertiblePair);
			if (converter != null) {
				return converter;
			}
		}
	}
	return null;
}
 
Example #8
Source File: GenericConversionService.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private GenericConverter getRegisteredConverter(TypeDescriptor sourceType,
		TypeDescriptor targetType, ConvertiblePair convertiblePair) {

	// Check specifically registered converters
	ConvertersForPair convertersForPair = this.converters.get(convertiblePair);
	if (convertersForPair != null) {
		GenericConverter converter = convertersForPair.getConverter(sourceType, targetType);
		if (converter != null) {
			return converter;
		}
	}
	// Check ConditionalConverters for a dynamic match
	for (GenericConverter globalConverter : this.globalConverters) {
		if (((ConditionalConverter) globalConverter).matches(sourceType, targetType)) {
			return globalConverter;
		}
	}
	return null;
}
 
Example #9
Source File: CustomConversions.java    From dubbox with Apache License 2.0 6 votes vote down vote up
private void registerConversion(Object converter) {
	Class<?> type = converter.getClass();
	boolean isWriting = type.isAnnotationPresent(WritingConverter.class);
	boolean isReading = type.isAnnotationPresent(ReadingConverter.class);

	if (!isReading && !isWriting) {
		isReading = true;
		isWriting = true;
	}

	if (converter instanceof GenericConverter) {
		GenericConverter genericConverter = (GenericConverter) converter;
		for (ConvertiblePair pair : genericConverter.getConvertibleTypes()) {
			register(new ConvertibleContext(pair, isReading, isWriting));
		}
	} else if (converter instanceof Converter) {
		Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(converter.getClass(), Converter.class);
		register(new ConvertibleContext(arguments[0], arguments[1], isReading, isWriting));
	} else {
		throw new IllegalArgumentException(
				"Unsupported Converter type! Expected either GenericConverter if Converter.");
	}
}
 
Example #10
Source File: CustomConversions.java    From dubbox with Apache License 2.0 6 votes vote down vote up
Class<?> getCustomTarget(Class<?> sourceType, Class<?> expectedTargetType, Iterable<ConvertiblePair> pairs) {
	Assert.notNull(sourceType);
	Assert.notNull(pairs);

	ConvertiblePair expectedTypePair = new ConvertiblePair(sourceType,
			expectedTargetType != null ? expectedTargetType : Any.class);

	if (cache.containsKey(expectedTypePair)) {
		Class<?> cachedTargetType = cache.get(expectedTypePair);
		return cachedTargetType != Any.class ? cachedTargetType : null;
	}

	for (ConvertiblePair typePair : pairs) {
		if (typePair.getSourceType().isAssignableFrom(sourceType)) {
			Class<?> targetType = typePair.getTargetType();
			if (expectedTargetType == null || targetType.isAssignableFrom(expectedTargetType)) {
				cache.putIfAbsent(expectedTypePair, targetType);
				return targetType;
			}
		}
	}

	cache.putIfAbsent(expectedTypePair, Any.class);
	return null;
}
 
Example #11
Source File: CustomConversions.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void register(ConvertibleContext context) {
	ConvertiblePair pair = context.getConvertible();
	if (context.isReading()) {
		readingPairs.add(pair);
	}
	if (context.isWriting()) {
		writingPairs.add(pair);
		customSimpleTypes.add(pair.getSourceType());
	}
}
 
Example #12
Source File: GenericConversionService.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private ConvertersForPair getMatchableConverters(ConvertiblePair convertiblePair) {
	ConvertersForPair convertersForPair = this.converters.get(convertiblePair);
	if (convertersForPair == null) {
		convertersForPair = new ConvertersForPair();
		this.converters.put(convertiblePair, convertersForPair);
	}
	return convertersForPair;
}
 
Example #13
Source File: GenericConversionService.java    From java-technology-stack with MIT License 5 votes vote down vote up
private ConvertersForPair getMatchableConverters(ConvertiblePair convertiblePair) {
	ConvertersForPair convertersForPair = this.converters.get(convertiblePair);
	if (convertersForPair == null) {
		convertersForPair = new ConvertersForPair();
		this.converters.put(convertiblePair, convertersForPair);
	}
	return convertersForPair;
}
 
Example #14
Source File: GenericConversionService.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void add(GenericConverter converter) {
	Set<ConvertiblePair> convertibleTypes = converter.getConvertibleTypes();
	if (convertibleTypes == null) {
		Assert.state(converter instanceof ConditionalConverter,
				"Only conditional converters may return null convertible types");
		this.globalConverters.add(converter);
	}
	else {
		for (ConvertiblePair convertiblePair : convertibleTypes) {
			ConvertersForPair convertersForPair = getMatchableConverters(convertiblePair);
			convertersForPair.add(converter);
		}
	}
}
 
Example #15
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 #16
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 #17
Source File: CustomConversions.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
 * Create new instance registering given converters
 *
 * @param converters
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public CustomConversions(List converters) {
	this.converters = (converters != null ? new ArrayList<Object>(converters) : new ArrayList<Object>());
	this.readingPairs = new HashSet<ConvertiblePair>();
	this.writingPairs = new HashSet<ConvertiblePair>();
	this.customSimpleTypes = new HashSet<Class<?>>();

	this.simpleTypeHolder = new SimpleTypeHolder(customSimpleTypes, SolrSimpleTypes.HOLDER);

	this.converters.add(GeoConverters.StringToPointConverter.INSTANCE);
	this.converters.add(GeoConverters.Point3DToStringConverter.INSTANCE);
	this.converters.add(new SolrjConverters.UpdateToSolrInputDocumentConverter());

	// Register Joda-Time converters only if Joda-Time was found in the
	// classpath.
	if (VersionUtil.isJodaTimeAvailable()) {
		this.converters.add(DateTimeConverters.DateToJodaDateTimeConverter.INSTANCE);
		this.converters.add(DateTimeConverters.JodaDateTimeToDateConverter.INSTANCE);
		this.converters.add(DateTimeConverters.DateToLocalDateTimeConverter.INSTANCE);
		this.converters.add(DateTimeConverters.JodaLocalDateTimeToDateConverter.INSTANCE);
	}

	for (Object converter : this.converters) {
		registerConversion(converter);
	}
}
 
Example #18
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 #19
Source File: GenericConversionService.java    From java-technology-stack with MIT License 5 votes vote down vote up
public void add(GenericConverter converter) {
	Set<ConvertiblePair> convertibleTypes = converter.getConvertibleTypes();
	if (convertibleTypes == null) {
		Assert.state(converter instanceof ConditionalConverter,
				"Only conditional converters may return null convertible types");
		this.globalConverters.add(converter);
	}
	else {
		for (ConvertiblePair convertiblePair : convertibleTypes) {
			ConvertersForPair convertersForPair = getMatchableConverters(convertiblePair);
			convertersForPair.add(converter);
		}
	}
}
 
Example #20
Source File: GenericConversionService.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public void add(GenericConverter converter) {
	Set<ConvertiblePair> convertibleTypes = converter.getConvertibleTypes();
	if (convertibleTypes == null) {
		Assert.state(converter instanceof ConditionalConverter,
				"Only conditional converters may return null convertible types");
		this.globalConverters.add(converter);
	}
	else {
		for (ConvertiblePair convertiblePair : convertibleTypes) {
			ConvertersForPair convertersForPair = getMatchableConverters(convertiblePair);
			convertersForPair.add(converter);
		}
	}
}
 
Example #21
Source File: ConverterRegistration.java    From spring-data-crate with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link ConverterRegistration}.
 *
 * @param convertiblePair must not be {@literal null}.
 * @param isReading whether to force to consider the converter for reading.
 * @param isWriting whether to force to consider the converter for reading.
 */
public ConverterRegistration(ConvertiblePair convertiblePair, boolean isReading, boolean isWriting) {
  notNull(convertiblePair, "Convertible Pair is required");

  this.convertiblePair = convertiblePair;
  reading = isReading;
  writing = isWriting;
}
 
Example #22
Source File: GenericConversionService.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private ConvertersForPair getMatchableConverters(ConvertiblePair convertiblePair) {
	ConvertersForPair convertersForPair = this.converters.get(convertiblePair);
	if (convertersForPair == null) {
		convertersForPair = new ConvertersForPair();
		this.converters.put(convertiblePair, convertersForPair);
	}
	return convertersForPair;
}
 
Example #23
Source File: EnumToMessageConverterTest.java    From springlets with Apache License 2.0 5 votes vote down vote up
@Test
public void checkValidConvertibleTypes() {
  // Prepare

  // Exercise
  Set<ConvertiblePair> convertibleTypes = converter.getConvertibleTypes();

  // Validate
  assertThat(convertibleTypes).isNotEmpty()
      .containsExactly(new ConvertiblePair(Enum.class, String.class));
}
 
Example #24
Source File: EntityToStringConverterTest.java    From springlets with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for {@link io.springlets.format.EntityToStringConverter#getConvertibleTypes()}.
 */
@Test
public void testGetConvertibleTypes() {
  // Prepare

  // Exercise
  Set<ConvertiblePair> convertibleTypes = converter.getConvertibleTypes();

  // Validate
  assertThat(convertibleTypes).isNotEmpty()
      .containsExactly(new ConvertiblePair(Object.class, String.class));
}
 
Example #25
Source File: GenericConversionService.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public void add(GenericConverter converter) {
	Set<ConvertiblePair> convertibleTypes = converter.getConvertibleTypes();
	if (convertibleTypes == null) {
		Assert.state(converter instanceof ConditionalConverter,
				"Only conditional converters may return null convertible types");
		this.globalConverters.add(converter);
	}
	else {
		for (ConvertiblePair convertiblePair : convertibleTypes) {
			ConvertersForPair convertersForPair = getMatchableConverters(convertiblePair);
			convertersForPair.add(converter);
		}
	}
}
 
Example #26
Source File: GenericConversionService.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private ConvertersForPair getMatchableConverters(ConvertiblePair convertiblePair) {
	ConvertersForPair convertersForPair = this.converters.get(convertiblePair);
	if (convertersForPair == null) {
		convertersForPair = new ConvertersForPair();
		this.converters.put(convertiblePair, convertersForPair);
	}
	return convertersForPair;
}
 
Example #27
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 #28
Source File: CustomConversions.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public ConvertiblePair getConvertible() {
	return convertible;
}
 
Example #29
Source File: GenericConversionService.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
	return null;
}
 
Example #30
Source File: GenericConversionService.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public ConverterAdapter(Converter<?, ?> converter, ResolvableType sourceType, ResolvableType targetType) {
	this.converter = (Converter<Object, Object>) converter;
	this.typeInfo = new ConvertiblePair(sourceType.toClass(), targetType.toClass());
	this.targetType = targetType;
}