Top Description Fields Constructors Methods
org.springframework.core.convert.support

pack-priv final Class ObjectToObjectConverter

extends Object
implements ConditionalGenericConverter
Class Inheritance
All Implemented Interfaces
org.springframework.core.convert.converter.ConditionalGenericConverter, org.springframework.core.convert.converter.ConditionalConverter, org.springframework.core.convert.converter.GenericConverter
Imports
java.lang.reflect.Constructor, .Executable, .InvocationTargetException, .Method, .Modifier, java.util.Collections, .Map, .Set, org.springframework.core.convert.ConversionFailedException, .TypeDescriptor, org.springframework.core.convert.converter.ConditionalGenericConverter, org.springframework.lang.Nullable, org.springframework.util.ClassUtils, .ConcurrentReferenceHashMap, .ReflectionUtils

Generic converter that uses conventions to convert a source object to a targetType by delegating to a method on the source object or to a static factory method or constructor on the targetType.

Conversion Algorithm

  1. Invoke a non-static to[targetType.simpleName]() method on the source object that has a return type assignable to targetType, if such a method exists. For example, org.example.Bar Foo#toBar() is a method that follows this convention.
  2. Otherwise invoke a static valueOf(sourceType) or Java 8 style static of(sourceType) or from(sourceType) method on the targetType that has a return type related to targetType, if such a method exists. For example, a static Foo.of(sourceType) method that returns a Foo, SuperFooType, or SubFooType is a method that follows this convention. java.time.ZoneId#of(String) is a concrete example of such a static factory method which returns a subtype of ZoneId.
  3. Otherwise invoke a constructor on the targetType that accepts a single sourceType argument, if such a constructor exists.
  4. Otherwise throw a ConversionFailedException or IllegalStateException.

Warning

this converter does not support the Object#toString() or String#valueOf(Object) methods for converting from a sourceType to java.lang.String. For toString() support, use FallbackObjectToStringConverter instead.

Authors
Keith Donald, Juergen Hoeller, Sam Brannen
Since
3.0
See Also
FallbackObjectToStringConverter

Field Summary

Modifier and TypeField and Description
private static final Map<Class<?>, Executable>

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
private static boolean
areRelatedTypes(Class<?> type1, Class<?> type2)

Determine if the two types reside in the same type hierarchy (i.e., type 1 is assignable to type 2 or vice versa).

public Object
convert(Object
the source object to convert (may be null)
source
,
TypeDescriptor
the type descriptor of the field we are converting from
sourceType
,
TypeDescriptor
the type descriptor of the field we are converting to
targetType
)

Implements org.springframework.core.convert.converter.GenericConverter.convert.

Convert the source object to the targetType described by the TypeDescriptor.

private static Constructor<?>
determineFactoryConstructor(Class<?> targetClass, Class<?> sourceClass)

private static Method
determineFactoryMethod(Class<?> targetClass, Class<?> sourceClass)

private static Method
determineToMethod(Class<?> targetClass, Class<?> sourceClass)

public Set<GenericConverter.ConvertiblePair>
getConvertibleTypes()

Implements org.springframework.core.convert.converter.GenericConverter.getConvertibleTypes.

Return the source and target types that this converter can convert between.

private static Executable
getValidatedExecutable(Class<?> targetClass, Class<?> sourceClass)

pack-priv static boolean
hasConversionMethodOrConstructor(Class<?> targetClass, Class<?> sourceClass)

private static boolean
isApplicable(Executable executable, Class<?> sourceClass)

public boolean
matches(TypeDescriptor
the type descriptor of the field we are converting from
sourceType
,
TypeDescriptor
the type descriptor of the field we are converting to
targetType
)

Implements org.springframework.core.convert.converter.ConditionalConverter.matches.

Should the conversion from sourceType to targetType currently under consideration be selected?

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

conversionExecutableCacheback to summary
private static final Map<Class<?>, Executable> conversionExecutableCache

Constructor Detail

ObjectToObjectConverterback to summary
pack-priv ObjectToObjectConverter()

Method Detail

areRelatedTypesback to summary
private static boolean areRelatedTypes(Class<?> type1, Class<?> type2)

Determine if the two types reside in the same type hierarchy (i.e., type 1 is assignable to type 2 or vice versa).

Since
5.3.21
See Also
ClassUtils#isAssignable(Class, Class)
convertback to summary
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType)

Implements org.springframework.core.convert.converter.GenericConverter.convert.

Doc from org.springframework.core.convert.converter.GenericConverter.convert.

Convert the source object to the targetType described by the TypeDescriptor.

Parameters
source:Object

the source object to convert (may be null)

sourceType:TypeDescriptor

the type descriptor of the field we are converting from

targetType:TypeDescriptor

the type descriptor of the field we are converting to

Returns:Object

the converted object

Annotations
@Override
@Nullable
determineFactoryConstructorback to summary
private static Constructor<?> determineFactoryConstructor(Class<?> targetClass, Class<?> sourceClass)
Annotations
@Nullable
determineFactoryMethodback to summary
private static Method determineFactoryMethod(Class<?> targetClass, Class<?> sourceClass)
Annotations
@Nullable
determineToMethodback to summary
private static Method determineToMethod(Class<?> targetClass, Class<?> sourceClass)
Annotations
@Nullable
getConvertibleTypesback to summary
public Set<GenericConverter.ConvertiblePair> getConvertibleTypes()

Implements org.springframework.core.convert.converter.GenericConverter.getConvertibleTypes.

Doc from org.springframework.core.convert.converter.GenericConverter.getConvertibleTypes.

Return the source and target types that this converter can convert between.

Each entry is a convertible source-to-target type pair.

For conditional converters this method may return null to indicate all source-to-target pairs should be considered.

Annotations
@Override
getValidatedExecutableback to summary
private static Executable getValidatedExecutable(Class<?> targetClass, Class<?> sourceClass)
Annotations
@Nullable
hasConversionMethodOrConstructorback to summary
pack-priv static boolean hasConversionMethodOrConstructor(Class<?> targetClass, Class<?> sourceClass)
isApplicableback to summary
private static boolean isApplicable(Executable executable, Class<?> sourceClass)
matchesback to summary
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType)

Implements org.springframework.core.convert.converter.ConditionalConverter.matches.

Doc from org.springframework.core.convert.converter.ConditionalConverter.matches.

Should the conversion from sourceType to targetType currently under consideration be selected?

Parameters
sourceType:TypeDescriptor

the type descriptor of the field we are converting from

targetType:TypeDescriptor

the type descriptor of the field we are converting to

Returns:boolean

true if conversion should be performed, false otherwise

Annotations
@Override