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 by 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's 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(@NotNull BeanDescriptionFactory beanDescriptionFactory,
@NotNull LeafValueHandler leafValueHandler) |
| Modifier and Type | Method and Description |
|---|---|
@Nullable Object |
convertToBean(@Nullable Object value,
@NotNull TypeInformation beanType,
@NotNull ConvertErrorRecorder errorRecorder)
Creates an object of the given type from the given value.
|
protected @Nullable Object |
convertValueForType(@NotNull MappingContext context,
@Nullable Object value)
Main method for converting a value to another type.
|
protected @Nullable Object |
createBean(@NotNull MappingContext context,
@Nullable Object value)
Converts the provided value to the requested JavaBeans class if possible.
|
protected @NotNull Object |
createBeanMatchingType(@NotNull MappingContext mappingContext)
Creates an object matching the given type information.
|
protected @Nullable Collection |
createCollection(@NotNull MappingContext context,
@Nullable Object value)
Handles the creation of Collection properties.
|
protected @NotNull Collection |
createCollectionMatchingType(@NotNull MappingContext mappingContext)
Creates a Collection of a type which can be assigned to the provided type.
|
protected @Nullable Object |
createExportValueForSpecialTypes(@Nullable Object value,
@NotNull Set<UUID> knownUniqueComments)
Handles values of types which need special handling (such as Optional).
|
protected @Nullable Map |
createMap(@NotNull MappingContext context,
@Nullable Object value)
Handles the creation of a Map property.
|
protected @NotNull Map |
createMapMatchingType(@NotNull MappingContext mappingContext)
Creates a Map of a type which can be assigned to the provided type.
|
protected @NotNull Object |
createOptional(@NotNull MappingContext context,
@Nullable Object value) |
protected @NotNull MappingContext |
createRootMappingContext(@NotNull TypeInformation beanType,
@NotNull ConvertErrorRecorder errorRecorder) |
protected @NotNull BeanDescriptionFactory |
getBeanDescriptionFactory() |
protected @NotNull LeafValueHandler |
getLeafValueHandler() |
protected @Nullable Object |
handleSpecialTypes(@NotNull MappingContext context,
@Nullable Object value)
Handles special types in the bean mapping process which require special handling.
|
@Nullable Object |
toExportValue(@Nullable Object value)
Converts a complex type such as a JavaBean object to simple types suitable for exporting.
|
protected @Nullable Object |
toExportValue(@Nullable Object value,
@NotNull Set<UUID> knownUniqueComments)
Transforms the given value to an object suitable for the export to a configuration file.
|
protected static @Nullable Object |
unwrapReturnNull(@Nullable Object o) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitconvertToBeanpublic static final Object RETURN_NULL
public MapperImpl()
public MapperImpl(@NotNull
@NotNull BeanDescriptionFactory beanDescriptionFactory,
@NotNull
@NotNull LeafValueHandler leafValueHandler)
@NotNull protected final @NotNull BeanDescriptionFactory getBeanDescriptionFactory()
@NotNull protected final @NotNull LeafValueHandler getLeafValueHandler()
@NotNull protected @NotNull MappingContext createRootMappingContext(@NotNull @NotNull TypeInformation beanType, @NotNull @NotNull ConvertErrorRecorder errorRecorder)
@Nullable public @Nullable Object toExportValue(@Nullable @Nullable Object value)
MapperTypeBasedProperty.toExportValue(T) method.toExportValue in interface Mappervalue - the object to convert to its export value@Nullable protected @Nullable Object toExportValue(@Nullable @Nullable Object value, @NotNull @NotNull Set<UUID> knownUniqueComments)
value - the value to transformknownUniqueComments - UUIDs of comments which should not be repeated that have already been included@Nullable protected @Nullable Object createExportValueForSpecialTypes(@Nullable @Nullable Object value, @NotNull @NotNull Set<UUID> knownUniqueComments)
RETURN_NULL to
signal that null should be used as the export value of the provided value.value - the value to convertknownUniqueComments - UUIDs of comments which should not be repeated that have already been includedRETURN_NULL, or null if not applicable@Nullable protected static @Nullable Object unwrapReturnNull(@Nullable @Nullable Object o)
@Nullable public @Nullable Object convertToBean(@Nullable @Nullable Object value, @NotNull @NotNull TypeInformation beanType, @NotNull @NotNull ConvertErrorRecorder errorRecorder)
MapperconvertToBean in interface Mappervalue - the value to convert (typically a Map)beanType - the required typeerrorRecorder - error recorder to register errors even if a valid value is returned@Nullable protected @Nullable Object convertValueForType(@NotNull @NotNull MappingContext context, @Nullable @Nullable Object value)
context - the mapping contextvalue - the value to convert from@Nullable protected @Nullable Object handleSpecialTypes(@NotNull @NotNull MappingContext context, @Nullable @Nullable Object value)
context - the mapping contextvalue - the value to convert from@Nullable protected @Nullable Collection createCollection(@NotNull @NotNull MappingContext context, @Nullable @Nullable Object value)
context - the mapping contextvalue - the value to map from@NotNull protected @NotNull Collection createCollectionMatchingType(@NotNull @NotNull MappingContext mappingContext)
mappingContext - the current mapping context with a collection type@Nullable protected @Nullable Map createMap(@NotNull @NotNull MappingContext context, @Nullable @Nullable Object value)
context - mapping contextvalue - value to map from@NotNull protected @NotNull Map createMapMatchingType(@NotNull @NotNull MappingContext mappingContext)
mappingContext - the current mapping context with a map type@NotNull protected @NotNull Object createOptional(@NotNull @NotNull MappingContext context, @Nullable @Nullable Object value)
@Nullable protected @Nullable Object createBean(@NotNull @NotNull MappingContext context, @Nullable @Nullable Object value)
context - mapping context (incl. desired type)value - the value from the property resource@NotNull protected @NotNull Object createBeanMatchingType(@NotNull @NotNull MappingContext mappingContext)
mappingContext - current mapping contextCopyright © 2016–2023 The AuthMe Team. All rights reserved.