Package org.modelmapper
Class ModelMapper
- java.lang.Object
-
- org.modelmapper.ModelMapper
-
public class ModelMapper extends Object
ModelMapper - Performs object mapping, maintainsConfiguration
and storesTypeMaps
.- To perform object mapping use
map
. - To configure the mapping of one type to another use
createTypeMap
. - To add mappings for specific properties use
addMappings
supplying aPropertyMap
. - To configure ModelMapper use
getConfiguration()
. - To validate mappings use
validate()
.
- To perform object mapping use
-
-
Constructor Summary
Constructors Constructor Description ModelMapper()
Creates a new ModelMapper.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <S,D>
voidaddConverter(Converter<S,D> converter)
Registers theconverter
to use when mapping instances of typesS
toD
.<S,D>
voidaddConverter(Converter<S,D> converter, Class<S> sourceType, Class<D> destinationType)
Registers theconverter
to use when mapping instances of typesS
toD
.<S,D>
TypeMap<S,D>addMappings(PropertyMap<S,D> propertyMap)
Adds mappings from thepropertyMap
into the TypeMap corresponding to source typeS
and destination typeD
.<S,D>
TypeMap<S,D>createTypeMap(Class<S> sourceType, Class<D> destinationType)
Creates a TypeMap for thesourceType
anddestinationType
using the ModelMapper's configuration.<S,D>
TypeMap<S,D>createTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName)
Creates a TypeMap for thesourceType
anddestinationType
identified by thetypeMapName
using the ModelMapper's configuration.<S,D>
TypeMap<S,D>createTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName, Configuration configuration)
Creates a TypeMap for thesourceType
anddestinationType
identified by thetypeMapName
using theconfiguration
.<S,D>
TypeMap<S,D>createTypeMap(Class<S> sourceType, Class<D> destinationType, Configuration configuration)
Creates a TypeMap for thesourceType
anddestinationType
using theconfiguration
.<S,D>
TypeMap<S,D>createTypeMap(S source, Class<D> destinationType)
Creates a TypeMap for thesource
's type anddestinationType
using the ModelMapper's configuration.<S,D>
TypeMap<S,D>createTypeMap(S source, Class<D> destinationType, String typeMapName)
Creates a TypeMap for thesource
's type anddestinationType
identified by thetypeMapName
using the ModelMapper's configuration.<S,D>
TypeMap<S,D>createTypeMap(S source, Class<D> destinationType, String typeMapName, Configuration configuration)
Creates a TypeMap for thesource
's type anddestinationType
identified by thetypeMapName
using theconfiguration
.<S,D>
TypeMap<S,D>createTypeMap(S source, Class<D> destinationType, Configuration configuration)
Creates a TypeMap for thesource
's type anddestinationType
using theconfiguration
.<S,D>
TypeMap<S,D>emptyTypeMap(Class<S> sourceType, Class<D> destinationType)
Creates an empty TypeMap for thesourceType
,destinationType
.<S,D>
TypeMap<S,D>emptyTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName)
Creates an empty TypeMap for thesourceType
,destinationType
.Configuration
getConfiguration()
Returns the ModelMapper's configuration.<S,D>
TypeMap<S,D>getTypeMap(Class<S> sourceType, Class<D> destinationType)
Returns the TypeMap for thesourceType
anddestinationType
, else returnsnull
if none exists.<S,D>
TypeMap<S,D>getTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName)
Returns the TypeMap for thesourceType
,destinationType
andtypeMapName
, else returnsnull
if none exists.Collection<TypeMap<?,?>>
getTypeMaps()
Returns all TypeMaps for the ModelMapper.<D> D
map(Object source, Class<D> destinationType)
Mapssource
to an instance ofdestinationType
.<D> D
map(Object source, Class<D> destinationType, String typeMapName)
Mapssource
to an instance ofdestinationType
.void
map(Object source, Object destination)
Mapssource
todestination
.void
map(Object source, Object destination, String typeMapName)
Mapssource
todestination
.<D> D
map(Object source, Type destinationType)
Mapssource
to an instance ofdestinationType
.<D> D
map(Object source, Type destinationType, String typeMapName)
Mapssource
to an instance ofdestinationType
.ModelMapper
registerModule(Module module)
Register a module<S,D>
TypeMap<S,D>typeMap(Class<S> sourceType, Class<D> destinationType)
Returns the TypeMap for thesourceType
,destinationType
, creates TypeMap automatically if none exists.<S,D>
TypeMap<S,D>typeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName)
Returns the TypeMap for thesourceType
,destinationType
, andtypeMapName
creates TypeMap automatically if none exists.void
validate()
Validates that every top level destination property for each configured TypeMap is mapped to one and only one source property, or that aConverter
wasset
for the TypeMap.
-
-
-
Method Detail
-
addConverter
public <S,D> void addConverter(Converter<S,D> converter)
Registers theconverter
to use when mapping instances of typesS
toD
. Theconverter
will beset
against TypeMap corresponding to theconverter
's type argumentsS
andD
.- Type Parameters:
S
- source typeD
- destination type- Parameters:
converter
- to register- Throws:
IllegalArgumentException
- ifconverter
is null or if type argumentsS
andD
are not declared for theconverter
- See Also:
TypeMap.setConverter(Converter)
-
addConverter
public <S,D> void addConverter(Converter<S,D> converter, Class<S> sourceType, Class<D> destinationType)
Registers theconverter
to use when mapping instances of typesS
toD
. Theconverter
will beset
against TypeMap corresponding to theconverter
's type argumentsS
andD
.- Type Parameters:
S
- source typeD
- destination type- Parameters:
converter
- to register- Throws:
IllegalArgumentException
- ifconverter
is null or if type argumentsS
andD
are not declared for theconverter
- See Also:
TypeMap.setConverter(Converter)
-
addMappings
public <S,D> TypeMap<S,D> addMappings(PropertyMap<S,D> propertyMap)
Adds mappings from thepropertyMap
into the TypeMap corresponding to source typeS
and destination typeD
. Explicit mappings defined in thepropertyMap
will override any implicit mappings for the same properties.- Type Parameters:
S
- source typeD
- destination type- Parameters:
propertyMap
- from which mappings should be loaded- Returns:
- TypeMap corresponding to the
propertyMap
- Throws:
IllegalArgumentException
- ifpropertyMap
is nullConfigurationException
- if a configuration error occurs while adding mappings for thepropertyMap
-
createTypeMap
public <S,D> TypeMap<S,D> createTypeMap(Class<S> sourceType, Class<D> destinationType)
Creates a TypeMap for thesourceType
anddestinationType
using the ModelMapper's configuration.- Type Parameters:
S
- source typeD
- destination type- Parameters:
sourceType
-destinationType
-- Throws:
IllegalArgumentException
- ifsourceType
ordestinationType
are nullIllegalStateException
- if a TypeMap already exists forsourceType
anddestinationType
ConfigurationException
- if the ModelMapper cannot create the TypeMap- See Also:
getTypeMap(Class, Class)
-
createTypeMap
public <S,D> TypeMap<S,D> createTypeMap(Class<S> sourceType, Class<D> destinationType, Configuration configuration)
Creates a TypeMap for thesourceType
anddestinationType
using theconfiguration
.- Type Parameters:
S
- source typeD
- destination type- Parameters:
sourceType
-destinationType
-configuration
- to apply to TypeMap- Throws:
IllegalArgumentException
- ifsourceType
,destinationType
orconfiguration
are nullIllegalStateException
- if a TypeMap already exists forsourceType
anddestinationType
ConfigurationException
- if the ModelMapper cannot create the TypeMap- See Also:
getTypeMap(Class, Class)
-
createTypeMap
public <S,D> TypeMap<S,D> createTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName)
Creates a TypeMap for thesourceType
anddestinationType
identified by thetypeMapName
using the ModelMapper's configuration.- Type Parameters:
S
- source typeD
- destination type- Parameters:
sourceType
-destinationType
-typeMapName
-- Throws:
IllegalArgumentException
- ifsourceType
,destinationType
ortypeMapName
are nullIllegalStateException
- if a TypeMap already exists forsourceType
,destinationType
andtypeMapName
ConfigurationException
- if the ModelMapper cannot create the TypeMap- See Also:
getTypeMap(Class, Class, String)
-
createTypeMap
public <S,D> TypeMap<S,D> createTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName, Configuration configuration)
Creates a TypeMap for thesourceType
anddestinationType
identified by thetypeMapName
using theconfiguration
.- Type Parameters:
S
- source typeD
- destination type- Parameters:
sourceType
-destinationType
-typeMapName
-configuration
- to apply to TypeMap- Throws:
IllegalArgumentException
- ifsourceType
,destinationType
,typeMapName
orconfiguration
are nullIllegalStateException
- if a TypeMap already exists forsourceType
,destinationType
andtypeMapName
ConfigurationException
- if the ModelMapper cannot create the TypeMap- See Also:
getTypeMap(Class, Class, String)
-
createTypeMap
public <S,D> TypeMap<S,D> createTypeMap(S source, Class<D> destinationType)
Creates a TypeMap for thesource
's type anddestinationType
using the ModelMapper's configuration. Useful for creating TypeMaps for generic source data structures.- Type Parameters:
S
- source typeD
- destination type- Parameters:
source
-destinationType
-- Throws:
IllegalArgumentException
- ifsource
ordestinationType
are nullIllegalStateException
- if a TypeMap already exists forsource
's type anddestinationType
ConfigurationException
- if the ModelMapper cannot create the TypeMap- See Also:
getTypeMap(Class, Class)
-
createTypeMap
public <S,D> TypeMap<S,D> createTypeMap(S source, Class<D> destinationType, Configuration configuration)
Creates a TypeMap for thesource
's type anddestinationType
using theconfiguration
. Useful for creating TypeMaps for generic source data structures.- Type Parameters:
S
- source typeD
- destination type- Parameters:
source
-destinationType
-configuration
- to apply to TypeMap- Throws:
IllegalArgumentException
- ifsource
,destinationType
orconfiguration
are nullIllegalStateException
- if a TypeMap already exists forsource
's type anddestinationType
ConfigurationException
- if the ModelMapper cannot create the TypeMap- See Also:
getTypeMap(Class, Class)
-
createTypeMap
public <S,D> TypeMap<S,D> createTypeMap(S source, Class<D> destinationType, String typeMapName)
Creates a TypeMap for thesource
's type anddestinationType
identified by thetypeMapName
using the ModelMapper's configuration. Useful for creating TypeMaps for generic source data structures.- Type Parameters:
S
- source typeD
- destination type- Parameters:
source
-destinationType
-typeMapName
-- Throws:
IllegalArgumentException
- ifsource
,destinationType
ortypeMapName
are nullIllegalStateException
- if a TypeMap already exists forsource
's type,destinationType
andtypeMapName
ConfigurationException
- if the ModelMapper cannot create the TypeMap- See Also:
getTypeMap(Class, Class, String)
-
createTypeMap
public <S,D> TypeMap<S,D> createTypeMap(S source, Class<D> destinationType, String typeMapName, Configuration configuration)
Creates a TypeMap for thesource
's type anddestinationType
identified by thetypeMapName
using theconfiguration
. Useful for creating TypeMaps for generic source data structures.- Type Parameters:
S
- source typeD
- destination type- Parameters:
source
-destinationType
-typeMapName
-configuration
- to apply to TypeMap- Throws:
IllegalArgumentException
- ifsource
,destinationType
,typeMapName
orconfiguration
are nullIllegalStateException
- if a TypeMap already exists forsource
's type,destinationType
andtypeMapName
ConfigurationException
- if the ModelMapper cannot create the TypeMap- See Also:
getTypeMap(Class, Class, String)
-
getConfiguration
public Configuration getConfiguration()
Returns the ModelMapper's configuration.
-
getTypeMap
public <S,D> TypeMap<S,D> getTypeMap(Class<S> sourceType, Class<D> destinationType)
Returns the TypeMap for thesourceType
anddestinationType
, else returnsnull
if none exists.- Type Parameters:
S
- source typeD
- destination type- Throws:
IllegalArgumentException
- issourceType
ordestinationType
are null- See Also:
createTypeMap(Class, Class)
-
getTypeMap
public <S,D> TypeMap<S,D> getTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName)
Returns the TypeMap for thesourceType
,destinationType
andtypeMapName
, else returnsnull
if none exists.- Type Parameters:
S
- source typeD
- destination type- Throws:
IllegalArgumentException
- issourceType
,destinationType
ortypeMapName
are null- See Also:
createTypeMap(Class, Class, String)
-
typeMap
public <S,D> TypeMap<S,D> typeMap(Class<S> sourceType, Class<D> destinationType)
Returns the TypeMap for thesourceType
,destinationType
, creates TypeMap automatically if none exists.- Type Parameters:
S
- source typeD
- destination type- Throws:
IllegalArgumentException
- issourceType
,destinationType
are null
-
typeMap
public <S,D> TypeMap<S,D> typeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName)
Returns the TypeMap for thesourceType
,destinationType
, andtypeMapName
creates TypeMap automatically if none exists.- Type Parameters:
S
- source typeD
- destination type- Throws:
IllegalArgumentException
- issourceType
,destinationType
ortypeMapName
are null
-
emptyTypeMap
public <S,D> TypeMap<S,D> emptyTypeMap(Class<S> sourceType, Class<D> destinationType)
Creates an empty TypeMap for thesourceType
,destinationType
.- Type Parameters:
S
- source typeD
- destination type- Throws:
IllegalArgumentException
- issourceType
ordestinationType
are null, orTypeMap<SourceType, DestinationType
already defined in the TypeMapStore
-
emptyTypeMap
public <S,D> TypeMap<S,D> emptyTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName)
Creates an empty TypeMap for thesourceType
,destinationType
.- Type Parameters:
S
- source typeD
- destination type- Throws:
IllegalArgumentException
- issourceType
ordestinationType
are null, orTypeMap<Source Type, DestinationType
already defined in the TypeMapStore
-
getTypeMaps
public Collection<TypeMap<?,?>> getTypeMaps()
Returns all TypeMaps for the ModelMapper.
-
map
public <D> D map(Object source, Class<D> destinationType)
Mapssource
to an instance ofdestinationType
. Mapping is performed according to the corresponding TypeMap. If no TypeMap exists forsource.getClass()
anddestinationType
then one is created.- Type Parameters:
D
- destination type- Parameters:
source
- object to map fromdestinationType
- type to map to- Returns:
- fully mapped instance of
destinationType
- Throws:
IllegalArgumentException
- ifsource
ordestinationType
are nullConfigurationException
- if the ModelMapper cannot find or create a TypeMap for the argumentsMappingException
- if a runtime error occurs while mapping
-
map
public <D> D map(Object source, Class<D> destinationType, String typeMapName)
Mapssource
to an instance ofdestinationType
. Mapping is performed according to the corresponding TypeMap for thetypeMapName
. If no TypeMap exists for thesource.getClass()
,destinationType
andtypeMapName
then one is created.- Type Parameters:
D
- destination type- Parameters:
source
- object to map fromdestinationType
- type to map totypeMapName
- name of existing TypeMap to use mappings from- Returns:
- fully mapped instance of
destinationType
- Throws:
IllegalArgumentException
- ifsource
,destinationType
ortypeMapName
are nullConfigurationException
- if the ModelMapper cannot find or create a TypeMap for the argumentsMappingException
- if a runtime error occurs while mapping
-
map
public void map(Object source, Object destination)
Mapssource
todestination
. Mapping is performed according to the corresponding TypeMap. If no TypeMap exists forsource.getClass()
anddestination.getClass()
then one is created.- Parameters:
source
- object to map fromdestination
- object to map to- Throws:
IllegalArgumentException
- ifsource
ordestination
are nullConfigurationException
- if the ModelMapper cannot find or create a TypeMap for the argumentsMappingException
- if an error occurs while mapping
-
map
public void map(Object source, Object destination, String typeMapName)
Mapssource
todestination
. Mapping is performed according to the corresponding TypeMap for thetypeMapName
. If no TypeMap exists for thesource.getClass()
,destination.getClass()
andtypeMapName
then one is created.- Parameters:
source
- object to map fromdestination
- object to map totypeMapName
- name of existing TypeMap to use mappings from- Throws:
IllegalArgumentException
- ifsource
,destination
ortypeMapName
are nullConfigurationException
- if the ModelMapper cannot find or create a TypeMap for the argumentsMappingException
- if an error occurs while mapping
-
map
public <D> D map(Object source, Type destinationType)
Mapssource
to an instance ofdestinationType
. Mapping is performed according to the corresponding TypeMap. If no TypeMap exists forsource.getClass()
anddestinationType
then one is created.To map a parameterized destination type, subclass
TypeToken
and obtain its Type:Type listType = new TypeToken<List<String>>() {}.getType(); List<String> strings = modelMapper.map(source, listType);
- Type Parameters:
D
- destination type- Parameters:
source
- object to map fromdestinationType
- type to map to- Returns:
- fully mapped instance of
destinationType
- Throws:
IllegalArgumentException
- ifsource
ordestinationType
are nullConfigurationException
- if the ModelMapper cannot find or create the TypeMapMappingException
- if a runtime error occurs while mapping
-
map
public <D> D map(Object source, Type destinationType, String typeMapName)
Mapssource
to an instance ofdestinationType
. Mapping is performed according to the corresponding TypeMap for thetypeMapName
. If no TypeMap exists for thesource.getClass()
,destination.getClass()
andtypeMapName
then one is created.To map a parameterized destination type, subclass
TypeToken
and obtain its Type:Type listType = new TypeToken<List<String>>() {}.getType(); List<String> strings = modelMapper.map(source, listType, "string-list");
- Type Parameters:
D
- destination type- Parameters:
source
- object to map fromdestinationType
- type to map totypeMapName
- name of existing TypeMap to use mappings from- Returns:
- fully mapped instance of
destinationType
- Throws:
IllegalArgumentException
- ifsource
,destinationType
ortypeMapName
are nullConfigurationException
- if the ModelMapper cannot find or create the TypeMapMappingException
- if a runtime error occurs while mapping
-
validate
public void validate()
Validates that every top level destination property for each configured TypeMap is mapped to one and only one source property, or that aConverter
wasset
for the TypeMap. If not, a ConfigurationException is thrown detailing any missing mappings.- Throws:
ValidationException
- if any TypeMaps contain unmapped properties
-
registerModule
public ModelMapper registerModule(Module module)
Register a module- Parameters:
module
- a module for extension
-
-