public class MapperImpl extends Object implements Mapper
Mapper.
Maps a section of a property resource to the provided JavaBean class. The mapping is based on the bean's properties,
whose names must correspond with the names in the property resource. For example, if a JavaBean class has a property
length and should be mapped from the property resource's value at path definition, the mapper will
look up definition.length to get the value of the JavaBean property.
Classes must be JavaBeans. These are simple classes with private fields, accompanied with getters and setters.
The mapper only considers properties which have both a getter and a setter method. Any Java class without
at least one property with both a getter and a setter is not considered as a JavaBean class. Such classes can
be supported by implementing a custom LeafValueHandler that performs the conversion from the value coming
from the property reader to an object of the class' type.
Recursion: the mapping of values to a JavaBean is performed recursively, i.e. a JavaBean may have other JavaBeans as fields and generic types at any arbitrary "depth."
Collections are only supported if they are explicitly typed, i.e. a field of List<String>
is supported but List<?> and List<T extends Number> are not supported. Specifically, you may
only declare fields of type List or Set, or a parent type (Collection
or Iterable).
Fields of type Map are supported also, with similar limitations. Additionally, maps may only have
String as key type, but no restrictions are imposed on the value type.
JavaBeans may have optional fields. If the mapper cannot map the property resource value to the corresponding
field, it only treats it as a failure if the field's value is null. If the field has a default value assigned
to it on initialization, the default value remains and the mapping process continues. A JavaBean field whose value is
null signifies a failure and stops the mapping process immediately.
| Modifier and Type | Field and Description |
|---|---|
static Object |
RETURN_NULL
Marker object to signal that null is meant to be used as value.
|
| Constructor and Description |
|---|
MapperImpl() |
MapperImpl(BeanDescriptionFactory beanDescriptionFactory,
LeafValueHandler leafValueHandler) |
| Modifier and Type | Method and Description |
|---|---|
Object |
convertToBean(Object value,
TypeInformation beanType)
Creates an object of the given type from the given value.
|
protected Object |
convertValueForType(MappingContext context,
Object value)
Main method for converting a value to another type.
|
protected Object |
createBean(MappingContext context,
Object value)
Converts the provided value to the requested JavaBeans class if possible.
|
protected Object |
createBeanMatchingType(TypeInformation typeInformation)
Creates an object matching the given type information.
|
protected Collection |
createCollection(MappingContext context,
Object value)
Handles the creation of Collection properties.
|
protected Collection |
createCollectionMatchingType(TypeInformation typeInformation)
Creates a Collection of a type which can be assigned to the provided type.
|
protected Object |
createExportValueForSpecialTypes(Object value)
Handles values of types which need special handling (such as Optional).
|
protected Map |
createMap(MappingContext context,
Object value)
Handles the creation of a Map property.
|
protected Map |
createMapMatchingType(TypeInformation typeInformation)
Creates a Map of a type which can be assigned to the provided type.
|
protected Object |
createOptional(MappingContext context,
Object value) |
protected MappingContext |
createRootMappingContext(TypeInformation beanType) |
protected BeanDescriptionFactory |
getBeanDescriptionFactory() |
protected LeafValueHandler |
getLeafValueHandler() |
protected Object |
handleSpecialTypes(MappingContext context,
Object value)
Handles special types in the bean mapping process which require special handling.
|
Object |
toExportValue(Object value)
Converts a complex type such as a JavaBean object to simple types suitable for exporting.
|
protected static Object |
unwrapReturnNull(Object o) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitconvertToBeanpublic static final Object RETURN_NULL
public MapperImpl()
public MapperImpl(BeanDescriptionFactory beanDescriptionFactory, LeafValueHandler leafValueHandler)
protected final BeanDescriptionFactory getBeanDescriptionFactory()
protected final LeafValueHandler getLeafValueHandler()
protected MappingContext createRootMappingContext(TypeInformation beanType)
public Object toExportValue(Object value)
MapperBeanProperty.toExportValue(T) method.toExportValue in interface Mappervalue - the object to convert to its export value@Nullable protected Object createExportValueForSpecialTypes(Object value)
RETURN_NULL to $
signal that null should be used as the export value of the provided value.value - the value to convertRETURN_NULL, or null if not applicable@Nullable public Object convertToBean(Object value, TypeInformation beanType)
MapperconvertToBean in interface Mappervalue - the value to convert (typically a Map)beanType - the required type@Nullable protected Object convertValueForType(MappingContext context, Object value)
context - the mapping contextvalue - the value to convert from@Nullable protected Object handleSpecialTypes(MappingContext context, Object value)
context - the mapping contextvalue - the value to convert from@Nullable protected Collection createCollection(MappingContext context, Object value)
context - the mapping contextvalue - the value to map fromprotected Collection createCollectionMatchingType(TypeInformation typeInformation)
typeInformation - the required collection type@Nullable protected Map createMap(MappingContext context, Object value)
context - mapping contextvalue - value to map fromprotected Map createMapMatchingType(TypeInformation typeInformation)
typeInformation - the required map typeprotected Object createOptional(MappingContext context, Object value)
@Nullable protected Object createBean(MappingContext context, Object value)
context - mapping context (incl. desired type)value - the value from the property resourceprotected Object createBeanMatchingType(TypeInformation typeInformation)
typeInformation - the required typeCopyright © 2016–2018 The AuthMe Team. All rights reserved.