Map Struct can even be used to “cherry pick” properties when source and target do not share the same nesting level (the same number of properties).
This can be done in the source – and in the target type.
In particular this means that the values are copied from source to target by plain getter/setter invocations instead of reflection or similar. Map Struct will only create a new mapping method if and only if the source and target property are properties of a Bean and they themselves are Beans or simple properties. they are not Collection-typed attributes with the same element type will be copied by creating a new instance of the target collection type containing the elements from the source property.
source or target type can be used to control how properties should be mapped when names do not match.
There is an elaborate example in our examples repository to explain how this problem can be overcome.
During compilation, Map Struct will generate an implementation of this interface.
This implementation uses plain Java method invocations for mapping between source and target objects, i.e. Compared to writing mapping code from hand, Map Struct saves time by generating code which is tedious and error-prone to write.
An advantage of this approach over declaring default methods is that additional fields could be declared in the mapper class. An error will be raised when such an ambiguity is not resolved.
For properties which only exist once in the given source objects it is optional to specify the source parameter’s name as it can be determined automatically.
In some cases you need mappings which don’t create a new instance of the target type but instead update an existing instance of that type.
This sort of mapping can be realized by adding a parameter for the target object and marking this parameter with you may also set the method’s return type to the type of the target parameter, which will cause the generated implementation to update the passed mapping target and return it as well.
This is the reference documentation of Map Struct, an annotation processor for generating type-safe, performant and dependency-free bean mapping code.