final class ObjectToObjectConverter extends java.lang.Object implements ConditionalGenericConverter
targetType
by delegating to a method on the source object or to
a static factory method or constructor on the targetType
.
to[targetType.simpleName]()
method on the
source object that has a return type equal to targetType
, if such
a method exists. For example, org.example.Bar Foo#toBar()
is a
method that follows this convention.
valueOf(sourceType)
or Java
8 style static of(sourceType)
or from(sourceType)
method on the targetType
, if such a method exists.
targetType
that accepts
a single sourceType
argument, if such a constructor exists.
ConversionFailedException
.
Warning: this converter does not support the
Object.toString()
method for converting from a sourceType
to java.lang.String
. For toString()
support, use
FallbackObjectToStringConverter
instead.
FallbackObjectToStringConverter
GenericConverter.ConvertiblePair
Modifier and Type | Field and Description |
---|---|
private static java.util.Map<java.lang.Class<?>,java.lang.reflect.Member> |
conversionMemberCache |
Constructor and Description |
---|
ObjectToObjectConverter() |
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
convert(java.lang.Object source,
TypeDescriptor sourceType,
TypeDescriptor targetType)
Convert the source object to the targetType described by the
TypeDescriptor . |
private static java.lang.reflect.Constructor<?> |
determineFactoryConstructor(java.lang.Class<?> targetClass,
java.lang.Class<?> sourceClass) |
private static java.lang.reflect.Method |
determineFactoryMethod(java.lang.Class<?> targetClass,
java.lang.Class<?> sourceClass) |
private static java.lang.reflect.Method |
determineToMethod(java.lang.Class<?> targetClass,
java.lang.Class<?> sourceClass) |
java.util.Set<GenericConverter.ConvertiblePair> |
getConvertibleTypes()
Return the source and target types that this converter can convert between.
|
private static java.lang.reflect.Member |
getValidatedMember(java.lang.Class<?> targetClass,
java.lang.Class<?> sourceClass) |
(package private) static boolean |
hasConversionMethodOrConstructor(java.lang.Class<?> targetClass,
java.lang.Class<?> sourceClass) |
private static boolean |
isApplicable(java.lang.reflect.Member member,
java.lang.Class<?> sourceClass) |
boolean |
matches(TypeDescriptor sourceType,
TypeDescriptor targetType)
Should the conversion from
sourceType to targetType currently under
consideration be selected? |
private static final java.util.Map<java.lang.Class<?>,java.lang.reflect.Member> conversionMemberCache
public java.util.Set<GenericConverter.ConvertiblePair> getConvertibleTypes()
GenericConverter
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.
getConvertibleTypes
in interface GenericConverter
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType)
ConditionalConverter
sourceType
to targetType
currently under
consideration be selected?matches
in interface ConditionalConverter
sourceType
- the type descriptor of the field we are converting fromtargetType
- the type descriptor of the field we are converting to@Nullable public java.lang.Object convert(@Nullable java.lang.Object source, TypeDescriptor sourceType, TypeDescriptor targetType)
GenericConverter
TypeDescriptor
.convert
in interface GenericConverter
source
- the source object to convert (may be null
)sourceType
- the type descriptor of the field we are converting fromtargetType
- the type descriptor of the field we are converting tostatic boolean hasConversionMethodOrConstructor(java.lang.Class<?> targetClass, java.lang.Class<?> sourceClass)
@Nullable private static java.lang.reflect.Member getValidatedMember(java.lang.Class<?> targetClass, java.lang.Class<?> sourceClass)
private static boolean isApplicable(java.lang.reflect.Member member, java.lang.Class<?> sourceClass)
@Nullable private static java.lang.reflect.Method determineToMethod(java.lang.Class<?> targetClass, java.lang.Class<?> sourceClass)
@Nullable private static java.lang.reflect.Method determineFactoryMethod(java.lang.Class<?> targetClass, java.lang.Class<?> sourceClass)
@Nullable private static java.lang.reflect.Constructor<?> determineFactoryConstructor(java.lang.Class<?> targetClass, java.lang.Class<?> sourceClass)