Package org.modelmapper
Class ModelMapper
java.lang.Object
org.modelmapper.ModelMapper
ModelMapper - Performs object mapping, maintains
Configuration and stores TypeMaps.
- To perform object mapping use
map. - To configure the mapping of one type to another use
createTypeMap. - To add mappings for specific properties use
addMappingssupplying aPropertyMap. - To configure ModelMapper use
getConfiguration(). - To validate mappings use
validate().
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<S,D> void addConverter(Converter<S, D> converter) Registers theconverterto use when mapping instances of typesStoD.<S,D> void addConverter(Converter<S, D> converter, Class<S> sourceType, Class<D> destinationType) Registers theconverterto use when mapping instances of typesStoD.<S,D> TypeMap<S, D> addMappings(PropertyMap<S, D> propertyMap) Adds mappings from thepropertyMapinto the TypeMap corresponding to source typeSand destination typeD.<S,D> TypeMap<S, D> createTypeMap(Class<S> sourceType, Class<D> destinationType) Creates a TypeMap for thesourceTypeanddestinationTypeusing the ModelMapper's configuration.<S,D> TypeMap<S, D> createTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName) Creates a TypeMap for thesourceTypeanddestinationTypeidentified by thetypeMapNameusing the ModelMapper's configuration.<S,D> TypeMap<S, D> createTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName, Configuration configuration) Creates a TypeMap for thesourceTypeanddestinationTypeidentified by thetypeMapNameusing theconfiguration.<S,D> TypeMap<S, D> createTypeMap(Class<S> sourceType, Class<D> destinationType, Configuration configuration) Creates a TypeMap for thesourceTypeanddestinationTypeusing theconfiguration.<S,D> TypeMap<S, D> createTypeMap(S source, Class<D> destinationType) Creates a TypeMap for thesource's type anddestinationTypeusing the ModelMapper's configuration.<S,D> TypeMap<S, D> createTypeMap(S source, Class<D> destinationType, String typeMapName) Creates a TypeMap for thesource's type anddestinationTypeidentified by thetypeMapNameusing 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 anddestinationTypeidentified by thetypeMapNameusing theconfiguration.<S,D> TypeMap<S, D> createTypeMap(S source, Class<D> destinationType, Configuration configuration) Creates a TypeMap for thesource's type anddestinationTypeusing 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.Returns the ModelMapper's configuration.<S,D> TypeMap<S, D> getTypeMap(Class<S> sourceType, Class<D> destinationType) Returns the TypeMap for thesourceTypeanddestinationType, else returnsnullif none exists.<S,D> TypeMap<S, D> getTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName) Returns the TypeMap for thesourceType,destinationTypeandtypeMapName, else returnsnullif none exists.Collection<TypeMap<?,?>> Returns all TypeMaps for the ModelMapper.<D> DMapssourceto an instance ofdestinationType.<D> DMapssourceto an instance ofdestinationType.voidMapssourcetodestination.voidMapssourcetodestination.<D> DMapssourceto an instance ofdestinationType.<D> DMapssourceto an instance ofdestinationType.registerModule(Module module) Register a module<S,D> TypeMap<S, D> Returns the TypeMap for thesourceType,destinationType, creates TypeMap automatically if none exists.<S,D> TypeMap<S, D> Returns the TypeMap for thesourceType,destinationType, andtypeMapNamecreates TypeMap automatically if none exists.voidvalidate()Validates that every top level destination property for each configured TypeMap is mapped to one and only one source property, or that aConverterwassetfor the TypeMap.
-
Constructor Details
-
ModelMapper
public ModelMapper()Creates a new ModelMapper.
-
-
Method Details
-
addConverter
Registers theconverterto use when mapping instances of typesStoD. Theconverterwill besetagainst TypeMap corresponding to theconverter's type argumentsSandD.- Type Parameters:
S- source typeD- destination type- Parameters:
converter- to register- Throws:
IllegalArgumentException- ifconverteris null or if type argumentsSandDare not declared for theconverter- See Also:
-
addConverter
public <S,D> void addConverter(Converter<S, D> converter, Class<S> sourceType, Class<D> destinationType) Registers theconverterto use when mapping instances of typesStoD. Theconverterwill besetagainst TypeMap corresponding to theconverter's type argumentsSandD.- Type Parameters:
S- source typeD- destination type- Parameters:
converter- to register- Throws:
IllegalArgumentException- ifconverteris null or if type argumentsSandDare not declared for theconverter- See Also:
-
addMappings
Adds mappings from thepropertyMapinto the TypeMap corresponding to source typeSand destination typeD. Explicit mappings defined in thepropertyMapwill 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- ifpropertyMapis nullConfigurationException- if a configuration error occurs while adding mappings for thepropertyMap
-
createTypeMap
Creates a TypeMap for thesourceTypeanddestinationTypeusing the ModelMapper's configuration.- Type Parameters:
S- source typeD- destination type- Parameters:
sourceType-destinationType-- Throws:
IllegalArgumentException- ifsourceTypeordestinationTypeare nullIllegalStateException- if a TypeMap already exists forsourceTypeanddestinationTypeConfigurationException- if the ModelMapper cannot create the TypeMap- See Also:
-
createTypeMap
public <S,D> TypeMap<S,D> createTypeMap(Class<S> sourceType, Class<D> destinationType, Configuration configuration) Creates a TypeMap for thesourceTypeanddestinationTypeusing theconfiguration.- Type Parameters:
S- source typeD- destination type- Parameters:
sourceType-destinationType-configuration- to apply to TypeMap- Throws:
IllegalArgumentException- ifsourceType,destinationTypeorconfigurationare nullIllegalStateException- if a TypeMap already exists forsourceTypeanddestinationTypeConfigurationException- if the ModelMapper cannot create the TypeMap- See Also:
-
createTypeMap
public <S,D> TypeMap<S,D> createTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName) Creates a TypeMap for thesourceTypeanddestinationTypeidentified by thetypeMapNameusing the ModelMapper's configuration.- Type Parameters:
S- source typeD- destination type- Parameters:
sourceType-destinationType-typeMapName-- Throws:
IllegalArgumentException- ifsourceType,destinationTypeortypeMapNameare nullIllegalStateException- if a TypeMap already exists forsourceType,destinationTypeandtypeMapNameConfigurationException- if the ModelMapper cannot create the TypeMap- See Also:
-
createTypeMap
public <S,D> TypeMap<S,D> createTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName, Configuration configuration) Creates a TypeMap for thesourceTypeanddestinationTypeidentified by thetypeMapNameusing theconfiguration.- Type Parameters:
S- source typeD- destination type- Parameters:
sourceType-destinationType-typeMapName-configuration- to apply to TypeMap- Throws:
IllegalArgumentException- ifsourceType,destinationType,typeMapNameorconfigurationare nullIllegalStateException- if a TypeMap already exists forsourceType,destinationTypeandtypeMapNameConfigurationException- if the ModelMapper cannot create the TypeMap- See Also:
-
createTypeMap
Creates a TypeMap for thesource's type anddestinationTypeusing 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- ifsourceordestinationTypeare nullIllegalStateException- if a TypeMap already exists forsource's type anddestinationTypeConfigurationException- if the ModelMapper cannot create the TypeMap- See Also:
-
createTypeMap
public <S,D> TypeMap<S,D> createTypeMap(S source, Class<D> destinationType, Configuration configuration) Creates a TypeMap for thesource's type anddestinationTypeusing 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,destinationTypeorconfigurationare nullIllegalStateException- if a TypeMap already exists forsource's type anddestinationTypeConfigurationException- if the ModelMapper cannot create the TypeMap- See Also:
-
createTypeMap
Creates a TypeMap for thesource's type anddestinationTypeidentified by thetypeMapNameusing 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,destinationTypeortypeMapNameare nullIllegalStateException- if a TypeMap already exists forsource's type,destinationTypeandtypeMapNameConfigurationException- if the ModelMapper cannot create the TypeMap- See Also:
-
createTypeMap
public <S,D> TypeMap<S,D> createTypeMap(S source, Class<D> destinationType, String typeMapName, Configuration configuration) Creates a TypeMap for thesource's type anddestinationTypeidentified by thetypeMapNameusing 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,typeMapNameorconfigurationare nullIllegalStateException- if a TypeMap already exists forsource's type,destinationTypeandtypeMapNameConfigurationException- if the ModelMapper cannot create the TypeMap- See Also:
-
getConfiguration
Returns the ModelMapper's configuration. -
getTypeMap
Returns the TypeMap for thesourceTypeanddestinationType, else returnsnullif none exists.- Type Parameters:
S- source typeD- destination type- Throws:
IllegalArgumentException- issourceTypeordestinationTypeare null- See Also:
-
getTypeMap
public <S,D> TypeMap<S,D> getTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName) Returns the TypeMap for thesourceType,destinationTypeandtypeMapName, else returnsnullif none exists.- Type Parameters:
S- source typeD- destination type- Throws:
IllegalArgumentException- issourceType,destinationTypeortypeMapNameare null- See Also:
-
typeMap
Returns the TypeMap for thesourceType,destinationType, creates TypeMap automatically if none exists.- Type Parameters:
S- source typeD- destination type- Throws:
IllegalArgumentException- issourceType,destinationTypeare null
-
typeMap
public <S,D> TypeMap<S,D> typeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName) Returns the TypeMap for thesourceType,destinationType, andtypeMapNamecreates TypeMap automatically if none exists.- Type Parameters:
S- source typeD- destination type- Throws:
IllegalArgumentException- issourceType,destinationTypeortypeMapNameare null
-
emptyTypeMap
Creates an empty TypeMap for thesourceType,destinationType.- Type Parameters:
S- source typeD- destination type- Throws:
IllegalArgumentException- issourceTypeordestinationTypeare null, orTypeMap<SourceType, DestinationTypealready 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- issourceTypeordestinationTypeare null, orTypeMap<Source Type, DestinationTypealready defined in the TypeMapStore
-
getTypeMaps
Returns all TypeMaps for the ModelMapper. -
map
Mapssourceto an instance ofdestinationType. Mapping is performed according to the corresponding TypeMap. If no TypeMap exists forsource.getClass()anddestinationTypethen 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- ifsourceordestinationTypeare nullConfigurationException- if the ModelMapper cannot find or create a TypeMap for the argumentsMappingException- if a runtime error occurs while mapping
-
map
Mapssourceto an instance ofdestinationType. Mapping is performed according to the corresponding TypeMap for thetypeMapName. If no TypeMap exists for thesource.getClass(),destinationTypeandtypeMapNamethen 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,destinationTypeortypeMapNameare nullConfigurationException- if the ModelMapper cannot find or create a TypeMap for the argumentsMappingException- if a runtime error occurs while mapping
-
map
Mapssourcetodestination. 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- ifsourceordestinationare nullConfigurationException- if the ModelMapper cannot find or create a TypeMap for the argumentsMappingException- if an error occurs while mapping
-
map
Mapssourcetodestination. Mapping is performed according to the corresponding TypeMap for thetypeMapName. If no TypeMap exists for thesource.getClass(),destination.getClass()andtypeMapNamethen one is created.- Parameters:
source- object to map fromdestination- object to map totypeMapName- name of existing TypeMap to use mappings from- Throws:
IllegalArgumentException- ifsource,destinationortypeMapNameare nullConfigurationException- if the ModelMapper cannot find or create a TypeMap for the argumentsMappingException- if an error occurs while mapping
-
map
Mapssourceto an instance ofdestinationType. Mapping is performed according to the corresponding TypeMap. If no TypeMap exists forsource.getClass()anddestinationTypethen one is created.To map a parameterized destination type, subclass
TypeTokenand 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- ifsourceordestinationTypeare nullConfigurationException- if the ModelMapper cannot find or create the TypeMapMappingException- if a runtime error occurs while mapping
-
map
Mapssourceto an instance ofdestinationType. Mapping is performed according to the corresponding TypeMap for thetypeMapName. If no TypeMap exists for thesource.getClass(),destination.getClass()andtypeMapNamethen one is created.To map a parameterized destination type, subclass
TypeTokenand 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,destinationTypeortypeMapNameare 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 aConverterwassetfor the TypeMap. If not, a ConfigurationException is thrown detailing any missing mappings.- Throws:
ValidationException- if any TypeMaps contain unmapped properties
-
registerModule
Register a module- Parameters:
module- a module for extension
-